Basic prototype
This commit is contained in:
parent
3c4c6df308
commit
a50a6e2734
10 changed files with 175 additions and 18 deletions
4
conf.lua
4
conf.lua
|
@ -1,8 +1,10 @@
|
||||||
WIDTH = 400
|
WIDTH = 528
|
||||||
HEIGHT = 300
|
HEIGHT = 300
|
||||||
MULTIPLIER = 1.2
|
MULTIPLIER = 1.2
|
||||||
|
|
||||||
function love.conf(t)
|
function love.conf(t)
|
||||||
|
--[[
|
||||||
t.window.width = MULTIPLIER * WIDTH
|
t.window.width = MULTIPLIER * WIDTH
|
||||||
t.window.height = MULTIPLIER * HEIGHT
|
t.window.height = MULTIPLIER * HEIGHT
|
||||||
|
--]]
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
|
|
||||||
rm garden.love
|
rm garden.love
|
||||||
zip -r garden.love res/ room/ *.lua
|
zip -r garden.love res/ room/ item/ *.lua
|
||||||
cp garden.love ~/storage/downloads/
|
cp garden.love ~/storage/downloads/
|
||||||
|
|
47
item.lua
Normal file
47
item.lua
Normal 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
16
item/crowbar.lua
Normal 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
5
items.lua
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
local m = {
|
||||||
|
['crowbar'] = require('item/crowbar')
|
||||||
|
}
|
||||||
|
|
||||||
|
return m
|
72
main.lua
72
main.lua
|
@ -12,7 +12,9 @@ screen = {
|
||||||
}
|
}
|
||||||
|
|
||||||
screen.mousepressed = window.mousepressed(function (self, x, y, button)
|
screen.mousepressed = window.mousepressed(function (self, x, y, button)
|
||||||
end)
|
log:log(string.format("%d, %d", x, y))
|
||||||
|
current_room:mousepressed(x, y, button)
|
||||||
|
end)
|
||||||
|
|
||||||
screen.draw = window.draw(function (self)
|
screen.draw = window.draw(function (self)
|
||||||
current_room:draw()
|
current_room:draw()
|
||||||
|
@ -26,34 +28,80 @@ inventory = {
|
||||||
items = {}
|
items = {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inventory.mousepressed = window.propagate{'items'}
|
||||||
|
|
||||||
inventory.draw = window.draw(function (self)
|
inventory.draw = window.draw(function (self)
|
||||||
for _, item in ipairs(self.items) do
|
local x = 0
|
||||||
-- item:setpos(x, y)
|
for _, item in pairs(self.items) do
|
||||||
|
item:setpos(x, 0)
|
||||||
item:draw()
|
item:draw()
|
||||||
|
x = item.width
|
||||||
end
|
end
|
||||||
window.draw_border(self.width, self.height)
|
window.draw_border(self.width, self.height)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
function inventory:pickup(item)
|
||||||
|
self.items[item.name] = item
|
||||||
|
end
|
||||||
|
|
||||||
log = {
|
log = {
|
||||||
x = inventory.width , y = 0,
|
x = inventory.width , y = 0,
|
||||||
width = 80,
|
width = WIDTH - screen.width,
|
||||||
height = HEIGHT,
|
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)
|
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)
|
window.draw_border(self.width, self.height)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
function love.load()
|
function log:log(str)
|
||||||
timer = 0
|
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
|
||||||
|
|
||||||
|
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
|
end
|
||||||
|
|
||||||
function love.mousepressed(x, y, button)
|
function log:info(o)
|
||||||
local x, y = x / MULTIPLIER, y / MULTIPLIER
|
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
|
end
|
||||||
|
|
||||||
function love.update(dt)
|
function love.update(dt)
|
||||||
timer = timer + dt
|
|
||||||
end
|
end
|
||||||
|
|
||||||
mw, mh = 0, 0
|
mw, mh = 0, 0
|
||||||
|
@ -62,6 +110,12 @@ if love.system.getOS() == 'android' then
|
||||||
mh = (love.window.getHeight() - HEIGHT) / 2
|
mh = (love.window.getHeight() - HEIGHT) / 2
|
||||||
end
|
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()
|
function love.draw()
|
||||||
love.graphics.push()
|
love.graphics.push()
|
||||||
love.graphics.translate(mw, mh)
|
love.graphics.translate(mw, mh)
|
||||||
|
|
16
object.lua
Normal file
16
object.lua
Normal 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
|
14
room.lua
14
room.lua
|
@ -5,6 +5,7 @@ local window = require('window')
|
||||||
-- - objects
|
-- - objects
|
||||||
-- - edges
|
-- - edges
|
||||||
local room = {
|
local room = {
|
||||||
|
x = 0, y = 0,
|
||||||
background = love.graphics.newImage("res/defaultbg.png"),
|
background = love.graphics.newImage("res/defaultbg.png"),
|
||||||
edges = {},
|
edges = {},
|
||||||
objects = {},
|
objects = {},
|
||||||
|
@ -33,6 +34,19 @@ function room:new()
|
||||||
return m
|
return m
|
||||||
end
|
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'}
|
room.mousepressed = window.propagate{'edges', 'objects'}
|
||||||
|
|
||||||
function room:draw()
|
function room:draw()
|
||||||
|
|
|
@ -4,4 +4,10 @@ local r = room:new()
|
||||||
|
|
||||||
r.background = love.graphics.newImage("res/defaultbg.png")
|
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
|
return r
|
||||||
|
|
|
@ -23,14 +23,11 @@ end
|
||||||
|
|
||||||
function w.propagate(tnames)
|
function w.propagate(tnames)
|
||||||
return function(self, x, y, button)
|
return function(self, x, y, button)
|
||||||
|
local x, y = x - self.x, y - self.y
|
||||||
|
|
||||||
for _, tname in ipairs(tnames) do
|
for _, tname in ipairs(tnames) do
|
||||||
for i, v in pairs(self[tname]) do
|
for i, v in pairs(self[tname]) do
|
||||||
local x, y = x - v.x, y - v.y
|
v:mousepressed(x, y, button)
|
||||||
|
|
||||||
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
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Reference in a new issue