player.lua 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. Player = class()
  2. Player.char = '@'
  3. function testColl(bullets, x, y)
  4. for k, b in pairs(bullets) do
  5. if b.x == x and b.y == y then
  6. return true
  7. end
  8. end
  9. return false
  10. end
  11. function testNPCColl(npcs, x, y)
  12. local touchNPCs = {}
  13. for k, n in pairs(npcs) do
  14. if n.x == x and n.y == y then
  15. touchNPCs[#touchNPCs + 1] = n
  16. end
  17. end
  18. return touchNPCs
  19. end
  20. function Player:init(x, y)
  21. self.x = x
  22. self.y = y
  23. self.dead = false
  24. self.deathMessage = 'You died.' -- Set depending on how you died!
  25. self.treasure = 0
  26. end
  27. function Player:draw(text)
  28. self.color = self.dead and COLOR.RED or nil
  29. text[self.y][self.x] = self
  30. end
  31. function Player:update(world, key)
  32. if key == ACTION.STAIR then
  33. if world.grid[self.y][self.x].stair then
  34. sounds.hihat:play()
  35. love.timer.sleep(0.2)
  36. sounds.hihat:play()
  37. love.timer.sleep(0.2)
  38. sounds.hihat:play()
  39. love.timer.sleep(0.4)
  40. world:nextLevel()
  41. end
  42. return
  43. end
  44. local vx, vy = 0, 0
  45. if key == ACTION.UPLEFT or key == ACTION.UP or key == ACTION.UPRIGHT then
  46. vy = -1
  47. elseif key == ACTION.DOWNLEFT or key == ACTION.DOWN or key == ACTION.DOWNRIGHT then
  48. vy = 1
  49. end
  50. if key == ACTION.UPLEFT or key == ACTION.LEFT or key == ACTION.DOWNLEFT then
  51. vx = -1
  52. elseif key == ACTION.UPRIGHT or key == ACTION.RIGHT or key == ACTION.DOWNRIGHT then
  53. vx = 1
  54. end
  55. local nx, ny = self.x + vx, self.y + vy
  56. if world.grid[ny][nx].solid or testColl(world.bullets, nx, ny) then
  57. nx, ny = self.x, self.y
  58. end
  59. --sounds.select:play()
  60. self.x, self.y = nx, ny
  61. self:light(world)
  62. self:checkBullet(world)
  63. self:checkNPC(world)
  64. world.grid[self.y][self.x]:collide(world)
  65. end
  66. function Player:light(world)
  67. world.grid[self.y][self.x]:light(world)
  68. end
  69. function Player:checkBullet(world)
  70. if testColl(world.bullets, self.x, self.y) then
  71. self:die()
  72. self.deathMessage = 'A bullet hit you.'
  73. end
  74. end
  75. function Player:checkNPC(world)
  76. local npcs = testNPCColl(world.npcs, self.x, self.y)
  77. for k, n in pairs(npcs) do
  78. if n.killsPlayer then
  79. self:die()
  80. self.deathMessage = 'A goblin ate you.'
  81. end
  82. end
  83. end
  84. function Player:die()
  85. self.dead = true
  86. sounds.death:play()
  87. end