-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.lua
300 lines (255 loc) · 8.76 KB
/
main.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
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
require "gameLevel"
require "gameScreen"
require "input"
require "collisions"
require "conversation"
require "character"
-- ##################################################################
-- # Constants
-- ##################################################################
walkingScreenWidth = love.graphics.getWidth() / 2
screenHeight = 600
playerSpeed = 150
friendSpeed = 100
carSpeed = 600
carWidth = 258
carHeight = 84
scrollSpeed = 100
numScreens = 10
frameSide = 32
playerStartX = 3 / 4 * walkingScreenWidth - frameSide * 2
playerStartY = screenHeight / 4 - frameSide / 2
friendStartX = 3 / 4 * walkingScreenWidth
friendStartY = screenHeight / 4
restartTimerMax = 3
restartTimer = 0
restarting = false
function love.load()
-- For ZeroBrane Studio
if arg[#arg] == "-debug" then require("mobdebug").start() end
--
playerImage = love.graphics.newImage("resources/images/player.png")
friendImage = love.graphics.newImage("resources/images/friend.png")
carImage = love.graphics.newImage("resources/images/car.png")
victory = love.audio
.newSource("resources/sound/Jingle_Win_01.mp3", "static")
failure = love.audio.newSource("resources/sound/Jingle_Lose_01.mp3",
"static")
skid = love.audio.newSource("resources/sound/skid.mp3", "static")
music = love.audio.newSource("resources/sound/Sound Way NES.mp3", "static")
imageWidth = playerImage:getWidth()
imageHeight = playerImage:getHeight()
local playerFrames = spriteSheet.SpriteSheet:new(friendImage:getWidth(),
friendImage:getHeight(),
frameSide, 16,
"columnsFirst")
local friendFrames = spriteSheet.SpriteSheet:new(friendImage:getWidth(),
friendImage:getHeight(),
frameSide, 16,
"columnsFirst")
local playerAnimations = {
standing = animation.Animation:new(0, 2, 2),
walking = animation.Animation:new(0.1, 1, 4)
}
local friendAnimations = {
standing = animation.Animation:new(0, 2, 2),
walking = animation.Animation:new(0.1, 1, 4)
}
objects = {}
player = character.Character:new(playerStartX, playerStartY, 40, 40,
playerSpeed, playerAnimations,
playerFrames, playerImage)
friend = character.Character:new(friendStartX, friendStartY, 40, 40,
friendSpeed, friendAnimations,
friendFrames, friendImage)
car = {xPos = walkingScreenWidth, yPos = screenHeight, image = carImage}
table.insert(objects, player)
table.insert(objects, friend)
table.insert(objects, car)
screens = {}
conversation.init(onNewTopic, onAnswer, onLose)
startGame()
end
function startGame()
music:setVolume(0.5)
love.audio.stop(music)
love.audio.play(music)
gameState = "Running"
conversation.reset("Get ready!")
player:reset()
friend:reset()
car = {
xPos = walkingScreenWidth + carWidth,
yPos = screenHeight,
image = carImage
}
screens = gameLevel.generateScreens(numScreens, walkingScreenWidth,
screenHeight)
end
function love.draw()
for i = 1, table.getn(screens) do screens[i]:draw() end
if car.flipped then
love.graphics.draw(car.image, car.xPos, car.yPos, 0, 1.4, 1.4)
else
love.graphics.draw(car.image, car.xPos, car.yPos, 0, -1.4, 1.4)
end
if (player.yPos > friend.yPos) then
friend:draw()
player:draw()
else
player:draw()
friend:draw()
end
conversation.draw()
end
function love.update(dt)
friend:update(dt)
player:update(dt)
if restarting then
conversation.interrupt(gameState)
if restartTimer < restartTimerMax then
restartTimer = restartTimer + dt
updateEnding(dt)
else
restartTimer = 0
restarting = false
startGame()
end
else
conversation.update(dt)
updateCharacter(friend, dt, getFriendDirections)
moveCharacter(friend, dt)
setFriendSpeed()
updateCharacter(player, dt, input.getMovementInput)
moveCharacter(player, dt)
for i = 1, table.getn(screens) do
screens[i]:update(scrollSpeed, dt)
end
end
end
function updateCharacter(char, dt, getDirections)
char.dx = 0
char.dy = -scrollSpeed
local directions = getDirections()
local up, left, down, right = directions["up"], directions["left"],
directions["down"], directions["right"]
char.animations[char.currentAnimation]:update(dt)
if not (up or left or down or right) then
char:setAnimation("standing")
return
end
char:setAnimation("walking")
local speed = char.speed
if ((down or up) and (left or right)) then speed = speed / math.sqrt(2) end
if down and char.yPos < screenHeight - char.height then
char.dy = char.dy + speed
elseif up then
char.dy = char.dy - speed
end
if right and char.xPos < walkingScreenWidth - char.width then
char.dx = char.dx + speed
elseif left and char.xPos > 0 then
char.dx = char.dx - speed
end
end
function moveCharacter(char, dt)
-- Update character position
char.xPos = char.xPos + char.dx * dt
char.yPos = char.yPos + char.dy * dt
if collisions.checkOverlap(player, friend) then
collisions.resolveCollision(player, friend, dt)
end
for i = 1, table.getn(screens) do
local screen = screens[i]
if collisions.checkOverlap(char, screen) then
char.screenLayout = screen.layout
if char == player and char.screenLayout == "finish" then
gameState = "Finished"
restart("")
end
for j = 1, table.getn(screens) do
local barrier = screens[j]:getBarrier()
if barrier then
if collisions.checkOverlap(char, barrier) then
collisions.resolveCollision(char, barrier, scrollSpeed,
dt)
break
end
else
local hazard = screens[j]:getHazard()
if hazard then
if collisions.checkOverlap(char, hazard) then
love.audio.play(skid)
positionCar()
gameState = "NearMiss"
player.hazard = hazard
restart()
end
end
end
end
end
end
if char == player and char.yPos < 0 then
gameState = "EatenByScroll"
restart()
end
end
function getFriendDirections()
down = true
up = false
left = false
right = false
if friend.screenLayout == "leftToRight" and friend.xPos < friendStartX then
right = true
elseif friend.screenLayout == "rightToLeft" and friend.xPos > 1 / 4 *
walkingScreenWidth + frameSide then
left = true
end
return {up = up, left = left, down = down, right = right}
end
function setFriendSpeed()
if friend.yPos < screenHeight / 2 then
friend.speed = friendSpeed * 1.2
else
friend.speed = friendSpeed
end
end
function restart()
love.audio.stop(music)
if gameState == "Finished" then
friend:showBubble("heart")
love.audio.play(victory)
else
love.audio.play(failure)
friend:showBubble("shout")
end
restarting = true
end
function updateEnding(dt) if gameState == "NearMiss" then updateCar(dt) end end
function positionCar()
if string.find(player.screenLayout, "left") then car.xPos = -carWidth end
end
function updateCar(dt)
if player.yPos < player.hazard.yPos + 400 - carHeight then
car.yPos = player.yPos
else
car.yPos = player.hazard.yPos + 400 - carHeight
end
if string.find(player.screenLayout, "left") then
car.flipped = true
if car.xPos < player.xPos - carWidth then
car.xPos = car.xPos + dt * carSpeed
end
else
if car.xPos > player.xPos + player.width + carWidth then
car.xPos = car.xPos - dt * carSpeed
end
end
end
function onNewTopic() friend:showBubble("speech") end
function onAnswer() player:showBubble("speech") end
function onLose()
gameState = "WrongAnswer"
restart()
end