Mothback/code/spawn.lua
2022-03-11 15:44:52 +01:00

189 lines
4.9 KiB
Lua

LoadedObjects.Spawns = {}
function addSpawn(archetype, ...)
local o = {
archetype = archetype,
args = {...}
}
table.insert(LoadedObjects.Spawns, o)
end
function activateSpawns()
for _, spawn in pairs(LoadedObjects.Spawns) do
spawn.archetype:new(unpack(spawn.args))
end
end
function deselectSpawns()
for _, spawn in pairs(LoadedObjects.Spawns) do
spawn.selected = nil
end
end
function selectSpawns(x,y)
for _, spawn in pairs(LoadedObjects.Spawns) do
local offset_x, offset_y = spawn.archetype.display:getCenteredOffset()
local left = spawn.args[1] - Camera.pos.x - offset_x
local top = spawn.args[2] - Camera.pos.y - offset_y
local right = spawn.args[1] - Camera.pos.x + offset_x
local bottom = spawn.args[2] - Camera.pos.y + offset_y
local x = (x / game.scale)
local y = (y / game.scale)
if x >= left and y >= top and x <= right and y <= bottom then
spawn.selected = true
end
if spawn.selected then
love.graphics.setColor(0,1,1,1)
else
love.graphics.setColor(0,1,0,1)
end
love.graphics.rectangle("fill",left-2,top-2,4,4)
love.graphics.rectangle("fill",right-2,bottom-2,4,4)
love.graphics.setColor(1,1,1,1)
end
end
function moveSpawns(x,y)
for _, spawn in pairs(LoadedObjects.Spawns) do
if spawn.selected then
spawn.args[1] = math.floor(x/game.scale)+Camera.pos.x
spawn.args[2] = math.floor(y/game.scale)+Camera.pos.y
end
end
end
function promptSpawnArchetype()
if Prompt.active_prompt then
Prompt.active_prompt.canceled = true
end
for _, spawn in pairs(LoadedObjects.Spawns) do
if spawn.selected then
local offset_x, offset_y = spawn.archetype.display:getCenteredOffset()
local text = ""
for i=1, #spawn.args do
if i > 1 then text = text .. ", " end
text = text .. tostring(spawn.args[i])
end
local prompt = Prompt:new({
name = "archetype",
input = spawn.archetype.type,
spawn = spawn,
func = function(prompt)
print("return "..prompt.input)
local f = loadstring("return "..prompt.input)
if f ~= nil then
local succ, arch = pcall(f)
print("archetype changed --",succ)
print("from: ", spawn.archetype.type)
if not succ
or type(arch) ~= "table"
or type(arch.type) ~= "string"
then
arch = spawn.archetype
end
print("to: ", arch.type)
spawn.archetype = arch
else
print("invalid input for prompt "..prompt.name)
end
end,
})
prompt.pos.x = (spawn.args[1]-4)*game.scale - Camera.pos.x - offset_x
prompt.pos.y = (spawn.args[2]-20)*game.scale - Camera.pos.y - offset_y
prompt:activate()
end
end
end
function checkArgsAreInvalid(args)
for _, arg in pairs(args) do
if arg == nil then return true end
end
end
function promptSpawnArgs()
if Prompt.active_prompt then
Prompt.active_prompt.canceled = true
end
for _, spawn in pairs(LoadedObjects.Spawns) do
if spawn.selected then
local offset_x, offset_y = spawn.archetype.display:getCenteredOffset()
local text = ""
for i=1, #spawn.args do
if i > 1 then text = text .. ", " end
text = text .. tostring(spawn.args[i])
end
local prompt = Prompt:new({
name = "args",
input = text,
func = function(prompt)
local f = loadstring("return {"..prompt.input.."}")
if f ~= nil then
local succ, args = pcall(f)
print("args changed --",succ)
print("from: ", unpack(args))
if not succ
or checkArgsAreInvalid(args)
then
args = spawn.args
end
print("to: ", unpack(args))
spawn.args = args
else
print("invalid input for prompt "..prompt.name)
end
end,
})
prompt.pos.x = (spawn.args[1]-4)*game.scale - Camera.pos.x - offset_x
prompt.pos.y = (spawn.args[2]-4)*game.scale - Camera.pos.y - offset_y
prompt:activate()
end
end
end
function drawSpawns()
for _, spawn in pairs(LoadedObjects.Spawns) do
local offset_x, offset_y = spawn.archetype.display:getCenteredOffset()
love.graphics.setColor(1,1,1,1)
spawn.archetype.display:draw(
spawn.args[1] - Camera.pos.x - offset_x,
spawn.args[2] - Camera.pos.y - offset_y
)
if spawn.selected then
love.graphics.setColor(0,1,1,1)
else
love.graphics.setColor(0,1,0,1)
end
love.graphics.rectangle(
"line",
spawn.args[1] - Camera.pos.x - offset_x,
spawn.args[2] - Camera.pos.y - offset_y,
spawn.args[1] - Camera.pos.x + offset_x - (spawn.args[1] - Camera.pos.x - offset_x),
spawn.args[2] - Camera.pos.y + offset_y - (spawn.args[2] - Camera.pos.y - offset_y)
)
if spawn.selected then
local text = spawn.archetype.type.."\n---\nPosition\n["..spawn.args[1]..","..spawn.args[2].."]"
if #spawn.args > 2 then
text = text .. "\n---\nData:\n"
for i=3, #spawn.args do
if i > 3 then text = text .. ", " end
text = text .. tostring(spawn.args[i])
end
end
drawTextBox(
text,
spawn.args[1] - Camera.pos.x + 20,
spawn.args[2] - Camera.pos.y
)
end
end
end