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 4 by tsugehara, Tue Jan 15 05:10:41 2013 UTC revision 7 by tsugehara, Thu Jan 17 12:03:51 2013 UTC
# Line 303  var E = (function () { Line 303  var E = (function () {
303      }      }
304      E.prototype.enablePointingEvent = function () {      E.prototype.enablePointingEvent = function () {
305          this.pointCapture = true;          this.pointCapture = true;
306            if(!this.inputDown) {
307                this.inputDown = new Trigger();
308            }
309            if(!this.inputUp) {
310                this.inputUp = new Trigger();
311            }
312            if(!this.inputMove) {
313                this.inputMove = new Trigger();
314            }
315      };      };
316      E.prototype.disablePointingEvent = function () {      E.prototype.disablePointingEvent = function () {
317          delete this.pointCapture;          delete this.pointCapture;
# Line 475  var E = (function () { Line 484  var E = (function () {
484                  childEntity.destroy();                  childEntity.destroy();
485              }              }
486          }          }
487            if(this.inputDown) {
488                this.inputDown.destroy();
489                delete this.inputDown;
490            }
491            if(this.inputUp) {
492                this.inputUp.destroy();
493                delete this.inputUp;
494            }
495            if(this.inputMove) {
496                this.inputMove.destroy();
497                delete this.inputMove;
498            }
499      };      };
500      E.prototype.offset = function () {      E.prototype.offset = function () {
501          var parent_offset = this.parent ? this.parent.offset() : {          var parent_offset = this.parent ? this.parent.offset() : {
# Line 509  var E = (function () { Line 530  var E = (function () {
530          }          }
531          return null;          return null;
532      };      };
533      E.prototype.update = function () {      E.prototype.update = function (t) {
534      };      };
535      E.prototype.interval = function () {      E.prototype.interval = function () {
536      };      };
# Line 621  var Trigger = (function () { Line 642  var Trigger = (function () {
642              });              });
643          }          }
644      };      };
645        Trigger.prototype.destroy = function () {
646            this.handlers = new Array();
647        };
648      Trigger.prototype.removeAll = function (owner) {      Trigger.prototype.removeAll = function (owner) {
649          var ret = [];          var ret = [];
650          var tmp;          var tmp;
# Line 728  var Resource = (function () { Line 752  var Resource = (function () {
752  })();  })();
753  var Scene = (function () {  var Scene = (function () {
754      function Scene(game) {      function Scene(game) {
755            this.game = game;
756          this.layers = {          this.layers = {
757          };          };
758          this.layers["root"] = new Layer(this);          this.layers["root"] = new Layer(this);
759          this.layerCount = 1;          this.layerCount = 1;
         this.game = game;  
760          this.mode = new Array();          this.mode = new Array();
761          this.started = new Trigger();          this.started = new Trigger();
762          this.ended = new Trigger();          this.ended = new Trigger();
# Line 777  var Scene = (function () { Line 801  var Scene = (function () {
801          }          }
802          var mode = this.mode.pop();          var mode = this.mode.pop();
803          if(mode) {          if(mode) {
             if(this[mode + "Hide"]) {  
                 this[mode + "Hide"]();  
             }  
804              if(this[mode + "End"]) {              if(this[mode + "End"]) {
805                  this[mode + "End"]();                  this[mode + "End"]();
806              }              }
# Line 882  var Sprite = (function (_super) { Line 903  var Sprite = (function (_super) {
903      };      };
904      return Sprite;      return Sprite;
905  })(E);  })(E);
906  var CharacterAngle;  var Angle;
907  (function (CharacterAngle) {  (function (Angle) {
908      CharacterAngle._map = [];      Angle._map = [];
909      CharacterAngle._map[0] = "left";      Angle._map[0] = "left";
910      CharacterAngle.left = 0;      Angle.left = 0;
911      CharacterAngle._map[1] = "right";      Angle._map[1] = "right";
912      CharacterAngle.right = 1;      Angle.right = 1;
913      CharacterAngle._map[2] = "up";      Angle._map[2] = "up";
914      CharacterAngle.up = 2;      Angle.up = 2;
915      CharacterAngle._map[3] = "down";      Angle._map[3] = "down";
916      CharacterAngle.down = 3;      Angle.down = 3;
917  })(CharacterAngle || (CharacterAngle = {}));  })(Angle || (Angle = {}));
918  var Character = (function (_super) {  var Character = (function (_super) {
919      __extends(Character, _super);      __extends(Character, _super);
920      function Character(width, height, image, wait) {      function Character(width, height, image, wait) {
# Line 903  var Character = (function (_super) { Line 924  var Character = (function (_super) {
924              wait = 200;              wait = 200;
925          }          }
926          this.startTimer(wait);          this.startTimer(wait);
927            this.animeCnt = 2;
928            this.charaSeq = 0;
929            this.charaCol = 1;
930            this.movePixel = 64;
931            this.moveTime = 300;
932      }      }
933      Character.prototype.moveLeft = function (stackNext) {      Character.prototype.moveLeft = function (stackNext) {
934          if(this.move(-64, 0, 20)) {          if(this.move(-this.movePixel, 0, this.moveTime)) {
935              this.angle(CharacterAngle.left);              this.angle(Angle.left);
936              return true;              return true;
937          }          }
938          if(stackNext && this.moveInfo.t * 2 >= this.moveInfo.f) {          if(stackNext && this.moveInfo.t * 2 >= this.moveInfo.f) {
# Line 915  var Character = (function (_super) { Line 941  var Character = (function (_super) {
941          return false;          return false;
942      };      };
943      Character.prototype.moveRight = function (stackNext) {      Character.prototype.moveRight = function (stackNext) {
944          if(this.move(64, 0, 20)) {          if(this.move(this.movePixel, 0, this.moveTime)) {
945              this.angle(CharacterAngle.right);              this.angle(Angle.right);
946              return true;              return true;
947          }          }
948          if(stackNext && this.moveInfo.t * 2 >= this.moveInfo.f) {          if(stackNext && this.moveInfo.t * 2 >= this.moveInfo.f) {
# Line 925  var Character = (function (_super) { Line 951  var Character = (function (_super) {
951          return false;          return false;
952      };      };
953      Character.prototype.moveUp = function (stackNext) {      Character.prototype.moveUp = function (stackNext) {
954          if(this.move(0, -64, 20)) {          if(this.move(0, -this.movePixel, this.moveTime)) {
955              this.angle(CharacterAngle.up);              this.angle(Angle.up);
956              return true;              return true;
957          }          }
958          if(stackNext && this.moveInfo.t * 2 >= this.moveInfo.f) {          if(stackNext && this.moveInfo.t * 2 >= this.moveInfo.f) {
# Line 935  var Character = (function (_super) { Line 961  var Character = (function (_super) {
961          return false;          return false;
962      };      };
963      Character.prototype.moveDown = function (stackNext) {      Character.prototype.moveDown = function (stackNext) {
964          if(this.move(0, 64, 20)) {          if(this.move(0, 64, this.moveTime)) {
965              this.angle(CharacterAngle.down);              this.angle(Angle.down);
966              return true;              return true;
967          }          }
968          if(stackNext && this.moveInfo.t * 2 >= this.moveInfo.f) {          if(stackNext && this.moveInfo.t * 2 >= this.moveInfo.f) {
# Line 957  var Character = (function (_super) { Line 983  var Character = (function (_super) {
983              f: f,              f: f,
984              t: 0              t: 0
985          };          };
         this.dx = x;  
         this.dy = y;  
986          this.start();          this.start();
987          return true;          return true;
988      };      };
989      Character.prototype.update = function () {      Character.prototype.update = function (t) {
990          if(this.moving) {          if(this.moving) {
991              this.moveInfo.t++;              this.moveInfo.t += t;
992              if(this.moveInfo.t < this.moveInfo.f) {              if(this.moveInfo.t < this.moveInfo.f) {
993                  this.moveTo(this.moveInfo.x + Math.round((this.moveInfo.dx - this.moveInfo.x) / this.moveInfo.f * this.moveInfo.t), this.moveInfo.y + Math.round((this.moveInfo.dy - this.moveInfo.y) / this.moveInfo.f * this.moveInfo.t));                  this.moveTo(this.moveInfo.x + Math.round((this.moveInfo.dx - this.moveInfo.x) / this.moveInfo.f * this.moveInfo.t), this.moveInfo.y + Math.round((this.moveInfo.dy - this.moveInfo.y) / this.moveInfo.f * this.moveInfo.t));
994              } else {              } else {
995                  if(this.moveInfo.t == this.moveInfo.f) {                  if(this.moveInfo.t >= this.moveInfo.f) {
996                      this.moveTo(this.moveInfo.dx, this.moveInfo.dy);                      this.moveTo(this.moveInfo.dx, this.moveInfo.dy);
997                      this.endMove();                      this.endMove();
998                  }                  }
# Line 993  var Character = (function (_super) { Line 1017  var Character = (function (_super) {
1017      };      };
1018      Character.prototype.angle = function (angle) {      Character.prototype.angle = function (angle) {
1019          var f;          var f;
1020            var rowP = Math.floor(this.charaSeq / this.charaCol) * 4;
1021          switch(angle) {          switch(angle) {
1022              case CharacterAngle.up: {              case Angle.up: {
1023                  f = 3 * 3;                  f = this.animeCnt * (this.charaSeq % this.charaCol) + this.charaCol * this.animeCnt * (3 + rowP);
1024                  break;                  break;
1025    
1026              }              }
1027              case CharacterAngle.down: {              case Angle.down: {
1028                  f = 0 * 3;                  f = this.animeCnt * (this.charaSeq % this.charaCol) + this.charaCol * this.animeCnt * (0 + rowP);
1029                  break;                  break;
1030    
1031              }              }
1032              case CharacterAngle.left: {              case Angle.left: {
1033                  f = 3 * 1;                  f = this.animeCnt * (this.charaSeq % this.charaCol) + this.charaCol * this.animeCnt * (1 + rowP);
1034                  break;                  break;
1035    
1036              }              }
1037              case CharacterAngle.right: {              case Angle.right: {
1038                  f = 3 * 2;                  f = this.animeCnt * (this.charaSeq % this.charaCol) + this.charaCol * this.animeCnt * (2 + rowP);
1039                  break;                  break;
1040    
1041              }              }
1042          }          }
1043          this.frame = [          this.frame = [];
1044              0 + f,          if(this.animeCnt % 2 == 1) {
1045              1 + f,              for(var i = 0; i < this.animeCnt; i++) {
1046              2 + f,                  this.frame.push(i + f);
1047              1 + f              }
1048          ];              for(var i = this.animeCnt - 2; i > 0; i--) {
1049                    this.frame.push(i + f);
1050                }
1051            } else {
1052                for(var i = 0; i < this.animeCnt; i++) {
1053                    this.frame.push(i + f);
1054                }
1055            }
1056      };      };
1057      Character.prototype.interval = function () {      Character.prototype.interval = function () {
1058          this.fno = (this.fno + 1) % this.frame.length;          this.fno = (this.fno + 1) % this.frame.length;
# Line 1028  var Character = (function (_super) { Line 1060  var Character = (function (_super) {
1060      };      };
1061      return Character;      return Character;
1062  })(Sprite);  })(Sprite);
1063    var CharacterFactory = (function () {
1064        function CharacterFactory(width, height, image) {
1065            this.width = width;
1066            this.height = height;
1067            this.image = image;
1068            this.wait = 200;
1069            this.charaCol = 1;
1070            this.animeCnt = 2;
1071            this.moveTime = 20;
1072            this.movePixel = 64;
1073            this.angle = Angle.down;
1074            this.createClass = Character;
1075        }
1076        CharacterFactory.prototype.create = function (charaSeq, offset, angle) {
1077            var c = new this.createClass(this.width, this.height, this.image, this.wait);
1078            c.charaSeq = charaSeq;
1079            c.charaCol = this.charaCol;
1080            c.animeCnt = this.animeCnt;
1081            c.moveTime = this.moveTime;
1082            c.movePixel = this.movePixel;
1083            c.angle((angle == undefined) ? this.angle : angle);
1084            if(offset) {
1085                c.moveTo(offset.x, offset.y);
1086            }
1087            return c;
1088        };
1089        return CharacterFactory;
1090    })();
1091  var Label = (function (_super) {  var Label = (function (_super) {
1092      __extends(Label, _super);      __extends(Label, _super);
1093      function Label(text, fontSize, fontColor, baseline) {      function Label(text, fontSize, fontColor, baseline) {
# Line 1155  var Tile = (function (_super) { Line 1215  var Tile = (function (_super) {
1215              }              }
1216          }          }
1217      };      };
     Tile.prototype.createSprite = function (area) {  
         var canvas = window.createCanvas(area.width, area.height);  
         var context = canvas.getContext("2d");  
         context.drawImage(this.canvas, area.x, area.y, area.width, area.height, 0, 0, area.width, area.height);  
         return new Sprite(area.width, area.height, canvas);  
     };  
1218      Tile.prototype.draw = function (area, context) {      Tile.prototype.draw = function (area, context) {
1219          var src = new Area(-area.x, -area.y, area.width, area.height);          var src = new Area(-area.x, -area.y, area.width, area.height);
1220          var dist = new Area(0, 0, area.width, area.height);          var dist = new Area(0, 0, area.width, area.height);
# Line 1212  var Layer = (function (_super) { Line 1266  var Layer = (function (_super) {
1266          this.x = 0;          this.x = 0;
1267          this.y = 0;          this.y = 0;
1268          this.scene = scene;          this.scene = scene;
1269            this.width = this.scene.game.width;
1270            this.height = this.scene.game.height;
1271          this.isUpdated = true;          this.isUpdated = true;
1272      }      }
1273      Layer.prototype.hasBuffer = function () {      Layer.prototype.hasBuffer = function () {
# Line 1225  var Layer = (function (_super) { Line 1281  var Layer = (function (_super) {
1281      };      };
1282      Layer.prototype.refresh = function (must) {      Layer.prototype.refresh = function (must) {
1283          if(must || this.hasBuffer()) {          if(must || this.hasBuffer()) {
1284              this.canvas = window.createCanvas(this.scene.game.width, this.scene.game.height);              this.canvas = window.createCanvas(this.width, this.height);
1285              this.context = this.canvas.getContext("2d");              this.context = this.canvas.getContext("2d");
1286              this.updated();              this.updated();
1287          }          }
# Line 1328  var InputPointEvent = (function (_super) Line 1384  var InputPointEvent = (function (_super)
1384      return InputPointEvent;      return InputPointEvent;
1385  })(InputEvent);  })(InputEvent);
1386  var Renderer = (function () {  var Renderer = (function () {
1387      function Renderer(game) {      function Renderer() {
1388          var _this = this;          var _this = this;
         this.game = game;  
         var canvas = window.createCanvas(this.game.width, this.game.height);  
         var exists_container = document.getElementById("jgame");  
         if(!exists_container) {  
             var div = document.createElement("div");  
             div.id = "jgame";  
             var bodies = document.getElementsByTagName("body");  
             if(bodies.length == 0) {  
                 throw "can not initialize game engine";  
             }  
             bodies[0].appendChild(div);  
             exists_container = div;  
         }  
         exists_container.appendChild(canvas);  
         this.front = canvas;  
         this.fc = this.front.getContext("2d");  
         this.bg = this.fc.getImageData(0, 0, this.game.width, this.game.height);  
         for(var i = 0; i < this.bg.data.length; i++) {  
             this.bg.data[i] = 255;  
         }  
         this.buffer = window.createCanvas(this.game.width, this.game.height);  
         this.bc = this.buffer.getContext("2d");  
1389          this.radian = Math.PI / 180;          this.radian = Math.PI / 180;
1390          this.drawOptionFunctions = {          this.drawOptionFunctions = {
1391              transform: function (c, entity, params) {              transform: function (c, entity, params) {
# Line 1368  var Renderer = (function () { Line 1402  var Renderer = (function () {
1402              }              }
1403          };          };
1404      }      }
     Renderer.prototype.changeScene = function (scene) {  
         this.scene = scene;  
     };  
1405      Renderer.prototype.getMatrix = function (width, height, scaleX, scaleY, angle) {      Renderer.prototype.getMatrix = function (width, height, scaleX, scaleY, angle) {
1406          var r = angle / 180 * Math.PI;          var r = angle * this.radian;
1407          var _cos = Math.cos(r);          var _cos = Math.cos(r);
1408          var _sin = Math.sin(r);          var _sin = Math.sin(r);
1409          var a = _cos * scaleX;          var a = _cos * scaleX;
# Line 1390  var Renderer = (function () { Line 1421  var Renderer = (function () {
1421              (-b * w - d * h + h)              (-b * w - d * h + h)
1422          ];          ];
1423      };      };
1424      Renderer.prototype.render = function () {      Renderer.prototype.renderParent = function (parent, c, area) {
1425            if(!area) {
1426                area = new Area(Math.round(parent.x), Math.round(parent.y), parent.width, parent.height);
1427            }
1428            c.save();
1429            if(parent.options) {
1430                this.useDrawOption(parent, c);
1431            }
1432            for(var i = 0; i < parent.entities.length; i++) {
1433                this.renderEntity(area, parent.entities[i], c);
1434            }
1435            c.restore();
1436        };
1437        Renderer.prototype.renderEntity = function (area, entity, c) {
1438            if(entity.disableTransform) {
1439                entity.draw(area, c);
1440            } else {
1441                c.save();
1442                c.translate(area.x + entity.x, area.y + entity.y);
1443                if(entity.options) {
1444                    this.useDrawOption(entity, c);
1445                }
1446                entity.draw(area, c);
1447                c.restore();
1448            }
1449        };
1450        Renderer.prototype.useDrawOption = function (entity, c) {
1451            for(var p in entity.options) {
1452                if(this.drawOptionFunctions[p]) {
1453                    this.drawOptionFunctions[p].call(this, c, entity, entity.options[p]);
1454                } else {
1455                    c[p] = entity.options[p];
1456                }
1457            }
1458        };
1459        return Renderer;
1460    })();
1461    var RenderTransferMode;
1462    (function (RenderTransferMode) {
1463        RenderTransferMode._map = [];
1464        RenderTransferMode._map[0] = "Transfer";
1465        RenderTransferMode.Transfer = 0;
1466        RenderTransferMode._map[1] = "Flip";
1467        RenderTransferMode.Flip = 1;
1468        RenderTransferMode._map[2] = "Direct";
1469        RenderTransferMode.Direct = 2;
1470    })(RenderTransferMode || (RenderTransferMode = {}));
1471    var GameRenderer = (function (_super) {
1472        __extends(GameRenderer, _super);
1473        function GameRenderer(game, container, transferMode) {
1474            _super.call(this);
1475            this.game = game;
1476            this.container = container ? container : document.getElementById("jgame");
1477            if(!this.container) {
1478                var div = document.createElement("div");
1479                div.id = "jgame";
1480                var bodies = document.getElementsByTagName("body");
1481                if(bodies.length == 0) {
1482                    throw "can not initialize game engine";
1483                }
1484                bodies[0].appendChild(div);
1485                this.container = div;
1486            }
1487            this.changeTransferMode(transferMode ? transferMode : RenderTransferMode.Transfer);
1488            this.bg = this.fc.getImageData(0, 0, this.game.width, this.game.height);
1489            for(var i = 0; i < this.bg.data.length; i++) {
1490                this.bg.data[i] = 255;
1491            }
1492        }
1493        GameRenderer.prototype.changeTransferMode = function (mode) {
1494            this.transferMode = mode;
1495            if(this.transferMode == RenderTransferMode.Flip) {
1496                this.container.style.position = "relative";
1497                this.container.style.width = this.game.width + "px";
1498                this.container.style.height = this.game.height + "px";
1499            }
1500            this.refresh();
1501        };
1502        GameRenderer.prototype.changeScene = function (scene) {
1503            this.scene = scene;
1504        };
1505        GameRenderer.prototype.flip = function () {
1506            var c = this.fc;
1507            this.fc = this.bc;
1508            this.bc = this.fc;
1509            this.flipNo = this.flipNo ? 0 : 1;
1510            this.buffer[this.flipNo].style.zIndex = "1";
1511            this.buffer[this.flipNo ? 0 : 1].style.zIndex = "0";
1512        };
1513        GameRenderer.prototype.render = function () {
1514          if(this.scene.layerCount == 1) {          if(this.scene.layerCount == 1) {
1515              var layer = this.scene.layers["root"];              var layer = this.scene.layers["root"];
1516              if(!layer.isUpdate()) {              if(!layer.isUpdate()) {
1517              } else {              } else {
1518                  this.bc.putImageData(this.bg, 0, 0);                  if(!this.disableClear) {
1519                  this.renderLayer(layer, this.bc);                      this.bc.putImageData(this.bg, 0, 0);
1520                    }
1521                    this.renderParent(layer, this.bc);
1522                  layer.reflected();                  layer.reflected();
1523                  this.fc.drawImage(this.buffer, 0, 0);                  if(this.transferMode == RenderTransferMode.Flip) {
1524                        this.flip();
1525                    } else {
1526                        if(this.bc != this.fc) {
1527                            this.fc.drawImage(this.buffer[1], 0, 0);
1528                        }
1529                    }
1530              }              }
1531          } else {          } else {
1532              var hasUpdate = false;              var hasUpdate = false;
# Line 1409  var Renderer = (function () { Line 1537  var Renderer = (function () {
1537                  }                  }
1538              }              }
1539              if(hasUpdate) {              if(hasUpdate) {
1540                  this.bc.putImageData(this.bg, 0, 0);                  if(!this.disableClear) {
1541                        this.bc.putImageData(this.bg, 0, 0);
1542                    }
1543                  for(var i in this.scene.layers) {                  for(var i in this.scene.layers) {
1544                      var layer = this.scene.layers[i];                      var layer = this.scene.layers[i];
1545                      if(layer.isUpdate()) {                      if(layer.isUpdate()) {
1546                          layer.context.clearRect(0, 0, this.game.width, this.game.height);                          layer.context.clearRect(0, 0, this.game.width, this.game.height);
1547                          this.renderLayer(layer, layer.context);                          this.renderParent(layer, layer.context);
1548                      }                      }
1549                      this.bc.drawImage(layer.canvas, 0, 0);                      this.bc.drawImage(layer.canvas, 0, 0);
1550                      layer.reflected();                      layer.reflected();
1551                  }                  }
1552                  this.fc.drawImage(this.buffer, 0, 0);                  if(this.transferMode == RenderTransferMode.Flip) {
1553              }                      this.flip();
         }  
     };  
     Renderer.prototype.refresh = function () {  
         this.buffer = window.createCanvas(this.game.width, this.game.height);  
         this.bc = this.buffer.getContext("2d");  
         this.front = window.createCanvas(this.game.width, this.game.height);  
         this.fc = this.front.getContext("2d");  
         var jgame = document.getElementById("jgame");  
         jgame.innerHTML = "";  
         jgame.appendChild(this.front);  
     };  
     Renderer.prototype.renderLayer = function (layer, c) {  
         var area = new Area(layer.x, layer.y, this.game.width, this.game.height);  
         var emptyArea = new Area(0, 0, this.game.width, this.game.height);  
         c.save();  
         if(layer.options) {  
             for(var p in layer.options) {  
                 if(this.drawOptionFunctions[p]) {  
                     this.drawOptionFunctions[p].call(this, c, layer, layer.options[p]);  
1554                  } else {                  } else {
1555                      c[p] = layer.options[p];                      if(this.bc != this.fc) {
1556                            this.fc.drawImage(this.buffer[1], 0, 0);
1557                        }
1558                  }                  }
1559              }              }
1560          }          }
1561          for(var i = 0; i < layer.entities.length; i++) {      };
1562              var entity = layer.entities[i];      GameRenderer.prototype.refresh = function () {
1563              if(entity.disableTransform) {          delete this.buffer;
1564                  layer.entities[i].draw(area, c);          this.buffer = new Array();
1565              } else {          if(this.transferMode == RenderTransferMode.Flip) {
1566                  if(entity.options) {              this.container.innerHTML = "";
1567                      c.save();              for(var i = 0; i < 2; i++) {
1568                      c.translate(area.x + entity.x, area.y + entity.y);                  this.buffer[i] = window.createCanvas(this.game.width, this.game.height);
1569                      for(var p in entity.options) {                  ; ;
1570                          if(this.drawOptionFunctions[p]) {                  this.buffer[i].style.position = "absolute";
1571                              this.drawOptionFunctions[p].call(this, c, entity, entity.options[p]);                  this.buffer[i].style.zIndex = i.toString();
1572                          } else {                  this.container.appendChild(this.buffer[i]);
1573                              c[p] = entity.options[p];              }
1574                          }              this.fc = this.buffer[1].getContext("2d");
1575                      }              this.bc = this.buffer[0].getContext("2d");
1576                      layer.entities[i].draw(area, c);              this.flipNo = 1;
1577                      c.restore();          } else {
1578                  } else {              if(this.transferMode == RenderTransferMode.Transfer) {
1579                      c.save();                  this.container.innerHTML = "";
1580                      c.translate(area.x + entity.x, area.y + entity.y);                  for(var i = 0; i < 2; i++) {
1581                      layer.entities[i].draw(area, c);                      this.buffer[i] = window.createCanvas(this.game.width, this.game.height);
1582                      c.restore();                      ; ;
1583                  }                  }
1584                    this.container.appendChild(this.buffer[0]);
1585                    this.fc = this.buffer[0].getContext("2d");
1586                    this.bc = this.buffer[1].getContext("2d");
1587                } else {
1588                    this.container.innerHTML = "";
1589                    this.buffer[0] = window.createCanvas(this.game.width, this.game.height);
1590                    ; ;
1591                    this.container.appendChild(this.buffer[0]);
1592                    this.fc = this.buffer[0].getContext("2d");
1593                    this.bc = this.fc;
1594              }              }
1595          }          }
         c.restore();  
1596      };      };
1597      return Renderer;      return GameRenderer;
1598  })();  })(Renderer);
1599    var BufferedRenderer = (function (_super) {
1600        __extends(BufferedRenderer, _super);
1601        function BufferedRenderer(size) {
1602            _super.call(this);
1603            this.size = size;
1604            this.refresh();
1605        }
1606        BufferedRenderer.prototype.clear = function () {
1607            this.c.clearRect(0, 0, this.size.w, this.size.h);
1608        };
1609        BufferedRenderer.prototype.createSprite = function (area) {
1610            var canvas = window.createCanvas(area.width, area.height);
1611            var context = canvas.getContext("2d");
1612            context.drawImage(this.buffer, area.x, area.y, area.width, area.height, 0, 0, area.width, area.height);
1613            return new Sprite(area.width, area.height, canvas);
1614        };
1615        BufferedRenderer.prototype.renderUnit = function (entity) {
1616            var area = new Area(0, 0, entity.width, entity.height);
1617            this.renderEntity(area, entity, this.c);
1618        };
1619        BufferedRenderer.prototype.renderLayer = function (layer) {
1620            this.renderParent(layer, this.c);
1621        };
1622        BufferedRenderer.prototype.renderScene = function (scene) {
1623            this.clear();
1624            for(var i in scene.layers) {
1625                this.renderLayer(scene.layers[i]);
1626            }
1627        };
1628        BufferedRenderer.prototype.refresh = function () {
1629            delete this.buffer;
1630            this.buffer = window.createCanvas(this.size.w, this.size.h);
1631            this.c = this.buffer.getContext("2d");
1632        };
1633        return BufferedRenderer;
1634    })(Renderer);
1635  var GameTimer = (function () {  var GameTimer = (function () {
1636      function GameTimer(wait) {      function GameTimer(wait) {
1637          this.wait = wait;          this.wait = wait;
# Line 1487  var GameTimer = (function () { Line 1645  var GameTimer = (function () {
1645      };      };
1646      GameTimer.prototype.fire = function (t) {      GameTimer.prototype.fire = function (t) {
1647          this.tick = t + this.wait;          this.tick = t + this.wait;
1648          this.trigger.fire(t);          this.trigger.fastFire(t);
1649      };      };
1650      return GameTimer;      return GameTimer;
1651  })();  })();
# Line 1496  var Game = (function () { Line 1654  var Game = (function () {
1654          this._exit = false;          this._exit = false;
1655          this.width = width;          this.width = width;
1656          this.height = height;          this.height = height;
         this.updateTime = 16;  
1657          this.targetFps = 0;          this.targetFps = 0;
1658          this.loaded = new Trigger();          this.loaded = new Trigger();
1659          this.update = new Trigger();          this.update = new Trigger();
         this.render = new Trigger();  
1660          this.inputDown = new Trigger();          this.inputDown = new Trigger();
1661          this.inputUp = new Trigger();          this.inputUp = new Trigger();
1662          this.inputMove = new Trigger();          this.inputMove = new Trigger();
# Line 1509  var Game = (function () { Line 1665  var Game = (function () {
1665          this.scenes = new Array();          this.scenes = new Array();
1666          this.scenes.push(this.currentScene);          this.scenes.push(this.currentScene);
1667          this.resource = Resource.getInstance();          this.resource = Resource.getInstance();
1668          this.renderer = new Renderer(this);          var container, transferMode;
1669            for(var i = 2; i < arguments.length; i++) {
1670                if(arguments[i] instanceof HTMLElement) {
1671                    container = arguments[i];
1672                } else {
1673                    transferMode = arguments[i];
1674                }
1675            }
1676            this.renderer = new GameRenderer(this, container, transferMode);
1677          this.renderer.changeScene(this.currentScene);          this.renderer.changeScene(this.currentScene);
1678          this.keyboardHandler();          this.keyboardHandler();
1679          this.pointHandler();          this.pointHandler();
# Line 1521  var Game = (function () { Line 1685  var Game = (function () {
1685      Game.prototype.refresh = function () {      Game.prototype.refresh = function () {
1686          if(document.addEventListener) {          if(document.addEventListener) {
1687              if(this.isTouchEnable()) {              if(this.isTouchEnable()) {
1688                  this.renderer.front.removeEventListener("mousedown", this.onmousedown, false);                  this.renderer.container.removeEventListener("mousedown", this.onmousedown, false);
1689                  this.renderer.front.removeEventListener("mousemove", this.onmousemove, false);                  this.renderer.container.removeEventListener("mousemove", this.onmousemove, false);
1690                  this.renderer.front.removeEventListener("mouseup", this.onmouseup, false);                  this.renderer.container.removeEventListener("mouseup", this.onmouseup, false);
1691              } else {              } else {
1692                  this.renderer.front.removeEventListener("mousedown", this.onmousedown, false);                  this.renderer.container.removeEventListener("mousedown", this.onmousedown, false);
1693                  this.renderer.front.removeEventListener("mousemove", this.onmousemove, false);                  this.renderer.container.removeEventListener("mousemove", this.onmousemove, false);
1694                  this.renderer.front.removeEventListener("mouseup", this.onmouseup, false);                  this.renderer.container.removeEventListener("mouseup", this.onmouseup, false);
1695              }              }
1696          } else {          } else {
1697              if(this.isTouchEnable()) {              if(this.isTouchEnable()) {
1698                  this.renderer.front.detachEvent("onmousedown", this.onmousedown);                  this.renderer.container.detachEvent("onmousedown", this.onmousedown);
1699                  this.renderer.front.detachEvent("onmousemove", this.onmousemove);                  this.renderer.container.detachEvent("onmousemove", this.onmousemove);
1700                  this.renderer.front.detachEvent("onmouseup", this.onmouseup);                  this.renderer.container.detachEvent("onmouseup", this.onmouseup);
1701              } else {              } else {
1702                  this.renderer.front.detachEvent("onmousedown", this.onmousedown);                  this.renderer.container.detachEvent("onmousedown", this.onmousedown);
1703                  this.renderer.front.detachEvent("onmousemove", this.onmousemove);                  this.renderer.container.detachEvent("onmousemove", this.onmousemove);
1704                  this.renderer.front.detachEvent("onmouseup", this.onmouseup);                  this.renderer.container.detachEvent("onmouseup", this.onmouseup);
1705              }              }
1706          }          }
1707          this.renderer.refresh();          this.renderer.refresh();
# Line 1603  var Game = (function () { Line 1767  var Game = (function () {
1767          };          };
1768          if(document.addEventListener) {          if(document.addEventListener) {
1769              if(this.isTouchEnable()) {              if(this.isTouchEnable()) {
1770                  this.renderer.front.addEventListener("mousedown", this.onmousedown, false);                  this.renderer.container.addEventListener("mousedown", this.onmousedown, false);
1771                  this.renderer.front.addEventListener("mousemove", this.onmousemove, false);                  this.renderer.container.addEventListener("mousemove", this.onmousemove, false);
1772                  this.renderer.front.addEventListener("mouseup", this.onmouseup, false);                  this.renderer.container.addEventListener("mouseup", this.onmouseup, false);
1773              } else {              } else {
1774                  this.renderer.front.addEventListener("mousedown", this.onmousedown, false);                  this.renderer.container.addEventListener("mousedown", this.onmousedown, false);
1775                  this.renderer.front.addEventListener("mousemove", this.onmousemove, false);                  this.renderer.container.addEventListener("mousemove", this.onmousemove, false);
1776                  this.renderer.front.addEventListener("mouseup", this.onmouseup, false);                  this.renderer.container.addEventListener("mouseup", this.onmouseup, false);
1777              }              }
1778          } else {          } else {
1779              if(this.isTouchEnable()) {              if(this.isTouchEnable()) {
1780                  this.renderer.front.attachEvent("onmousedown", onmousedown);                  this.renderer.container.attachEvent("onmousedown", onmousedown);
1781                  this.renderer.front.attachEvent("onmousemove", onmousemove);                  this.renderer.container.attachEvent("onmousemove", onmousemove);
1782                  this.renderer.front.attachEvent("onmouseup", onmouseup);                  this.renderer.container.attachEvent("onmouseup", onmouseup);
1783              } else {              } else {
1784                  this.renderer.front.attachEvent("onmousedown", onmousedown);                  this.renderer.container.attachEvent("onmousedown", onmousedown);
1785                  this.renderer.front.attachEvent("onmousemove", onmousemove);                  this.renderer.container.attachEvent("onmousemove", onmousemove);
1786                  this.renderer.front.attachEvent("onmouseup", onmouseup);                  this.renderer.container.attachEvent("onmouseup", onmouseup);
1787              }              }
1788          }          }
1789      };      };
# Line 1711  var Game = (function () { Line 1875  var Game = (function () {
1875      Game.prototype.r = function (name) {      Game.prototype.r = function (name) {
1876          return this.resource.get(name);          return this.resource.get(name);
1877      };      };
1878      Game.prototype.preloadArray = function (ary) {      Game.prototype.preloadArray = function (ary, loadingScene) {
1879          var param = {          var param = {
1880          };          };
1881          for(var i = 0; i < ary.length; i++) {          for(var i = 0; i < ary.length; i++) {
# Line 1719  var Game = (function () { Line 1883  var Game = (function () {
1883          }          }
1884          this.preload(param);          this.preload(param);
1885      };      };
1886      Game.prototype.preload = function (ary) {      Game.prototype.preload = function (ary, loadingScene) {
1887          for(var i in ary) {          for(var i in ary) {
1888              this.resource.load(i, ary[i]);              this.resource.load(i, ary[i]);
1889          }          }
1890          var loadingScene = new LoadingScene(this, this.resource);          if(!loadingScene) {
1891                loadingScene = new LoadingScene(this, this.resource);
1892            }
1893          this.changeScene(loadingScene);          this.changeScene(loadingScene);
1894          this.resource.loaded.handle(this, this.preloadComplete);          this.resource.loaded.handle(this, this.preloadComplete);
1895      };      };
# Line 1741  var Game = (function () { Line 1907  var Game = (function () {
1907              if(_this.tick > (t + 10000) || (_this.tick + 10000) < t) {              if(_this.tick > (t + 10000) || (_this.tick + 10000) < t) {
1908                  _this.tick = t - 1;                  _this.tick = t - 1;
1909                  _this.renderTick = t - _this.targetFps;                  _this.renderTick = t - _this.targetFps;
1910                    if(_this.enterFrame) {
1911                        _this.enterFrameTick = t - 1;
1912                    }
1913                  _this.refresh();                  _this.refresh();
1914              }              }
1915              while(_this.tick < t) {              if(_this.tick < t) {
1916                  _this.update.fire(_this.updateTime);                  _this.update.fire(t - _this.tick);
1917                  _this.tick += _this.updateTime;                  _this.tick = t;
1918                }
1919                if(_this.enterFrame) {
1920                    if(!_this.enterFrameTick) {
1921                        _this.enterFrameTick = t - 1;
1922                    }
1923                    while((_this.enterFrameTick + 16) < t) {
1924                        _this.enterFrameTick += 16;
1925                        _this.enterFrame.fire();
1926                    }
1927              }              }
1928              for(var i = 0; i < _this.timers.length; i++) {              for(var i = 0; i < _this.timers.length; i++) {
1929                  _this.timers[i].tryFire(t);                  _this.timers[i].tryFire(t);
# Line 1754  var Game = (function () { Line 1932  var Game = (function () {
1932                  if(_this.fps) {                  if(_this.fps) {
1933                      fps_stack.shift();                      fps_stack.shift();
1934                      fps_stack.push(t);                      fps_stack.push(t);
1935                      _this.fps.innerHTML = Math.round(60000 / (t - fps_stack[0])).toString();                      _this.fps.innerHTML = Math.round(20000 / (t - fps_stack[0])).toString();
1936                    }
1937                    if(_this.render) {
1938                        _this.render.fire();
1939                  }                  }
                 _this.render.fire();  
1940                  _this.renderer.render();                  _this.renderer.render();
1941                  _this.renderTick = t;                  _this.renderTick = t;
1942              }              }
# Line 1767  var Game = (function () { Line 1947  var Game = (function () {
1947          this.tick = window.getTime();          this.tick = window.getTime();
1948          this.renderTick = this.tick - this.targetFps;          this.renderTick = this.tick - this.targetFps;
1949          if(this.fps) {          if(this.fps) {
1950              for(var i = 0; i < 60; i++) {              for(var i = 0; i < 20; i++) {
1951                  fps_stack.push(0);                  fps_stack.push(0);
1952              }              }
1953          }          }
# Line 1776  var Game = (function () { Line 1956  var Game = (function () {
1956      return Game;      return Game;
1957  })();  })();
1958  ; ;  ; ;
1959    var FrameGame = (function (_super) {
1960        __extends(FrameGame, _super);
1961        function FrameGame(width, height, fps) {
1962            _super.call(this, width, height);
1963            this._fps = fps;
1964            this.targetFps = Math.floor(1000 / this._fps);
1965            Timeline.prototype.isFrameBased = true;
1966        }
1967        FrameGame.prototype.main = function () {
1968            var _this = this;
1969            var fps_stack = new Array();
1970            var _main = function () {
1971                var t = window.getTime();
1972                if(_this.tick > (t + 10000) || (_this.tick + 10000) < t) {
1973                    _this.tick = t - 1;
1974                    _this.renderTick = t - _this.targetFps;
1975                    _this.refresh();
1976                }
1977                for(var i = 0; i < _this.timers.length; i++) {
1978                    _this.timers[i].tryFire(t);
1979                }
1980                if((_this.renderTick + _this.targetFps) <= t) {
1981                    if(_this.fps) {
1982                        fps_stack.shift();
1983                        fps_stack.push(t);
1984                        _this.fps.innerHTML = Math.round(60000 / (t - fps_stack[0])).toString();
1985                    }
1986                    if(_this.enterFrame) {
1987                        _this.enterFrame.fire();
1988                    }
1989                    _this.update.fire(t - _this.tick);
1990                    _this.tick = t;
1991                    if(_this.render) {
1992                        _this.render.fire();
1993                    }
1994                    _this.renderer.render();
1995                    _this.renderTick = t;
1996                }
1997                if(!_this._exit) {
1998                    window.requestAnimationFrame(_main);
1999                }
2000            };
2001            this.tick = window.getTime();
2002            this.renderTick = this.tick - this.targetFps;
2003            if(this.fps) {
2004                for(var i = 0; i < 60; i++) {
2005                    fps_stack.push(0);
2006                }
2007            }
2008            window.requestAnimationFrame(_main);
2009        };
2010        return FrameGame;
2011    })(Game);
2012  var Action = (function () {  var Action = (function () {
2013      function Action(param) {      function Action(param) {
2014          this.added_to_timeline = new Trigger();          this.added_to_timeline = new Trigger();
# Line 2000  var Timeline = (function () { Line 2233  var Timeline = (function () {
2233          this.queue = new Array();          this.queue = new Array();
2234          this.paused = false;          this.paused = false;
2235          this.looped = false;          this.looped = false;
         this.isFrameBased = false;  
2236          this._parallel = null;          this._parallel = null;
2237          this._activated = false;          this._activated = false;
2238      }      }
# Line 2344  var Timeline = (function () { Line 2576  var Timeline = (function () {
2576      };      };
2577      return Timeline;      return Timeline;
2578  })();  })();
2579    Timeline.prototype.isFrameBased = false;

Legend:
Removed from v.4  
changed lines
  Added in v.7

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