-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjump.p8
244 lines (220 loc) · 10.1 KB
/
jump.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
pico-8 cartridge // http://www.pico-8.com
version 38
__lua__
p={}
ground=119
debug={}
function _init()
setPosition(10, 10)
p.vx = 0
p.vy = 0
p.w = 3
p.h = 7
p.leftoverDX = 0
p.leftoverDY = 0
p.momentum = 0
setState(0) -- 0: ground, 1: inair
poke(0X5F5C, 255) -- never repeat inputs
end
function _update()
updatePlayerInputs()
movePlayer()
if p.momentum > 0 and getTimeSinceMove() > 1.0 then
p.momentum = 0
end
end
function getSpeed()
if (p.momentum == 1) return 1.3
if (p.momentum == 2) return 1.5
if (p.momentum == 3) return 1.7
if (p.momentum == 4) return 2.1
return 1
end
function incMomentum()
p.momentum = min(p.momentum+1, 4)
end
function updatePlayerInputs()
local dv = getSpeed()
if (btn(0)) p.vx -= dv
if (btn(1)) p.vx += dv
if (btn(4)) updateJumpInput()
end
function setPosition(x, y)
if x ~= p.x or y ~= p.y then
p.x = x
p.y = y
p.lastMoveTime = time()
end
end
function getTimeSinceMove()
return time() - p.lastMoveTime
end
function setState(newState)
if newState ~= p.state then
p.state = newState
p.stateStartTime = time()
end
end
function getStateDuration()
return time() - p.stateStartTime
end
function canJump()
return p.state == 0 or (p.state == 1 and getStateDuration() < 0.1)
end
function updateJumpInput()
if canJump() and btnp(4) then
p.vy -= 5
setState(1)
incMomentum()
else
if p.state == 1 and btn(4) and getStateDuration() < 0.3 then
p.vy -= 0.6
end
end
end
function remap_cx(cx)
return cx % 64 -- infinite 64 wide map
end
function intersect(x,y,w,h,flag)
for i=x,x+w do
local cx = remap_cx(i \ 8)
for j=y,y+h do
local cy = j \ 8
local tile = mget(cx, cy)
if fget(tile, flag) then
return true
end
end
end
return false
end
function largerWithDir(a, b, dir)
if dir < 0 then
if (a < b) return a else return b
else
if (a > b) return a else return b
end
end
function move(requestedDX, requestedDY)
local fullDX,fullDY = requestedDX + p.leftoverDX, requestedDY + p.leftoverDY
local dx, dy = flr(fullDX), flr(fullDY)
local safeX, safeY = p.x, p.y
local dirX, dirY = dx / abs(dx), dy / abs(dy)
local collisionReport = {false, false}
for deltaX=0,abs(dx) do
for deltaY=0,abs(dy) do
if not collisionReport[1] then
local x, y = largerWithDir(p.x + dirX*deltaX, safeX, dirX), safeY
local isColliding = intersect(x, y, p.w, p.h, 0)
if isColliding then
collisionReport[1] = true
else
safeX = x
end
end
if not collisionReport[2] then
x, y = safeX, largerWithDir(p.y + dirY*deltaY, safeY, dirY)
isColliding = intersect(x, y, p.w, p.h, 0)
if isColliding then
collisionReport[2] = true
else
safeY = y
end
end
end
end
setPosition(safeX, safeY)
p.leftoverDX = fullDX - dx
p.leftoverDY = fullDY - dy
return collisionReport
end
function detectGround()
return intersect(p.x, p.y+p.h+1, p.w, 0, 0)
end
function movePlayer()
local report = move(p.vx, p.vy)
local horizontalCollision, verticalCollision = report[1], report[2]
if horizontalCollision then
p.vx = 0
p.momentum = 0
end
if verticalCollision then
p.vy = 0
end
local onGround = detectGround()
if onGround then
if p.vy >= 0 then
p.vy = 0
setState(0)
end
else
setState(1)
p.vy += 1
end
p.vx = 0
end
function _draw()
cls(0)
local camX, camY = p.x-32, 0
camera(camX, camY)
drawWorld(camX, camY)
drawPlayer()
camera()
print("p: {"..p.x..","..p.y.."} v: {"..p.vx..","..p.vy.."} idle: "..getTimeSinceMove())
print("m: "..p.momentum.." s: "..p.state.." ("..getStateDuration()..")")
end
function drawPlayer()
local x0 = p.x
local x1 = x0 + p.w
local y0 = p.y
local y1 = y0 + p.h
local c = 11
if (p.state == 1) c = 14
rectfill(x0,y0,x1,y1,c)
end
function drawWorld(camX, camY)
-- camera()
-- local minCX, maxCX = (camX-64) \ 8, (camX+64) \ 8
-- if minCX \ 64 ~= maxCX \ 64 then
-- map(remap_cx(minCX),0,0,0,64,32)
-- map(remap_cx(maxCX-64),0,0,0,64,32)
-- else
-- map(remap_cx(minCX),0,0,0,64,32)
-- end
-- camera(camX, camY)
-- map(0,0,-64*8,0,64,32)
map(0,0,0,0,64,32)
-- map(0,0,64*8,0,64,32)
-- for x=0,128 do
-- tline(0,x,128,x,0,x\8)
-- end
end
__gfx__
00000000cccccccc0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000cccccccc0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00700700cccccccc0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00077000cccccccc0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00077000cccccccc0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00700700cccccccc0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000cccccccc0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000cccccccc0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
__gff__
0001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
__map__
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000001010000000000000000000000000000000000000000000000000000000000000000000000000001010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000010100000000000000000000000000000000000000000000000000000001010000000001010000000001010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000001010000000001010000000000000000000001010000000000000000000001010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000010100000000000000000001010000000000000000000001010000000000000000000001010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000010100000000000000010100000000000000000001010000000000000000000001010000000000000000000001010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000