-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.lua
317 lines (274 loc) · 8.34 KB
/
player.lua
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
307
308
309
310
311
312
313
314
315
316
317
local point = {}
point.__index = point
function point.new(x, y)
return setmetatable({ x = x, y = y }, point)
end
function point:__add(p)
return point.new(self.x + p.x, self.y + p.y)
end
function point:__sub(p)
return point.new(self.x - p.x, self.y - p.y)
end
function point:__tostring()
return "{" .. self.x .. ", " .. self.y .. "}"
end
function point:length()
return sqrt(abs(self.x * self.x + self.y * self.y))
end
local vector = {}
vector.__index = vector
function vector.new(origin, magnitude)
return setmetatable({ point = origin, magnitude = magnitude }, vector)
end
function vector:__tostring()
return tostr(self.point) .. "->" .. tostr(self.magnitude)
end
function vector:angle()
return atan2(self.magnitude.x, self.magnitude.y)
end
local player = {}
player.__index = player
function player.new(x, y)
return setmetatable(
{
x = x,
y = y,
dx = 0,
dy = 0,
a_go = 0.25,
a_stop = 0.25,
xv_max = 2,
yv_max = 5,
height = 4,
width = 4,
run_multiple = 1.5,
freefall = false,
h_flip = false
},
player
)
end
function player:tl()
return point.new(self.x, self.y)
end
function player:tr()
return point.new(self.x + self.width, self.y)
end
function player:bl()
return point.new(self.x, self.y + self.height)
end
function player:br()
return point.new(self.x + self.width, self.y + self.height)
end
function truncate(value, bits)
return shl(flr(shr(value, bits)), bits)
end
function movequad(v, size)
-- rect(v.point.x, v.point.y, v.point.x + size.x, v.point.y + size.y, time() * 60)
local angle = v:angle()
local best_hit = vector.new(v.point + v.magnitude, point.new(0, 0))
local best_impulse = point.new(0, 0)
local best_distance = v.magnitude:length()
-- printh("dir:" .. tostr(v.magnitude) .. "|" .. v.magnitude:length())
--Check Horizontal Lines
if angle ~= 0.0 and angle ~= 0.5 then
local atan = -1 / (sin(angle) / cos(angle))
local ray
if angle < 0.5 then
-- up
local ry = truncate(v.point.y, 3) - 1
line(0, ry, 128, ry, time() * 15)
local yo = -8
ray = vector.new(
point.new((v.point.y - ry) * atan + v.point.x, ry),
point.new(-yo * atan, yo)
)
elseif angle > 0.5 then
-- down
local ry = truncate(v.point.y + size.y, 3) + 8
local yo = 8
ray = vector.new(
point.new((v.point.y + size.y - ry) * atan + v.point.x, ry),
point.new(-yo * atan, yo)
)
end
local d = (ray.point - v.point):length()
local hit = castbeam(ray, point.new(size.x, 0), d, v.magnitude:length())
if hit ~= nil then
if v.magnitude.y >= 0 then
hit.y -= size.y
end
local hit_direction = hit - v.point
rectfill(hit.x, hit.y, hit.x + size.x, hit.y + size.y, 8)
local hit_v = vector.new(
hit,
point.new(v.magnitude.x - hit_direction.x, 0)
)
-- printh("hdir:" .. tostr(hit_direction) .. "|" .. tostr(hit_direction:length(), 1))
-- local hit_distance = hit_direction:length()
if hit_direction:length() <= best_distance then
best_hit = hit_v
best_impulse = v.magnitude - hit_v.magnitude
-- printh("impulse: " .. tostr(best_impulse))
best_distance = hit_direction:length()
end
end
end
--Check Vertical Lines
if angle ~= 0.25 and angle ~= 0.75 then
local ntan = -sin(angle) / cos(angle)
local ray
if 0.25 < angle and angle < 0.75 then
-- left
local rx = truncate(v.point.x, 3) - 1
local xo = -8
ray = vector.new(
point.new(rx, (v.point.x - rx) * ntan + v.point.y),
point.new(xo, -xo * ntan)
)
elseif angle < 0.25 or angle > 0.75 then
-- right
local rx = truncate(v.point.x + size.x, 3) + 8
line(rx, 0, rx, 128, time() * 15)
local xo = 8
ray = vector.new(
point.new(rx, (v.point.x + size.x - rx) * ntan + v.point.y),
point.new(xo, -xo * ntan)
)
end
local d = (ray.point - v.point):length()
local hit = castbeam(ray, point.new(0, size.y), d, v.magnitude:length())
if hit ~= nil then
if v.magnitude.x >= 0 then
hit.x -= size.x
end
local hit_direction = hit - v.point
rectfill(hit.x, hit.y, hit.x + size.x, hit.y + size.y, 9)
local hit_v = vector.new(
hit,
point.new(0, v.magnitude.y - hit_direction.y)
)
-- printh("vdir:" .. tostr(hit_direction) .. "|" .. tostr(hit_direction:length(), 1))
-- local hit_distance = hit_direction:length()
if hit_direction:length() <= best_distance then
best_hit = hit_v
best_impulse = v.magnitude - hit_v.magnitude
-- printh("impulse: " .. tostr(best_impulse))
best_distance = hit_direction:length()
end
end
end
return best_hit, best_impulse
end
function notsolid(p)
return not fget(mget(flr(shr(p.x, 3)), flr(shr(p.y, 3))), tile_flag.solid)
end
function castbeam(v, size, d, max_d)
local hit
for i = 0, max_d, v.magnitude:length() do
if notsolid(v.point) and notsolid(v.point + size) then
v.point += v.magnitude
line(v.point.x, v.point.y, v.point.x + size.x, v.point.y + size.y, 6)
else
-- printh("beam hit:" .. tostr(v.point))
-- Do wall backoff here
v.point.x -= v.magnitude.x / 8
v.point.y -= v.magnitude.y / 8
-- printh("beam backoff:" .. tostr(v.point))
line(v.point.x, v.point.y, v.point.x + size.x, v.point.y + size.y, 11)
return v.point
end
end
end
errorshown = false
function player:update()
if self.x < 64 then
if not errorshown then
printh("OOB at " .. tostr(self.x, 1) .. "," .. tostr(self.y, 1))
errorshown = true
end
return
end
printh("*** player:update() ***")
-- self.dx = sin(time() / 30) * 8
-- self.width = (sin(time()) + 1) * 4
-- self.height = (sin(time() + 0.25) + 1) * 4
local function dir_input(bid_minus, bid_plus, v, a, f)
if btn(bid_minus) then
v -= a
elseif v < 0 then
v = min(v + f, 0)
end
if btn(bid_plus) then
v += a
elseif v > 0 then
v = max(v - f, 0)
end
return v
end
self.dx = dir_input(0, 1, self.dx, self.a_go, self.a_stop)
if btn(2) then
if not notsolid(point.new(self.x, self.y + self.height + 1)) or not notsolid(point.new(self.x + self.width, self.y + self.height + 1)) then
self.dy = -16
end
end
-- self.dy = dir_input(2, 3, self.dy, self.a_go, 0)
-- gravity
self.dy += 0.5
self.dx = mid(-self.xv_max, self.dx, self.xv_max)
self.dy = mid(-self.yv_max, self.dy, self.yv_max)
-- self.dx = dir_input(1, self.dx, self.a_go, -self.a_stop)
-- self.dy = dir_input(2, self.dy, -self.a_go, self.a_stop)
-- self.dy = dir_input(3, self.dy, self.a_go, -self.a_stop)
-- x, y, dx, dy = raycast(table.unpack(self:tl()), self.dx, self.dy)
-- x, y, dx, dy = raycast(self.x, self.y, self.dx, self.dy)
-- x, y, dx, dy = raycast(self.x, self.y, self.dx, self.dy)
-- x, y, dx, dy = raycast(self.x, self.y, self.dx, self.dy)
-- for i = 1, 50 do
-- local corners = { self:tl(), self:tr(), self:bl(), self:br() }
local p = point.new(self.x, self.y)
local v = point.new(self.dx, self.dy)
line(p.x, p.y, (p + v).x, (p + v).y, time() * 12.1)
line(p.x, p.y + self.height, (p + v).x, (p + v).y + self.height, time() * 11.9)
line(p.x + self.width, p.y, (p + v).x + self.width, (p + v).y, time() * 12)
line(p.x + self.width, p.y + self.height, (p + v).x + self.width, (p + v).y + self.height, time() * 12.2)
if v:length() > 0.001 then
local physics_vector = vector.new(p, v)
local size = point.new(self.width, self.height)
local impulse
repeat
printh("x:" .. physics_vector.point.x .. " y:" .. physics_vector.point.x)
physics_vector, impulse = movequad(physics_vector, size)
rect(physics_vector.point.x, physics_vector.point.y, physics_vector.point.x + self.width, physics_vector.point.y + self.height, time() * 60)
printh(physics_vector.magnitude)
printh(physics_vector.point)
-- break
until physics_vector.magnitude:length() < 0.25
self.x = flr(physics_vector.point.x + 0)
self.y = flr(physics_vector.point.y + 0)
if self.x < 64 then
printh("vector" .. tostr(physics_vector.point) .. " after flr()" .. flr(physics_vector.point.x))
end
self.dx -= flr(impulse.x)
self.dy -= flr(impulse.y)
else
self.dx = 0
self.dy = 0
self.x = flr(self.x)
self.y = flr(self.y)
end
end
function player:draw()
-- spr(2, self.x, self.y, 1, 1, self.h_flip)
-- circ(self.x + self.dx, self.y + self.dy, 2, 14)
end
-- INFO: *** player:update() ***
-- INFO: x:64 y:64
-- INFO: dir:{-0.25, 5}|5.0062
-- INFO: beam hit:{63.7505, 119}
-- INFO: hdir:{-0.2495, 5}|0x0005.0197
-- INFO: impulse: {-0.2495, 5}
-- INFO: {-0.0005, 0}
-- INFO: {63.7505, 114}
-- INFO: vector{63.7505, 114} after flr()63
-- INFO: OOB at 0x003f.0000,0x0072.0000