-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.lua
146 lines (96 loc) · 1.99 KB
/
game.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
--
-- game.lua
-- controls the flow of the game
--
require 'resources'
require 'map'
require 'player'
Game = {}
Game.__index = Game
local map, player, friend
function Game.new()
local self = {}
setmetatable(self, Game)
self.state = 'intro'
self.level = 1
return self
end
function Game:load()
map = Introscreen.new()
player = Player.new()
end
function Game:update(dt)
-- intro state
if self.state == 'intro' then
map:update(dt)
-- begin game with finished
if map.page == map.lastpage then
self.state = 'play'
map = Map.new(self.level)
player.active = true
end
end
-- play state
if self.state == 'play' then
self.state = player:update(dt, map)
map:update(dt)
-- talk state
elseif self.state == 'talk' then
friend.speaking = true
map:update(dt)
if friend:isDone() then
self.state = 'play'
end
-- gameover state
elseif self.state == 'gameover' then
player:kill()
map = Endscreen.new()
end
end
function Game:keypressed(key)
-- exit game if 'escape' is pressed
if key == 'escape' then
love.event.push('quit')
end
if self.state == 'intro' then
-- continue with spacebar
if key == 'space' then
map.page = map.page + 1
end
end
if self.state == 'play' then
-- talk if spacebar is pressed within range
if key == 'space' and player.state == 'standing' then
friend = player:isNearFriend(map.friends)
if friend then
self.state = 'talk'
end
end
end
if self.state == 'talk' then
-- exit talking mode if 'x' is pressed
if key == 'x' then
friend.speaking = false
self.state = 'play'
-- continue talking with spacebar
elseif key == 'space' then
friend.lineNum = friend.lineNum + 1
end
end
if self.state == 'gameover' then
if key == 'q' then
love.event.push('quit')
elseif key == 'space' then
self:reload()
end
end
end
function Game:draw()
map:draw()
if player.active then player:draw() end
end
function Game:reload()
self.state = 'play'
self.level = 1
self:load()
end