From 340ff8f519ae6a7a2da46f192e1b3baab538e9b9 Mon Sep 17 00:00:00 2001 From: Hyeonung Baek Date: Sat, 4 Jan 2025 15:53:45 +0900 Subject: [PATCH] A set of updates - add object door - refactor log - etc --- entity.lua | 18 ++++++++++++---- export.sh | 2 +- item.lua | 2 ++ item/key.lua | 20 +++++++++++++++++ items.lua | 5 ----- main.lua | 55 ++++++++++++++++++++++++++--------------------- obj/door.lua | 50 ++++++++++++++++++++++++++++++++++++++++++ room.lua | 10 ++++++++- room/default.lua | 4 +--- room/doortest.lua | 17 +++++++++++++++ rooms.lua | 1 + util.lua | 13 +++++++++++ 12 files changed, 158 insertions(+), 39 deletions(-) create mode 100644 item/key.lua delete mode 100644 items.lua create mode 100644 obj/door.lua create mode 100644 room/doortest.lua diff --git a/entity.lua b/entity.lua index eecd0cc..0d5c6cc 100644 --- a/entity.lua +++ b/entity.lua @@ -47,15 +47,15 @@ local function draw_line(l) end end -function m:draw() +function m:draw_lines(lines) love.graphics.push('all') love.graphics.translate(self.x, self.y) love.graphics.setLineWidth(3) - if type(self.lines[1]) == 'string' then - draw_line(self.lines) + if type(lines[1]) == 'string' then + draw_line(lines) else - for _, l in ipairs(self.lines) do + for _, l in ipairs(lines) do draw_line(l) end end @@ -63,4 +63,14 @@ function m:draw() love.graphics.pop() end +function m:draw() + self:draw_lines(self.lines) +end + +function m:install(room, x, y) + local e = self:new() + e:setpos(x, y) + room:insert(e) +end + return m diff --git a/export.sh b/export.sh index 0f534ea..ca2a908 100755 --- a/export.sh +++ b/export.sh @@ -1,5 +1,5 @@ #!/bin/sh rm garden.love -zip -r garden.love res/ room/ item/ *.lua +zip -r garden.love res/ room/ obj/ item/ *.lua cp garden.love ~/storage/downloads/ diff --git a/item.lua b/item.lua index fffdd53..770e29a 100644 --- a/item.lua +++ b/item.lua @@ -26,6 +26,8 @@ end function m:itemMode() self.mousepressed = window.mousepressed(function (self, x, y, button) log:info(self) + inventory.selected_item = self + log:format("selected idx : %s", inventory.selected_item.idx) end) end diff --git a/item/key.lua b/item/key.lua new file mode 100644 index 0000000..676f6f8 --- /dev/null +++ b/item/key.lua @@ -0,0 +1,20 @@ +local window = require('window') +local item = require('item') + +-- A key. +local k = item:from{ + width = 20, + height = 20, + lines = { + {'fill', {0, 0, 0, 5, 5, 5, 5, 0}}, + {'line', {5, 5, 20, 20}}, + {'line', {10, 10, 5, 15}}, + {'line', {15, 15, 10, 20}}, + }, + + name = "key", + description = "A key for locked containers with varying volume", + code = 0, +} + +return k diff --git a/items.lua b/items.lua deleted file mode 100644 index ad8ec75..0000000 --- a/items.lua +++ /dev/null @@ -1,5 +0,0 @@ -local m = { - ['crowbar'] = require('item/crowbar') -} - -return m diff --git a/main.lua b/main.lua index c192b2b..6a1a464 100644 --- a/main.lua +++ b/main.lua @@ -1,4 +1,5 @@ -- Imports +local util = require("util") local window = require("window") local button = require("button") local rooms = require("rooms") @@ -29,23 +30,31 @@ inventory = { x = 0, y = 0, width = screen.width, height = 60, - items = {} + selected_item = nil, + items = {}, } inventory.mousepressed = window.propagate{'items'} inventory.draw = window.draw(function (self) - local x = 0 - for _, item in pairs(self.items) do - item:setpos(x, 0) + for i, item in pairs(self.items) do + if self.selected_item and self.selected_item.idx == i then + love.graphics.circle('line', 60*i - 30, 30, 25) + end + item:draw() - x = item.width end + window.draw_border(self.width, self.height) end) function inventory:pickup(item) - table.insert(self.items, item) + util.add_entity(self.items, item) + + local x = (#self.items - 1) * 60 + (60 - item.width) / 2 + local y = (60 - item.height) / 2 + + item:setpos(x, y) end log = { @@ -55,44 +64,39 @@ log = { lines = {}, } -log.limit = log.width - 10 - local font = love.graphics.getFont() local font_height = font:getHeight('L') local line_limit = log.height / font_height +log.limit = log.width - 10 + log.draw = window.draw(function (self) - local i = 0 - for _, l in ipairs(self.lines) do - local rw, wl = font:getWrap(l, self.limit) + local h = 0 + for i, l in ipairs(self.lines) do local th = font:getHeight(l) - love.graphics.printf(l, 5, 5 + i*th, self.limit) - i = i + #wl + love.graphics.printf(l, 5, 5 + h, self.limit) + h = h + th end window.draw_border(self.width, self.height) end) function log:log(str) - table.insert(self.lines, str) + local rw, wl = font:getWrap(str, self.limit) - local i = 0 - for _, l in ipairs(self.lines) do - local rw, wl = font:getWrap(l, self.limit) - local th = font:getHeight(l) - i = i + #wl + for _, l in ipairs(wl) do + table.insert(self.lines, l) end - while i > line_limit do - local l = self.lines[1] - local rw, wl = font:getWrap(l, self.limit) - local th = font:getHeight(l) - - i = i - #wl + while #self.lines > line_limit do table.remove(self.lines, 1) end end +function log:format(...) + log:log(string.format(...)) +end + function log:info(o) if o._t == 'item' then self:log(string.format("It's %s. %s", o.name, o.description)) @@ -116,6 +120,7 @@ end function love.mousepressed(x, y, button) local x, y = (x - mw) / MULTIPLIER, (y - mh) / MULTIPLIER + screen:mousepressed(x, y, button) inventory:mousepressed(x, y, button) end diff --git a/obj/door.lua b/obj/door.lua new file mode 100644 index 0000000..53d8c97 --- /dev/null +++ b/obj/door.lua @@ -0,0 +1,50 @@ +local window = require('window') +local entity = require('entity') + +local locked_door = { + {'fill', {0, 0, 0, 120, 60, 120, 60, 0}}, + {'fill', {10, 10, 50, 10, 50, 50, 10, 50}}, + {'line', {30, 10, 30, 50}}, + {'line', {10, 30, 50, 30}}, +} + +local open_door = { + {'fill', {0, 0, 0, 120, 60, 120, 60, 0}}, +} + +local m = entity:from{ + width = 60, + height = 120, + locked = true, + code = 0, +} + +function m:draw() + if self.locked then + self:draw_lines(locked_door) + else + self:draw_lines(open_door) + end +end + +m.mousepressed = window.mousepressed(function (self) + if self.locked then + local i = inventory.selected_item + if i and i.code then + if i.code == self.code then + self.locked = false + log:log("Door opened.") + else + log:log("This seems not a matching key.") + inventory.selected_item = nil + end + else + log:log("It's locked.") + end + else + log:format("You enter %s", self.destination) + set_room(self.destination) + end +end) + +return m diff --git a/room.lua b/room.lua index e2ed0c7..2c5686f 100644 --- a/room.lua +++ b/room.lua @@ -6,12 +6,20 @@ local entity = require('entity') -- - objects -- - edges local room = entity:from{ - x = 0, y = 0, background = love.graphics.newImage("res/defaultbg.png"), edges = {}, objects = {}, } +local oldnew = room.new +function room:new() + local r = oldnew(room) + r.edges = {} + r.objects = {} + + return r +end + function room:insert(obj) table.insert(self.objects, obj) obj.idx = #self.objects diff --git a/room/default.lua b/room/default.lua index 02c1303..3704955 100644 --- a/room/default.lua +++ b/room/default.lua @@ -4,12 +4,10 @@ local crowbar = require('item/crowbar') local r = room:new() -r.background = love.graphics.newImage("res/defaultbg.png") - crowbar:install(r, 100, 160) edge:set(r, 'up', "default") edge:set(r, 'down', "default") edge:set(r, 'left', "default") -edge:set(r, 'right', "default") +edge:set(r, 'right', "doortest") return r diff --git a/room/doortest.lua b/room/doortest.lua new file mode 100644 index 0000000..2187f2c --- /dev/null +++ b/room/doortest.lua @@ -0,0 +1,17 @@ +local room = require('room') +local door = require('obj/door') +local key = require('item/key') + +local r = room:new() + +door:from{ + destination = "default", + locked = true, + code = 45100, +} + :install(r, 40, 80) + +key:from{ code = 45100 } + :install(r, 180, 160) + +return r diff --git a/rooms.lua b/rooms.lua index d0b7ddf..ecd9fa0 100644 --- a/rooms.lua +++ b/rooms.lua @@ -1,5 +1,6 @@ local m = { ['default'] = require('room/default'), + ['doortest'] = require('room/doortest'), } return m diff --git a/util.lua b/util.lua index 4e2bb61..320e92c 100644 --- a/util.lua +++ b/util.lua @@ -21,4 +21,17 @@ function m.inside(px, py, x, y, w, h) m.between(py, y, y+h) end +function m.add_entity(a, x) + table.insert(a, x) + x.idx = #a +end + +function m.del_entity(a, x) + table.remove(a, x) + + for i, v in ipairs(a) do + v.idx = i + end +end + return m