-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolis.js
269 lines (225 loc) · 8.04 KB
/
polis.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
function Polis(){
var maptiles;
var tileOffs = {
0: 'wood',
1: 'food',
2: 'stone',
3: 'gold',
4: 'none',
5: 'gatherer'
};
var tileIDs = {
wood: 0,
food: 1,
stone: 2,
gold: 3,
none: 4,
gatherer: 5
};
var makeResource = function(res, row, col) {
return createObject({components: {position: new cPosition(col * 32, 32 + (row * 32)), resource: new cResource(tileIDs[res]),
drawable: new cDrawableResource()}}, this.map);
};
var constructors = {
0: function(row, col){ return makeResource('wood', row, col);},
1: function(row, col){ return makeResource('food', row, col);},
2: function(row, col){ return makeResource('stone', row, col);},
3: function(row, col){ return makeResource('gold', row, col);},
5: function(row, col){ var ret = makeResource('gatherer', row, col);
ret.components.resourcegatherer = new cResourceGatherer();
ret.components.resourcegatherer.owner = ret;
return ret;
},
};
var tileOffsets = {
0: 4,
1: 97,
2: 10,
3: 302,
4: 2,
5: 7
};
var resources = [0, 0, 0, 0];
var tiles = null;
map = [];
this.map = map;
var currentBuild;
// this.resourceTiles = [new Sprite()]
setBuilding = function(bld) {
console.log("YES " + bld);
currentBuild.res = bld;
// sprite: function() { return new jaws.Sprite({image: tiles.frames[tileOffsets[this.res]], x: 768-34, y: 0});}};
};
this.setup = function()
{
// Load tilesheet
tiles = new jaws.SpriteSheet({image: "tileset.png", frame_size: [32, 32], scale_image: 1, orientation: "right"});
this.tiles = tiles;
// Generate map
for (i = 0; i < 23 * 24; i += 1)
{
var number = Math.random();
var res = -1;
if (number < 0.1)
{
res = tileIDs.gold;
}
else if (number < 0.25)
{
res = tileIDs.stone;
}
else if (number < 0.5)
{
res = tileIDs.wood;
}
else if (number < 0.7)
{
res = tileIDs.food;
}
else
{
res = tileIDs.none;
}
var tmpObj = createObject({components: {position: new cPosition(Math.floor(i % 24) * 32, 32 + (Math.floor(i / 24) * 32)), resource: new cResource(res),
drawable: new cDrawableResource()}}, this.map);
this.map.push(tmpObj);
}
// Create a settlement
/*var tmp = createObject({components: {position: new cPosition(2 * 32, 3 * 32), resourcegatherer: new cResourceGatherer(), drawable: new cDrawable(this.tiles.frames[tileOffsets[tileIDs.gatherer]])}}, this.map);
var tmp2 = createObject({components: {position: new cPosition(4 * 32, 3 * 32), resourcegatherer: new cResourceGatherer(), drawable: new cDrawable(this.tiles.frames[tileOffsets[tileIDs.gatherer]])}}, this.map);
this.map[50] = tmp;
this.map[52] = tmp2;*/
// Initialize the starting selected build
currentBuild = {res: tileIDs.wood, sprite: function() { return new jaws.Sprite({image: tiles.frames[tileOffsets[this.res]], x: 768-34, y: 0});}};
// Setup key bindings
['1', '2', '3', '4', '6'].forEach(function (e) {
jaws.on_keydown(e, _.partial(setBuilding, e - 1));
});
// Bind mouse
document.onclick = function(){
var clickSpot = clickToPosition();
// console.log(clickSpot.row + " " + clickSpot.col + " CURBUILD " + currentBuild.res);
var clickIdx = positionToIndex(clickSpot.row, clickSpot.col, 24);
var tmpVal = (constructors[currentBuild.res])(clickSpot.row, clickSpot.col);
map[clickIdx] = tmpVal;
// console.log(clickIdx + map[clickIdx]);
// map[clickIdx].components.resource = new cResource(currentBuild.res);};
}
};
this.update = function()
{
_.each(this.map, function(elm, idx, l){elm.update();});
};
this.draw = function()
{
jaws.context.fillStyle = "#74cf45";
jaws.context.fillRect(0, 0, 768, 768);
_.each(this.map, function(elm, idx, l)
{elm.draw();});
jaws.context.fillStyle = "#691f01";
jaws.context.fillRect(0, 0, 768, 32);
jaws.context.fillStyle = "white";
jaws.context.font = "28px Serif";
jaws.context.fillText("W: " + resources[tileIDs.wood] + " F: " + resources[tileIDs.food] + " S: " + resources[tileIDs.stone] + " G: " + resources[tileIDs.gold], 8, 30);
currentBuild.sprite().draw();
};
function createObject(o, map)
{
function componentDraw()
{
_.each(this.components, function(elm, idx, l)
{ if (elm.draw) { elm.draw();} });
};
function componentUpdate()
{
_.each(this.components, function(elm, idx, l)
{ if (elm.update) { elm.update();} });
};
function componentClaim(o)
{
_.each(o.components, function(elm, idx, l)
{ elm.owner = o });
};
var ret = o;
componentClaim(ret);
ret.draw = _.bind(componentDraw, ret);
ret.update = _.bind(componentUpdate, ret);
ret.map = map;
return ret;
};
function cPosition(x, y)
{
this.x = x;
this.y = y;
};
function cDrawable(sprite)
{
this.sprite = new jaws.Sprite({image: sprite});
this.draw = function()
{
this.sprite.x = this.owner.components.position.x;
this.sprite.y = this.owner.components.position.y;
this.sprite.draw();
};
};
function cDrawableResource()
{
this.sprite = null;
this.draw = function()
{
this.sprite = new jaws.Sprite({image: tiles.frames[tileOffsets[this.owner.components.resource.resource]]});
this.sprite.x = this.owner.components.position.x;
this.sprite.y = this.owner.components.position.y;
this.sprite.draw();
};
};
function clickToPosition() {
return {row: Math.floor(jaws.mouse_y / 32) - 1, col: Math.floor(jaws.mouse_x / 32)};
}
function positionToIndex(row, col, rowLength)
{
return Math.floor(row * rowLength) + col;
}
function cResource(r)
{
this.resource = r;
}
function cResourceGatherer()
{
this.interval = 5000;
this.time = 0;
this.efficiency = 1;
this.update = function()
{
this.time += jaws.game_loop.tick_duration;
if (this.time >= this.interval)
{
this.proc();
this.time = 0;
}
};
this.proc = function()
{
var index = positionToIndex(Math.floor((this.owner.components.position.y - 32) / 32), Math.floor(this.owner.components.position.x / 32), 24);
var adjacentTiles = [this.owner.map[index - 1], this.owner.map[index - 24], this.owner.map[index + 1], this.owner.map[index + 24]];
_.each(adjacentTiles, function(elm, idx, l)
{
// Handle the (literal) edge case of being on a border.
if (elm) {
if (elm.components.resource)
{
// console.log("resource " + elm.components.resource.resource);
var rsc = elm.components.resource;
// console.log("rsc" + rsc);
resource = rsc.resource;
// console.log(elm.components.position.x + ", " + elm.components.position.y);
if (resource == tileIDs.food || resource == tileIDs.wood || resource == tileIDs.stone || resource == tileIDs.gold)
{
resources[resource] += 1;
}
}
}
});
};
}
}