Repository navigation
Expand file tree
/
Copy pathgame.lua
More file actions
206 lines (173 loc) · 3 KB
/
Copy pathgame.lua
File metadata and controls
206 lines (173 loc) · 3 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
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
-- Game State
game={
state=nil,
timer=0,
over=false,
winner=nil,
team_id=1,
teams={
{
name="blue",
col=12,
units={},
unit_id=1
},
{
name="red",
col=8,
units={},
unit_id=1
}
}
}
function game:handle_input()
local s=self.state:handle_input(self)
if s then
self.state=s
s:enter(self)
end
end
function game:update()
local s=self.state:update(self)
if s then
self.state=s
s:enter(self)
end
end
function game:init()
self.state=create_spawn_state()
self.state:enter(self)
end
function game:end_turn()
self.state=create_post_turn_state()
self.state:enter(self)
end
-- pre game
function create_spawn_state()
local function enter(s,g)
g.timer=pregame_time
end
local function handle_input(s,g)
if btnp(❎) then
return create_player_turn_state(true)
end
end
local function update(s,g)
g.timer-=1/fps
if g.timer<=0 then
return create_player_turn_state(true)
end
end
return {
name="spawning",
enter=enter,
handle_input=handle_input,
update=update
}
end
-- player turn
function create_player_turn_state(first)
local function enter(s,g)
if not first then
-- select next unit
local t=g.teams[g.team_id]
t.unit_id+=1
if (t.unit_id>#t.units) t.unit_id=1
-- select next team
g.team_id+=1
if (g.team_id>#g.teams) g.team_id=1
end
g.timer=turn_time
local t=g.teams[g.team_id]
local u=t.units[t.unit_id]
g.unit=u
main_camera.target=g.unit
end
local function handle_input(s,g)
if not menu.open then
if g.unit.item then
g.unit.item:handle_input(g.unit)
else
g.unit:handle_input()
end
end
if not g.unit.item
or not g.unit.item.fired
then
menu:handle_input(g.unit)
end
end
local function update(s,g)
g.timer-=1/fps
if g.timer<=0 then
return create_post_turn_state()
end
end
return {
name="player_turn",
enter=enter,
handle_input=handle_input,
update=update
}
end
-- post turn
function create_post_turn_state()
local function enter(s,g)
menu.index=1
menu.open=false
g.unit.item=nil
g.timer=postturn_time
end
local function update(s,g)
-- wait for units to be stable
local stable=true
for e in all(ents) do
if e.idle==false then
stable=false
end
end
if stable then
g.timer-=1/fps
end
if g.timer<=0 then
local gameover,winner=check_victory()
if not gameover then
return create_player_turn_state()
else
game.over=true
game.winner=winner
return create_end_game_state()
end
end
end
return {
name="post_turn",
enter=enter,
handle_input=noop,
update=update
}
end
-- post game
function create_end_game_state()
return {
name="end_game",
enter=noop,
handle_input=noop,
update=noop
}
end
function check_victory()
local c=0 -- # of teams with units alive
local tid=nil -- team id
for k,t in pairs(game.teams) do
if (#t.units>0) then
c+=1
tid=k
end
end
local gameover=false
local winner=nil
if (c<=1) gameover=true
if (c==1) winner=tid
return gameover,winner
end