Repository navigation
Expand file tree
/
Copy pathunit.lua
More file actions
255 lines (216 loc) · 3.92 KB
/
Copy pathunit.lua
File metadata and controls
255 lines (216 loc) · 3.92 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
-- Unit
function create_unit(x,y,col)
local function handle_input(u)
local s=u.state:handle_input(u)
if s then
u.state=s
s:enter(u)
end
end
local function update(u)
if (u.item) u.item:update(u)
local s=u.state:update(u)
if s then
u.state=s
s:enter(u)
end
end
local function draw(u)
local x,y=scrn_xy(u.x,u.y,u.z)
local sx,sy=scrn_xy(u.x,u.y,0)
pal(13, u.col)
ovalfill(sx-2,sy-1,sx+2,sy+1,5)
spr(u.f,
x-4,y-8,
1,1, u.dx==-1
)
pal()
-- draw hp
if menu.open then
local hp=tostr(u.hp)
print(hp,x-#hp*2,y-14,7)
end
end
local u={
x=x,y=y,z=8,
vx=0,vy=0,vz=0,
dx=1,dy=1,
col=col, -- color
f=1, -- frame
hp=100,
death_radius=2,
death_dmg=25,
idle_timer=0,
idle=true,
state=create_standing_state(),
item=nil,
inventory={
{item=nil},
{item=create_bazooka()},
{item=create_hand_grenade(), ammo=3},
{item=create_skip()}
},
fired=false,
handle_input=handle_input,
update=update,
draw=draw,
take_damage=take_damage,
kill=kill,
consume_ammo=consume_ammo
}
return u
end
function update_unit_physics(u)
move(u)
-- bouncing back
if (u.z<=0 and u.vz<-0.1) then
u.vz*=-0.25
end
-- gravity
if (u.z>0) u.vz+=gravity/fps
-- snap to ground
if u.z<0.0125 then
u.z=0
if (u.vz<0.1) u.vz=0
end
-- friction (lower for more)
local f=.2
if (u.z==0) u.vx*=f u.vy*=f
-- check collisions
if (u.x<0) u.x=0
if (u.y<0) u.y=0
if (u.x>map_size) u.x=map_size
if (u.y>map_size) u.y=map_size
-- reset vel when close to 0
if (abs(u.vx)<=0.0001) u.vx=0
if (abs(u.vy)<=0.0001) u.vy=0
if (abs(u.vz)<=0.0001) u.vz=0
if not is_moving(u) then
u.idle_timer+=1/fps
else
u.idle_timer=0
u.idle=false
end
if u.idle_timer>=0.1 then
u.idle=true
end
end
-- standing state
function create_standing_state()
return {
enter=function(s,u)
u.f=1
end,
handle_input=function(s,u)
ac=3 -- acceleration
local dx=0
local dy=0
if (btn(⬅️)) dx-=1 u.dx=-1
if (btn(➡️)) dx+=1 u.dx= 1
if (btn(⬆️)) dy-=1 u.dy=-1
if (btn(⬇️)) dy+=1 u.dy= 1
-- normalize dir
local l=sqrt(dx*dx+dy*dy)
if (l>0) dx/=l dy/=l
u.vx+=dx*ac u.vy+=dy*ac
-- update sprite frame
u.f=1
if (u.dy==-1) u.f=2
end,
update=function(s,u)
update_unit_physics(u)
if u.z>0.0125 then
return create_flying_state()
end
if u.hp<=0 and u.idle then
return create_dying_state()
end
end
}
end
-- flying state
function create_flying_state()
return {
grounded_time=0,
enter=function(s,u)
u.f=5
end,
handle_input=noop,
update=function(s,u)
update_unit_physics(u)
if u.z<=0.0125 then
s.grounded_time+=1/fps
end
if s.grounded_time>=0.2 then
return create_standing_state()
end
end
}
end
-- dying state
function create_dying_state()
return {
anim_t=0,
anim_dur=0.25,
anim_start=17,
anim_frames=2,
expld_t=0,
expld_delay=0.5,
enter=function(s,u)
u.f=s.anim_start
u.idle=false
end,
handle_input=noop,
update=function(s,u)
if s.anim_t<s.anim_dur then
s.anim_t+=1/fps
elseif s.expld_t<s.expld_delay then
s.expld_t+=1/fps
else
create_explosion(
u,u.death_radius,u.death_dmg
)
return create_dead_state()
end
u.f=s.anim_start+s.anim_t/s.anim_dur*s.anim_frames
end
}
end
-- dead state
function create_dead_state()
return {
enter=function(s,u)
u.f=20
u.dead=true
u.idle=true
end,
handle_input=noop,
update=noop
}
end
function consume_ammo(u, id)
for i in all(u.inventory) do
if i and i.k==id and i.ammo then
i.ammo-=1
if i.ammo<=0 then
del(u.inventory, i)
end
end
end
end
function take_damage(u,amount)
main_camera.target=u
u.hp=max(0,u.hp-amount)
if (u.hp<=0) u:kill()
end
function kill(u)
for t in all(game.teams) do
local deleted=del(t.units, u)
-- adjust active unit index
if deleted and
t.unit_id>#t.units
then
t.unit_id=1
end
end
end