-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.lua
More file actions
177 lines (143 loc) · 3.76 KB
/
Copy pathmain.lua
File metadata and controls
177 lines (143 loc) · 3.76 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
RNG = love.math.newRandomGenerator(love.timer.getTime())
local Signal = require 'modules.hump.signal'
Timer = require 'modules.hump.timer'
GameState = require "modules.hump.gamestate"
Const = require 'src.const'
Joysticks = require 'src.joysticks'
Keyboard = require 'src.keyboard'
Round = require 'src.round'
World = require 'src.classes.world'
MAX_ROUNDS = 5
local roundNum
local round
local menuWorld
local menu = {}
local game = {}
local pause = {}
local over = {}
local wins = {}
function drawCenteredTextAtHeight(msg, y, dontSetColor)
love.graphics.push()
if dontSetColor == nil then
love.graphics.setColor(255, 255, 255)
end
love.graphics.printf(msg, 0, y, love.graphics.getWidth(), 'center')
love.graphics.pop()
end
function fadeScreen()
love.graphics.push()
love.graphics.setColor(0, 0, 0, 200)
love.graphics.rectangle('fill', 0, 0, love.graphics.getWidth(), love.graphics.getHeight())
love.graphics.pop()
end
function love.load()
roundNum = 1
Signal.register('new_round', function(player)
roundNum = roundNum + 1
local maxPlayer = nil
local maxWins = -1
if wins[player.id] then
wins[player.id] = wins[player.id] + 1
else
wins[player.id] = 1
end
if wins[player.id] > maxWins then
maxWins = wins[player.id]
maxPlayer = player.id
end
if wins[player.id] == 3 then
GameState.switch(over)
else
if maxPlayer then
round = Round(roundNum, maxPlayer)
else
round = Round(roundNum)
end
end
end)
Joysticks.init()
GameState.registerEvents()
GameState.switch(menu)
round = Round(roundNum)
Const.fonts.titleFont:setFilter('nearest', 'nearest')
end
function menu:enter(from)
self.from = from;
menuWorld = World(true)
end
function menu:update(dt)
menuWorld:update(dt)
end
function menu:keypressed(key,code)
if key == 'return' then
GameState.switch(game)
end
end
function menu:draw()
menuWorld:draw()
fadeScreen()
love.graphics.setFont(Const.fonts.titleFont)
drawCenteredTextAtHeight("Press Enter to Start", love.graphics.getHeight() / 2)
end
function game:enter(from)
wins = {}
self.from = from
end
function game:update(dt)
round:update(dt)
end
function game:keypressed(key, code)
if key == 'escape' then
GameState.switch(pause)
elseif key == 'r' then
Signal.emit('new_round')
end
end
function game:draw()
round:draw()
end
function pause:enter(from)
self.from = from;
end
function pause:keypressed(key, code)
if key == 'escape' then
GameState.switch(game)
elseif key == 'q' then
love.event.quit()
end
end
function pause:update(dt)
end
function pause:draw()
round:draw()
fadeScreen()
love.graphics.setFont(Const.fonts.titleFont)
drawCenteredTextAtHeight("Paused", 10);
drawCenteredTextAtHeight("Press ESCAPE to RESUME", love.graphics.getHeight() / 2 - 25);
drawCenteredTextAtHeight("Press Q to QUIT", love.graphics.getHeight()/2 + 25);
end
function over:enter(from)
self.from = from
end
function over:keypressed(key, code)
if key == 'return' then
roundNum = 1
round = Round(roundNum)
GameState.switch(game)
end
end
function over:update(dt)
end
function over:draw()
local winner
for id, v in pairs(wins) do
if v == 3 then
winner = id
break
end
end
drawCenteredTextAtHeight('Game Over', love.graphics.getHeight() / 2 - 40)
local r, g, b, a = Const.colors[winner]()
love.graphics.setColor(r, g, b, a)
drawCenteredTextAtHeight('Player ' .. winner .. ' wins!', love.graphics.getHeight() / 2, false)
end