spear.lua 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. Spear = extend(Projectile)
  2. Spear.scale = .3
  3. function Spear:activate()
  4. self.body = love.physics.newBody(ctx.world, self.x, self.y, 'dynamic')
  5. self.shape = love.physics.newCircleShape(10)
  6. self.fixture = love.physics.newFixture(self.body, self.shape)
  7. self.fixture:setCategory(6)
  8. self.fixture:setFriction(10)
  9. self.body:setUserData(self)
  10. self.body:applyLinearImpulse((100 + love.math.random() * 100) * self.direction, -100 - love.math.random() * 100)
  11. self.prevx = self.x
  12. self.prevy = self.y
  13. self.deathTimer = 0
  14. ctx.event:emit('view.register', {object = self})
  15. end
  16. function Spear:deactivate()
  17. self.body:destroy()
  18. ctx.event:emit('view.unregister', {object = self})
  19. end
  20. function Spear:update()
  21. self.prevx = self.body:getX()
  22. self.prevy = self.body:getY()
  23. self.deathTimer = timer.rot(self.deathTimer, function()
  24. ctx.projectiles:remove(self)
  25. end)
  26. end
  27. function Spear:draw()
  28. local g = love.graphics
  29. local image = data.media.graphics.dinoland.spear
  30. local x = math.lerp(self.prevx, self.body:getX(), ls.accum / ls.tickrate)
  31. local y = math.lerp(self.prevy, self.body:getY(), ls.accum / ls.tickrate)
  32. local angle = math.direction(self.prevx, self.prevy, self.body:getPosition())
  33. g.setColor(255, 255, 255)
  34. g.draw(image, x, y, angle, self.scale / 2, self.scale, image:getWidth(), image:getHeight() / 2)
  35. end
  36. function Spear:paused()
  37. self.prevx = self.body:getX()
  38. self.prevy = self.body:getY()
  39. end
  40. function Spear:collideWith(other)
  41. if isa(other, Map) then
  42. self.deathTimer = .05
  43. return true
  44. end
  45. return false
  46. end