Basic prototype

This commit is contained in:
백현웅 2025-01-01 18:43:35 +09:00
parent 3c4c6df308
commit a50a6e2734
10 changed files with 175 additions and 18 deletions

View file

@ -1,8 +1,10 @@
WIDTH = 400
WIDTH = 528
HEIGHT = 300
MULTIPLIER = 1.2
function love.conf(t)
--[[
t.window.width = MULTIPLIER * WIDTH
t.window.height = MULTIPLIER * HEIGHT
--]]
end

View file

@ -1,5 +1,5 @@
#!/bin/sh
rm garden.love
zip -r garden.love res/ room/ *.lua
zip -r garden.love res/ room/ item/ *.lua
cp garden.love ~/storage/downloads/

47
item.lua Normal file
View file

@ -0,0 +1,47 @@
local window = require('window')
-- item prototype
-- name
-- image
-- description
local m = {
_t = 'item',
x = 0,
y = 0,
width = 60,
height = 60,
name = "item",
description = "sample description",
}
function m:new()
local mt = { __index = m }
local i = {}
setmetatable(i, mt)
return i
end
function m:setpos(x, y)
self.x = x
self.y = y
end
function m:draw()
love.graphics.draw(self.image, self.x, self, y)
end
function m:pickupMode(room)
self.mousepressed = window.mousepressed(function (self, x, y, button)
self:itemMode()
self.room:remove(self)
inventory:pickup(self)
end)
end
function m:itemMode()
self.mousepressed = window.mousepressed(function (self, x, y, button)
log:info(self)
end)
end
return m

16
item/crowbar.lua Normal file
View file

@ -0,0 +1,16 @@
local item = require('item')
local m = item:new()
m.name = "Crowbar"
m.description = "A durable tool for opening wooden boxes."
function m:draw()
love.graphics.push('all')
love.graphics.translate(self.x, self.y)
love.graphics.setLineWidth(3)
love.graphics.line(10, 20, 20, 10, 25, 10, 30, 20, 40, 50)
love.graphics.pop()
end
return m

5
items.lua Normal file
View file

@ -0,0 +1,5 @@
local m = {
['crowbar'] = require('item/crowbar')
}
return m

View file

@ -12,6 +12,8 @@ screen = {
}
screen.mousepressed = window.mousepressed(function (self, x, y, button)
log:log(string.format("%d, %d", x, y))
current_room:mousepressed(x, y, button)
end)
screen.draw = window.draw(function (self)
@ -26,34 +28,80 @@ inventory = {
items = {}
}
inventory.mousepressed = window.propagate{'items'}
inventory.draw = window.draw(function (self)
for _, item in ipairs(self.items) do
-- item:setpos(x, y)
local x = 0
for _, item in pairs(self.items) do
item:setpos(x, 0)
item:draw()
x = item.width
end
window.draw_border(self.width, self.height)
end)
function inventory:pickup(item)
self.items[item.name] = item
end
log = {
x = inventory.width , y = 0,
width = 80,
width = WIDTH - screen.width,
height = HEIGHT,
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.draw = window.draw(function (self)
local i = 0
for _, l in ipairs(self.lines) do
local rw, wl = font:getWrap(l, self.limit)
local th = font:getHeight(l)
love.graphics.printf(l, 5, 5 + i*th, self.limit)
i = i + #wl
end
window.draw_border(self.width, self.height)
end)
function love.load()
timer = 0
function log:log(str)
table.insert(self.lines, str)
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
end
function love.mousepressed(x, y, button)
local x, y = x / MULTIPLIER, y / MULTIPLIER
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
table.remove(self.lines, 1)
end
end
function log:info(o)
if o._t == 'item' then
self:log(string.format("It's %s. %s", o.name, o.description))
else
self:log("There's no info for that.")
end
end
function love.load()
log:log("Welcome to the garden.")
end
function love.update(dt)
timer = timer + dt
end
mw, mh = 0, 0
@ -62,6 +110,12 @@ if love.system.getOS() == 'android' then
mh = (love.window.getHeight() - HEIGHT) / 2
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
function love.draw()
love.graphics.push()
love.graphics.translate(mw, mh)

16
object.lua Normal file
View file

@ -0,0 +1,16 @@
local window = require('window')
-- prototype of immovable objects
local m = {}
m.__index = m
function m:new()
return setmetatable({}, self)
end
m.mousepressed = window.mousepressed(function (self, x, y, button)
end)
function m:draw()
love.graphics.draw(self.image, self.x, self.y)
end

View file

@ -5,6 +5,7 @@ local window = require('window')
-- - objects
-- - edges
local room = {
x = 0, y = 0,
background = love.graphics.newImage("res/defaultbg.png"),
edges = {},
objects = {},
@ -33,6 +34,19 @@ function room:new()
return m
end
function room:insert(obj)
table.insert(self.objects, obj)
obj.idx = #self.objects
end
function room:remove(obj)
table.remove(self.objects, obj.idx)
for i, v in ipairs(self.objects) do
v.idx = i
end
end
room.mousepressed = window.propagate{'edges', 'objects'}
function room:draw()

View file

@ -4,4 +4,10 @@ local r = room:new()
r.background = love.graphics.newImage("res/defaultbg.png")
local crowbar = require('item/crowbar')
crowbar:pickupMode()
crowbar:setpos(100, 160)
crowbar.room = r
r:insert(crowbar)
return r

View file

@ -23,14 +23,11 @@ end
function w.propagate(tnames)
return function(self, x, y, button)
local x, y = x - self.x, y - self.y
for _, tname in ipairs(tnames) do
for i, v in pairs(self[tname]) do
local x, y = x - v.x, y - v.y
if 0 <= x and x <= v.width and 0 <= y and y <= v.height then
v:mousepressed(x, y, button)
return
end
end
end
end