From 39b65571a0b3836689b45910c240cdb2ad812a02 Mon Sep 17 00:00:00 2001 From: lustlion Date: Thu, 10 Mar 2022 10:21:10 +0100 Subject: [PATCH] more editor work on handling entity spawns --- code/animation.lua | 4 +++ code/editor.lua | 20 ++++++----- code/entities/cursed_book.lua | 1 + code/entities/decoration.lua | 1 + code/entities/fairy.lua | 1 + code/entities/hook_anchor.lua | 1 + code/entities/kupo.lua | 1 + code/entities/player.lua | 1 + code/keybind.lua | 1 + code/spawn.lua | 68 +++++++++++++++++++++++++++++++++++ 10 files changed, 91 insertions(+), 8 deletions(-) diff --git a/code/animation.lua b/code/animation.lua index 54d1cf8..4bbe39c 100644 --- a/code/animation.lua +++ b/code/animation.lua @@ -15,6 +15,10 @@ function Animation:new(anim_data,speed) return o end +function Animation:getCenteredOffset() + return self.imgs[1]:getWidth()/2, self.imgs[1]:getHeight()/2 +end + function Animation:change(anim_data) if anim_data.path == self.path then diff --git a/code/editor.lua b/code/editor.lua index 63334d5..6de1452 100644 --- a/code/editor.lua +++ b/code/editor.lua @@ -139,6 +139,7 @@ function drawEditor() drawGameworldForeground() endGameworldDraw() doEditorEdit() + drawSpawns() drawEditorRooms() drawSelectingPaletteTile() @@ -206,15 +207,18 @@ function doEditorEdit() and love.keyboard.isDown("lshift") ~= true and love.keyboard.isDown("lctrl") ~= true then - if Keybind:checkDown(Keybind.generic.lclick) - and selecting_tile ~= nil - then - setTile(vertical,horizontal,selecting_tile) - elseif Keybind:checkDown(Keybind.generic.rclick) then - setTile(vertical,horizontal,0) + if selecting_tile ~= nil then + if Keybind:checkDown(Keybind.generic.lclick) then + setTile(vertical,horizontal,selecting_tile) + elseif Keybind:checkDown(Keybind.generic.rclick) then + setTile(vertical,horizontal,0) + end + reloadLevelTiles() + else + if Keybind:checkDown(Keybind.generic.lclick) then + selectSpawns(mouse_x,mouse_y) + end end - reloadLevelTiles() - elseif Keybind:checkPressed(Keybind.generic.lshift) then expandLevelCanvas(math.sign(expand_h),math.sign(expand_v)) reloadLevelTiles() diff --git a/code/entities/cursed_book.lua b/code/entities/cursed_book.lua index a0f3527..b4494ef 100644 --- a/code/entities/cursed_book.lua +++ b/code/entities/cursed_book.lua @@ -1,5 +1,6 @@ CursedBook = Entity:new() CursedBook.type = "CursedBook" +CursedBook.display = Animation:new(animation.cursed_book.flying) function CursedBook:new(x,y) local o = Entity:new(x,y) diff --git a/code/entities/decoration.lua b/code/entities/decoration.lua index d0f79ad..8f6bb53 100644 --- a/code/entities/decoration.lua +++ b/code/entities/decoration.lua @@ -1,5 +1,6 @@ Decoration = Entity:new() Decoration.type = "Decoration" +Decoration.display = nil function Decoration:new(x,y,animation,light_radius) local o = Entity:new(x,y) diff --git a/code/entities/fairy.lua b/code/entities/fairy.lua index 07fe733..6ccd688 100644 --- a/code/entities/fairy.lua +++ b/code/entities/fairy.lua @@ -1,5 +1,6 @@ Fairy = Entity:new() Fairy.type = "Fairy" +Fairy.display = Animation:new(animation.fairy.flying) function Fairy:new(x,y) local o = Entity:new(x,y) diff --git a/code/entities/hook_anchor.lua b/code/entities/hook_anchor.lua index 73fcc47..a5f9377 100644 --- a/code/entities/hook_anchor.lua +++ b/code/entities/hook_anchor.lua @@ -1,5 +1,6 @@ HookAnchor = Entity:new() HookAnchor.type = "HookAnchor" +HookAnchor.display = Animation:new(animation.fairy.flying) function HookAnchor:new(x,y,hook_distance) local o = Entity:new(x,y) diff --git a/code/entities/kupo.lua b/code/entities/kupo.lua index ec91591..82d3da9 100644 --- a/code/entities/kupo.lua +++ b/code/entities/kupo.lua @@ -1,5 +1,6 @@ Kupo = Entity:new() Kupo.type = "Kupo" +Kupo.display = Animation:new(animation.kupo.body) function Kupo:new(x,y) local o = Entity:new(x,y) diff --git a/code/entities/player.lua b/code/entities/player.lua index 87acd49..be6ee03 100644 --- a/code/entities/player.lua +++ b/code/entities/player.lua @@ -1,5 +1,6 @@ Player = Entity:new() Player.type = "Player" +Player.display = Animation:new(animation.nancy.idle) function Player:new(x,y) local o = Entity:new(x,y) diff --git a/code/keybind.lua b/code/keybind.lua index c98e169..21bf7fb 100644 --- a/code/keybind.lua +++ b/code/keybind.lua @@ -128,6 +128,7 @@ function Keybind:default() Keybind.generic.lclick = { keys = {1}} Keybind.generic.rclick = { keys = {2}} Keybind.generic.lshift = { keys = {"lshift"}} + Keybind.generic.alt = { keys = {"alt"}} Keybind.generic.lctrl = { keys = {"lctrl"}} end diff --git a/code/spawn.lua b/code/spawn.lua index f964e03..196f4be 100644 --- a/code/spawn.lua +++ b/code/spawn.lua @@ -13,3 +13,71 @@ function activateSpawns() spawn.archetype:new(unpack(spawn.args)) end end + +function selectSpawns(x,y) + for _, spawn in pairs(LoadedObjects.Spawns) do + spawn.selected = nil + 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 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 = "" + for i=1, #spawn.args do + text = text .. tostring(spawn.args[i])..", " + end + love.graphics.print( + spawn.archetype.type, + spawn.args[1] - Camera.pos.x + 20, + spawn.args[2] - Camera.pos.y + ) + love.graphics.print( + text, + spawn.args[1] - Camera.pos.x + 20, + spawn.args[2] - Camera.pos.y + 20 + ) + end + end +end