-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmob.js
190 lines (173 loc) · 5.25 KB
/
mob.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
const { world_size } = require('./constants')
const { distance_between } = require('./utilities')
class Mob {
constructor(team, id, x, y){
this.stats = {
max_hp: 30,
hp: 30,
speed: 7,
attack_damage: 3,
reachable_range: 180,
attack_available_timestamp: Date.now(),
cd: 4000,
exp: 50,
x: x,
y: y,
}
this.nickname = ''
this.team = team
this.id = id
this.moved = true
this.action = this.action.bind(this)
this.movement = this.movement.bind(this)
this.check_boundary = this.check_boundary.bind(this)
this.die = this.die.bind(this)
this.to_exp = this.to_exp.bind(this)
this.attack = this.attack.bind(this)
this.drop_coins = this.drop_coins.bind(this)
this.gain_exp = this.gain_exp.bind(this)
}
gain_exp(exp){
this.stats.exp += exp
this.stats.attack += 1
}
get is_cooldown(){
return this.stats.attack_available_timestamp >= Date.now()
}
cooldown(){
this.stats.attack_available_timestamp = Date.now() + this.stats.cd
}
to_exp(){
return this.stats.exp
}
check_boundary(){
if(this.stats.x < 0) this.stats.x = 0
if(this.stats.y < 0) this.stats.y = 0
if(this.stats.x > world_size.width) this.stats.x = world_size.width
if(this.stats.y > world_size.height - 50) this.stats.y = world_size.height - 50
}
drop_coins(){
const coins = require('./game').state.objects.coins
const displacement = 100
const reward = 30
coins.data[Date.now()] = {
x: this.stats.x + (2 * Math.random() - 1) * displacement,
y: this.stats.y + (2 * Math.random() - 1) * displacement,
value: Math.floor(reward * Math.random())
}
}
movement(update){
if(this.stats.dead)return;
this.stats.facing = update.facing
this.moved = update.moved
if(this.moved){
if(this.stats.facing === 'up')this.stats.y -= this.stats.speed
else if(this.stats.facing === 'down')this.stats.y += this.stats.speed
else if(this.stats.facing === 'left')this.stats.x -= this.stats.speed
else if(this.stats.facing === 'right')this.stats.x += this.stats.speed
}
this.check_boundary()
}
attack(event_target){
let target
const game = require('./game')
switch(event_target.type){
case 'tower':
target = game.state.objects.towers[event_target.id]
break
case 'player':
target = game.state.players[event_target.id]
break
case 'mob':
target = game.state.objects.mobs.data[event_target.id]
break
}
if(!this.is_cooldown && this.team !== target.team && distance_between(this, target) <= this.stats.reachable_range && !target.stats.dead){
target.stats.hp -= this.stats.attack_damage
this.cooldown()
if(target.stats.hp <= 0) {
target.die()
this.gain_exp(target.to_exp())
}
game.updates.attacks.push({ target: event_target, type: 'normal_attack'})
}
}
action(){
let start
let target
let type
switch(this.team){
case 1:
start = 5
break
case 2:
start = 0
break
}
let min_distance = 1e9
const game = require('./game')
game.state.objects.towers
.slice(start, start + 5)
.forEach(tower => {
let distance = distance_between(this, tower)
if(distance < min_distance && !tower.stats.dead){
min_distance = distance
target = tower
type = 'tower'
}
})
const players = game.state.players
Object.keys(players)
.filter(key => players[key].team != this.team)
.forEach(key => {
let distance = distance_between(this, players[key])
if(distance < min_distance && !players[key].stats.dead){
min_distance = distance
target = players[key]
type = 'player'
}
})
const mobs = game.state.objects.mobs.data
Object.keys(mobs)
.filter(key => mobs[key].team != this.team)
.forEach(key => {
let distance = distance_between(this, mobs[key])
if(distance < min_distance && !mobs[key].stats.dead){
min_distance = distance
target = mobs[key]
type = 'mob'
}
})
if(!target) return;
const error = 30
if(min_distance > this.stats.reachable_range){
let dx = Math.abs(target.stats.x - this.stats.x)
let dy = Math.abs(target.stats.y - this.stats.y)
if(dx >= error){
if(target.stats.x > this.stats.x)
this.movement(({ facing: 'right', moved: true }))
else if(target.stats.x < this.stats.x)
this.movement(({ facing: 'left', moved: true }))
}
else if(dy >= error){
if(target.stats.y > this.stats.y)
this.movement(({ facing: 'down', moved: true }))
else if(target.stats.y < this.stats.y)
this.movement(({ facing: 'up', moved: true }))
}
}
else {
this.movement(({ facing: this.stats.facing, moved: false }))
this.attack({ type: type, id: target.id })
}
}
die(){
this.stats.dead = true
this.stats.hp = 0
this.drop_coins()
const game = require('./game')
game.state.objects.mobs.removed.push(this.id)
delete game.state.objects.mobs.data[this.id]
}
}
module.exports = Mob