-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.lua
More file actions
243 lines (232 loc) · 9.84 KB
/
main.lua
File metadata and controls
243 lines (232 loc) · 9.84 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
constants = require("constants")
utils = require("utils")
player = require("player")
sound = require("game.sound")
music = require("game.music")
enemy = require("enemy")
function love.load()
gravity = constants.GRAVITY
platforms = {}
particles = {}
coins = {}
bullets = {}
end_points = {}
tileSize = constants.TILE_SIZE
mapWidth = config.map.width
mapHeight = config.map.height
camera = { x = player.startX + player.width / 2 - window.width / 2 - window.width }
isResetting = false
resetCameraTargetX = 0
cameraSpeed = constants.CAMERA_SPEED
is_initial_scrolling = true
initial_scroll_timer = 0
is_end_scrolling = false
end_scroll_timer = 0
loadMap("assets/levels/map.txt")
end
function loadMap(filename)
local file = love.filesystem.read(filename)
local y = 0
local enemy_positions = {}
for line in file:gmatch("[^\r\n]+") do
for x = 1, #line do
local char = line:sub(x, x)
if char == "#" then
table.insert(platforms, {
x = (x - 1) * tileSize,
y = y * tileSize,
width = tileSize,
height = tileSize
})
elseif char == "@" then
player.x = (x - 1) * tileSize
player.y = y * tileSize
player.startX = player.x
player.startY = player.y
camera.x = player.startX + player.width / 2 - window.width / 2 - window.width
elseif char == "a" then
table.insert(enemy_positions, { x = (x - 1) * tileSize, y = y * tileSize })
elseif char == "$" then
table.insert(coins, {
x = (x - 1) * tileSize,
y = y * tileSize,
width = constants.COIN_SIZE,
height = constants.COIN_SIZE,
is_collected = false
})
elseif char == "+" then
table.insert(coins, {
x = (x - 1) * tileSize,
y = y * tileSize,
width = constants.COIN_SIZE,
height = constants.COIN_SIZE,
is_collected = false,
is_powerup = true
})
elseif char == "*" then
table.insert(end_points, {
x = (x - 1) * tileSize,
y = y * tileSize,
width = tileSize,
height = tileSize
})
end
end
y = y + 1
end
enemy.load(enemy_positions)
end
function resetLevel()
player.reset()
isResetting = true
end_points = {}
resetCameraTargetX = math.max(0, math.min(player.startX + player.width / 2 - window.width / 2, config.map.width * tileSize - window.width))
end
function love.update(dt)
if is_initial_scrolling then
initial_scroll_timer = initial_scroll_timer + dt
local target_camera_x = math.max(0, math.min(player.startX + player.width / 2 - window.width / 2, config.map.width * tileSize - window.width))
local start_camera_x = player.startX + player.width / 2 - window.width / 2 - window.width
local t = math.min(initial_scroll_timer / constants.TRANSITION_TIME, 1.0)
camera.x = start_camera_x + (target_camera_x - start_camera_x) * t
if initial_scroll_timer >= constants.TRANSITION_TIME then
is_initial_scrolling = false
camera.x = target_camera_x
end
elseif is_end_scrolling then
end_scroll_timer = end_scroll_timer + dt
local start_camera_x = camera.x
local target_camera_x = start_camera_x + window.width
local t = math.min(end_scroll_timer / constants.TRANSITION_TIME, 1.0)
camera.x = start_camera_x + ((target_camera_x - start_camera_x) * t)
if end_scroll_timer >= constants.TRANSITION_TIME then
is_end_scrolling = false
camera.x = target_camera_x
end
elseif isResetting then
local distance = resetCameraTargetX - camera.x
if math.abs(distance) < 1 then
camera.x = resetCameraTargetX
player.x = player.startX
player.y = player.startY
isResetting = false
player.reset()
else
camera.x = camera.x + distance * math.min(cameraSpeed * dt / math.abs(distance), 1)
end
else
local should_reset, end_reached = player.update(dt, platforms, enemy.getEnemies(), coins, end_points, bullets, particles, gravity, mapWidth, tileSize)
if should_reset then
resetLevel()
elseif end_reached then
is_end_scrolling = true
end_scroll_timer = 0
end
enemy.update(dt, platforms)
for i = #bullets, 1, -1 do
local bullet = bullets[i]
bullet.x = bullet.x + bullet.velocityX * dt
bullet.lifetime = bullet.lifetime - dt
if bullet.lifetime <= 0 then
table.remove(bullets, i)
else
for j = #enemy.getEnemies(), 1, -1 do
if utils.checkCollision(bullet, enemy.getEnemies()[j]) then
for k = 1, constants.PARTICLE_COUNT do
table.insert(particles, {
x = enemy.getEnemies()[j].x + math.random(-10, 10),
y = enemy.getEnemies()[j].y + math.random(-10, 10),
size = math.random(4, 8),
velocityX = math.random(-100, 100),
velocityY = math.random(-200, -50),
lifetime = math.random(0.5, 1.0)
})
end
table.remove(enemy.getEnemies(), j)
table.remove(bullets, i)
break
end
end
end
end
for i = #particles, 1, -1 do
local particle = particles[i]
particle.x = particle.x + particle.velocityX * dt
particle.y = particle.y + particle.velocityY * dt
particle.velocityY = particle.velocityY + gravity * dt
particle.lifetime = particle.lifetime - dt
if particle.lifetime <= 0 then
table.remove(particles, i)
end
end
if not is_initial_scrolling and not is_end_scrolling then
local target_camera_x = player.x + player.width / 2 - window.width / 2
target_camera_x = math.max(0, math.min(target_camera_x, config.map.width * tileSize - window.width))
local distance = target_camera_x - camera.x
camera.x = camera.x + distance * math.min(cameraSpeed * dt / math.abs(distance), 1)
end
end
end
function love.keypressed(key)
if key == "space" then
player.shoot(bullets)
end
end
function love.draw()
love.graphics.push()
love.graphics.translate(-camera.x, 0)
love.graphics.setColor(constants.COLORS.PLATFORM)
for _, platform in ipairs(platforms) do
love.graphics.rectangle("fill", platform.x, platform.y, platform.width, platform.height)
end
if not isResetting and not player.is_exploding then
if player.is_blinking and math.floor(player.blink_count) % 2 == 1 then
love.graphics.setColor(constants.COLORS.PLAYER_BLINKING)
elseif player.is_powerup_blinking and math.floor(player.powerup_blink_timer / constants.BLINK_INTERVAL) % 2 == 1 then
love.graphics.setColor(constants.COLORS.PLAYER_POWERUP)
elseif player.can_shoot then
love.graphics.setColor(constants.COLORS.PLAYER_POWERUP)
else
love.graphics.setColor(constants.COLORS.PLAYER_NORMAL)
end
love.graphics.rectangle("fill", player.x, player.y, player.width, player.height)
end
love.graphics.setColor(constants.COLORS.ENEMY)
for _, e in ipairs(enemy.getEnemies()) do
love.graphics.rectangle("fill", e.x, e.y, e.width, player.height)
end
love.graphics.setColor(constants.COLORS.PARTICLE)
for _, particle in ipairs(particles) do
love.graphics.rectangle("fill", particle.x, particle.y, particle.size, particle.size)
end
love.graphics.setColor(constants.COLORS.COIN)
for _, coin in ipairs(coins) do
if not coin.is_collected and not coin.is_powerup then
love.graphics.rectangle("fill", coin.x, coin.y, coin.width, coin.height)
end
end
love.graphics.setColor(constants.COLORS.POWERUP)
for _, coin in ipairs(coins) do
if not coin.is_collected and coin.is_powerup then
love.graphics.rectangle("fill", coin.x, coin.y, coin.width, coin.height)
end
end
love.graphics.setColor(constants.COLORS.BULLET)
for _, bullet in ipairs(bullets) do
love.graphics.rectangle("fill", bullet.x, bullet.y, bullet.width, bullet.height)
end
love.graphics.setColor(constants.COLORS.END_POINT)
for _, end_point in ipairs(end_points) do
love.graphics.rectangle("fill", end_point.x, end_point.y, end_point.width, end_point.height)
end
love.graphics.pop()
if is_initial_scrolling then
local alpha = 1.0 - math.min(initial_scroll_timer / constants.TRANSITION_TIME, 1.0)
love.graphics.setColor(constants.COLORS.BLACK[1], constants.COLORS.BLACK[2], constants.COLORS.BLACK[3], alpha)
love.graphics.rectangle("fill", 0, 0, window.width, love.graphics.getHeight())
elseif is_end_scrolling then
local alpha = 1.0 - math.min(end_scroll_timer / constants.TRANSITION_TIME, 1.0)
love.graphics.setColor(constants.COLORS.BLACK[1], constants.COLORS.BLACK[2], constants.COLORS.BLACK[3], alpha)
love.graphics.rectangle("fill", 0, 0, window.width, love.graphics.getHeight())
end
end