diff options
| -rw-r--r-- | hud.lua | 54 | ||||
| -rw-r--r-- | main.lua | 3 | ||||
| -rw-r--r-- | pickup.lua | 65 | ||||
| -rw-r--r-- | player.lua | 3 | ||||
| -rw-r--r-- | tilemap.lua | 8 | ||||
| -rw-r--r-- | world.lua | 18 |
6 files changed, 140 insertions, 11 deletions
@@ -4,15 +4,14 @@ local HUD = {} HUD.__index = HUD HUD.slots = { - { enabled = false }, - { enabled = false }, + { enabled = false, object = "key", name = "Key" }, + { enabled = false, object = "dbljump", name = "Double Jump" }, { enabled = false }, { enabled = false }, } local SLOT_SIZE = 16 local SLOT_SPACING = 8 -local LOREM = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua." local function createMissingTexture() local size = SLOT_SIZE @@ -42,6 +41,18 @@ function HUD:new() self.screenW = 0 self.screenH = 0 self.drawScale = 1 + self.player = nil + + for _, slot in ipairs(HUD.slots) do + if slot.object == "key" then + slot.image = love.graphics.newImage("assets/misc/key.png") + slot.image:setFilter("nearest", "nearest") + elseif slot.object == "dbljump" then + slot.image = love.graphics.newImage("assets/misc/dbljump.png") + slot.image:setFilter("nearest", "nearest") + end + end + return self end @@ -64,12 +75,21 @@ function HUD:computeSlotRects(screenW, screenH, drawScale) return rects end -function HUD:update(dt, screenW, screenH, drawScale) +function HUD:update(dt, screenW, screenH, drawScale, player) self.screenW = screenW self.screenH = screenH self.drawScale = drawScale + self.player = player self.slotRects = self:computeSlotRects(screenW, screenH, drawScale) + if player and player.pickups then + for _, slot in ipairs(HUD.slots) do + if slot.object then + slot.enabled = (player.pickups[slot.object] or 0) > 0 + end + end + end + local mx, my = love.mouse.getPosition() local prevHovered = self.hoveredSlot self.hoveredSlot = nil @@ -82,10 +102,19 @@ function HUD:update(dt, screenW, screenH, drawScale) end if self.hoveredSlot and self.hoveredSlot ~= prevHovered then + local slot = HUD.slots[self.hoveredSlot] + local text = "Planned feature that didn't make it into the game ;(" + if slot.name then + local count = 0 + if player and player.pickups and slot.object then + count = player.pickups[slot.object] or 0 + end + text = slot.name .. " x" .. count + end local rect = self.slotRects[self.hoveredSlot] local centerX = screenW / 2 local topY = rect.y + rect.h + 8 * drawScale - self.textbox:show(LOREM, { centerX, topY, "center" }, "show", { + self.textbox:show(text, { centerX, topY, "center" }, "show", { fontSize = 16, centeredText = true, wrapToFit = true, @@ -104,7 +133,7 @@ function HUD:mousepressed(mx, my, button) for i, rect in ipairs(self.slotRects) do if mx >= rect.x and mx < rect.x + rect.w and my >= rect.y and my < rect.y + rect.h then - HUD.slots[i].enabled = not HUD.slots[i].enabled + --HUD.slots[i].enabled = not HUD.slots[i].enabled break end end @@ -119,10 +148,17 @@ function HUD:draw() love.graphics.rectangle("fill", 0, 0, screenW, screenH) for i, rect in ipairs(self.slotRects) do - love.graphics.setColor(1, 1, 1, 1) - love.graphics.draw(self.missingTexture, rect.x, rect.y, 0, drawScale, drawScale) + local slot = HUD.slots[i] + local img = slot.image or self.missingTexture + + if slot.enabled then + love.graphics.setColor(1, 1, 1, 1) + else + love.graphics.setColor(1, 1, 1, 0.25) + end + love.graphics.draw(img, rect.x, rect.y, 0, drawScale, drawScale) - if HUD.slots[i].enabled then + if slot.enabled then love.graphics.setColor(0, 1, 0, 0.3) love.graphics.rectangle("fill", rect.x, rect.y, rect.w, rect.h) end @@ -90,7 +90,8 @@ end function states.hud.update(dt) if hud then local w, h = love.graphics.getWidth(), love.graphics.getHeight() - hud:update(dt, w, h, finalScale) + local player = world and world:getPlayer() + hud:update(dt, w, h, finalScale, player) end end diff --git a/pickup.lua b/pickup.lua new file mode 100644 index 0000000..69fe37d --- /dev/null +++ b/pickup.lua @@ -0,0 +1,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
\ No newline at end of file @@ -7,7 +7,7 @@ setmetatable(Player, { __index = Entity }) local MOVE_SPEED = 65 local SWIM_SPEED = 100 -local JUMP_FORCE = -200 +local JUMP_FORCE = -210 local GROUND_LAYER = "ground" function Player.new(world, spawnX, spawnY) @@ -17,6 +17,7 @@ function Player.new(world, spawnX, spawnY) self.spawnX = spawnX or 0 self.spawnY = spawnY or 0 + self.pickups = {} self.directionState = { 'side', 'up', 'down', 'side_up', 'side_down' } self.directionIndex = 1 diff --git a/tilemap.lua b/tilemap.lua index f706e55..72a186e 100644 --- a/tilemap.lua +++ b/tilemap.lua @@ -3,6 +3,7 @@ local Liquid = require("liquid") local LiquidSurface = require("liquidSurface") local Spike = require("spike") local TextTrigger = require("textTrigger") +local Pickup = require("pickup") local Tilemap = {} Tilemap.__index = Tilemap @@ -56,6 +57,7 @@ function Tilemap:new(mapPath, tilesets) self.entitiesLiquidSurfaces = {} self.entitiesSpikes = {} self.entitiesTextTriggers = {} + self.entitiesPickups = {} self.layerBackground = nil self.layerDecorationBackground = nil self.layerDecorationForeground = nil @@ -142,6 +144,8 @@ function Tilemap:new(mapPath, tilesets) table.insert(self.entitiesLiquidPolygons, Liquid:new(entity)) elseif name == "liquid_surface" then table.insert(self.entitiesLiquidSurfaces, LiquidSurface:new(entity)) + elseif name == "pickups" then + table.insert(self.entitiesPickups, Pickup.new(entity)) end end end @@ -175,6 +179,10 @@ function Tilemap:getEntitiesTextTriggers() return self.entitiesTextTriggers end +function Tilemap:getEntitiesPickups() + return self.entitiesPickups +end + function Tilemap:getEntitiesCameraBorders() return self.entitiesCameraBorders end @@ -25,6 +25,7 @@ function World:new() self.liquidSurfaceFixtures = {} self.spikes = {} self.textTriggers = {} + self.pickups = {} self.refractionCanvas = nil self.liquidShader = nil self.activeSplashes = {} @@ -160,6 +161,13 @@ function World:load(mapPath, tilesets) table.insert(self.entities, textTrigger) end + for _, pickup in ipairs(self.tilemap:getEntitiesPickups()) do + pickup:assignImage() + pickup:setWorldPhysics(self.physicsWorld) + table.insert(self.pickups, pickup) + table.insert(self.entities, pickup) + end + local ok, shader = pcall(love.graphics.newShader, "shaders/liquid.glsl") self.liquidShader = ok and shader or nil if not self.liquidShader then @@ -211,11 +219,21 @@ function World:handleTextTrigger(trigger, player) end end +function World:handlePickup(pickup, player) + if pickup and player and pickup.isPickup and self:_isPlayerLike(player) then + pickup:pickup(self, player) + end +end + function World:_onBeginContact(udA, udB, nx, ny, contact) if type(udA) == "table" and udA.isTextTrigger then self:handleTextTrigger(udA, udB) elseif type(udB) == "table" and udB.isTextTrigger then self:handleTextTrigger(udB, udA) + elseif type(udA) == "table" and udA.isPickup then + self:handlePickup(udA, udB) + elseif type(udB) == "table" and udB.isPickup then + self:handlePickup(udB, udA) end if udA == "ground" and self:_isTrackedEntity(udB) then |
