Develop and Download Open Source Software

Browse Subversion Repository

Diff of /trunk/src/js/jgame.js

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 74 by tsugehara, Sat Mar 30 16:16:23 2013 UTC revision 75 by tsugehara, Sun Mar 31 04:04:43 2013 UTC
# Line 165  var InputEventType; Line 165  var InputEventType;
165      InputEventType._map[2] = "Point";      InputEventType._map[2] = "Point";
166      InputEventType.Point = 2;      InputEventType.Point = 2;
167  })(InputEventType || (InputEventType = {}));  })(InputEventType || (InputEventType = {}));
168    var InputEventAction;
169    (function (InputEventAction) {
170        InputEventAction._map = [];
171        InputEventAction._map[0] = "Unknown";
172        InputEventAction.Unknown = 0;
173        InputEventAction._map[1] = "Down";
174        InputEventAction.Down = 1;
175        InputEventAction._map[2] = "Move";
176        InputEventAction.Move = 2;
177        InputEventAction._map[3] = "Up";
178        InputEventAction.Up = 3;
179    })(InputEventAction || (InputEventAction = {}));
180  var Keytype;  var Keytype;
181  (function (Keytype) {  (function (Keytype) {
182      Keytype._map = [];      Keytype._map = [];
# Line 1240  var Resource = (function () { Line 1252  var Resource = (function () {
1252      function Resource() {      function Resource() {
1253          this.requests = [];          this.requests = [];
1254          this.loaded = new Trigger();          this.loaded = new Trigger();
1255            this.added = new Trigger();
1256          this.clear();          this.clear();
1257          this.loaders = {          this.loaders = {
1258          };          };
# Line 1294  var Resource = (function () { Line 1307  var Resource = (function () {
1307              ext = dot[dot.length - 1];              ext = dot[dot.length - 1];
1308          }          }
1309          ext = ext.toLowerCase();          ext = ext.toLowerCase();
1310          if(this.loaders[ext]) {          var loader = this.loaders[ext] ? this.loaders[ext] : this.loaders["default"];
1311              this.loaders[ext].load(url, name);          loader.load(url, name);
1312          } else {          this.added.fire({
1313              this.loaders["default"].load(url, name);              name: name,
1314          }              url: url,
1315                loader: loader
1316            });
1317        };
1318        Resource.prototype.loadManual = function (name) {
1319            this.requests.push(name);
1320            this.added.fire({
1321                name: name,
1322                url: null,
1323                loader: null
1324            });
1325        };
1326        Resource.prototype.completeManual = function (name) {
1327            this.requestCompleted(name);
1328      };      };
1329      return Resource;      return Resource;
1330  })();  })();
# Line 2285  var LoadingScene = (function (_super) { Line 2311  var LoadingScene = (function (_super) {
2311          _super.call(this, game);          _super.call(this, game);
2312          this.resource = resource;          this.resource = resource;
2313          this.resource.loaded.handle(this, this.complete);          this.resource.loaded.handle(this, this.complete);
2314            this.resource.added.handle(this, this.added);
2315          this.requestCount = this.resource.requests.length;          this.requestCount = this.resource.requests.length;
         this.jgameRequestCount = this.requestCount;  
2316          this.finished = new Trigger();          this.finished = new Trigger();
         this.otherResources = {  
         };  
         this.otherResourceCount = 0;  
         this.otherResourceCompleted = 0;  
         this.lastCnt = 0;  
2317          if(!noShape) {          if(!noShape) {
2318              this.shape = new Shape(game.width, 32);              this.shape = new Shape(game.width, 32);
2319              this.shape.moveTo(0, game.height / 2 - 16);              this.shape.moveTo(0, game.height / 2 - 16);
# Line 2302  var LoadingScene = (function (_super) { Line 2323  var LoadingScene = (function (_super) {
2323              this.append(this.shapeP);              this.append(this.shapeP);
2324          }          }
2325      }      }
     LoadingScene.prototype.addOtherResource = function (identify) {  
         this.requestCount++;  
         this.otherResourceCount++;  
     };  
     LoadingScene.prototype.otherResourceComplete = function (identify) {  
         this.otherResources[identify]--;  
         this.otherResourceCompleted++;  
         this.complete(this.lastCnt);  
     };  
2326      LoadingScene.prototype.complete = function (cnt) {      LoadingScene.prototype.complete = function (cnt) {
2327          this.lastCnt = cnt;          var per = (this.requestCount - cnt) / this.requestCount;
         var per = (this.jgameRequestCount - cnt + this.otherResourceCompleted) / this.requestCount;  
2328          this.shapeP.width = this.game.width * per;          this.shapeP.width = this.game.width * per;
2329          this.shapeP.updated();          this.shapeP.updated();
2330          if(per == 1) {          if(per == 1) {
2331              this.resource.loaded.remove(this, this.complete);              this.resource.loaded.remove(this, this.complete);
2332                this.resource.added.remove(this, this.added);
2333              this.end();              this.end();
2334              this.finished.fire();              this.finished.fire();
2335          }          }
2336      };      };
2337        LoadingScene.prototype.added = function (e) {
2338            this.requestCount++;
2339        };
2340      return LoadingScene;      return LoadingScene;
2341  })(Scene);  })(Scene);
2342  var InputEvent = (function () {  var InputEvent = (function () {
2343      function InputEvent(type, param) {      function InputEvent(type, action, param) {
2344          this.type = type;          this.type = type;
2345            this.action = action;
2346          this.param = param;          this.param = param;
2347      }      }
2348      return InputEvent;      return InputEvent;
2349  })();  })();
2350  var InputKeyboardEvent = (function (_super) {  var InputKeyboardEvent = (function (_super) {
2351      __extends(InputKeyboardEvent, _super);      __extends(InputKeyboardEvent, _super);
2352      function InputKeyboardEvent(key, e) {      function InputKeyboardEvent(action, key, e) {
2353          _super.call(this, InputEventType.Keyboard, e);          _super.call(this, InputEventType.Keyboard, action, e);
2354          this.key = key;          this.key = key;
2355      }      }
2356      return InputKeyboardEvent;      return InputKeyboardEvent;
2357  })(InputEvent);  })(InputEvent);
2358  var InputPointEvent = (function (_super) {  var InputPointEvent = (function (_super) {
2359      __extends(InputPointEvent, _super);      __extends(InputPointEvent, _super);
2360      function InputPointEvent(e, entity, point) {      function InputPointEvent(action, e, entity, point) {
2361          _super.call(this, InputEventType.Point, e);          _super.call(this, InputEventType.Point, action, e);
2362          var entityOffset = entity.offset();          var entityOffset = entity.offset();
2363          this.entity = entity;          this.entity = entity;
2364          this.point = point;          this.point = point;
# Line 2569  var GameRenderer = (function (_super) { Line 2585  var GameRenderer = (function (_super) {
2585      GameRenderer.prototype.refresh = function () {      GameRenderer.prototype.refresh = function () {
2586          delete this.buffer;          delete this.buffer;
2587          this.buffer = new Array();          this.buffer = new Array();
         var bounding = this.handler.getBoundingClientRect();  
         this._pageX = Math.round(window["scrollX"] || window.pageXOffset + bounding.left);  
         this._pageY = Math.round(window["scrollY"] || window.pageYOffset + bounding.top);  
2588          if(this.transferMode == RenderTransferMode.Flip) {          if(this.transferMode == RenderTransferMode.Flip) {
2589              this.handler.innerHTML = "";              this.handler.innerHTML = "";
2590              for(var i = 0; i < 2; i++) {              for(var i = 0; i < 2; i++) {
# Line 2612  var GameRenderer = (function (_super) { Line 2625  var GameRenderer = (function (_super) {
2625                  this.handler.style.top = this.frontCanvasOffset.y + "px";                  this.handler.style.top = this.frontCanvasOffset.y + "px";
2626              }              }
2627          }          }
2628            var bounding = this.handler.getBoundingClientRect();
2629            this._pageX = Math.round(window["scrollX"] || window.pageXOffset + bounding.left);
2630            this._pageY = Math.round(window["scrollY"] || window.pageYOffset + bounding.top);
2631      };      };
2632      return GameRenderer;      return GameRenderer;
2633  })(Renderer);  })(Renderer);
# Line 2738  var Game = (function () { Line 2754  var Game = (function () {
2754          this.width = width;          this.width = width;
2755          this.height = height;          this.height = height;
2756          this.targetFps = 0;          this.targetFps = 0;
2757            this.autoLoading = true;
2758          this.loaded = new Trigger();          this.loaded = new Trigger();
2759          this.update = new Trigger();          this.update = new Trigger();
2760          this.pointDown = new Trigger();          this.pointDown = new Trigger();
# Line 2765  var Game = (function () { Line 2782  var Game = (function () {
2782              this.renderer = new GameRenderer(this, container, transferMode);              this.renderer = new GameRenderer(this, container, transferMode);
2783              this.renderer.changeScene(this.scene);              this.renderer.changeScene(this.scene);
2784          }          }
2785            this.eventQueue = [];
2786            this.inputEventMap = {
2787            };
2788            this.inputEventMap[InputEventType.Keyboard] = {
2789            };
2790            this.inputEventMap[InputEventType.Keyboard][InputEventAction.Down] = "keyDown";
2791            this.inputEventMap[InputEventType.Keyboard][InputEventAction.Up] = "keyUp";
2792            this.inputEventMap[InputEventType.Point] = {
2793            };
2794            this.inputEventMap[InputEventType.Point][InputEventAction.Down] = "pointDown";
2795            this.inputEventMap[InputEventType.Point][InputEventAction.Move] = "pointMove";
2796            this.inputEventMap[InputEventType.Point][InputEventAction.Up] = "pointUp";
2797          this.keyboardHandler();          this.keyboardHandler();
2798          this.pointHandler();          this.pointHandler();
2799          if(document.getElementById("fps_show")) {          if(document.getElementById("fps_show")) {
# Line 2826  var Game = (function () { Line 2855  var Game = (function () {
2855          return typeof div.ontouchstart === 'function';          return typeof div.ontouchstart === 'function';
2856      };      };
2857      Game.prototype.getOffsetByEvent = function (e) {      Game.prototype.getOffsetByEvent = function (e) {
2858          if(e.offsetX === undefined) {          e.offset = {
2859              e.offsetX = e.layerX;              x: e.pageX - this.renderer._pageX,
2860              e.offsetY = e.layerY;              y: e.pageY - this.renderer._pageY
2861          }          };
         if(JGUtil.isStyleScale) {  
             return {  
                 x: this.scale ? e.offsetX / this.scale : e.offsetX,  
                 y: this.scale ? e.offsetY / this.scale : e.offsetY  
             };  
         }  
2862          return {          return {
2863              x: e.offsetX,              x: this.scale ? e.offset.x / this.scale : e.offset.x,
2864              y: e.offsetY              y: this.scale ? e.offset.y / this.scale : e.offset.y
2865          };          };
2866      };      };
2867      Game.prototype.onmousedown = function (e) {      Game.prototype.onmousedown = function (e) {
# Line 2853  var Game = (function () { Line 2876  var Game = (function () {
2876              if(!dragObj) {              if(!dragObj) {
2877                  dragObj = layer;                  dragObj = layer;
2878              }              }
2879              this.dragParam = new InputPointEvent(e, dragObj, offset);              this.dragParam = new InputPointEvent(InputEventAction.Down, e, dragObj, offset);
2880              this.pointDown.fire(this.dragParam);              this.eventQueue.push(this.dragParam);
             if(this.scene.pointDown) {  
                 this.scene.pointDown.fire(this.dragParam);  
             }  
             if(dragObj.pointDown) {  
                 dragObj.pointDown.fire(this.dragParam);  
             }  
2881              break;              break;
2882          }          }
2883          e.preventDefault();          e.preventDefault();
# Line 2871  var Game = (function () { Line 2888  var Game = (function () {
2888          var touches = e.changedTouches;          var touches = e.changedTouches;
2889          for(var i = 0, l = touches.length; i < l; i++) {          for(var i = 0, l = touches.length; i < l; i++) {
2890              var touch = touches[i];              var touch = touches[i];
             touch.offsetX = touch.pageX - this.renderer._pageX;  
             touch.offsetY = touch.pageY - this.renderer._pageY;  
2891              var offset = this.getOffsetByEvent(touch);              var offset = this.getOffsetByEvent(touch);
2892              while(layer = layers.pop()) {              while(layer = layers.pop()) {
2893                  if(!layer.pointCapture) {                  if(!layer.pointCapture) {
# Line 2882  var Game = (function () { Line 2897  var Game = (function () {
2897                  if(!dragObj) {                  if(!dragObj) {
2898                      dragObj = layer;                      dragObj = layer;
2899                  }                  }
2900                  this.dragParam = new InputPointEvent(touch, dragObj, offset);                  this.dragParam = new InputPointEvent(InputEventAction.Down, touch, dragObj, offset);
2901                  this.pointDown.fire(this.dragParam);                  this.eventQueue.push(this.dragParam);
                 if(this.scene.pointDown) {  
                     this.scene.pointDown.fire(this.dragParam);  
                 }  
                 if(dragObj.pointDown) {  
                     dragObj.pointDown.fire(this.dragParam);  
                 }  
2902                  break;                  break;
2903              }              }
2904          }          }
# Line 2899  var Game = (function () { Line 2908  var Game = (function () {
2908          if(!this.dragParam) {          if(!this.dragParam) {
2909              return;              return;
2910          }          }
2911          var param = new InputPointEvent(e, this.dragParam.entity, this.getOffsetByEvent(e));          var param = new InputPointEvent(InputEventAction.Move, e, this.dragParam.entity, this.getOffsetByEvent(e));
2912          if(this.dragParam.entity.pointMove) {          this.eventQueue.push(param);
             this.dragParam.entity.pointMove.fire(param);  
         }  
         if(this.scene.pointMove) {  
             this.scene.pointMove.fire(param);  
         }  
         this.pointMove.fire(param);  
2913          e.preventDefault();          e.preventDefault();
2914      };      };
2915      Game.prototype.ontouchmove = function (e) {      Game.prototype.ontouchmove = function (e) {
# Line 2916  var Game = (function () { Line 2919  var Game = (function () {
2919          var touches = e.changedTouches;          var touches = e.changedTouches;
2920          for(var i = 0, l = touches.length; i < l; i++) {          for(var i = 0, l = touches.length; i < l; i++) {
2921              var touch = touches[i];              var touch = touches[i];
             touch.offsetX = touch.pageX - this.renderer._pageX;  
             touch.offsetY = touch.pageY - this.renderer._pageY;  
2922              var offset = this.getOffsetByEvent(touch);              var offset = this.getOffsetByEvent(touch);
2923              var param = new InputPointEvent(touch, this.dragParam.entity, offset);              var param = new InputPointEvent(InputEventAction.Move, touch, this.dragParam.entity, offset);
2924              if(this.dragParam.entity.pointMove) {              this.eventQueue.push(param);
                 this.dragParam.entity.pointMove.fire(param);  
             }  
             if(this.scene.pointMove) {  
                 this.scene.pointMove.fire(param);  
             }  
             this.pointMove.fire(param);  
2925          }          }
2926          e.preventDefault();          e.preventDefault();
2927      };      };
# Line 2934  var Game = (function () { Line 2929  var Game = (function () {
2929          if(!this.dragParam) {          if(!this.dragParam) {
2930              return;              return;
2931          }          }
2932          var param = new InputPointEvent(e, this.dragParam.entity, this.getOffsetByEvent(e));          var param = new InputPointEvent(InputEventAction.Up, e, this.dragParam.entity, this.getOffsetByEvent(e));
2933          if(this.dragParam.entity.pointUp) {          this.eventQueue.push(param);
             this.dragParam.entity.pointUp.fire(param);  
         }  
         if(this.scene.pointUp) {  
             this.scene.pointUp.fire(param);  
         }  
         this.pointUp.fire(param);  
2934          this.dragParam = null;          this.dragParam = null;
2935          e.preventDefault();          e.preventDefault();
2936      };      };
# Line 2952  var Game = (function () { Line 2941  var Game = (function () {
2941          var touches = e.changedTouches;          var touches = e.changedTouches;
2942          for(var i = 0, l = touches.length; i < l; i++) {          for(var i = 0, l = touches.length; i < l; i++) {
2943              var touch = touches[i];              var touch = touches[i];
             touch.offsetX = touch.pageX - this.renderer._pageX;  
             touch.offsetY = touch.pageY - this.renderer._pageY;  
2944              var offset = this.getOffsetByEvent(touch);              var offset = this.getOffsetByEvent(touch);
2945              var param = new InputPointEvent(touch, this.dragParam.entity, offset);              var param = new InputPointEvent(InputEventAction.Up, touch, this.dragParam.entity, offset);
2946              if(this.dragParam.entity.pointUp) {              this.eventQueue.push(param);
                 this.dragParam.entity.pointUp.fire(param);  
             }  
             if(this.scene.pointUp) {  
                 this.scene.pointUp.fire(param);  
             }  
             this.pointUp.fire(param);  
2947              this.dragParam = null;              this.dragParam = null;
2948          }          }
2949          e.preventDefault();          e.preventDefault();
# Line 2983  var Game = (function () { Line 2964  var Game = (function () {
2964          }          }
2965      };      };
2966      Game.prototype.onkeydown = function (e) {      Game.prototype.onkeydown = function (e) {
2967          var keyParam = new InputKeyboardEvent(this.keymap[e.keyCode], e);          var keyParam = new InputKeyboardEvent(InputEventAction.Down, this.keymap[e.keyCode], e);
2968          this.keyDown.fire(keyParam);          this.eventQueue.push(keyParam);
         if(this.scene.keyDown) {  
             this.scene.keyDown.fire(keyParam);  
         }  
2969          if(this.keymap[e.keyCode] != undefined) {          if(this.keymap[e.keyCode] != undefined) {
2970              e.preventDefault();              e.preventDefault();
2971          }          }
2972      };      };
2973      Game.prototype.onkeyup = function (e) {      Game.prototype.onkeyup = function (e) {
2974          var keyParam = new InputKeyboardEvent(this.keymap[e.keyCode], e);          var keyParam = new InputKeyboardEvent(InputEventAction.Up, this.keymap[e.keyCode], e);
2975          this.keyUp.fire(keyParam);          this.eventQueue.push(keyParam);
         if(this.scene.keyUp) {  
             this.scene.keyUp.fire(keyParam);  
         }  
2976          if(this.keymap[e.keyCode] != undefined) {          if(this.keymap[e.keyCode] != undefined) {
2977              e.preventDefault();              e.preventDefault();
2978          }          }
# Line 3094  var Game = (function () { Line 3069  var Game = (function () {
3069      Game.prototype.s = function (name) {      Game.prototype.s = function (name) {
3070          return this.resource.sound(name);          return this.resource.sound(name);
3071      };      };
3072      Game.prototype.preload = function (ary, loadingScene) {      Game.prototype.preload = function (ary) {
3073          if(ary instanceof Array) {          if(ary instanceof Array) {
3074              for(var i = 0; i < ary.length; i++) {              for(var i = 0; i < ary.length; i++) {
3075                  this.resource.load(ary[i], ary[i]);                  this.resource.load(ary[i], ary[i]);
3076              }              }
3077          } else if(typeof ary == "string") {          } else if(typeof ary == "string") {
             var hasLoadingScene = false;  
3078              for(var i = 0; i < arguments.length; i++) {              for(var i = 0; i < arguments.length; i++) {
3079                  if(typeof arguments[i] != "string") {                  this.resource.load(arguments[i], arguments[i]);
                     loadingScene = arguments[i];  
                     hasLoadingScene = true;  
                 } else {  
                     this.resource.load(arguments[i], arguments[i]);  
                 }  
             }  
             if(!hasLoadingScene) {  
                 loadingScene = new LoadingScene(this, this.resource);  
3080              }              }
3081          } else {          } else {
3082              for(var i in ary) {              for(var i in ary) {
3083                  this.resource.load(i, ary[i]);                  this.resource.load(i, ary[i]);
3084              }              }
3085          }          }
3086          if(!loadingScene) {          if(this.autoLoading) {
3087              loadingScene = new LoadingScene(this, this.resource);              var loadingScene = new LoadingScene(this, this.resource);
3088                loadingScene.finished.handle(this, this.preloadComplete);
3089                this.changeScene(loadingScene);
3090          }          }
         loadingScene.finished.handle(this, this.preloadComplete);  
         this.changeScene(loadingScene);  
3091      };      };
3092      Game.prototype.preloadOther = function (identity) {      Game.prototype.preloadOther = function (identity) {
3093          var loadingScene = this.scene;          this.resource.loadManual(identity);
         loadingScene.addOtherResource(identity);  
3094      };      };
3095      Game.prototype.preloadCompleteOther = function (identity) {      Game.prototype.preloadCompleteOther = function (identity) {
3096          var loadingScene = this.scene;          this.resource.completeManual(identity);
         loadingScene.otherResourceComplete(identity);  
3097      };      };
3098      Game.prototype.preloadComplete = function () {      Game.prototype.preloadComplete = function () {
3099          this.loaded.fire();          this.loaded.fire();
# Line 3138  var Game = (function () { Line 3102  var Game = (function () {
3102          this.renderer.render();          this.renderer.render();
3103          this._exit = true;          this._exit = true;
3104      };      };
3105        Game.prototype.raiseInputEvent = function () {
3106            var e;
3107            if(this.eventQueue.length > 1) {
3108                console.log("input event: " + this.eventQueue.length);
3109            }
3110            while(e = this.eventQueue.shift()) {
3111                var n = this.inputEventMap[e.type][e.action];
3112                if(e.type == InputEventType.Keyboard) {
3113                    if(this.scene[n]) {
3114                        this.scene[n].fire(e);
3115                    }
3116                    this[n].fire(e);
3117                } else {
3118                    if((e).entity[n]) {
3119                        (e).entity[n].fire(e);
3120                    }
3121                    if(this.scene[n]) {
3122                        this.scene[n].fire(e);
3123                    }
3124                    this[n].fire(e);
3125                }
3126            }
3127        };
3128      Game.prototype.main = function () {      Game.prototype.main = function () {
3129          var _this = this;          var _this = this;
3130          var fps_stack = new Array();          var fps_stack = new Array();
# Line 3148  var Game = (function () { Line 3135  var Game = (function () {
3135              if(_this.tick > (t + 10000) || (_this.tick + 10000) < t) {              if(_this.tick > (t + 10000) || (_this.tick + 10000) < t) {
3136                  _this.tick = t - 1;                  _this.tick = t - 1;
3137                  _this.renderTick = t - _this.targetFps;                  _this.renderTick = t - _this.targetFps;
                 if(_this.enterFrame) {  
                     _this.enterFrameTick = t - 1;  
                 }  
3138                  _this.refresh();                  _this.refresh();
3139              }              }
3140              if(_this.tick < t) {              if(_this.tick < t) {
3141                    _this.raiseInputEvent();
3142                  _this.update.fire(t - _this.tick);                  _this.update.fire(t - _this.tick);
3143                  _this.tick = t;                  _this.tick = t;
3144              }              }
             if(_this.enterFrame) {  
                 if(!_this.enterFrameTick) {  
                     _this.enterFrameTick = t - 1;  
                 }  
                 while((_this.enterFrameTick + 16) < t) {  
                     _this.enterFrameTick += 16;  
                     _this.enterFrame.fire();  
                 }  
             }  
3145              for(var i = 0; i < _this.timers.length; i++) {              for(var i = 0; i < _this.timers.length; i++) {
3146                  _this.timers[i].tryFire(t);                  _this.timers[i].tryFire(t);
3147              }              }

Legend:
Removed from v.74  
changed lines
  Added in v.75

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26