pull out calc_velocity
also prepare constraints to change color based on whether it changed the requested thrust
This commit is contained in:
		| @@ -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 | ||||
|   | ||||
		Reference in New Issue
	
	Block a user