-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenemy.lua
More file actions
247 lines (206 loc) · 8.17 KB
/
enemy.lua
File metadata and controls
247 lines (206 loc) · 8.17 KB
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
CRAB = "crabe"
BIRD = "oiseau"
CORAL = "corail"
HOLE = "trou"
JUMP = "JUMP"
DODGE = "DODGE"
enemy = {}
enemy.init = function()
enemy.isBegin = false
enemy.array={}
enemy.array.setEasy = function()
for i, v in ipairs(enemy.array) do
v.hitboxMultiplier = 0.8
v.hitboxWidth = v.hitboxWidth * v.hitboxMultiplier
v.hitboxHeight = v.hitboxHeight * v.hitboxMultiplier
end
end
enemy.array.setHard = function()
for i, v in ipairs(enemy.array) do
v.hitboxMultiplier = 1
v.hitboxWidth = v.hitboxWidth * v.hitboxMultiplier
v.hitboxHeight = v.hitboxHeight * v.hitboxMultiplier
end
end
enemy.array.update = function(dt, persoX, persoY, persoHeight, persoWidth, speed)
for i, v in ipairs(enemy.array) do
v.update(dt, speed)
if v.collision(persoX, persoY, persoHeight, persoWidth) == true and not(Turtle.lose) then
Turtle.lose = true
Game.setPercent(v.y)
snd_lose:play()
music_man.stop()
end
end
enemy.array.removeDead()
end
enemy.array.removeDead = function()
for i = #enemy.array, 1, -1 do
if not enemy.array[i].isAlive then
table.remove(enemy.array, i)
end
end
end
enemy.array.draw = function()
--love.graphics.setColor(1, 0, 0)
--love.graphics.print(enemy.array[1].hitboxWidth, 10, 10)
for i, v in ipairs(enemy.array) do
v.draw()
end
end
end
enemy.new = function(x, y, speed, type)
local Enemy = {}
Enemy.x = x
Enemy.y = y
Enemy.speed = speed
Enemy.speedx = love.math.random(80,160) * (love.math.random(0,1)*2-1)
Enemy.type = type
Enemy.image = nil
Enemy.width = 180
Enemy.height = 80
Enemy.hitboxMultiplier = 1
Enemy.hitboxWidth = Enemy.width * Enemy.hitboxMultiplier
Enemy.hitboxHeight = Enemy.height * Enemy.hitboxMultiplier
Enemy.sx = nil
Enemy.sy = nil
Enemy.isAlive = true
Enemy.canCollide = true
Enemy.jumpCollision = nil
Enemy.centerX = nil
Enemy.centerY = nil
Enemy.nbFrame = 1
if Enemy.type == CRAB then
Enemy.image = enemyImage.crab
Enemy.jumpCollision = false
Enemy.nbFrame = 5
Enemy.width = 180
Enemy.height = 83
Enemy.hitboxWidth = Enemy.width-20
Enemy.hitboxHeight = Enemy.height-20
elseif Enemy.type == BIRD then
Enemy.image = enemyImage.bird
Enemy.jumpCollision = true
Enemy.nbFrame = 2
Enemy.width = 83
Enemy.height = Enemy.width/1.5
Enemy.hitboxWidth = Enemy.width
Enemy.hitboxHeight = Enemy.height-15
Enemy.vector = {x = 0, y = 0}
elseif Enemy.type == CORAL then
Enemy.image = enemyImage.coral
Enemy.jumpCollision = true
Enemy.nbFrame = 1
Enemy.speedx = 0
Enemy.width = 130
Enemy.height = Enemy.width/1.20
Enemy.hitboxWidth = Enemy.width - 20
Enemy.hitboxHeight = Enemy.height - 20
elseif Enemy.type == JUMP then
Enemy.image = enemyImage.jump
Enemy.canCollide = false
Enemy.jumpCollision = false
Enemy.width = 510
Enemy.height = 510/1.81
Enemy.hitboxWidth = Enemy.width
Enemy.hitboxHeight = Enemy.height
Enemy.nbFrame = 1
Enemy.speedx = 0
elseif Enemy.type == DODGE then
Enemy.image = enemyImage.dodge
Enemy.canCollide = false
Enemy.jumpCollision = false
Enemy.width = 510
Enemy.height = 510/1.81
Enemy.hitboxWidth = Enemy.width
Enemy.hitboxHeight = Enemy.height
Enemy.nbFrame = 1
Enemy.speedx = 0
end
--Enemy.canCollide = false
Enemy.sx = Enemy.width/(Enemy.image:getWidth()/Enemy.nbFrame)
Enemy.sy = Enemy.height/Enemy.image:getHeight()
function Enemy.birdMove(dt)
if distance(0,Turtle.y,0,Enemy.y) <= 300 and Enemy.vector.x == 0 then
Enemy.vector = {x = Turtle.x-Enemy.x , y = Turtle.y-Enemy.y}
normalize(Enemy.vector)
end
Enemy.x = Enemy.x + (Enemy.vector.x * 100)*dt
Enemy.y = Enemy.y + (Enemy.vector.y * 100)*dt
end
Enemy.animation = newAnimation(Enemy.image, Enemy.image:getWidth()/Enemy.nbFrame, Enemy.image:getHeight(), 0.2, Enemy.nbFrame)
if Enemy.speedx > 0 then Enemy.sx = - Enemy.sx end
Enemy.animation:seek( love.math.random(1,Enemy.nbFrame))
function Enemy.update(dt, speed)
Enemy.speed = speed or Enemy.speed
if Enemy.isAlive then
if enemy.isBegin then
Enemy.x = Enemy.x + Enemy.speedx *dt
if Enemy.x < 140 and Enemy.speedx ~= 0 then
Enemy.x = 140
Enemy.speedx = - Enemy.speedx
Enemy.sx = -Enemy.sx
end
if Enemy.x >610 - Enemy.width/2 and Enemy.speedx ~= 0 then
Enemy.x = 610 - Enemy.width/2
Enemy.speedx = - Enemy.speedx
Enemy.sx = -Enemy.sx
end
Enemy.y = Enemy.y + Enemy.speed*dt
if Enemy.type == BIRD then
Enemy.birdMove(dt)
end
Enemy.animation:play()
else
Enemy.animation:stop()
end
Enemy.animation:update(dt)
end
if Enemy.y > screen.H+100 then
Enemy.isAlive = false
end
end
function Enemy.draw()
if Enemy.isAlive and Enemy.y+Enemy.height > 0 then
love.graphics.setColor(1, 1, 1)
Enemy.animation:draw(Enemy.x,Enemy.y,0,Enemy.sx,Enemy.sy,(Enemy.image:getWidth()/Enemy.nbFrame)*0.5,Enemy.image:getHeight()*0.5)
--love.graphics.setColor(1, 0, 0)
--love.graphics.rectangle("line", Enemy.rectEnemy[1].x, Enemy.rectEnemy[1].y, Enemy.hitboxWidth, Enemy.hitboxHeight)
end
end
function Enemy.collision(persoX, persoY, persoHeight, persoWidth)
rectPerso = {
{x = persoX - persoWidth/2, y = persoY - persoHeight/2},
{x = persoX + persoWidth/2, y = persoY - persoHeight/2},
{x = persoX - persoWidth/2, y = persoY + persoHeight/2},
{x = persoX + persoWidth/2, y = persoY + persoHeight/2},
}
Enemy.rectEnemy = {
{x = Enemy.x - Enemy.hitboxWidth/2, y = Enemy.y - Enemy.hitboxHeight/2},
{x = Enemy.x + Enemy.hitboxWidth/2, y = Enemy.y - Enemy.hitboxHeight/2},
{x = Enemy.x - Enemy.hitboxWidth/2, y = Enemy.y + Enemy.hitboxHeight/2},
{x = Enemy.x + Enemy.hitboxWidth/2, y = Enemy.y + Enemy.hitboxHeight/2},
}
if Enemy.canCollide then
if
collide(rectPerso, Enemy.rectEnemy[1]) or
collide(rectPerso, Enemy.rectEnemy[2]) or
collide(rectPerso, Enemy.rectEnemy[3]) or
collide(rectPerso, Enemy.rectEnemy[4]) or
collide(Enemy.rectEnemy, rectPerso[1]) or
collide(Enemy.rectEnemy, rectPerso[2]) or
collide(Enemy.rectEnemy, rectPerso[3]) or
collide(Enemy.rectEnemy, rectPerso[4]) then
if not Enemy.jumpCollision and Turtle.state=='jump' then
return false
else
return true
end
end
return false
else
return false
end
end
return Enemy
end