-
Notifications
You must be signed in to change notification settings - Fork 0
/
enemies.lua
175 lines (135 loc) · 4.76 KB
/
enemies.lua
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
--
-- Created by IntelliJ IDEA.
-- User: arne
-- Date: 19.02.17
-- Time: 13:43
-- To change this template use File | Settings | File Templates.
--
local functions = {}
local simple_enemies = {}
local enemy_types = { "simple" }
local enemy_tweening_values = { simple = 0 }
local hc = require("hc")
local timer = require("hump.timer")
require("difficulty_handler")
require("scaling")
functions.enable_enemy_spawning = true
local SHIP_FRAGMENT_SCALE = scaling.get("ship_fragment_scale")
local SHIP_FRAGMENT_FLIGHT_DURATION = 1.6
local function create_enemy()
local new_enemy = {}
--- load texture
new_enemy.texture = love.graphics.newImage("img/enemy_ship_2_body.png")
--- store width and height
new_enemy.width, new_enemy.height = new_enemy.texture:getDimensions()
--- no scaling
new_enemy.scale = scaling.get("enemy_simple_scale")
new_enemy.score = difficulty.get("enemy_simple_score", current_level())
new_enemy.type = "simple"
--- find free position
local position_found = false
while not position_found do
new_enemy.x = math.random(love.graphics.getWidth(), love.graphics.getWidth() + 100)
new_enemy.y = math.random(new_enemy.height, love.graphics.getHeight() - 2 * new_enemy.height)
--return self:polygon(
-- -- x,
-- -- y,
-- -- x+w,
-- -- y,
-- -- x+w,
-- -- y+h,
-- -- x,
-- -- y+h)
new_enemy.shape = hc.polygon(
new_enemy.x,
new_enemy.y,
new_enemy.x + new_enemy.width * new_enemy.scale,
new_enemy.y,
new_enemy.x + new_enemy.width * new_enemy.scale,
new_enemy.y + new_enemy.height * new_enemy.scale,
new_enemy.x,
new_enemy.y + new_enemy.height * new_enemy.scale
)
new_enemy.shape.object_type = "enemy"
position_found = true
for _, _ in pairs(hc.collisions(new_enemy.shape)) do
-- if we found a collision, that enemy will not be added; its shape must be deleted from the collision system
position_found = false
hc.remove(new_enemy.shape)
end
end
table.insert(simple_enemies, new_enemy)
end
local function create_simple_enemies()
while #simple_enemies < difficulty.get("enemy_simple_count", current_level()) do
create_enemy()
end
end
local function remove_enemy(enemy_shape, enemy_index)
hc.remove(enemy_shape)
table.remove(simple_enemies, enemy_index)
end
functions.update = function(dt, station)
if functions.enable_enemy_spawning then
create_simple_enemies()
end
for index, enemy in ipairs(simple_enemies) do
local enemy_speed = scaling.get_enemy_speed(enemy.type, current_level()) + enemy_tweening_values[enemy.type]
enemy.x = enemy.x - (dt * enemy_speed)
enemy.shape:move(-dt * enemy_speed, 0)
-- if this enemy is hidden by the station, remove it
if station:contains_polygon(enemy.shape) then
remove_enemy(enemy.shape, index)
end
if enemy.x + enemy.width < 0 then
remove_enemy(enemy.shape, index)
end
end
end
functions.set_enemies_spawning = function(spawn)
functions.enable_enemy_spawning = spawn
end
functions.leave = function()
simple_enemies = {}
end
local function trigger_speed_tween_up(level)
local TWEEN_DURATION = 1
for _, enemy_type in ipairs(enemy_types) do
local old_speed = scaling.get_enemy_speed(enemy_type, level - 1)
local new_speed = scaling.get_enemy_speed(enemy_type, level)
enemy_tweening_values[enemy_type] = old_speed - new_speed
timer.tween(TWEEN_DURATION, enemy_tweening_values, { simple = 0 }, 'out-quad')
end
end
functions.enter = function()
signal.register("levelup", trigger_speed_tween_up)
end
functions.draw = function()
for _, item in ipairs(simple_enemies) do
love.graphics.drawObject(item)
end
end
functions.remove_colliding_enemies = function(shape, on_kill)
local had_collision = false
for i, enemy in ipairs(simple_enemies) do
if enemy.shape:collidesWith(shape) then
table.remove(simple_enemies, i)
hc.remove(enemy.shape)
had_collision = true
flyapartomatic.spawn({
"img/simple_enemy_ship_fragment_1.png",
"img/simple_enemy_ship_fragment_2.png",
"img/simple_enemy_ship_fragment_3.png",
"img/simple_enemy_ship_fragment_4.png",
"img/simple_enemy_ship_fragment_5.png",
},
enemy.x,
enemy.y,
SHIP_FRAGMENT_FLIGHT_DURATION,
SHIP_FRAGMENT_SCALE)
on_kill(enemy)
end
end
return had_collision
end
return functions