-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunit.js
223 lines (212 loc) · 6.15 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
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
// server side we import CONSTANTS
if( 'undefined' != typeof global ) {
var helper = require("./helper.js");
var CONSTANTS = helper.CONSTANTS;
}
// Unit Prototype
/**
* Prototype for a unit
* @constructor
*/
var Unit = function(/*int*/player, /*int*/ team, /*int*/hp, /*int*/type, /*Coordinate*/startCoord, /*image*/pic, /*image*/cdImage, /*int*/showNum){
this.player = player; //Starts from 0
this.team = team; //Starts from 0
this.hp = hp;
this.type = type; //0.Vampire 1.Wolf 2.Hunter 3.Zombie 4.Wizard 5.Unknown
this.range = 2;
this.x = startCoord.X;
this.y = startCoord.Y;
this.serverIsVisible = false; // visible to enemy
this.attacked = false;
this.heartImage = CONSTANTS.heart; // hp image
this.image = pic; // unit image
this.cdImage = cdImage; // cd image
this.cooldown = 0;
this.lastCooldownTime = 0;
this.attacking = false;
this.damaged = false;
this.death = null;
this.lostHP = 0;
this.terrain = null;
this.buff = null;
this.attack = 50;
this.defense = 1;
this.showNum = 0;
this.hitCounter = 20;
this.lastHitType = null;
if (showNum)
this.showNum = 1;
}
// server side we export Unit.
if( 'undefined' != typeof global ) {
module.exports = Unit;
}
Unit.prototype.setcd = function(/*float*/ time){
if ('undefined' != typeof global) {
var now = new Date();
this.lastCooldownTime = now.getTime();
} else {
this.cooldown = time;
var self = this;
var now, before = new Date();
var cding = window.setInterval(function(){
now = new Date();
if(self.cooldown>=0){
var elapsedTime = now.getTime() - before.getTime();
if (elapsedTime > 100) // in case tab is not active in Chrome
self.cooldown = self.cooldown - Math.round(elapsedTime/100)/10;
else
self.cooldown = self.cooldown - 0.1;
}else{
self.cooldown = 0;
window.clearInterval(cding);
}
before = new Date();
} ,100);
}
}
/**
* Unit Method: return a Kinetic.Image to be put into the unit group.
* @this {Unit}
*/
Unit.prototype.draw = function(/*Point*/p, /*int*/height) {
var groupToDraw = new Kinetic.Group();
// draw unit
var unitToDraw;
if (this.hitCounter < 20) {
var hitImage;
if (this.lastHitType == "small")
hitImage = gc.getHitImgs.small[this.team][this.type];
else
hitImage = gc.getHitImgs.big[this.team][this.type];
unitToDraw = new Kinetic.Image({
image: hitImage,
x: Math.floor(p.X - this.image.width/2),
y: Math.floor(p.Y + height*2/5 - this.image.height + 5),
width: this.image.width,
height: this.image.height
});
var offset = Math.floor(this.hitCounter/2);
unitToDraw.setCrop({x:120*offset, y:0, width:120, height:120});
this.hitCounter++;
} else if (this.cooldown > 0.2 && this.cdImage) {
unitToDraw = new Kinetic.Image({
image: this.cdImage,
x: Math.floor(p.X - this.image.width/2),
y: Math.floor(p.Y + height*2/5 - this.image.height + 5),
width: this.image.width,
height: this.image.height
});
var offset = Math.round((5 - this.cooldown/2)*10);
unitToDraw.setCrop({x:120*offset, y:0, width:120, height:120});
} else {
unitToDraw = new Kinetic.Image({
image: this.image,
x: Math.floor(p.X - this.image.width/2),
y: Math.floor(p.Y + height*2/5 - this.image.height + 5),
width: this.image.width,
height: this.image.height
});
}
groupToDraw.add(unitToDraw);
// draw hp
for (var i = 0; i < this.hp; i++) {
var hpHeart = new Kinetic.Image({
image: this.heartImage,
x: Math.floor(p.X + 22),
y: Math.floor(p.Y + 15 * i - 30)
});
groupToDraw.add(hpHeart);
}
// draw number
if (this.showNum && this.type != 5) {
var num = this.type + 1;
var numText = new Kinetic.Text({
fontFamily: "Impact",
fontSize: 30,
stroke: "white",
text: num,
x: p.X,
y: p.Y
});
groupToDraw.add(numText);
}
return groupToDraw;
};
Unit.prototype.guess = function(/*int*/ guess){
this.type = guess;
this.image = gc.sprites[this.player][guess];
this.cdImage = gc.cooldown[this.player][guess];
}
Unit.prototype.minusHP = function(/*int*/hp){
this.lostHP = this.hp - hp;
this.hp = hp;
if(this.lostHP!=0){
this.hitCounter = 0; // show get hit animation
if (this.lostHP == 1)
this.lastHitType = "small";
else
this.lastHitType = "big";
if(this.team == gc.team && hp!= 0){
if(this.lostHP == 1){
soundAssets.attack_1sound.play();
ObjectiveCCall("playSound", ["attack_1"]);
}else{
soundAssets.attack_2sound.play();
ObjectiveCCall("playSound", ["attack_2"]);
}
}
else{
if(this.lostHP == 1 && hp!= 0){
soundAssets.attack1sound.play();
ObjectiveCCall("playSound", ["attack1"]);
}else if(this.type == 0){
soundAssets.kosound.play();
ObjectiveCCall("playSound", ["ko"]);
gc.vampireKO = true;
}
else if(hp!= 0){
soundAssets.attack2sound.play();
ObjectiveCCall("playSound", ["attack2"]);
}
}
}
}
Unit.prototype.gotHit = function(/*Unit*/enemy){
var damage = enemy.buff?(enemy.attack+enemy.buff.attackBuff):enemy.attack;
var defense = this.buff?(this.defense*this.buff.defenseBuff):this.defense;
//Calculate type advantage
if(enemy.type == 4){
if(this.type == 0){
this.hp = 0;
}else if(enemy.type == this.type){
this.hp = this.hp - 1;
}else if(enemy.type > this.type){
enemy.hp = enemy.hp - 1;
}
}else{
if(enemy.type < this.type){
this.hp = this.hp - 2;
}
else if(enemy.type == this.type){
this.hp = this.hp - 1;
}
else if(enemy.type > this.type){
enemy.hp = enemy.hp - 1;
}
}
if(this.hp < 0){
this.hp = 0;
}
if(enemy.hp < 0){
enemy.hp = 0;
}
};
Unit.prototype.serverSetVisible = function(/*boolean*/ isVisible) {
var old = this.serverIsVisible;
this.serverIsVisible = isVisible;
if (!old && isVisible)
return true;
else
return false;
}