Advertisement
daniel-k

board game

Jan 21st, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var ctx = null;
  2. var gameMap = [
  3.     0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  4.     0, 1, 1, 1, 1, 1, 1, 1, 1, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0,
  5.     0, 2, 2, 2, 1, 1, 1, 1, 1, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0,
  6.     0, 1, 1, 2, 1, 0, 0, 0, 0, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0,
  7.     0, 1, 1, 2, 1, 0, 2, 2, 0, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0,
  8.     0, 1, 1, 2, 1, 0, 2, 2, 0, 4, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
  9.     0, 1, 1, 2, 2, 2, 2, 2, 0, 4, 4, 4, 1, 1, 1, 0, 2, 2, 2, 0,
  10.     0, 1, 1, 2, 1, 0, 2, 2, 0, 1, 1, 4, 1, 1, 1, 0, 2, 2, 2, 0,
  11.     0, 1, 1, 2, 1, 0, 2, 2, 0, 1, 1, 4, 1, 1, 1, 0, 2, 2, 2, 0,
  12.     0, 1, 1, 2, 1, 0, 0, 0, 0, 1, 1, 4, 1, 1, 0, 0, 0, 2, 0, 0,
  13.     0, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 4, 1, 1, 0, 2, 2, 2, 2, 0,
  14.     0, 1, 1, 2, 2, 2, 2, 2, 2, 1, 4, 4, 1, 1, 0, 2, 2, 2, 2, 0,
  15.     0, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0,
  16.     0, 1, 1, 1, 1, 1, 1, 1, 1, 4, 4, 1, 1, 1, 0, 2, 2, 2, 2, 0,
  17.     0, 1, 1, 1, 1, 1, 1, 1, 1, 4, 1, 1, 1, 1, 0, 2, 2, 2, 2, 0,
  18.     0, 1, 1, 1, 1, 1, 1, 1, 1, 4, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,
  19.     0, 1, 1, 1, 1, 1, 1, 1, 1, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0,
  20.     0, 1, 1, 1, 1, 1, 1, 1, 1, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0,
  21.     0, 1, 1, 1, 1, 1, 1, 1, 1, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0,
  22.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  23. ];
  24. var mapTileData = new TileMap();
  25.  
  26. var roofList = [
  27.     { x:5, y:3, w:4, h:7, data: [
  28.         10, 10, 11, 11,
  29.         10, 10, 11, 11,
  30.         10, 10, 11, 11,
  31.         10, 10, 11, 11,
  32.         10, 10, 11, 11,
  33.         10, 10, 11, 11,
  34.         10, 10, 11, 11
  35.     ]},
  36.     { x:15, y:5, w:5, h:4, data: [
  37.         10, 10, 11, 11, 11,
  38.         10, 10, 11, 11, 11,
  39.         10, 10, 11, 11, 11,
  40.         10, 10, 11, 11, 11
  41.     ]},
  42.     { x:14, y:9, w:6, h:7, data: [
  43.         10, 10, 10, 11, 11, 11,
  44.         10, 10, 10, 11, 11, 11,
  45.         10, 10, 10, 11, 11, 11,
  46.         10, 10, 10, 11, 11, 11,
  47.         10, 10, 10, 11, 11, 11,
  48.         10, 10, 10, 11, 11, 11,
  49.         10, 10, 10, 11, 11, 11
  50.     ]}
  51. ];
  52.  
  53. var tileW = 40, tileH = 40;
  54. var mapW = 20, mapH = 20;
  55. var currentSecond = 0, frameCount = 0, framesLastSecond = 0, lastFrameTime = 0;
  56.  
  57. var tileset = null, tilesetURL = "tileset.png", tilesetLoaded = false;
  58.  
  59. var gameTime = 0;
  60. var gameSpeeds = [
  61.     {name:"Normal", mult:1},
  62.     {name:"Slow", mult:0.3},
  63.     {name:"Fast", mult:3},
  64.     {name:"Paused", mult:0}
  65. ];
  66. var currentSpeed = 0;
  67.  
  68. function Sprite(data)
  69. {
  70.     this.animated   = data.length > 1;
  71.     this.frameCount = data.length;
  72.     this.duration   = 0;
  73.     this.loop       = true;
  74.    
  75.     if(data.length > 1)
  76.     {
  77.         for(var i in data)
  78.         {
  79.             if(typeof data[i].d=='undefined')
  80.             {
  81.                 data[i].d = 100;
  82.             }
  83.             this.duration+= data[i].d;
  84.            
  85.             if(typeof data[i].loop!='undefined')
  86.             {
  87.                 this.loop = data[i].loop ? true : false;
  88.             }
  89.         }
  90.     }
  91.    
  92.     this.frames     = data;
  93. }
  94. Sprite.prototype.draw = function(t, x, y)
  95. {
  96.     var frameIdx = 0;
  97.    
  98.     if(!this.loop && this.animated && t>=this.duration)
  99.     {
  100.         frameIdx = (this.frames.length - 1);
  101.     }
  102.     else if(this.animated)
  103.     {
  104.         t = t % this.duration;
  105.         var totalD = 0;
  106.        
  107.         for(var i in this.frames)
  108.         {
  109.             totalD+= this.frames[i].d;
  110.             frameIdx = i;
  111.            
  112.             if(t<=totalD)
  113.             {
  114.                 break;
  115.             }
  116.         }
  117.     }
  118.    
  119.     var offset = (typeof this.frames[frameIdx].offset=='undefined' ?
  120.         [0,0] : this.frames[frameIdx].offset);
  121.    
  122.     ctx.drawImage(tileset,
  123.         this.frames[frameIdx].x, this.frames[frameIdx].y,
  124.         this.frames[frameIdx].w, this.frames[frameIdx].h,
  125.         x + offset[0], y + offset[1],
  126.         this.frames[frameIdx].w, this.frames[frameIdx].h);
  127. };
  128.  
  129. var itemTypes = {
  130.     1 : {
  131.         name : "Star",
  132.         maxStack : 2,
  133.         sprite : new Sprite([{x:240,y:0,w:40,h:40}]),
  134.         offset : [0,0]
  135.     }
  136. };
  137.  
  138. function Stack(id, qty)
  139. {
  140.     this.type = id;
  141.     this.qty = qty;
  142. }
  143. function Inventory(s)
  144. {
  145.     this.spaces     = s;
  146.     this.stacks     = [];
  147. }
  148. Inventory.prototype.addItems = function(id, qty)
  149. {
  150.     for(var i = 0; i < this.spaces; i++)
  151.     {
  152.         if(this.stacks.length<=i)
  153.         {
  154.             var maxHere = (qty > itemTypes[id].maxStack ?
  155.                 itemTypes[id].maxStack : qty);
  156.             this.stacks.push(new Stack(id, maxHere));
  157.            
  158.             qty-= maxHere;
  159.         }
  160.         else if(this.stacks[i].type == id &&
  161.             this.stacks[i].qty < itemTypes[id].maxStack)
  162.         {
  163.             var maxHere = (itemTypes[id].maxStack - this.stacks[i].qty);
  164.             if(maxHere > qty) { maxHere = qty; }
  165.            
  166.             this.stacks[i].qty+= maxHere;
  167.             qty-= maxHere;
  168.         }
  169.        
  170.         if(qty==0) { return 0; }
  171.     }
  172.    
  173.     return qty;
  174. };
  175.  
  176. function PlacedItemStack(id, qty)
  177. {
  178.     this.type = id;
  179.     this.qty = qty;
  180.     this.x = 0;
  181.     this.y = 0;
  182. }
  183. PlacedItemStack.prototype.placeAt = function(nx, ny)
  184. {
  185.     if(mapTileData.map[toIndex(this.x, this.y)].itemStack==this)
  186.     {
  187.         mapTileData.map[toIndex(this.x, this.y)].itemStack = null;
  188.     }
  189.    
  190.     this.x = nx;
  191.     this.y = ny;
  192.    
  193.     mapTileData.map[toIndex(nx, ny)].itemStack = this;
  194. };
  195.  
  196. var objectCollision = {
  197.     none        : 0,
  198.     solid       : 1
  199. };
  200. var objectTypes = {
  201.     1 : {
  202.         name : "Box",
  203.         sprite : new Sprite([{x:40,y:160,w:40,h:40}]),
  204.         offset : [0,0],
  205.         collision : objectCollision.solid,
  206.         zIndex : 1
  207.     },
  208.     2 : {
  209.         name : "Broken Box",
  210.         sprite : new Sprite([{x:40,y:200,w:40,h:40}]),
  211.         offset : [0,0],
  212.         collision : objectCollision.none,
  213.         zIndex : 1
  214.     },
  215.     3 : {
  216.         name : "Tree top",
  217.         sprite : new Sprite([{x:80,y:160,w:80,h:80}]),
  218.         offset : [-20,-20],
  219.         collision : objectCollision.solid,
  220.         zIndex : 3
  221.     }
  222. };
  223. function MapObject(nt)
  224. {
  225.     this.x      = 0;
  226.     this.y      = 0;
  227.     this.type   = nt;
  228. }
  229. MapObject.prototype.placeAt = function(nx, ny)
  230. {
  231.     if(mapTileData.map[toIndex(this.x, this.y)].object==this)
  232.     {
  233.         mapTileData.map[toIndex(this.x, this.y)].object = null;
  234.     }
  235.    
  236.     this.x = nx;
  237.     this.y = ny;
  238.    
  239.     mapTileData.map[toIndex(nx, ny)].object = this;
  240. };
  241.  
  242. var floorTypes = {
  243.     solid   : 0,
  244.     path    : 1,
  245.     water   : 2,
  246.     ice     : 3,
  247.     conveyorU   : 4,
  248.     conveyorD   : 5,
  249.     conveyorL   : 6,
  250.     conveyorR   : 7,
  251.     grass       : 8
  252. };
  253. var tileTypes = {
  254.     0 : { colour:"#685b48", floor:floorTypes.solid,
  255.         sprite:new Sprite([{x:0,y:0,w:40,h:40}])    },
  256.     1 : { colour:"#5aa457", floor:floorTypes.grass,
  257.         sprite:new Sprite([{x:40,y:0,w:40,h:40}])   },
  258.     2 : { colour:"#e8bd7a", floor:floorTypes.path,
  259.         sprite:new Sprite([{x:80,y:0,w:40,h:40}])   },
  260.     3 : { colour:"#286625", floor:floorTypes.solid,
  261.         sprite:new Sprite([{x:120,y:0,w:40,h:40}])  },
  262.     4 : { colour:"#678fd9", floor:floorTypes.water,
  263.         sprite:new Sprite([
  264.             {x:160,y:0,w:40,h:40,d:200}, {x:200,y:0,w:40,h:40,d:200},
  265.             {x:160,y:40,w:40,h:40,d:200}, {x:200,y:40,w:40,h:40,d:200},
  266.             {x:160,y:40,w:40,h:40,d:200}, {x:200,y:0,w:40,h:40,d:200}
  267.         ])},
  268.     5 : { colour:"#eeeeff", floor:floorTypes.ice,
  269.         sprite:new Sprite([{x:120,y:120,w:40,h:40}])    },
  270.     6 : { colour:"#cccccc", floor:floorTypes.conveyorL,
  271.         sprite:new Sprite([
  272.             {x:0,y:40,w:40,h:40,d:200}, {x:40,y:40,w:40,h:40,d:200},
  273.             {x:80,y:40,w:40,h:40,d:200}, {x:120,y:40,w:40,h:40,d:200}
  274.         ])},
  275.     7 : { colour:"#cccccc", floor:floorTypes.conveyorR,
  276.         sprite:new Sprite([
  277.             {x:120,y:80,w:40,h:40,d:200}, {x:80,y:80,w:40,h:40,d:200},
  278.             {x:40,y:80,w:40,h:40,d:200}, {x:0,y:80,w:40,h:40,d:200}
  279.         ])},
  280.     8 : { colour:"#cccccc", floor:floorTypes.conveyorD,
  281.         sprite:new Sprite([
  282.             {x:160,y:200,w:40,h:40,d:200}, {x:160,y:160,w:40,h:40,d:200},
  283.             {x:160,y:120,w:40,h:40,d:200}, {x:160,y:80,w:40,h:40,d:200}
  284.         ])},
  285.     9 : { colour:"#cccccc", floor:floorTypes.conveyorU,
  286.         sprite:new Sprite([
  287.             {x:200,y:80,w:40,h:40,d:200}, {x:200,y:120,w:40,h:40,d:200},
  288.             {x:200,y:160,w:40,h:40,d:200}, {x:200,y:200,w:40,h:40,d:200}
  289.         ])},
  290.    
  291.     10 : { colour:"#ccaa00", floor:floorTypes.solid,
  292.         sprite:new Sprite([{x:40,y:120,w:40,h:40}])},
  293.     11 : { colour:"#ccaa00", floor:floorTypes.solid,
  294.         sprite:new Sprite([{x:80,y:120,w:40,h:40}])}
  295. };
  296.  
  297. function Tile(tx, ty, tt)
  298. {
  299.     this.x          = tx;
  300.     this.y          = ty;
  301.     this.type       = tt;
  302.     this.roof       = null;
  303.     this.roofType   = 0;
  304.     this.eventEnter = null;
  305.     this.object     = null;
  306.     this.itemStack  = null;
  307. }
  308.  
  309. function TileMap()
  310. {
  311.     this.map    = [];
  312.     this.w      = 0;
  313.     this.h      = 0;
  314.     this.levels = 4;
  315. }
  316. TileMap.prototype.buildMapFromData = function(d, w, h)
  317. {
  318.     this.w      = w;
  319.     this.h      = h;
  320.    
  321.     if(d.length!=(w*h)) { return false; }
  322.    
  323.     this.map.length = 0;
  324.    
  325.     for(var y = 0; y < h; y++)
  326.     {
  327.         for(var x = 0; x < w; x++)
  328.         {
  329.             this.map.push( new Tile(x, y, d[((y*w)+x)]) );
  330.         }
  331.     }
  332.    
  333.     return true;
  334. };
  335. TileMap.prototype.addRoofs = function(roofs)
  336. {
  337.     for(var i in roofs)
  338.     {
  339.         var r = roofs[i];
  340.        
  341.         if(r.x < 0 || r.y < 0 || r.x >= this.w || r.y >= this.h ||
  342.             (r.x+r.w)>this.w || (r.y+r.h)>this.h ||
  343.             r.data.length!=(r.w*r.h))
  344.         {
  345.             continue;
  346.         }
  347.        
  348.         for(var y = 0; y < r.h; y++)
  349.         {
  350.             for(var x = 0; x < r.w; x++)
  351.             {
  352.                 var tileIdx = (((r.y+y)*this.w)+r.x+x);
  353.                
  354.                 this.map[tileIdx].roof = r;
  355.                 this.map[tileIdx].roofType = r.data[((y*r.w)+x)];
  356.             }
  357.         }
  358.     }
  359. };
  360.  
  361. var directions = {
  362.     up      : 0,
  363.     right   : 1,
  364.     down    : 2,
  365.     left    : 3
  366. };
  367.  
  368. var keysDown = {
  369.     37 : false,
  370.     38 : false,
  371.     39 : false,
  372.     40 : false,
  373.     80 : false
  374. };
  375.  
  376. var viewport = {
  377.     screen      : [0,0],
  378.     startTile   : [0,0],
  379.     endTile     : [0,0],
  380.     offset      : [0,0],
  381.     update      : function(px, py) {
  382.         this.offset[0] = Math.floor((this.screen[0]/2) - px);
  383.         this.offset[1] = Math.floor((this.screen[1]/2) - py);
  384.  
  385.         var tile = [ Math.floor(px/tileW), Math.floor(py/tileH) ];
  386.  
  387.         this.startTile[0] = tile[0] - 1 - Math.ceil((this.screen[0]/2) / tileW);
  388.         this.startTile[1] = tile[1] - 1 - Math.ceil((this.screen[1]/2) / tileH);
  389.  
  390.         if(this.startTile[0] < 0) { this.startTile[0] = 0; }
  391.         if(this.startTile[1] < 0) { this.startTile[1] = 0; }
  392.  
  393.         this.endTile[0] = tile[0] + 1 + Math.ceil((this.screen[0]/2) / tileW);
  394.         this.endTile[1] = tile[1] + 1 + Math.ceil((this.screen[1]/2) / tileH);
  395.  
  396.         if(this.endTile[0] >= mapW) { this.endTile[0] = mapW-1; }
  397.         if(this.endTile[1] >= mapH) { this.endTile[1] = mapH-1; }
  398.     }
  399. };
  400.  
  401. var player = new Character();
  402.  
  403. function Character()
  404. {
  405.     this.tileFrom   = [1,1];
  406.     this.tileTo     = [1,1];
  407.     this.timeMoved  = 0;
  408.     this.dimensions = [30,30];
  409.     this.position   = [45,45];
  410.  
  411.     this.delayMove  = {};
  412.     this.delayMove[floorTypes.path]         = 400;
  413.     this.delayMove[floorTypes.grass]        = 800;
  414.     this.delayMove[floorTypes.ice]          = 300;
  415.     this.delayMove[floorTypes.conveyorU]    = 200;
  416.     this.delayMove[floorTypes.conveyorD]    = 200;
  417.     this.delayMove[floorTypes.conveyorL]    = 200;
  418.     this.delayMove[floorTypes.conveyorR]    = 200;
  419.  
  420.     this.direction  = directions.up;
  421.     this.sprites = {};
  422.     this.sprites[directions.up]     = new Sprite([{x:0,y:120,w:30,h:30}]);
  423.     this.sprites[directions.right]  = new Sprite([{x:0,y:150,w:30,h:30}]);
  424.     this.sprites[directions.down]   = new Sprite([{x:0,y:180,w:30,h:30}]);
  425.     this.sprites[directions.left]   = new Sprite([{x:0,y:210,w:30,h:30}]);
  426.    
  427.     this.inventory = new Inventory(3);
  428. }
  429. Character.prototype.placeAt = function(x, y)
  430. {
  431.     this.tileFrom   = [x,y];
  432.     this.tileTo     = [x,y];
  433.     this.position   = [((tileW*x)+((tileW-this.dimensions[0])/2)),
  434.         ((tileH*y)+((tileH-this.dimensions[1])/2))];
  435. };
  436. Character.prototype.processMovement = function(t)
  437. {
  438.     if(this.tileFrom[0]==this.tileTo[0] && this.tileFrom[1]==this.tileTo[1]) { return false; }
  439.  
  440.     var moveSpeed = this.delayMove[tileTypes[mapTileData.map[toIndex(this.tileFrom[0],this.tileFrom[1])].type].floor];
  441.  
  442.     if((t-this.timeMoved)>=moveSpeed)
  443.     {
  444.         this.placeAt(this.tileTo[0], this.tileTo[1]);
  445.  
  446.         if(mapTileData.map[toIndex(this.tileTo[0], this.tileTo[1])].eventEnter!=null)
  447.         {
  448.             mapTileData.map[toIndex(this.tileTo[0], this.tileTo[1])].eventEnter(this);
  449.         }
  450.  
  451.         var tileFloor = tileTypes[mapTileData.map[toIndex(this.tileFrom[0], this.tileFrom[1])].type].floor;
  452.  
  453.         if(tileFloor==floorTypes.ice)
  454.         {
  455.             if(this.canMoveDirection(this.direction))
  456.             {
  457.                 this.moveDirection(this.direction, t);
  458.             }
  459.         }
  460.         else if(tileFloor==floorTypes.conveyorL && this.canMoveLeft())  { this.moveLeft(t); }
  461.         else if(tileFloor==floorTypes.conveyorR && this.canMoveRight()) { this.moveRight(t); }
  462.         else if(tileFloor==floorTypes.conveyorU && this.canMoveUp())    { this.moveUp(t); }
  463.         else if(tileFloor==floorTypes.conveyorD && this.canMoveDown())  { this.moveDown(t); }
  464.     }
  465.     else
  466.     {
  467.         this.position[0] = (this.tileFrom[0] * tileW) + ((tileW-this.dimensions[0])/2);
  468.         this.position[1] = (this.tileFrom[1] * tileH) + ((tileH-this.dimensions[1])/2);
  469.  
  470.         if(this.tileTo[0] != this.tileFrom[0])
  471.         {
  472.             var diff = (tileW / moveSpeed) * (t-this.timeMoved);
  473.             this.position[0]+= (this.tileTo[0]<this.tileFrom[0] ? 0 - diff : diff);
  474.         }
  475.         if(this.tileTo[1] != this.tileFrom[1])
  476.         {
  477.             var diff = (tileH / moveSpeed) * (t-this.timeMoved);
  478.             this.position[1]+= (this.tileTo[1]<this.tileFrom[1] ? 0 - diff : diff);
  479.         }
  480.  
  481.         this.position[0] = Math.round(this.position[0]);
  482.         this.position[1] = Math.round(this.position[1]);
  483.     }
  484.  
  485.     return true;
  486. }
  487. Character.prototype.canMoveTo = function(x, y)
  488. {
  489.     if(x < 0 || x >= mapW || y < 0 || y >= mapH) { return false; }
  490.     if(typeof this.delayMove[tileTypes[mapTileData.map[toIndex(x,y)].type].floor]=='undefined') { return false; }
  491.     if(mapTileData.map[toIndex(x,y)].object!=null)
  492.     {
  493.         var o = mapTileData.map[toIndex(x,y)].object;
  494.         if(objectTypes[o.type].collision==objectCollision.solid)
  495.         {
  496.             return false;
  497.         }
  498.     }
  499.     return true;
  500. };
  501. Character.prototype.canMoveUp       = function() { return this.canMoveTo(this.tileFrom[0], this.tileFrom[1]-1); };
  502. Character.prototype.canMoveDown     = function() { return this.canMoveTo(this.tileFrom[0], this.tileFrom[1]+1); };
  503. Character.prototype.canMoveLeft     = function() { return this.canMoveTo(this.tileFrom[0]-1, this.tileFrom[1]); };
  504. Character.prototype.canMoveRight    = function() { return this.canMoveTo(this.tileFrom[0]+1, this.tileFrom[1]); };
  505. Character.prototype.canMoveDirection = function(d) {
  506.     switch(d)
  507.     {
  508.         case directions.up:
  509.             return this.canMoveUp();
  510.         case directions.down:
  511.             return this.canMoveDown();
  512.         case directions.left:
  513.             return this.canMoveLeft();
  514.         default:
  515.             return this.canMoveRight();
  516.     }
  517. };
  518.  
  519. Character.prototype.moveLeft    = function(t) { this.tileTo[0]-=1; this.timeMoved = t; this.direction = directions.left; };
  520. Character.prototype.moveRight   = function(t) { this.tileTo[0]+=1; this.timeMoved = t; this.direction = directions.right; };
  521. Character.prototype.moveUp      = function(t) { this.tileTo[1]-=1; this.timeMoved = t; this.direction = directions.up; };
  522. Character.prototype.moveDown    = function(t) { this.tileTo[1]+=1; this.timeMoved = t; this.direction = directions.down; };
  523. Character.prototype.moveDirection = function(d, t) {
  524.     switch(d)
  525.     {
  526.         case directions.up:
  527.             return this.moveUp(t);
  528.         case directions.down:
  529.             return this.moveDown(t);
  530.         case directions.left:
  531.             return this.moveLeft(t);
  532.         default:
  533.             return this.moveRight(t);
  534.     }
  535. };
  536. Character.prototype.pickUp = function()
  537. {
  538.     if(this.tileTo[0]!=this.tileFrom[0] ||
  539.         this.tileTo[1]!=this.tileFrom[1])
  540.     {
  541.         return false;
  542.     }
  543.    
  544.     var is = mapTileData.map[toIndex(this.tileFrom[0],
  545.                 this.tileFrom[1])].itemStack;
  546.    
  547.     if(is!=null)
  548.     {
  549.         var remains = this.inventory.addItems(is.type, is.qty);
  550.  
  551.         if(remains) { is.qty = remains; }
  552.         else
  553.         {
  554.             mapTileData.map[toIndex(this.tileFrom[0],
  555.                 this.tileFrom[1])].itemStack = null;
  556.         }
  557.     }
  558.    
  559.     return true;
  560. };
  561.  
  562. function toIndex(x, y)
  563. {
  564.     return((y * mapW) + x);
  565. }
  566.  
  567. window.onload = function()
  568. {
  569.     ctx = document.getElementById('game').getContext("2d");
  570.     requestAnimationFrame(drawGame);
  571.     ctx.font = "bold 10pt sans-serif";
  572.  
  573.     window.addEventListener("keydown", function(e) {
  574.         if(e.keyCode>=37 && e.keyCode<=40) { keysDown[e.keyCode] = true; }
  575.         if(e.keyCode==80) { keysDown[e.keyCode] = true; }
  576.     });
  577.     window.addEventListener("keyup", function(e) {
  578.         if(e.keyCode>=37 && e.keyCode<=40) { keysDown[e.keyCode] = false; }
  579.         if(e.keyCode==83)
  580.         {
  581.             currentSpeed = (currentSpeed>=(gameSpeeds.length-1) ? 0 : currentSpeed+1);
  582.         }
  583.         if(e.keyCode==80) { keysDown[e.keyCode] = false; }
  584.     });
  585.  
  586.     viewport.screen = [document.getElementById('game').width,
  587.         document.getElementById('game').height];
  588.  
  589.     tileset = new Image();
  590.     tileset.onerror = function()
  591.     {
  592.         ctx = null;
  593.         alert("Failed loading tileset.");
  594.     };
  595.     tileset.onload = function() { tilesetLoaded = true; };
  596.     tileset.src = tilesetURL;
  597.    
  598.     mapTileData.buildMapFromData(gameMap, mapW, mapH);
  599.     mapTileData.addRoofs(roofList);
  600.     mapTileData.map[((2*mapW)+2)].eventEnter = function()
  601.         { console.log("Entered tile 2,2"); };
  602.    
  603.     var mo1 = new MapObject(1); mo1.placeAt(2, 4);
  604.     var mo2 = new MapObject(2); mo2.placeAt(2, 3);
  605.    
  606.     var mo11 = new MapObject(1); mo11.placeAt(6, 4);
  607.     var mo12 = new MapObject(2); mo12.placeAt(7, 4);
  608.    
  609.     var mo4 = new MapObject(3); mo4.placeAt(4, 5);
  610.     var mo5 = new MapObject(3); mo5.placeAt(4, 8);
  611.     var mo6 = new MapObject(3); mo6.placeAt(4, 11);
  612.    
  613.     var mo7 = new MapObject(3); mo7.placeAt(2, 6);
  614.     var mo8 = new MapObject(3); mo8.placeAt(2, 9);
  615.     var mo9 = new MapObject(3); mo9.placeAt(2, 12);
  616.    
  617.     for(var i = 3; i < 8; i++)
  618.     {
  619.         var ps = new PlacedItemStack(1, 1); ps.placeAt(i, 1);
  620.     }
  621.     for(var i = 3; i < 8; i++)
  622.     {
  623.         var ps = new PlacedItemStack(1, 1); ps.placeAt(3, i);
  624.     }
  625. };
  626.  
  627. function drawGame()
  628. {
  629.     if(ctx==null) { return; }
  630.     if(!tilesetLoaded) { requestAnimationFrame(drawGame); return; }
  631.  
  632.     var currentFrameTime = Date.now();
  633.     var timeElapsed = currentFrameTime - lastFrameTime;
  634.     gameTime+= Math.floor(timeElapsed * gameSpeeds[currentSpeed].mult);
  635.  
  636.     var sec = Math.floor(Date.now()/1000);
  637.     if(sec!=currentSecond)
  638.     {
  639.         currentSecond = sec;
  640.         framesLastSecond = frameCount;
  641.         frameCount = 1;
  642.     }
  643.     else { frameCount++; }
  644.  
  645.     if(!player.processMovement(gameTime) && gameSpeeds[currentSpeed].mult!=0)
  646.     {
  647.         if(keysDown[38] && player.canMoveUp())          { player.moveUp(gameTime); }
  648.         else if(keysDown[40] && player.canMoveDown())   { player.moveDown(gameTime); }
  649.         else if(keysDown[37] && player.canMoveLeft())   { player.moveLeft(gameTime); }
  650.         else if(keysDown[39] && player.canMoveRight())  { player.moveRight(gameTime); }
  651.         else if(keysDown[80]) { player.pickUp(); }
  652.     }
  653.  
  654.     viewport.update(player.position[0] + (player.dimensions[0]/2),
  655.         player.position[1] + (player.dimensions[1]/2));
  656.    
  657.     var playerRoof1 = mapTileData.map[toIndex(
  658.         player.tileFrom[0], player.tileFrom[1])].roof;
  659.     var playerRoof2 = mapTileData.map[toIndex(
  660.         player.tileTo[0], player.tileTo[1])].roof;
  661.  
  662.     ctx.fillStyle = "#000000";
  663.     ctx.fillRect(0, 0, viewport.screen[0], viewport.screen[1]);
  664.    
  665.     for(var z = 0; z < mapTileData.levels; z++)
  666.     {
  667.  
  668.     for(var y = viewport.startTile[1]; y <= viewport.endTile[1]; ++y)
  669.     {
  670.         for(var x = viewport.startTile[0]; x <= viewport.endTile[0]; ++x)
  671.         {
  672.             if(z==0)
  673.             {
  674.                 tileTypes[mapTileData.map[toIndex(x,y)].type].sprite.draw(
  675.                     gameTime,
  676.                     viewport.offset[0] + (x*tileW),
  677.                     viewport.offset[1] + (y*tileH));
  678.             }
  679.             else if(z==1)
  680.             {
  681.                 var is = mapTileData.map[toIndex(x,y)].itemStack;
  682.                 if(is!=null)
  683.                 {
  684.                     itemTypes[is.type].sprite.draw(
  685.                         gameTime,
  686.                         viewport.offset[0] + (x*tileW) + itemTypes[is.type].offset[0],
  687.                         viewport.offset[1] + (y*tileH) + itemTypes[is.type].offset[1]);
  688.                 }
  689.             }
  690.            
  691.             var o = mapTileData.map[toIndex(x,y)].object;
  692.             if(o!=null && objectTypes[o.type].zIndex==z)
  693.             {
  694.                 var ot = objectTypes[o.type];
  695.                
  696.                 ot.sprite.draw(gameTime,
  697.                     viewport.offset[0] + (x*tileW) + ot.offset[0],
  698.                     viewport.offset[1] + (y*tileH) + ot.offset[1]);
  699.             }
  700.            
  701.             if(z==2 &&
  702.                 mapTileData.map[toIndex(x,y)].roofType!=0 &&
  703.                 mapTileData.map[toIndex(x,y)].roof!=playerRoof1 &&
  704.                 mapTileData.map[toIndex(x,y)].roof!=playerRoof2)
  705.             {
  706.                 tileTypes[mapTileData.map[toIndex(x,y)].roofType].sprite.draw(
  707.                     gameTime,
  708.                     viewport.offset[0] + (x*tileW),
  709.                     viewport.offset[1] + (y*tileH));
  710.             }
  711.         }
  712.     }
  713.    
  714.         if(z==1)
  715.         {
  716.             player.sprites[player.direction].draw(
  717.                 gameTime,
  718.                 viewport.offset[0] + player.position[0],
  719.                 viewport.offset[1] + player.position[1]);
  720.         }
  721.    
  722.     }
  723.    
  724.     ctx.textAlign = "right";
  725.    
  726.     for(var i = 0; i < player.inventory.spaces; i++)
  727.     {
  728.         ctx.fillStyle = "#ddccaa";
  729.         ctx.fillRect(10 + (i * 50), 350,
  730.             40, 40);
  731.        
  732.         if(typeof player.inventory.stacks[i]!='undefined')
  733.         {
  734.             var it = itemTypes[player.inventory.stacks[i].type];
  735.            
  736.             it.sprite.draw(gameTime,
  737.                 10 + (i * 50) + it.offset[0],
  738.                 350 + it.offset[1]);
  739.            
  740.             if(player.inventory.stacks[i].qty>1)
  741.             {
  742.                 ctx.fillStyle = "#000000";
  743.                 ctx.fillText("" + player.inventory.stacks[i].qty,
  744.                     10 + (i*50) + 38,
  745.                     350 + 38);
  746.             }
  747.         }
  748.     }
  749.     ctx.textAlign = "left";
  750.  
  751.     ctx.fillStyle = "#ff0000";
  752.     ctx.fillText("FPS: " + framesLastSecond, 10, 20);
  753.     ctx.fillText("Game speed: " + gameSpeeds[currentSpeed].name, 10, 40);
  754.  
  755.     lastFrameTime = currentFrameTime;
  756.     requestAnimationFrame(drawGame);
  757. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement