-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmenu.lua
210 lines (182 loc) · 5.15 KB
/
menu.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
--[[
This source code is provided for educational purposes only.
Copyright by Eric Gaudet.
If you like reading the source code of this game, please consider supporting
the author by buying the game at http://www.rti-zone.org/eyes-in-the-night/
]]
require "credits"
require "instructions"
require "chapter"
require "game"
require "game_mode"
menu = {}
local menu_img
local buttons = {}
local button_labels = {}
local eyes = {}
local selected = 0
score = 0
local function survival_init_map() end
local function survival_end_condition() return false end
local function add_button(label, x0, y0, x1, y1)
buttons[label] = {
x0 = x0,
y0 = y0,
x1 = x1,
y1 = y1
}
table.insert(button_labels, label)
end
local function find_button(x, y)
for k, v in pairs(buttons) do
if v.x0<=x and x<=v.x1
and v.y0<=y and y<=v.y1
then
return k
end
end
end
function menu.load()
menu_img = newPaddedImage("assets/menu1.png")
cont_img = newPaddedImage("assets/menu_cont.png")
surv_img = newPaddedImage("assets/menu_surv.png")
table.insert(eyes, new_eye(.2, 50, 410, "purple"))
table.insert(eyes, new_eye(.3, 550, 60, "green"))
table.insert(eyes, new_eye(.15, 480, 460, "brown"))
add_button("continue", 83, 216, 531, 267)
add_button("new", 140, 278, 475, 327)
add_button("survival", 102, 335, 513, 390)
add_button("instructions", 158, 395, 436, 442)
add_button("credits", 230, 456, 380, 506)
love.audio.stop()
music_current = nil
end
function menu.draw()
love.graphics.setColor(255, 255, 255, 255)
love.graphics.draw(menu_img, 0, 0, 0.0, 1.0, 1.0, 0, 0)
local mx = love.mouse.getX()
local my = love.mouse.getY()
if selected>0 then
mb = button_labels[selected]
mx = (buttons[mb].x0 + buttons[mb].x1) / 2
my = (buttons[mb].y0 + buttons[mb].y1) / 2
else
mb = find_button(mx,my)
end
for k, v in pairs(eyes) do
v:draw(mx,my)
end
if userdata.level > 1 and userdata.level < 34 then
love.graphics.draw(cont_img, 88, 221, 0.0, 1.0, 1.0, 0, 0)
love.graphics.setColor(255, 255, 255, 105)
love.graphics.printf("Lvl\n"..userdata.level, 525, 221, 80, "center")
love.graphics.setColor(255, 255, 255, 255)
end
if userdata.survival then
love.graphics.draw(surv_img, 107, 340, 0.0, 1.0, 1.0, 0, 0)
love.graphics.setColor(255, 255, 255, 160)
love.graphics.printf("Best Survival\n"..userdata.best.." points", 310, 540, 300, "center")
love.graphics.setColor(255, 255, 255, 255)
end
if userdata.adventure>=0 then
love.graphics.setColor(255, 255, 255, 160)
love.graphics.printf("Best Adventure\n"..userdata.adventure.." continues", 10, 540, 300, "center")
love.graphics.setColor(255, 255, 255, 255)
end
if mb then
local box = buttons[mb]
love.graphics.setColor(255, 255, 0, 180)
love.graphics.rectangle("line", box.x0, box.y0, box.x1-box.x0, box.y1-box.y0 )
love.graphics.setColor(255, 255, 255, 255)
end
end
function menu.update(dt) end
local function do_continue()
game_mode = new_adventure_mode()
set_level(userdata.level)
game_mode.num_ball = userdata.balls
score = userdata.score
userdata.continue = userdata.continue + 1
save_userdata()
change_state(chapter)
end
local function do_newgame()
game_mode = new_adventure_mode()
set_level(1)
score = 0
change_state(chapter)
end
local function do_survival()
game_mode = new_survival_mode()
score = 0
change_state(game)
end
function menu.mousemoved(x, y, dx, dy)
selected = 0
end
local function executeSelected(mr)
if mr == "continue" and userdata.level > 1 and userdata.level < 34 then
do_continue()
elseif mr == "new" then
do_newgame()
elseif userdata.survival and mr == "survival" then
do_survival()
elseif mr == "instructions" then
change_state(instructions)
elseif mr == "credits" then
change_state(credits)
end
end
function menu.mousereleased(x,y,b)
mp = find_button(mousex, mousey)
mr = find_button(x, y)
if mp ~= mr then
return
end
executeSelected(mr)
end
function menu.keypressed(key)
if key == "p" or key == "P"
or key == "g" or key == "G"
then
executeSelected("continue")
elseif key == "return" or key == " " then
if selected == 0 then
executeSelected("continue")
else
executeSelected(button_labels[selected])
end
elseif key == "n" or key == "N" then
executeSelected("new")
elseif userdata.survival and (key == "s" or key == "S") then
executeSelected("survival")
elseif key == "i" or key == "I" then
executeSelected("instructions")
elseif key == "c" or key == "C" then
executeSelected("credits")
elseif key == "up" then
if selected>1 then
selected = selected - 1
else
selected = 1
end
if selected == 1 and not (userdata.level > 1 and userdata.level < 34) then
selected = 2
end
if selected == 3 and not userdata.survival then
selected = 2
end
elseif key == "down" then
if selected<5 then
selected = selected + 1
else
selected = 5
end
if selected == 1 and not (userdata.level > 1 and userdata.level < 34) then
selected = 2
end
if selected == 3 and not userdata.survival then
selected = 4
end
end
end