summaryrefslogtreecommitdiff
path: root/pickup.lua
blob: 69fe37d9a156015407eb0c59f44e82cc945eeae9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
local Entity = require("entity")

local Pickup = {}
Pickup.__index = Pickup

function Pickup.new(entity)
    local self = setmetatable({}, Pickup)

    self.x = entity.x
    self.y = entity.y
    self.width = entity.width
    self.height = entity.height
    self.image = nil
    self.body = nil
    self.fixture = nil
    self.object = entity:get("object", "")
    self.isPickup = true
    self.collected = false

    return self
end

function Pickup:assignImage()
    if self.object == "key" then
        self.image = love.graphics.newImage("assets/misc/key.png")
    elseif self.object == "dbljump" then
        self.image = love.graphics.newImage("assets/misc/dbljump.png")
    end
end

function Pickup:setWorldPhysics(world)
    local cx = self.x + self.width / 2
    local cy = self.y + self.height / 2

    self.body = love.physics.newBody(world, cx, cy, "static")
    local shape = love.physics.newRectangleShape(self.width, self.height)

    self.fixture = love.physics.newFixture(self.body, shape)
    self.fixture:setSensor(true)
    self.fixture:setUserData(self)
end

function Pickup:syncFromPhysicsBody() end

function Pickup:update(dt) end

function Pickup:draw()
    if not self.collected then
        love.graphics.draw(self.image, self.x, self.y)
    end
end

function Pickup:pickup(world, player)
    if self.collected then return end
    self.collected = true
    world.player.pickups[self.object] = (world.player.pickups[self.object] or 0) + 1
    world.playerTextbox:show(
        "Picked up " .. self.object .. " (" .. world.player.pickups[self.object] .. ")",
        { player.x + player.width/2, player.y - 20, "center" },
        "write",
        { life = 3, fontSize = 9, centeredText = true, wrapToFit = true }
    )
end

return Pickup