-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhud.lua
188 lines (181 loc) · 6.94 KB
/
hud.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
hud = {msgFadetimemax = .15, msgFadeouttime = 0, msgFadeintime = 0}
--we need to store the old ones, we do this once, to prevent EVUL things from happening
local oldUpdate
local oldKeypressed
local oldJoystickpressed
local msgwidth
--new callbacks, local, nobody else needs it
local function msgUpdate(dt)
if hud.msgFadeintime > 0 then
hud.msgFadeintime = hud.msgFadeintime - dt
if hud.msgFadeintime < 0 then
hud.msgFadeintime = 0
end
end
if hud.msgFadeouttime > 0 then
hud.msgFadeouttime = hud.msgFadeouttime - dt
if hud.msgFadeouttime < 0 then
hud.msgFadeouttime = 0
hud.messagebox = nil
hud.speechbox = nil
love.update = oldUpdate
love.keypressed = oldKeypressed
love.joystickpressed = oldJoystickpressed
if hud.cb then hud.cb(hud.userd) end
end
end
end
local function msgKeypressed(key)
if key == 'return' then
hud.msgFadeouttime = hud.msgFadetimemax-hud.msgFadeintime
end
end
local function msgJoystickpressed(j, key)
if j ~= activejoystick then return end
if key == 0 then
msgKeypressed('return')
end
end
function hud.draw()
--set the camera to the HUD camera, 1:1 scale
setCamera(cameras.hud)
--store the stuff we need to use a lot, this saves us from calling
--these functions a lot, saves CPU
local col = {love.graphics.getColor()}
local width = love.graphics.getWidth()
local height = love.graphics.getHeight()
--set the color
love.graphics.setColor(175, 175, 50)
--draw the inside
if hud.score or dbg then
love.graphics.rectangle('fill', width/2-200, 0, 400, 50)
love.graphics.triangle('fill', width/2-200, 0, width/2-200, 50, width/2-250, 0)
love.graphics.triangle('fill', width/2+200, 0, width/2+200, 50, width/2+250, 0)
end
if hud.lvl1 or dbg then
love.graphics.rectangle('fill', 0, 0, 100, 30)
love.graphics.rectangle('fill', 0, 30, 80, 20)
love.graphics.triangle('fill', 80, 30, 100, 30, 80, 50)
end
if hud.lvl2 or dbg then
love.graphics.rectangle('fill', width-100, 0, 100, 30)
love.graphics.rectangle('fill', width-80, 30, 80, 20)
love.graphics.triangle('fill', width-80, 30, width-80, 50, width-100, 30)
end
love.graphics.setColor(0, 0, 0)
--draw the outline
if hud.score or dbg then
love.graphics.line(width/2-250, 0, width/2-200, 50)
love.graphics.line(width/2-200, 50, width/2+200, 50)
love.graphics.line(width/2+200, 50, width/2+250, 0)
end
if hud.lvl1 or dbg then
love.graphics.line(0, 50, 80, 50)
love.graphics.line(100, 0, 100, 30)
love.graphics.line(80, 50, 100, 30)
end
if hud.lvl2 or dbg then
love.graphics.line(width, 50, width-80, 50)
love.graphics.line(width-100, 0, width-100, 30)
love.graphics.line(width-100, 30, width-80, 50)
end
--and, draw the inside
if hud.score then
love.graphics.drawf(tostring(game.score), width/2-5, 30, 20, 'center')
end
--debug code
if dbg then
love.graphics.setColor(255, 0, 0)
local x = width-120
local basey = love.graphics.getHeight()/2
local line = love.graphics.getFont():getHeight() + 2
love.graphics.print("FPS: " .. love.timer.getFPS(), x, basey)
love.graphics.print("Jump: " .. tostring(game.allowjump), x, basey+line)
love.graphics.print("Finished: " .. tostring(game.finished), x, basey+2*line)
end
--mesage boxes, they are part of the HUD, so this is the perfect place
if hud.messagebox then
--set the color to transparent black, and draw that over the entire screen
love.graphics.setColor(0, 0, 0, 100)
love.graphics.rectangle('fill', 0, 0, width, height)
--now start drawing those contents
love.graphics.setColor(255, 255, 255)
local lineheight = (hud.msgFadeouttime > 0 and hud.msgFadeouttime/hud.msgFadetimemax or (hud.msgFadeintime > 0 and 1-hud.msgFadeintime/hud.msgFadetimemax or 1)) * height/4
love.graphics.rectangle('fill', 0, height/4, width, lineheight)
love.graphics.setColor(0,255,0)
love.graphics.setLineWidth(2)
love.graphics.line(0, height/4, width, height/4)
love.graphics.line(0, height/4+lineheight, width, height/4+lineheight)
love.graphics.line(0, height/4+lineheight-20, width, height/4+lineheight-20)
love.graphics.setColor(0,255,0,100)
love.graphics.rectangle('fill', 0, height/4+lineheight-20, width, 20)
love.graphics.setColor(0, 0, 0)
love.graphics.print("Press Enter to continue", width*.75-165, height/4+lineheight-6)
if hud.msgFadeintime ==0 and hud.msgFadeouttime ==0 then
love.graphics.drawf(hud.messagebox, width/4, height*3/8-20, width/2, 'center')
end
--NOTE: I don't get the feeling love.align_center works as it should
end
if hud.speechbox then
love.graphics.setColor(255, 255, 255)
local lineheight = (hud.msgFadeouttime > 0 and hud.msgFadeouttime/hud.msgFadetimemax or (hud.msgFadeintime > 0 and 1-hud.msgFadeintime/hud.msgFadetimemax or 1)) * 100
love.graphics.rectangle('fill', 20, height-lineheight-20, width-40, lineheight)
love.graphics.setColor(0,255,0)
love.graphics.setLineWidth(2)
love.graphics.line(20, height-lineheight-20, width-20, height-lineheight-20)
love.graphics.line(20, height-lineheight, width-20, height-lineheight)
love.graphics.line(20, height-lineheight-20, 20, height-20)
love.graphics.line(width-20, height-lineheight-20, width-20, height-20)
love.graphics.line(20, height-20, width-20, height-20)
love.graphics.setColor(0,255,0,100)
love.graphics.rectangle('fill', 20, height-lineheight-20, width-40, 20)
love.graphics.setColor(0, 0, 0)
if hud.msgFadeintime ==0 and hud.msgFadeouttime ==0 then
love.graphics.print(hud.speaker, 24, height-lineheight-6)
love.graphics.print(hud.speechbox, 28, height-lineheight+20)
love.graphics.print("Press Enter to continue", width-msgwidth-30, height-30)
end
end
--restore settings, we don't want to impact any other drawing
love.graphics.setColor(unpack(col))
console:draw()
setCamera(cameras.default)
end
function hud.messageBox(text, cb, userd) --create a message box
--store the callbacks, if necessary (happens once)
if not oldUpdate then oldUpdate = love.update end
if not oldKeypressed then oldKeypressed = love.keypressed end
if not oldJoystickpressed then oldJoystickpressed = love.joystickpressed end
--load the new ones
love.update = msgUpdate
love.keypressed = msgKeypressed
love.joystickpressed = msgJoystickpressed
--set the text
hud.messagebox = text
hud.msgFadeintime = hud.msgFadetimemax
hud.cb = cb
hud.userd = userd
end
function hud.speechBox(speaker, text, cb, userd) --create a message box
--store the callbacks, if necessary (happens once)
if not oldUpdate then oldUpdate = love.update end
if not oldKeypressed then oldKeypressed = love.keypressed end
if not oldJoystickpressed then oldJoystickpressed = love.joystickpressed end
--load the new ones
love.update = msgUpdate
love.keypressed = msgKeypressed
love.joystickpressed = msgJoystickpressed
--set the text
hud.speechbox = text
hud.speaker = speaker
hud.msgFadeintime = hud.msgFadetimemax
hud.cb = cb
hud.userd = userd
if not msgwidth then
msgwidth = love.graphics.getFont():getWidth("Press Enter to continue")
end
end
--Testing function:
function sB()
hud.speechBox("rude", "OBEY!")
end