200 lines
5.3 KiB
Lua
200 lines
5.3 KiB
Lua
local player = {
|
|
x = 0,
|
|
y = 0,
|
|
vx = 0,
|
|
vy = 0,
|
|
width = 12,
|
|
height = 12,
|
|
speed = 1,
|
|
|
|
facing = 1,
|
|
}
|
|
|
|
function player.reset(self, starting_items)
|
|
self.score = 0
|
|
|
|
self.maxHp = 3
|
|
self.hp = self.maxHp
|
|
|
|
self.oxygen_max = 60
|
|
self.oxygen = self.oxygen_max
|
|
|
|
self.cooldown = 0
|
|
|
|
self.attack_cooldown = 0
|
|
self.attack_x = 0
|
|
self.attack_y = 0
|
|
|
|
self.items = starting_items
|
|
end
|
|
|
|
function player.can_hold(self, item, systems)
|
|
return (self.items[item] or 0) < (systems.items[item].stack or 1)
|
|
end
|
|
|
|
function player.add_item(self, item, systems)
|
|
if not self:can_hold(item, systems) then
|
|
return false
|
|
end
|
|
|
|
if not self.items[item] then
|
|
self.items[item] = 1
|
|
else
|
|
self.items[item] = self.items[item] + 1
|
|
end
|
|
|
|
return true
|
|
end
|
|
|
|
function player.remove_item(self, item)
|
|
if self.items[item] then
|
|
self.items[item] = self.items[item] - 1
|
|
|
|
if self.items[item] <= 0 then
|
|
self.items[item] = nil
|
|
|
|
if self.equipment == item then
|
|
self.equipment = nil
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
function player.item_count(self)
|
|
local count = 0
|
|
for _,_ in pairs(self.items) do
|
|
count = count + 1
|
|
end
|
|
|
|
return count
|
|
end
|
|
|
|
function player.swing(self, sprite, length, on_hit)
|
|
if self.attack_cooldown <= 0 then
|
|
self.attack_cooldown = length + 0.1
|
|
self.attack_sprite = sprite
|
|
|
|
self.attack_hitbox = {
|
|
width = self.attack_sprite:getWidth(),
|
|
height = self.attack_sprite:getHeight(),
|
|
}
|
|
self.attack_hitbox.x, self.attack_hitbox.y = self:facing_offset(self.attack_hitbox.width, self.attack_hitbox.height)
|
|
end
|
|
end
|
|
|
|
function player.on_hit(self, other, systems)
|
|
local item = systems.items[self.equipment]
|
|
if item and item.on_hit then
|
|
item:on_hit(self, other, systems)
|
|
end
|
|
end
|
|
|
|
function player.update(self, level, dt)
|
|
self.vx = 0
|
|
self.vy = 0
|
|
self.attack_x = 0
|
|
self.attack_y = 0
|
|
|
|
if love.keyboard.isDown("left") then
|
|
self.vx = self.vx - self.speed
|
|
self.facing = 1
|
|
end
|
|
|
|
if love.keyboard.isDown("right") then
|
|
self.vx = self.vx + self.speed
|
|
self.facing = 3
|
|
end
|
|
|
|
if love.keyboard.isDown("up") then
|
|
self.vy = self.vy - self.speed
|
|
self.facing = 2
|
|
end
|
|
|
|
if love.keyboard.isDown("down") then
|
|
self.vy = self.vy + self.speed
|
|
self.facing = 4
|
|
end
|
|
|
|
if not level:getTileCollisionAtPosition(self.x + self.vx, self.y) and not level:getTileCollisionAtPosition(self.x + self.width + self.vx - 1, self.y) and not level:getTileCollisionAtPosition(self.x + self.vx, self.y + self.height - 1) and not level:getTileCollisionAtPosition(self.x + self.width + self.vx - 1, self.y + self.height - 1) then
|
|
self.x = self.x + self.vx
|
|
end
|
|
if not level:getTileCollisionAtPosition(self.x, self.y + self.vy) and not level:getTileCollisionAtPosition(self.x, self.y + self.height + self.vy - 1) and not level:getTileCollisionAtPosition(self.x + self.width - 1, self.y + self.vy) and not level:getTileCollisionAtPosition(self.x + self.width - 1, self.y + self.height + self.vy - 1) then
|
|
self.y = self.y + self.vy
|
|
end
|
|
|
|
if self.attack_cooldown > 0 then
|
|
self.attack_cooldown = self.attack_cooldown - dt
|
|
|
|
if self.attack_cooldown < 0.1 then
|
|
self.attack_sprite = nil
|
|
self.attack_hitbox = nil
|
|
else
|
|
self.attack_hitbox = {
|
|
width = self.attack_sprite:getWidth(),
|
|
height = self.attack_sprite:getHeight(),
|
|
}
|
|
self.attack_hitbox.x, self.attack_hitbox.y = self:facing_offset(self.attack_hitbox.width, self.attack_hitbox.height)
|
|
end
|
|
else
|
|
self.attack_cooldown = 0
|
|
end
|
|
|
|
if self.cooldown > 0 then
|
|
self.cooldown = self.cooldown - dt
|
|
else
|
|
self.cooldown = 0
|
|
end
|
|
|
|
if level.levelData.oxygen then
|
|
self.oxygen = self.oxygen_max
|
|
else
|
|
if self.oxygen > 0 then
|
|
self.oxygen = self.oxygen - dt
|
|
elseif self.hp > 0 then
|
|
self.hurt(3)
|
|
end
|
|
end
|
|
end
|
|
|
|
function player.hurt(self, damage)
|
|
if self.cooldown == 0 then
|
|
self.cooldown = 1
|
|
self.hp = self.hp - damage
|
|
end
|
|
end
|
|
|
|
function player.add_oxygen(self, oxygen)
|
|
self.oxygen = math.min(self.oxygen + oxygen, self.oxygen_max)
|
|
end
|
|
|
|
function player.interact(self, systems)
|
|
local item = systems.items[self.equipment]
|
|
if item and item.interact then
|
|
item:interact(self, systems)
|
|
end
|
|
end
|
|
|
|
function player.draw(self)
|
|
love.graphics.setColor(1 - self.cooldown, 1 - self.cooldown, 1 - self.cooldown)
|
|
love.graphics.rectangle("fill", self.x, self.y, self.width, self.height)
|
|
|
|
if self.attack_sprite then
|
|
love.graphics.setColor(1, 1, 1)
|
|
love.graphics.draw(self.attack_sprite, self.attack_hitbox.x, self.attack_hitbox.y)
|
|
end
|
|
end
|
|
|
|
function player.facing_offset(self, width, height)
|
|
if self.facing == 1 then -- Left
|
|
return self.x - width, self.y
|
|
elseif self.facing == 2 then -- Up
|
|
return self.x, self.y - height
|
|
elseif self.facing == 3 then -- Right
|
|
return self.x + self.width, self.y
|
|
elseif self.facing == 4 then -- Down
|
|
return self.x, self.y + self.height
|
|
end
|
|
end
|
|
|
|
return player
|