annotate game/js/tower.js @ 10:22b604680c5a

adjusted monster movement to be less uniform
author ark
date 2026-10-19 16:56 +0200
parents 910b26feca76
children 8df95050fc8b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6
910b26feca76 full game flow
ark
parents: 4
diff changeset
1 // towers: targeting, attacks, upgrade, sell
1
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
2
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
3 window.MM = window.MM || {};
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
4
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
5 MM.tower = (function () {
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
6 "use strict";
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
7
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
8 var cfg = MM.config;
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
9 // how long a melee attack shows its spikes pushed out
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
10 var BURST_TIME = 0.3;
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
11
6
910b26feca76 full game flow
ark
parents: 4
diff changeset
12 // sides: for t0, which sides draw spikes (map.facingSides); all four
910b26feca76 full game flow
ark
parents: 4
diff changeset
13 // when left out
910b26feca76 full game flow
ark
parents: 4
diff changeset
14 function create(type, x, y, sides) {
1
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
15 return {
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
16 type: type,
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
17 def: cfg.tower[type],
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
18 level: 1,
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
19 x: x,
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
20 y: y,
6
910b26feca76 full game flow
ark
parents: 4
diff changeset
21 sides: sides || [true, true, true, true],
910b26feca76 full game flow
ark
parents: 4
diff changeset
22 // purchases, {cost, pause}: pause is the number of the pause
910b26feca76 full game flow
ark
parents: 4
diff changeset
23 // the purchase was made in, 0 while the game ran
910b26feca76 full game flow
ark
parents: 4
diff changeset
24 paid: [],
1
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
25 angle: -Math.PI / 2,
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
26 cooldown: 0,
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
27 burst: 0
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
28 };
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
29 }
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
30
6
910b26feca76 full game flow
ark
parents: 4
diff changeset
31 // cost of the next level, null at the top level. level n costs the
910b26feca76 full game flow
ark
parents: 4
diff changeset
32 // base cost x n.
910b26feca76 full game flow
ark
parents: 4
diff changeset
33 function upgradeCost(t) {
910b26feca76 full game flow
ark
parents: 4
diff changeset
34 return t.level < t.def.damage.length ? t.def.cost * (t.level + 1) : null;
910b26feca76 full game flow
ark
parents: 4
diff changeset
35 }
910b26feca76 full game flow
ark
parents: 4
diff changeset
36
910b26feca76 full game flow
ark
parents: 4
diff changeset
37 // coins back for selling the tower: half of everything paid for it,
910b26feca76 full game flow
ark
parents: 4
diff changeset
38 // rounded down, except purchases made during the current pause, which
910b26feca76 full game flow
ark
parents: 4
diff changeset
39 // come back in full. pause is the number of the current pause, 0 while
910b26feca76 full game flow
ark
parents: 4
diff changeset
40 // the game runs.
910b26feca76 full game flow
ark
parents: 4
diff changeset
41 function refund(t, pause) {
910b26feca76 full game flow
ark
parents: 4
diff changeset
42 var rest = 0, full = 0;
910b26feca76 full game flow
ark
parents: 4
diff changeset
43 t.paid.forEach(function (p) {
910b26feca76 full game flow
ark
parents: 4
diff changeset
44 if (pause && p.pause === pause) full += p.cost;
910b26feca76 full game flow
ark
parents: 4
diff changeset
45 else rest += p.cost;
910b26feca76 full game flow
ark
parents: 4
diff changeset
46 });
910b26feca76 full game flow
ark
parents: 4
diff changeset
47 return Math.floor(rest * cfg.tower_rules.sell_refund) + full;
910b26feca76 full game flow
ark
parents: 4
diff changeset
48 }
910b26feca76 full game flow
ark
parents: 4
diff changeset
49
1
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
50 function sprite(t) {
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
51 return t.level === 1 ? t.type : t.type + "_" + t.level;
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
52 }
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
53
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
54 function range(t) {
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
55 return t.def.range[t.level - 1];
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
56 }
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
57
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
58 function damage(t) {
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
59 return t.def.damage[t.level - 1];
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
60 }
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
61
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
62 function inRange(t, m) {
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
63 var r = range(t);
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
64 var dx = m.x - t.x, dy = m.y - t.y;
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
65 return dx * dx + dy * dy <= r * r;
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
66 }
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
67
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
68 // the monster in range that is furthest along, that is with the least
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
69 // distance left to its exit
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
70 function target(t, monsters) {
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
71 var best = null, left = Infinity;
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
72 for (var i = 0; i < monsters.length; i++) {
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
73 var m = monsters[i];
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
74 if (!m.alive || !inRange(t, m)) continue;
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
75 var l = MM.monster.remaining(m);
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
76 if (l < left) {
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
77 left = l;
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
78 best = m;
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
79 }
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
80 }
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
81 return best;
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
82 }
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
83
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
84 function update(t, game, dt) {
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
85 if (t.cooldown > 0) t.cooldown -= dt;
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
86 if (t.burst > 0) t.burst -= dt;
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
87 var melee = t.def.projectile_speed === 0;
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
88 var m = target(t, game.monsters);
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
89 if (!m) return;
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
90 if (!melee) t.angle = Math.atan2(m.y - t.y, m.x - t.x);
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
91 if (t.cooldown > 0) return;
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
92 t.cooldown += t.def.attack_time;
4
2d46729a022e sound processing and in-game sounds
ark
parents: 1
diff changeset
93 // sound events t0_attack for the melee tower, t1_shot and so on
2d46729a022e sound processing and in-game sounds
ark
parents: 1
diff changeset
94 MM.audio.play(t.type + (melee ? "_attack" : "_shot"));
1
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
95 if (melee) {
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
96 // every monster in range at once
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
97 var hit = game.monsters.filter(function (o) { return o.alive && inRange(t, o); });
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
98 hit.forEach(function (o) { game.damage(o, damage(t)); });
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
99 t.burst = BURST_TIME;
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
100 } else {
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
101 game.projectiles.push(MM.projectile.create(t, m, damage(t)));
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
102 }
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
103 }
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
104
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
105 return { create: create, update: update, sprite: sprite, range: range,
6
910b26feca76 full game flow
ark
parents: 4
diff changeset
106 upgradeCost: upgradeCost, refund: refund, BURST_TIME: BURST_TIME };
1
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
107 })();