123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- Player = class()
- Player.char = '@'
- function testColl(bullets, x, y)
- for k, b in pairs(bullets) do
- if b.x == x and b.y == y then
- return true
- end
- end
- return false
- end
- function testNPCColl(npcs, x, y)
- local touchNPCs = {}
- for k, n in pairs(npcs) do
- if n.x == x and n.y == y then
- touchNPCs[#touchNPCs + 1] = n
- end
- end
- return touchNPCs
- end
- function Player:init(x, y)
- self.x = x
- self.y = y
- self.dead = false
- self.deathMessage = 'You died.' -- Set depending on how you died!
- self.treasure = 0
- end
- function Player:draw(text)
- self.color = self.dead and COLOR.RED or nil
- text[self.y][self.x] = self
- end
- function Player:update(world, key)
- if key == ACTION.STAIR then
- if world.grid[self.y][self.x].stair then
- sounds.hihat:play()
- love.timer.sleep(0.2)
- sounds.hihat:play()
- love.timer.sleep(0.2)
- sounds.hihat:play()
- love.timer.sleep(0.4)
- world:nextLevel()
- end
- return
- end
- local vx, vy = 0, 0
- if key == ACTION.UPLEFT or key == ACTION.UP or key == ACTION.UPRIGHT then
- vy = -1
- elseif key == ACTION.DOWNLEFT or key == ACTION.DOWN or key == ACTION.DOWNRIGHT then
- vy = 1
- end
- if key == ACTION.UPLEFT or key == ACTION.LEFT or key == ACTION.DOWNLEFT then
- vx = -1
- elseif key == ACTION.UPRIGHT or key == ACTION.RIGHT or key == ACTION.DOWNRIGHT then
- vx = 1
- end
- local nx, ny = self.x + vx, self.y + vy
- if world.grid[ny][nx].solid or testColl(world.bullets, nx, ny) then
- nx, ny = self.x, self.y
- end
- --sounds.select:play()
- self.x, self.y = nx, ny
- self:light(world)
- self:checkBullet(world)
- self:checkNPC(world)
- world.grid[self.y][self.x]:collide(world)
- end
- function Player:light(world)
- world.grid[self.y][self.x]:light(world)
- end
- function Player:checkBullet(world)
- if testColl(world.bullets, self.x, self.y) then
- self:die()
- self.deathMessage = 'A bullet hit you.'
- end
- end
- function Player:checkNPC(world)
- local npcs = testNPCColl(world.npcs, self.x, self.y)
- for k, n in pairs(npcs) do
- if n.killsPlayer then
- self:die()
- self.deathMessage = 'A goblin ate you.'
- end
- end
- end
- function Player:die()
- self.dead = true
- sounds.death:play()
- end
|