-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
152 lines (138 loc) · 4.19 KB
/
game.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
var gamejs = require('gramework').gamejs,
conf = require('./conf'),
TileMap = require('gramework').tilemap.TileMap,
Scene = require('gramework').Scene,
Cursor = require('./cursor').Cursor,
Unit = require('./cursor').Unit,
GameController = require('gramework').input.GameController,
availableTiles = require('./tiles').availableTiles,
Player = require('./player').Player;
// Container for the entire game.
var Game = exports.Game = function () {
this.cont = new GameController();
this.paused = false;
this.turnQueue = [];
this.currentPlayer;
this.scene = new Scene({
width: 800,
height: 600
});
this.map = new TileMap(gamejs.utils.uri.resolve(document.location.href, './assets/images/maps/test_map.tmx'), {});
this.initialize();
};
Game.prototype.initialize = function() {
var player1 = new Player({ human: true });
var player2 = new Player({});
this.highlitTiles = [];
this.cursor = new Cursor({
width: 32,
height: 32,
x: 0,
y: 0,
tile_size: 32
});
var elf = new Unit({
width: 32,
height: 32,
x: 0,
y: 0,
tile_size: 32,
spriteSheet: conf.Images.elf,
animations: { frames: 2 },
map: this.map
});
var elf2 = new Unit({
width: 32,
height: 32,
x: 1,
y: 0,
tile_size: 32,
spriteSheet: conf.Images.elf,
animations: { frames: 2 },
map: this.map
});
var game = this;
this.controlMap = {
left: function () {
game.cursor.slide([-1, 0]);
},
up: function () {
game.cursor.slide([0, -1]);
},
right: function () {
game.cursor.slide([1, 0]);
},
down: function () {
game.cursor.slide([0, 1]);
},
action: function() {
game.map.deselectAll();
var tile = game.map.getTile(game.cursor.tile[0], game.cursor.tile[1]);
if (game.currentPlayer.isHuman()) {
game.currentPlayer.action(tile);
}
},
mousePos: function(pos) {
pos[0] = Math.floor(pos[0] / (game.cursor.tile_size * game.scene.camera.zoom));
pos[1] = Math.floor(pos[1] / (game.cursor.tile_size * game.scene.camera.zoom));
game.cursor.setTilePos(pos);
},
cancel: function() {
game.map.deselectAll();
game.currentPlayer.deselect();
}
};
this.scene.entities.forEach(function(entity){
this.map.getTile(entity.tile[0], entity.tile[1]).addOccupant(entity);
}, this);
this.scene.pushEntity(this.cursor);
this.scene.pushEntity(elf);
this.scene.pushEntity(elf2);
player1.addUnit(elf);
player1.addUnit(elf2);
elf.setTile(this.map.getTile(0,0));
elf2.setTile(this.map.getTile(1,0));
this.scene.pushLayer(this.map);
this.turnQueue.push(player1);
this.turnQueue.push(player2);
};
Game.prototype.draw = function(surface) {
this.scene.draw(surface);
};
Game.prototype.event = function(ev) {
var key = this.cont.handle(ev);
if (key) {
if (key.keyDown) {
this.controlMap[key.label]();
console.log(this.highlitTiles);
}
if (key.mousePos) {
this.controlMap.mousePos(key.mousePos);
}
}
};
Game.prototype.update = function(dt) {
//Manage the turn sequence if we have to
this.map.deselectAll();
if (this.currentPlayer === undefined){
this.setCurrentPlayer(this.turnQueue.shift());
this.currentPlayer.startTurn();
}
if (this.currentPlayer.isDone()){
this.turnQueue.push(this.currentPlayer);
this.setCurrentPlayer(this.turnQueue.shift());
this.currentPlayer.startTurn();
}
if (this.currentPlayer.hasUnitSelected()) {
var unit = this.currentPlayer.getSelectedUnit();
this.highlitTiles = unit.availableTiles;
this.highlitTiles.forEach(function(tile) {
tile.glow();
}, this);
}
if (dt > 1000 / 3) dt = 1000 / 3;
this.scene.update(dt);
};
Game.prototype.setCurrentPlayer = function(player) {
this.currentPlayer = player;
};