weapon.lua 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. local Weapon = class()
  2. function Weapon:activate(owner)
  3. self.timers = {}
  4. self.timers.shoot = 0
  5. self.timers.reload = 0
  6. self.timers.switch = 0
  7. self.currentAmmo = self.ammo
  8. self.currentClip = self.clip
  9. end
  10. function Weapon:update(owner)
  11. self.timers.shoot = timer.rot(self.timers.shoot)
  12. self.timers.reload = timer.rot(self.timers.reload, function()
  13. local amt = math.min(self.clip - self.currentClip, self.currentAmmo)
  14. self.currentClip = self.currentClip + amt
  15. self.currentAmmo = self.currentAmmo - amt
  16. ctx.event:emit('sound.play', {sound = 'reload', x = owner.x, y = owner.y})
  17. end)
  18. self.timers.switch = timer.rot(self.timers.switch)
  19. end
  20. function Weapon:draw(owner)
  21. local dir = owner.drawAngle
  22. local x, y, s = owner.drawX, owner.drawY, owner.drawScale
  23. local dx = owner.class.handx * owner.class.scale * s - owner.recoil
  24. local dy = owner.class.handy * owner.class.scale * s
  25. x = x + math.dx(dx, dir) - math.dy(dy, dir)
  26. y = y + math.dy(dx, dir) + math.dx(dy, dir)
  27. love.graphics.draw(self.image, x, y, dir, self.scale * s, self.scale * s, self.anchorx, self.anchory)
  28. end
  29. function Weapon:select(owner)
  30. self.timers.switch = self.switchTime
  31. ctx.event:emit('sound.play', {sound = 'switch', x = owner.x, y = owner.y})
  32. end
  33. function Weapon:canFire(owner)
  34. return self.timers.switch == 0 and self.timers.shoot == 0 and self.timers.reload == 0 and self.currentClip > 0
  35. end
  36. function Weapon:fire(owner)
  37. ctx.spells:activate(owner.id, data.spell[self.code])
  38. self.timers.shoot = self.fireTime
  39. self.currentClip = self.currentClip - 1
  40. if self.currentClip == 0 then self.timers.reload = self.reloadTime end
  41. owner.recoil = self.recoil
  42. end
  43. function Weapon:reload(owner)
  44. if self.currentClip < self.clip and self.timers.reload == 0 then
  45. self.timers.reload = self.reloadTime
  46. end
  47. end
  48. function Weapon:refillAmmo(owner)
  49. self.currentAmmo = math.min(self.currentAmmo + math.ceil(self.ammo / 4), self.ammo)
  50. end
  51. function Weapon:value(owner)
  52. if self.timers.switch > 0 then return self.timers.switch / self.switchTime
  53. elseif self.timers.reload > 0 then return self.timers.reload / self.reloadTime
  54. else return 0 end
  55. end
  56. function Weapon:ammoValue(owner)
  57. return self.currentClip, self.currentAmmo
  58. end
  59. return Weapon