comparison game/js/projectile.js @ 6:910b26feca76

full game flow
author ark
date 2026-10-19 01:05 +0200
parents 5492efd60e2c
children
comparison
equal deleted inserted replaced
5:67bf3c505145 6:910b26feca76
1 // projectiles: fly towards their target and damage it on arrival 1 // projectiles: fly towards their target and damage it on arrival, p2 also
2 // slows it
2 3
3 window.MM = window.MM || {}; 4 window.MM = window.MM || {};
4 5
5 MM.projectile = (function () { 6 MM.projectile = (function () {
6 "use strict"; 7 "use strict";
7 8
8 function create(tower, target, damage) { 9 function create(tower, target, damage) {
10 var def = tower.def;
9 return { 11 return {
10 kind: tower.def.projectile, 12 kind: def.projectile,
11 speed: tower.def.projectile_speed, 13 speed: def.projectile_speed,
12 x: tower.x, 14 x: tower.x,
13 y: tower.y, 15 y: tower.y,
14 tx: target.x, 16 tx: target.x,
15 ty: target.y, 17 ty: target.y,
16 target: target, 18 target: target,
17 damage: damage, 19 damage: damage,
20 slow: def.slow ? def.slow[tower.level - 1] : 0,
21 slowTime: def.slow_time,
18 angle: Math.atan2(target.y - tower.y, target.x - tower.x), 22 angle: Math.atan2(target.y - tower.y, target.x - tower.x),
19 done: false 23 done: false
20 }; 24 };
21 } 25 }
22 26
33 var dx = p.tx - p.x, dy = p.ty - p.y; 37 var dx = p.tx - p.x, dy = p.ty - p.y;
34 var d = Math.hypot(dx, dy); 38 var d = Math.hypot(dx, dy);
35 var step = p.speed * dt; 39 var step = p.speed * dt;
36 var reach = t ? Math.max(step, t.hitRadius) : step; 40 var reach = t ? Math.max(step, t.hitRadius) : step;
37 if (d <= reach) { 41 if (d <= reach) {
38 if (t) game.damage(t, p.damage); 42 if (t) {
43 if (p.slow) MM.monster.slow(t, p.slow, p.slowTime);
44 game.damage(t, p.damage);
45 }
39 p.done = true; 46 p.done = true;
40 return; 47 return;
41 } 48 }
42 p.angle = Math.atan2(dy, dx); 49 p.angle = Math.atan2(dy, dx);
43 p.x += dx / d * step; 50 p.x += dx / d * step;