-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunit.js
124 lines (105 loc) · 2.48 KB
/
unit.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
function Unit(id, name, sprite, move, health, damage, range, game)
{
Unit.baseConstructor.call(this, 0, 0, sprite);
/*
var col_elem = this.sprite.svg_object.getElementById("team_col");
col_elem.setAttribute("fill", "white");
*/
this.game = game;
this.id = id;
this.name = name;
this.move = move;
this.max_health = health;
this.health = health;
this.damage = damage;
this.range = range;
this.alive = true;
this.sprite.setText(this.health);
this.sprite.addStyleSheet("teams.css");
if (damage > 0)
{
this.canDamage = true;
this.canHeal = false;
}
else
{
this.canDamage = false;
this.canHeal = true;
}
}
KevLinDev.extend(Unit, RenderMapObject)
Unit.prototype.updateSprite =
function()
{
this.sprite.changeText(this.health);
}
Unit.prototype.destroy =
function()
{
this.sprite.destroy();
/* release any references so garbage collector can do it's thing */
delete this.tracker;
delete this.game;
delete this.sprite;
}
Unit.prototype.setTracker =
function(tracker)
{
this.tracker = tracker;
}
Unit.prototype.getTracker =
function()
{
return this.tracker;
}
Unit.prototype.moveTo =
function(x, y)
{
this.x = x;
this.y = y;
/*
* While it's probably ok to have a global game map, I'd prefer not to.
*/
this.sprite.setPosition(gamemap_get_map_screenx(x), gamemap_get_map_screeny(x, y));
}
Unit.prototype.healOther =
function(other_unit)
{
other_unit.heal();
}
Unit.prototype.heal =
function heal()
{
this.health = this.max_health + 1;
this.updateSprite();
}
Unit.prototype.doDamage =
function(other_unit)
{
other_unit.takeDamage(this.damage);
}
Unit.prototype.doDamageToBuilding =
function(building)
{
building.takeDamage(this.damage);
}
Unit.prototype.takeDamage =
function(damage)
{
this.health -= damage;
this.updateSprite();
if (this.health < 0)
{
this.alive = false;
}
}
Unit.prototype.isDead =
function()
{
return (this.health < 1 ? true : false);
}
Unit.prototype.getId =
function()
{
return this.id;
}