pull out calc_velocity

also prepare constraints to change color based on whether it changed the requested thrust
This commit is contained in:
Kistaro Windrider 2024-01-14 19:33:57 -08:00
parent aa25f87c46
commit d0a17488d0
Signed by: kistaro
SSH Key Fingerprint: SHA256:TBE2ynfmJqsAf0CP6gsflA0q5X5wD5fVKWPsZ7eVUg8

View File

@ -128,15 +128,20 @@ function ship:draw()
end
end
function calc_velocity(v0, t, vmax, drag)
local v1 = v0 + t
local sg = sgn(v1)
v1 -= sg*drag
if (sgn(v1) != sg) return 0
if (abs(v1) > vmax) return sg*vmax
return v1
end
function ship:update()
local t = btn(0) and -1 or btn(1) and 1 or 0
t *= self.thrust
t = constraints:constrain(self, t)
local s,lim=self.dx+t,self.maxspd
local sg = sgn(s)
s -= sg*self.drag
if (sgn(s) != sg) s=0
if (abs(s)>lim) s=sg*lim
local s = calc_velocity(self.dx, t, self.maxspd, self.drag)
self.x += s
self.dx = s
@ -153,9 +158,11 @@ end
constraints = {
ymin=20,
ymax=52,
color=10
}
function constraints:constrain(s, want)
self.color=10
return want
end
@ -185,7 +192,7 @@ end
function constraints:draw()
if (not self.xmin) return
rect(self.xmin, self.ymin, self.xmax, self.ymax, 10)
rect(self.xmin, self.ymin, self.xmax, self.ymax, self.color)
end
-->8