dummy.lua 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. local PlayerDummy = extend(app.player.base)
  2. local function drawTick() return tick - (interp / tickRate) end
  3. function PlayerDummy:activate()
  4. self.history = {}
  5. app.player.base.activate(self)
  6. end
  7. function PlayerDummy:update()
  8. if self.ded then
  9. return Player.update(self)
  10. end
  11. self:slot()
  12. app.player.base.update(self)
  13. end
  14. function PlayerDummy:get(t)
  15. if t == tick then return self end
  16. if #self.history < 2 then
  17. return setmetatable({
  18. x = self.x,
  19. y = self.y,
  20. z = self.z,
  21. angle = self.angle,
  22. tick = tick,
  23. }, self.meta)
  24. end
  25. while self.history[1].tick < tick - 1 / tickRate and #self.history > 2 do
  26. table.remove(self.history, 1)
  27. end
  28. -- Extrapolate if needed.
  29. if self.history[#self.history].tick < t then
  30. local h1, h2 = self.history[#self.history - 1], self.history[#self.history]
  31. local factor = math.min(1 + ((t - h2.tick) / (h2.tick - h1.tick)), .1 / tickRate)
  32. local t = table.interpolate(h1, h2, factor)
  33. t.angle = h2.angle
  34. return t
  35. end
  36. -- Search backwards through history until we find something.
  37. for i = #self.history, 1, -1 do
  38. if self.history[i].tick <= t then return self.history[i] end
  39. end
  40. return self.history[1]
  41. end
  42. function PlayerDummy:draw()
  43. if self.ded then return end
  44. local lerpd = table.interpolate(self:get(drawTick()), self:get(drawTick() + 1), tickDelta / tickRate)
  45. self.drawAngle = lerpd.angle
  46. self.drawX, self.drawY = ctx.view:three(lerpd.x, lerpd.y, lerpd.z)
  47. self.drawScale = 1 + (ctx.view:convertZ(lerpd.z) / 500)
  48. app.player.base.draw(lerpd)
  49. end
  50. function PlayerDummy:trace(data)
  51. if data.angle then data.angle = math.rad(data.angle) end
  52. if data.x then data.x = data.x / 10 end
  53. if data.y then data.y = data.y / 10 end
  54. table.insert(self.history, setmetatable({
  55. x = data.x,
  56. y = data.y,
  57. z = data.z,
  58. angle = data.angle,
  59. tick = data.tick
  60. }, self.meta))
  61. self.x, self.y, self.z, self.angle = data.x, data.y, data.z, data.angle
  62. self.health, self.shield = data.health or self.health, data.shield or self.shield
  63. self.weapon, self.skill = data.weapon or self.weapon, data.skill or self.skill
  64. end
  65. return PlayerDummy