-
Notifications
You must be signed in to change notification settings - Fork 1
/
enemieupdate.js
145 lines (135 loc) · 4.11 KB
/
enemieupdate.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
/**
* Created by Julien on 2016-01-30.
*/
var enemie_tic = 0;
function enemieupdate(){
for(var i = 0; i < enemies.length; i++) {
if(enemies[i].alive){
aiMovement(enemies[i], 200, enemiemovement);
alignDirection(enemies[i].body);
enemies[i].weapon.fire(enemies[i]);
}
// if(!isinvinsible && game.physics.arcade.collide(enemies[i], player) && !permainvincible)
// {
// player.damage(1);
// isinvinsible = true;
// }
}
}
function enemiemovement(enemie, targetDistance, distance, angle) {
//for60ticks
//var pos = player.getCurrentPosition();
//player.x;
//enemies.getpoint
if(distance < targetDistance)
{
if(angle < 22.5 && angle >= -22.5)
{
//right
enemie.body.velocity.x = +defaultmovement;
}
else if(angle <=-22.5 && angle > -67.5)
{
//rightup
enemie.body.velocity.x = +defaultmovement / 2;
enemie.body.velocity.y = -defaultmovement / 2;
}
else if(angle <= -67.5 && angle > -112.5)
{
//up
enemie.body.velocity.y = -defaultmovement;
}
else if (angle <=-112.5 && angle > -157.5) {
//leftup
enemie.body.velocity.x = -defaultmovement / 2;
enemie.body.velocity.y = -defaultmovement / 2;
}
else if (angle <= -157.5 || angle > 157.5)
{
//left
enemie.body.velocity.x = -defaultmovement;
}
else if (angle >=112.5 && angle < 157.5) {
//leftdown
enemie.body.velocity.x = -defaultmovement / 2;
enemie.body.velocity.y = defaultmovement / 2;
}
else if (angle >= 67.5 && angle < 112.5 )
{
//down
enemie.body.velocity.y = defaultmovement;
}
else if ( angle >=22.5 && angle < 67.5)
{
//rightdown
enemie.body.velocity.x = +defaultmovement / 2;
enemie.body.velocity.y = defaultmovement / 2;
}
}
else {
if (enemie_tic < 60) {
++enemie_tic;
return;
}
randomDirection(enemie.body);
enemie_tic = 0;
}
}
// @entity : sprite game state
// @targetDistance : distance from player (attack range)
// @movementFunction : specific movement function for sprite
function aiMovement(entity, targetDistance, movementFunction) {
var distance = game.physics.arcade.distanceBetween(player, entity);
var angle = game.physics.arcade.angleBetween(entity.body, new Phaser.Point(player.body.x+20,player.body.y+20));
angle = game.math.radToDeg(angle);
movementFunction(entity, targetDistance, distance, angle);
}
// @body : body param of sprite
function alignDirection(body) {
var dirangle = Math.atan2(body.velocity.y, body.velocity.x);
body.rotation = dirangle+90;
}
// @body : body param of sprite
function randomDirection(body) {
switch (game.rnd.integerInRange(0, 9)) {
case 0:
//nomovement
break;
case 1:
//right
body.velocity.x = defaultmovement;
break;
case 2:
//left
body.velocity.x = -defaultmovement;
break;
case 3:
//up
body.velocity.y = defaultmovement;
break;
case 4:
//down
body.velocity.y = -defaultmovement;
break;
case 5:
//rightup
body.velocity.x = defaultmovement / 2;
body.velocity.y = defaultmovement / 2;
break;
case 6:
//leftup
body.velocity.x = -defaultmovement / 2;
body.velocity.y = defaultmovement / 2;
break;
case 7:
//rightdown
body.velocity.x = defaultmovement / 2;
body.velocity.y = -defaultmovement / 2;
break;
case 8:
//leftdown
body.velocity.x = -defaultmovement / 2;
body.velocity.y = -defaultmovement / 2;
break;
}
}