canvas example of a beach war game

For this weeks canvas example I started working on an idea that I had for a simple strategy type game. The basic idea of what I had in mind is just a simple 2d grid with three index values for ground types that are water, beach, and land. The player can build structures on land, but not on beach or water cells. In the water enemy boats can spawn and attempt to attack and invade the beach.

1 - The utils library for canvas example beach

So with many of these canvas examples I end up making a utility library where I park methods that I think I will be using in more that one module at one point or another. Or another reason why I might place something there is to just reduce the complexity of another module. In any case it is a collection of methods that are relevant to the modules for this canvas example that include the modules for state, rendering, and any plug-ins I might make at some point if I end up making a plug-in system.

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
var utils = {};
utils.distance = function (x1, y1, x2, y2) {
return Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2));
};
utils.XP = (function () {
// set level with given xp
var set = function (xp) {
//return Math.sqrt(xp);
return (1 + Math.sqrt(1 + 8 * xp / 50)) / 2;
};
// get exp to the given level with given current_level and xp
var XPto = function (level) {
//return Math.pow(level, 2);
return ((Math.pow(level, 2) - level) * 50) / 2;
};
var parseByXP = function (xp, cap) {
var l = set(xp);
l = l > cap ? cap : l;
var level = Math.floor(l),
forNext = XPto(level + 1);
return {
level: level,
levelFrac: l,
xp: xp,
forNext: l === cap ? Infinity : forNext,
toNext: l === cap ? Infinity : forNext - xp
};
};
return {
parseByLevel: function (l, cap) {
return parseByXP(XPto(l, cap));
},
parseByXP: parseByXP
};
}
());

For now the module consists of the distance formula, and a an experience point system that consists of two methods. The distance formula if a usual suspect for a lot of these canvas examples, but the experience point system is thus far something that I am juts using in this project, at least at the point of this writing anyway.

2 - The game module

Here is the main game module that will return two public methods that are use to create a game state, as well as update that game state.

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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
var game = (function () {
// HARD CODED SETTINGS
// ********** ********** ********** ********** **********
// GRID 'Constants'
var GRID = {
w: 16,
h: 12,
cellSize: 16
};
// SPAWN 'Constants'
var SPAWN = {
rate: 0.5, // spawn rate in secs
playerMax: 5, // max player units
enemyMax: 10,
shotMax: 100,
blastMax: 50
};
var TURRET = {
maxKillLevel: 10,
minAttack: 1,
maxAttack: 10,
minAttackRange: 3,
maxAttackRange: 7,
minAccuracy: 0.4,
maxAccuracy: 0.9,
maxInaccuracy: 2
};
// CELLS create and get
// ********** ********** ********** ********** **********
// create the array of cell objects
var createCells = function (areaData) {
var i = 0,
len = GRID.w * GRID.h,
cells = [];
while (i < len) {
cells.push({
i: i,
x: i % GRID.w,
y: Math.floor(i / GRID.w),
areaType: areaData[i] === undefined ? 0 : areaData[i],
clear: true // nothing on it
})
i += 1;
}
return cells;
};
// get cell
var getCell = function (state, x, y) {
return state.cells[GRID.w * y + x];
};
var getRandomCell = function (pool) {
return pool[Math.floor(pool.length * Math.random())];
};
// get areas of type and clear status (optional)
// clear === undefined (clear and not clear tiles)
// clear === true (only clear tiles)
// clear === false (only not clear tiles)
var getAreas = function (state, areaType, clear) {
return state.cells.filter(function (cell) {
return String(cell.areaType) === String(areaType) && (clear === undefined ? true : clear === cell.clear);
});
};
// get cells near the cell
var getNear = function (state, cell, range, areaType) {
range = range || 1;
areaType = areaType === undefined ? 0 : areaType;
return state.cells.filter(function (target) {
return utils.distance(cell.x, cell.y, target.x, target.y) <= range;
}).filter(function (target) {
return String(areaType) === String(target.areaType) && cell.i != target.i;
}).filter(function (target) {
return target.clear;
})
};
// return a list of objects with landIndex values sorted by
// most amount of water tiles in the given range
var getBestTurretLands = function (state, range) {
var waterAreas = getAreas(state, 0);
return getAreas(state, 2, true).map(function (cell) {
var count = 0;
waterAreas.forEach(function (waterCell) {
if (utils.distance(cell.x, cell.y, waterCell.x, waterCell.y) <= range) {
count += 1;
}
});
return {
i: cell.i,
cell: cell,
waterCount: count
}
}).filter(function (obj) {
return obj.waterCount > 0;
}).sort(function (a, b) {
if (a.waterCount > b.waterCount) {
return -1;
}
if (a.waterCount < b.waterCount) {
return 1;
}
return 0;
});
};
// get border waters
var getBorderWaters = function (state) {
return getAreas(state, 0, true).filter(function (cell) {
return cell.x === 0 || cell.y == 0 || cell.x === GRID.w - 1 || cell.y === GRID.h - 1;
});
};
// BOAT and TURRET helpers
// ********** ********** ********** ********** **********
// set values of the given turret object bu the current killLevel value
var applyTurretKillLevel = function (tur) {
var per = tur.killLevel.level / TURRET.maxKillLevel,
delta;
// attack
delta = (TURRET.maxAttack - TURRET.minAttack) * per;
tur.attack = TURRET.minAttack + delta;
// attack range
delta = (TURRET.maxAttackRange - TURRET.minAttackRange) * per;
tur.attackRange = TURRET.minAttackRange + delta;
// accuracy
delta = (TURRET.maxAccuracy - TURRET.minAccuracy) * per;
tur.accuracy = TURRET.minAccuracy + delta;
};
// SPAWN boats and turrets
// ********** ********** ********** ********** **********
// spawn units
var spawn = function (state, secs) {
state.spawnSecs += secs;
if (state.spawnSecs >= SPAWN.rate) {
state.spawnSecs %= SPAWN.rate;
// player spawn
if (state.pool.player.length < SPAWN.playerMax) {
var freeLands = getBestTurretLands(state, TURRET.minAttackRange);
if (freeLands.length >= 1) {
var land = freeLands[0].cell;
land.clear = false;
var turret = {
x: land.x,
y: land.y,
h: 0, // heading
kills: 0,
killLevel: 1,
killsToNext: 0,
attack: 5,
attackRange: TURRET.minAttackRange,
fireRate: 0.1,
fireSecs: 0.2,
accuracy: 0.0,
shotPPS: 4,
shotBlastRadius: 1.5,
shotAttack: 2
};
turret.killLevel = utils.XP.parseByXP(turret.kills, TURRET.maxKillLevel);
applyTurretKillLevel(turret);
state.pool.player.push(turret);
}
}
// enemy
if (state.pool.enemy.length < SPAWN.enemyMax) {
var waters = getBorderWaters(state);
if (waters.length >= 1) {
var water = getRandomCell(waters);
water.clear = false;
state.pool.enemy.push({
x: water.x,
y: water.y,
hp: 100,
hpMax: 100,
secs: 0,
speed: 3
});
}
}
}
};
// UPDATE boats and turrets
// ********** ********** ********** ********** **********
// update boats
var updateBoats = function (state, secs) {
var i = state.pool.enemy.length,
boat;
while (i--) {
boat = state.pool.enemy[i];
boat.secs += secs;
if (boat.secs >= boat.speed) {
boat.secs %= boat.speed;
var near = getNear(state, boat, 1.5, 0);
if (near.length >= 1) {
var water = getRandomCell(near),
current = getCell(state, boat.x, boat.y);
current.clear = true;
water.clear = false;
boat.x = water.x;
boat.y = water.y;
}
}
// purge dead boats
if (boat.hp === 0) {
var cell = getCell(state, boat.x, boat.y);
cell.clear = true;
state.pool.enemy.splice(i, 1);
state.kills += 1;
}
}
};
// update player turrets
var updateTurrets = function (state, secs) {
var i = state.pool.player.length,
turret;
while (i--) {
turret = state.pool.player[i];
turret.fireSecs -= secs;
// fire
if (turret.fireSecs <= 0) {
turret.fireSecs = turret.fireRate + Math.abs(turret.fireSecs) % turret.fireRate;
var targets = state.pool.enemy.filter(function (boat) {
return utils.distance(boat.x, boat.y, turret.x, turret.y) <= turret.attackRange;
});
if (targets.length >= 1) {
var target = getRandomCell(targets);
// push shot
if (state.pool.shots.length < SPAWN.shotMax) {
var sx = turret.x + 0.5,
sy = turret.y + 0.5,
ma = TURRET.maxInaccuracy,
tx = target.x + 0.5 + (-ma + ma * 2 * Math.random()) * (1 - turret.accuracy),
ty = target.y + 0.5 + (-ma + ma * 2 * Math.random()) * (1 - turret.accuracy);
turret.h = Math.atan2(ty - sy, tx - sx);
state.pool.shots.push({
shotFrom: turret,
x: sx,
y: sy,
sx: sx,
sy: sy,
tx: tx,
ty: ty,
secs: 0,
d: utils.distance(sx, sy, tx, ty),
h: turret.h,
pps: turret.shotPPS,
blastRadius: turret.shotBlastRadius,
attack: turret.shotAttack
});
}
}
}
}
};
// SHOTS ans BLASTS
// ********** ********** ********** ********** **********
// shot blast
var spawnBlast = function (state, shot) {
var i = state.pool.enemy.length,
boat,
dam,
d;
// apply damage
while (i--) {
boat = state.pool.enemy[i];
d = utils.distance(shot.x, shot.y, boat.x + 0.5, boat.y + 0.5);
if (d <= shot.blastRadius) {
dam = shot.attack - d / shot.blastRadius * shot.attack;
// attack but do not purge boat, that is done in updateBoats
boat.hp -= dam;
boat.hp = boat.hp < 0 ? 0 : boat.hp;
// step kills for shotFrom if we have that value
var disp = shot.shotFrom;
if (boat.hp === 0 && disp != undefined) {
disp.kills += 1;
disp.killLevel = utils.XP.parseByXP(disp.kills, TURRET.maxKillLevel);
applyTurretKillLevel(disp);
}
}
}
// push blast
if (state.pool.blasts.length < SPAWN.blastMax) {
state.pool.blasts.push({
x: shot.x,
y: shot.y,
secs: 0,
secsMax: 1,
radius: shot.blastRadius
});
}
};
// update shots in the shot pool
var updateShots = function (state, secs) {
var i = state.pool.shots.length,
shot;
while (i--) {
shot = state.pool.shots[i];
shot.secs += secs;
var d = shot.pps * shot.secs;
d = d > shot.d ? shot.d : d;
shot.x = shot.sx + Math.cos(shot.h) * d;
shot.y = shot.sy + Math.sin(shot.h) * d;
if (d === shot.d) {
spawnBlast(state, shot);
state.pool.shots.splice(i, 1);
}
}
};
// PUBLIC API
// ********** ********** ********** ********** **********
var api = {
GRID: GRID,
SPAWN: SPAWN,
TURRET: TURRET
};
// create a state object
api.create = function (opt) {
opt = opt || {};
opt.areaData = opt.areaData || '';
var state = {
cells: createCells(opt.areaData),
lt: new Date(),
spawnSecs: 0,
kills: 0,
pool: {
player: [],
enemy: [],
shots: [],
blasts: []
}
};
return state;
};
// update a state object
api.update = function (state) {
// time
var now = new Date(),
t = now - state.lt,
secs = t / 1000;
// spawn
spawn(state, secs);
//purge(state);
updateBoats(state, secs);
updateTurrets(state, secs);
updateShots(state, secs);
// update blasts
var i = state.pool.blasts.length;
while (i--) {
var blast = state.pool.blasts[i];
blast.secs += secs;
if (blast.secs >= blast.secsMax) {
state.pool.blasts.splice(i, 1);
}
}
// set lt to now
state.lt = now;
};
return api;
}
());

3 - Draw.js

Now for the draw module that will help with rendering the current status of a game state object.

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
var draw = (function () {
var api = {};
api.back = function (ctx, canvas, op) {
ctx.fillStyle = 'rgba(0,0,0,' + op + ')';
ctx.fillRect(0, 0, canvas.width, canvas.height);
};
api.cells = function (ctx, state) {
var i = state.cells.length,
colors = ['blue', 'yellow', 'green'],
cellSize = game.GRID.cellSize,
cell;
while (i--) {
cell = state.cells[i];
ctx.fillStyle = colors[cell.areaType];
ctx.fillRect(cell.x * cellSize, cell.y * cellSize, cellSize, cellSize);
}
};
// old simple draw pool method
var drawPool = function (ctx, state, poolName) {
var i = state.pool[poolName].length,
colors = {
player: 'purple',
enemy: 'red'
},
cellSize = game.GRID.cellSize;
while (i--) {
disp = state.pool[poolName][i];
ctx.fillStyle = colors[poolName];
ctx.fillRect(disp.x * cellSize, disp.y * cellSize, cellSize, cellSize);
if (disp.hp != undefined) {
var per = disp.hp / disp.hpMax;
ctx.fillStyle = 'lime';
ctx.fillRect(disp.x * cellSize, disp.y * cellSize, cellSize * per, cellSize / 4);
}
}
};
// draw turrets method
var drawTurrets = function (ctx, state) {
var i = state.pool.player.length,
tur,
cellSize = game.GRID.cellSize;
while (i--) {
tur = state.pool.player[i];
ctx.fillStyle = 'pink';
ctx.save();
ctx.translate((tur.x + 0.5) * cellSize, (tur.y + 0.5) * cellSize);
ctx.rotate(tur.h);
ctx.beginPath();
ctx.moveTo(6, 0);
ctx.lineTo(6 * -1, 6 * -1);
ctx.lineTo(6 * -1, 6);
ctx.fill();
ctx.restore();
// attack range
ctx.strokeStyle = 'rgba(255,255,255,0.2)';
ctx.beginPath();
ctx.arc((tur.x + 0.5) * cellSize, (tur.y + 0.5) * cellSize, tur.attackRange * cellSize, 0, Math.PI * 2);
ctx.stroke();
// info
ctx.fillStyle = 'white';
ctx.textBaseline = 'top';
ctx.font = '8px arial';
ctx.fillText(tur.killLevel.level, tur.x * cellSize, tur.y * cellSize);
}
};
api.units = function (ctx, state) {
drawPool(ctx, state, 'enemy');
drawPool(ctx, state, 'player');
drawTurrets(ctx, state);
};
api.shots = function (ctx, state) {
var i = state.pool.shots.length,
cellSize = game.GRID.cellSize,
shot;
ctx.fillStyle = 'white';
while (i--) {
shot = state.pool.shots[i];
ctx.beginPath();
ctx.arc(shot.x * cellSize, shot.y * cellSize, 2, 0, Math.PI * 2);
ctx.fill();
}
};
api.blasts = function (ctx, state) {
var i = state.pool.blasts.length,
cellSize = game.GRID.cellSize,
blast;
while (i--) {
blast = state.pool.blasts[i];
var per = blast.secs / blast.secsMax;
per = per > 1 ? 1 : per;
ctx.beginPath();
ctx.fillStyle = 'rgba(255,128,0,' + (0.6 - (0.55 * per)) + ')';
ctx.arc(blast.x * cellSize, blast.y * cellSize, blast.radius * cellSize * per, 0, Math.PI * 2);
ctx.fill();
}
};
// basic info that I would want no matter what
api.info = function (ctx, state, yi) {
ctx.fillStyle = 'white';
ctx.textBaseline = 'top';
ctx.font = '10px arial';
ctx.fillText('kills: ' + state.kills, 10, 10 * yi);
};
api.debugInfoTurrets = function (ctx, state, yi, op) {
var i = state.pool.player.length;
ctx.fillStyle = 'rgba(255,255,255,' + op + ')';
ctx.textBaseline = 'top';
ctx.font = '10px arial';
ctx.lineWidth = 1;
while (i--) {
var tur = state.pool.player[i];
if (tur) {
var kl = tur.killLevel,
text = i + ') turret: { ' +
'k: ' + tur.kills + ', ' +
'kl: ' + kl.level + ', ' +
//'killsToNext: ' + kl.toNext +
'at: ' + tur.attack + ', ' +
'r: ' + tur.attackRange + ', ' +
'ac: ' + tur.accuracy +
' }';
ctx.fillText(text, 10, 10 * (yi + i));
}
}
};
return api;
}
());

4 - Main.js and index.html

Time to pull all of this together with a main.js file and a little html for now.

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
var canvas = document.getElementById('the-canvas'),
ctx = canvas.getContext('2d');
canvas.width = 320;
canvas.height = 240;
ctx.translate(0.5, 0.5);
var areaData = '' +
'0000000000000000' +
'0000000000000000' +
'0000000001110000' +
'0011111111210000' +
'0012211222210000' +
'0012222222211000' +
'0011222222111000' +
'0001122222111000' +
'0001111122210000' +
'0000001111110000' +
'0000000000000000';
var state = game.create({
areaData: areaData
});
// frame rate capping for model
// update and drawing so that this does
// nit eat up as much CPU overhead
var lt = new Date(),
targetFPS = 24,
targetDelay = 1000 / targetFPS;
var loop = function () {
var now = new Date(),
t = now - lt;
requestAnimationFrame(loop);
if (t >= targetDelay) {
game.update(state);
draw.back(ctx, canvas, 1);
draw.cells(ctx, state);
draw.units(ctx, state);
draw.shots(ctx, state);
draw.blasts(ctx, state);
draw.back(ctx, canvas, 0.2);
draw.info(ctx, state, 1);
//draw.debugInfoTurrets(ctx, state, 2, 0.5);
lt = now;
}
};
loop();
1
2
3
4
5
6
7
8
9
10
11
12
<html>
<head>
<title>canvas example game beach</title>
</head>
<body>
<canvas id="the-canvas" width="320" height="240"></canvas>
<script src="./lib/utils.js"></script>
<script src="./lib/game.js"></script>
<script src="./lib/draw.js"></script>
<script src="main.js"></script>
</body>
</html>

5 - Conclusion

This might end up being one of my canvas example that I might keep coming back to now and then in an effort to break the habit of starting a project but never really finishing it. I like how it is turning out so far, but this one does need a whole lot more work, and I am also not completely sure what the finished product will be if I keep working at it. The main thing I wanted t focus on with this one is to just try to make something that is fun.

With that being said maybe I will keep working on this canvas example, and if so I am sure i will come back and expand this post even further. I would like to break the habit once and for all, if not with this canvas example, one of them at least.