-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRGDCDemo.p8
306 lines (263 loc) · 11.6 KB
/
RGDCDemo.p8
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
pico-8 cartridge // http://www.pico-8.com
version 27
__lua__
--rgdc demo
--code heavily references
--tutorial from ⬇️⬇️⬇️
--https://nerdyteachers.com/explain/platformer/
--variables--------------------
function _init()
--this is a table
--it's kind of like a class!
--player holds an array (table)
--of variables relevant
--to itself
player={
sp=1, //start sprite
x=16, //position
y=48,
w=8, //sprite size
h=8,
flp=false, //flip
dx=0,
dy=0,
max_dx=2,
max_dy=3,
acc=0.5,
boost=3,
anim=0,
running=false,
jumping=false,
falling=false,
sliding=false,
landed=false
}
gravity=0.3
friction=0.85
--simple camera
cam_x=0
--map limits
map_start=0
map_end=1024
end
-->8
--update and draw--------------
function _update()
-- print(falling)
player_update()
player_animate()
spring()
--simple camera
cam_x=player.x-64+(player.w/2)
if cam_x<map_start then
cam_x=map_start
end
if cam_x>map_end-128 then
cam_x=map_end-128
end
camera(cam_x,0)
end
function _draw()
cls(7) --clears screen
map(0,0) --redraws map
--draws sprite
spr(player.sp,player.x,
player.y,1,1,player.flp)
end
-->8
--collisions-------------------
function collide_map(obj,aim,
flag)
--obj = table needs x,y,w,h
--aim = left,right,up,down
local x=obj.x local y=obj.y
local w=obj.w local h=obj.h
local x1=0 local y1=0
local x2=0 local y2=0
if aim=="left" then
x1=x-1 y1=y
x2=x y2=y+h-1
elseif aim=="right" then
x1=x+w-1 y1=y
x2=x+w y2=y+h-1
elseif aim=="up" then
x1=x+2 y1=y-1
x2=x+w-3 y2=y
elseif aim=="down" then
x1=x+2 y1=y+h
x2=x+w-3 y2=y+h
end
--pixels to tiles
x1/=8 y1/=8
x2/=8 y2/=8
if fget(mget(x1,y1), flag)
or fget(mget(x1,y2), flag)
or fget(mget(x2,y1), flag)
or fget(mget(x2,y2), flag) then
return true
else
return false
end
end
-->8
--player----------------------
function player_update()
--physics
player.dy+=gravity
player.dx*=friction
--controls
if btn(⬅️) then
player.dx-=player.acc
player.running=true
player.flp=true
end
if btn(➡️) then
player.dx+=player.acc
player.running=true
player.flp=false
end
--slide
if player.running
and not btn(⬅️)
and not btn(➡️)
and not player.falling
and not player.jumping then
player.running=false
player.sliding=true
end
--jump
if btnp(❎)
and player.landed then
player.dy-=player.boost
player.landed=false
end
--check collision up and down
if player.dy>0 then
player.falling=true
player.landed=false
player.jumping=false
player.dy=limit_speed(player.dy,player.max_dy)
if collide_map(player,"down",0) then
player.landed=true
player.falling=false
player.dy=0
player.y-=((player.y+player.h+1)%8)-1
end
elseif player.dy<0 then
player.jumping=true
if collide_map(player,"up",1) then
player.dy=0
end
end
--check collision left and right
if player.dx<0 then
player.dx=limit_speed(player.dx,player.max_dx)
if collide_map(player,"left",0) then
player.dx=0
end
elseif player.dx>0 then
player.dx=limit_speed(player.dx,player.max_dx)
if collide_map(player,"right",0) then
player.dx=0
end
end
--stop sliding
if player.sliding then
if abs(player.dx)<.2
or player.running then
player.dx=0
player.sliding=false
end
end
player.x+=player.dx
player.y+=player.dy
--limit player to map
if player.x<map_start then
player.x=map_start
end
if player.x>map_end-player.w then
player.x=map_end-player.w
end
end
function player_animate()
if player.jumping then
player.sp=2
elseif player.falling then
player.sp=4
-- elseif player.sliding then
-- player.sp=9
elseif player.running then
if time()-player.anim>.1 then
player.anim=time()
player.sp+=1
if player.sp>4 then
player.sp=1
end
end
else --player idle
player.sp=1
end
end
function limit_speed(num,maximum)
return mid(-maximum,num,maximum)
end
-->8
--we springing
function spring()
--foreach(s in springs)
if collide_map(player,"down",2) then
player.dy-=player.boost+1
player.landed=false
end
--end
end
__gfx__
0000000000000000004444400000000000444440b0bb0bbb000000000b0000000000000000000000000000000000000000000000000000000000000000000000
00000000004444400444ff40004444400444ff40b00b000b00000000b0b000000000000000000000000000000000000000000000000000000000000000000000
007007000444ff40044cffc00444ff40044cffc0b0b000b000000000b0b000000000000000000000000000000000000000000000000000000000000000000000
00077000044cffc004efffe0044cffc004efffe0b0bb00b0000000000b0000000000000000000000000000000000000000000000000000000000000000000000
0007700004efffe00444244004efffe0044424400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00700700044424400022210004442440002120000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000002220000010000000222000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000001010000000000000101000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000bbbbbbbb8888888800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000bbbbbbbb8888888800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000bbbbbbbb000dd00088888888000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000bfbbfbbb00dddd0088888888000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000bfffffbf0550055000dddd00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000ff4fffff0550055055000055000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000f4ffffff00dddd000dd00dd0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000ffffff4fffffffffffffffff000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
000000004ffff4ff0000001111000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
000000004fffffff0000001111000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000ff4fffff0000006666000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000ff4fff4f0000004666000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000f4fff4ff0000004444000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000fffffff40000444444440000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000f4fffff40000444444440000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000ff4fffff0000111111110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000bb0000111111110000000110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000bb0000111cc7110000000660000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000bb0000111c78110000000440000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000bb0000111788110000001111000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000bb0000111111110000001c71000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000bb0000444444440000001781000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000bb0000444444440000001111000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000bb0000044554400000004554000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
__gff__
0000000000000000000000000000000000010501000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
__map__
0000000000000000000000000028000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000031111111111111110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000031212121212121210000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000031212121212121210000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
000a000011000031212121212121210000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
1111111111111211212121212121210000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
2121212121212121212121212121210000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
__sfx__
000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
001000002300024000110001700013000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
001000000000017000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000