From 7595ed3b90b69a0d7160808a72ec01fded4d3d66 Mon Sep 17 00:00:00 2001 From: lustlion Date: Thu, 3 Feb 2022 05:31:25 +0100 Subject: [PATCH] Rehandled menus; reworked dialog, added dialogsequences instead of dialogboxes --- data/dialog_sequences.lua | 7 ++ data/{scripts => }/enums.lua | 0 data/locale/ENG.lua | 14 +++- data/locale/HEON.lua | 17 +++- data/particle.lua | 100 ++++++++++++++++++++++ data/scripts.lua | 11 +-- data/scripts/game.lua | 10 +-- data/scripts/locale.lua | 1 + data/scripts/menu.lua | 143 ++++++++++++++++++++++++++++++++ data/scripts/ui.lua | 127 ++-------------------------- data/scripts/ui/button.lua | 11 +-- data/scripts/ui/dialog.lua | 46 +++++----- data/scripts/ui/dialogboxes.lua | 9 -- main.lua | 13 ++- 14 files changed, 324 insertions(+), 185 deletions(-) create mode 100644 data/dialog_sequences.lua rename data/{scripts => }/enums.lua (100%) create mode 100644 data/particle.lua create mode 100644 data/scripts/menu.lua delete mode 100644 data/scripts/ui/dialogboxes.lua diff --git a/data/dialog_sequences.lua b/data/dialog_sequences.lua new file mode 100644 index 0000000..7106015 --- /dev/null +++ b/data/dialog_sequences.lua @@ -0,0 +1,7 @@ +DialogSequence = { + Example = { + {Locale.character.fairy.name,Locale.dialogue.example[1]}, + {Locale.character.chaos.name,Locale.dialogue.example[2]}, + {Locale.character.life.name,Locale.dialogue.example[3]} + } +} diff --git a/data/scripts/enums.lua b/data/enums.lua similarity index 100% rename from data/scripts/enums.lua rename to data/enums.lua diff --git a/data/locale/ENG.lua b/data/locale/ENG.lua index 8b5e23f..d5819ee 100644 --- a/data/locale/ENG.lua +++ b/data/locale/ENG.lua @@ -5,6 +5,14 @@ Locale.ui.pause_screen_resume = "Resume" Locale.ui.pause_screen_options = "Options" Locale.ui.pause_screen_exit = "Exit" -Locale.entity = {} -Locale.entity.fairy = {} -Locale.entity.fairy.name = "Ozy" +Locale.character = {} +Locale.character.fairy = {} +Locale.character.fairy.name = "Ozy" +Locale.character.chaos = {} +Locale.character.chaos.name = "Aelato" +Locale.character.life = {} +Locale.character.life.name = "Olidia" + + +Locale.dialogue = {} +Locale.dialogue.example = {"Hello!","Duh.","Lol"} diff --git a/data/locale/HEON.lua b/data/locale/HEON.lua index abd320d..03f647a 100644 --- a/data/locale/HEON.lua +++ b/data/locale/HEON.lua @@ -6,7 +6,18 @@ Locale.ui = {} Locale.ui.pause_screen_resume = "" Locale.ui.pause_screen_options = "" Locale.ui.pause_screen_exit = "" +Locale.character = {} +Locale.character.fairy = {} +Locale.character.fairy.name = "" +Locale.character.chaos = {} +Locale.character.chaos.name = "" +Locale.character.life = {} +Locale.character.life.name = "" -Locale.entity = {} -Locale.entity.fairy = {} -Locale.entity.fairy.name = "" + +Locale.dialogue = {} +Locale.dialogue.example = { + "", + "", + "" +} diff --git a/data/particle.lua b/data/particle.lua new file mode 100644 index 0000000..4784417 --- /dev/null +++ b/data/particle.lua @@ -0,0 +1,100 @@ +Particle = Entity:New(x,y) + + function Particle:New(x,y,particle_data) + local o = Entity:New(x,y) + + o.pos = {x = x, y = y} + + + o.speed = particle_data.speed or 0 + o.direction = particle_data.direction or o.direction + o.sprite_rotation = particle_data.sprite_rotation or o.sprite_rotation + o.sprite_offset = particle_data.sprite_offset or o.sprite_offset + o.sprite_scale = particle_data.sprite_scale or o.sprite_scale + o.sprite_tint = particle_data.sprite_tint or o.sprite_tint + o.sprite_alpha = particle_data.sprite_alpha or o.sprite_alpha + o.sprite_alpha_base = o.sprite_alpha + + o.sprite_flip = particle_data.sprite_flip or o.sprite_flip + o.animation_active = particle_data.animation_active or false + + o.time = 0.5 + o.timer = 0 + + o.vel = { + x = o.speed * math.cos(o.direction), + y = o.speed * math.sin(o.direction) + } + + o.speed_increase = particle_data.speed_increase or 0 + + if particle_data.light ~= nil then + o.lightRange = particle_data.light + local flicker = particle_data.light_flicker or nil + local color = particle_data.light_color or nil + o.light = CreateLight(o.pos.x,o.pos.y,o.lightRange,flicker,color) + end + + -- animations + if particle_data.animation ~= nil then + o.body = Animation:New(particle_data.animation) + o:centerOffset(o.body) + o:getBoundingBox(o.body) + if not o.animation_active then + o.body.speed = 0 + end + end + + table.insert(LoadedParticles,o) + o.id = #LoadedParticles + + setmetatable(o, self) + self.__index = self + return o + end + +function Particle:Kill() + if self.light ~= nil then + KillLight(self.light) + end + if self.id ~= nil then + for _, e in pairs(LoadedParticles) do + if e.id > self.id then + e.id = e.id - 1 + end + end + table.remove(LoadedParticles,self.id) + end + self = nil +end + +function Particle:HandleAnimation() + self.timer = self.timer + current_dt + self.sprite_alpha = self.sprite_alpha_base*(self.time-self.timer)/self.time + if self.light ~= nil then + self:LightAdjust() + self.light.range = self.lightRange * self.sprite_alpha/2 + end + if self.sprite_alpha < 0 then self:Kill() end + if self.body ~= nil then + self.body:Animate() + self:Draw(self.body) + end +end + +function Particle:DoPhysics() + -- adjust speed + if self.speed_increase ~= 0 then + self.speed = self.speed + self.speed_increase + self.vel.x = self.speed * math.cos(self.direction) + self.vel.y = self.speed * math.sin(self.direction) + end + -- move + self:CollisionMove() +end + +function Particle:Debug() + -- draw center CYAN + love.graphics.setColor(0,1,1) + love.graphics.circle("fill", -Camera.pos.x + self.pos.x, -Camera.pos.y + self.pos.y, 1) +end diff --git a/data/scripts.lua b/data/scripts.lua index ecc07e7..4671cb1 100644 --- a/data/scripts.lua +++ b/data/scripts.lua @@ -1,6 +1,6 @@ --- enums -require "data/scripts/enums" - +-- data +require "data/enums" +require "data/scripts/locale" -- support functions require "data/scripts/math" require "data/scripts/hex" @@ -10,18 +10,15 @@ require "data/scripts/entity" require "data/scripts/animation" require "data/scripts/collision" require "data/scripts/level" --- data require "data/scripts/camera" require "data/scripts/lights" require "data/scripts/objects" -- UI functions require "data/scripts/debug" require "data/scripts/keybind" +require "data/scripts/menu" require "data/scripts/ui" -- game loop require "data/scripts/game" require "data/scripts/gameworld" require "data/scripts/editor" - --- locale -require "data/scripts/locale" diff --git a/data/scripts/game.lua b/data/scripts/game.lua index ea57f26..5c60149 100644 --- a/data/scripts/game.lua +++ b/data/scripts/game.lua @@ -1,6 +1,5 @@ function GameStep() - if not do_pause then - SetCollisionFlags(main_Player) + if menu_type == "no" then for _, particle in pairs(LoadedParticles) do particle:Smart() particle:DoPhysics() @@ -32,10 +31,9 @@ function GameStep() end if Keybind:HasPressed(Keybind.debug.reload) then - --LoadLevel() - - menuPage = "example" - MenuInit(menuPage) + MenuClear() + menu_type = "dialog" + MenuInit("dialog",DialogSequence.Example) end if Keybind:HasPressed(Keybind.debug.editor) then diff --git a/data/scripts/locale.lua b/data/scripts/locale.lua index 8d538f6..9eaea70 100644 --- a/data/scripts/locale.lua +++ b/data/scripts/locale.lua @@ -1,4 +1,5 @@ function LocaleLoad(ISO639) local ISO639 = ISO639 or "ENG" dofile("Mothback/data/locale/"..ISO639..".lua") + dofile("Mothback/data/dialog_sequences.lua") end diff --git a/data/scripts/menu.lua b/data/scripts/menu.lua new file mode 100644 index 0000000..a40ac77 --- /dev/null +++ b/data/scripts/menu.lua @@ -0,0 +1,143 @@ +function MenuDraw(menu) + -- Set scale to 1 + love.graphics.scale(0.5,0.5) + if menu == "pause" then + MenuDrawPauseScreen() + elseif menu == "dialog" then + MenuDrawDialog() + end + + for _, element in pairs(UIElement) do + element:Draw() + end + -- Reset scale + love.graphics.scale(2,2) +end + +function MenuDrawPauseScreen() + -- Parameters + local pauseWidth = 640 + local pauseHeight = 480 + local pauseX = (game.width/2)-(pauseWidth/2) + local pauseY = (game.height/2)-(pauseHeight/2) + local mouse_x, mouse_y = love.mouse.getPosition() + -- Base items + love.graphics.setColor(0,0,0,0.3) + love.graphics.rectangle("fill", 0, 0, game.width, game.height) + love.graphics.setColor(1,1,1,1) + love.graphics.rectangle("fill", pauseX, pauseY, pauseWidth, pauseHeight) +end + +function MenuDrawDialog() +end + +function MenuStep(menu) + -- first get mouse + local mouse_x, mouse_y = love.mouse.getPosition() + for _, element in pairs(UIElement) do + if element.type == "Button" then + element:checkMouse(mouse_x, mouse_y) + elseif element.type == "Dialog" then + element:checkConfirm() + end + end + if menu == 0 then + elseif menu == "pause" then + MenuStepPauseScreen() + elseif menu == "dialog" then + MenuStepDialog() + end +end + +function MenuStepPauseScreen() + if PauseResume:getVariable() == true then + PauseResume = nil + PauseOptions = nil + PauseExit = nil + MenuExit() + elseif PauseExit:getVariable() == true then + love.event.quit() + end +end + +function MenuStepDialog() + if DialogContainer.value >= DialogContainer.target_value then + DialogContainer = nil + MenuExit() + end +end + +function MenuClear() + for _, element in pairs(UIElement) do + element = nil + end + UIElement = {} +end + +function MenuExit(to) + MenuClear() + local to = to or "no" + menu_type = to +end + +function MenuInit(menu,parameter) + -- main menu + if menu == "pause" then + MenuInitPauseScreen() + elseif menu == "dialog" then + if parameter == nil then + print("WARNING -- what dialog?") + parameter = DialogSequence.Example + end + MenuInitDialog(parameter) + end +end + +function MenuInitDialog(parameter) + DialogContainer = interfaceDialog:New() + DialogContainer:loadSequence(parameter) +end + +function MenuInitPauseScreen() + local buttonStandard = {width = 200, height = 30, separation = 10} + -- elements + PauseResume = interfaceButton:New( + game.width/2, + game.height/2-buttonStandard.height-buttonStandard.separation, + buttonStandard.width, + buttonStandard.height, + {false,true}, + 1, + { + text = Locale.ui.pause_screen_resume, + color = {0,0,0.5}, + color2 = {1,1,1} + } + ) + PauseOptions = interfaceButton:New( + game.width/2, + game.height/2, + buttonStandard.width, + buttonStandard.height, + {false,true}, + 1, + { + text = Locale.ui.pause_screen_options, + color = {0,0,0.5}, + color2 = {1,1,1} + } + ) + PauseExit = interfaceButton:New( + game.width/2, + game.height/2+buttonStandard.height+buttonStandard.separation, + buttonStandard.width, + buttonStandard.height, + {false,true}, + 1, + { + text = Locale.ui.pause_screen_exit, + color = {0,0,0.5}, + color2 = {1,1,1} + } + ) +end diff --git a/data/scripts/ui.lua b/data/scripts/ui.lua index 0f2c9e3..5ba5049 100644 --- a/data/scripts/ui.lua +++ b/data/scripts/ui.lua @@ -1,124 +1,9 @@ -function MenuDraw(menu) - -- Set scale to 1 - love.graphics.scale(0.5,0.5) - if menu == "pauseMenu" then - -- Parameters - local pauseWidth = 640 - local pauseHeight = 480 - local pauseX = (game.width/2)-(pauseWidth/2) - local pauseY = (game.height/2)-(pauseHeight/2) - local mouse_x, mouse_y = love.mouse.getPosition() - -- Base items - love.graphics.setColor(0,0,0,0.3) - love.graphics.rectangle("fill", 0, 0, game.width, game.height) - love.graphics.setColor(1,1,1,1) - love.graphics.rectangle("fill", pauseX, pauseY, pauseWidth, pauseHeight) - -- Reset scale - end - for _, element in pairs(UIElement) do - element:Draw() - end - love.graphics.scale(2,2) -end - -function MenuStep(menu) - -- first get mouse - local mouse_x, mouse_y = love.mouse.getPosition() - for _, element in pairs(UIElement) do - if element.type == "Button" then - element:checkMouse(mouse_x, mouse_y) - elseif element.type == "Dialog" then - element:checkConfirm() - end - end - if menu == 0 then - elseif menu == "pauseMenu" then - if PauseResume:getVariable() == true then - MenuExit(menu,0) - do_pause = false - elseif PauseExit:getVariable() == true then - love.event.quit() - end - else - for name, dialogbox in pairs(dialogboxes) do - if name == menu then - if dialogbox.object:getVariable() then - MenuExit(dialogbox.exit[1],dialogbox.exit[2]) - end - end - end - end -end - -function MenuExit(from,to) - for _, element in pairs(UIElement) do - element = nil - end - UIElement = {} - if from == "pauseMenu" then - PauseResume = nil - PauseOptions = nil - PauseExit = nil - else - for name, dialogbox in pairs(dialogboxes) do - if name == menu then dialogbox.object = nil end - end - end - menuPage = to or nil -end - -function MenuInit(menu) - local buttonStandard = {width = 200, height = 30, separation = 10} - -- main menu - if menu == "pauseMenu" then - -- elements - PauseResume = interfaceButton:New( - game.width/2, - game.height/2-buttonStandard.height-buttonStandard.separation, - buttonStandard.width, - buttonStandard.height, - {false,true}, - 1, - { - text = Locale.ui.pause_screen_resume, - color = {0,0,0.5}, - color2 = {1,1,1} - } - ) - PauseOptions = interfaceButton:New( - game.width/2, - game.height/2, - buttonStandard.width, - buttonStandard.height, - {false,true}, - 1, - { - text = Locale.ui.pause_screen_options, - color = {0,0,0.5}, - color2 = {1,1,1} - } - ) - PauseExit = interfaceButton:New( - game.width/2, - game.height/2+buttonStandard.height+buttonStandard.separation, - buttonStandard.width, - buttonStandard.height, - {false,true}, - 1, - { - text = Locale.ui.pause_screen_exit, - color = {0,0,0.5}, - color2 = {1,1,1} - } - ) - else - for name, dialogbox in pairs(dialogboxes) do - if name == menu then dialogbox.object = interfaceDialog:New(dialogbox.style) end - end - end -end - UIElement = {} + +function AddElement(self) + table.insert(UIElement,self) + self.id = #UIElement +end + require "data/scripts/ui/button" require "data/scripts/ui/dialog" -require "data/scripts/ui/dialogboxes" diff --git a/data/scripts/ui/button.lua b/data/scripts/ui/button.lua index 8125ab8..7e04fd1 100644 --- a/data/scripts/ui/button.lua +++ b/data/scripts/ui/button.lua @@ -53,18 +53,13 @@ function interfaceButton:getVariable() return self.target_variable end -function AddElement(self) - table.insert(UIElement,self) - self.id = #UIElement -end - function interfaceButton:checkMouse(mouse_x, mouse_y) if not self.clicked and mouse_x < self.pos.x + self.size.w/2 and mouse_x > self.pos.x - self.size.w/2 and mouse_y < self.pos.y + self.size.h/2 and mouse_y > self.pos.y - self.size.h/2 then - o.style.scale_proportion = o.style.selected.scale_proportion + self.style.scale_proportion = o.style.selected.scale_proportion if love.mouse.isDown(1) then self.clicked = true self.value = self.value + 1 @@ -73,8 +68,8 @@ function interfaceButton:checkMouse(mouse_x, mouse_y) end self.target_variable = self.values[self.value] end - else - o.style.scale_proportion = o.style.unselected.scale_proportion + elseif not love.mouse.isDown(1) then + self.style.scale_proportion = o.style.unselected.scale_proportion self.clicked = false end end diff --git a/data/scripts/ui/dialog.lua b/data/scripts/ui/dialog.lua index 5a9af8b..ffdb252 100644 --- a/data/scripts/ui/dialog.lua +++ b/data/scripts/ui/dialog.lua @@ -1,6 +1,5 @@ interfaceDialog = {type = "Dialog"} - --- centered buttons +-- dialog boxes function interfaceDialog:New(style) o = {} @@ -14,11 +13,10 @@ function interfaceDialog:New(style) } - o.values = {false,true} - o.value = 1 - o.target_variable = o.values[o.value] + o.value = 0 + o.target_value = 0 - o.clicked = false + local style = {} o.style = { content = style.content or nil, @@ -40,26 +38,31 @@ function interfaceDialog:New(style) return o end -function interfaceDialog:getVariable() - return self.target_variable +function interfaceDialog:updateContents() + if self.value < self.target_value then + self.contents = self.sequence[self.value] + if self.contents[1] == nil then self.contents[1] = "" end + if self.contents[2] == nil then self.contents[2] = "" end + if self.contents[3] == nil then self.contents[3] = "" end + end end -function AddElement(self) - table.insert(UIElement,self) - self.id = #UIElement +function interfaceDialog:loadSequence(sequence) + self.sequence = sequence + self.value = 1 + self.target_value = 1+#sequence + self:updateContents() end function interfaceDialog:checkConfirm() if not self.clicked then - if Keybind:HasPressed(Keybind.menu.confirm) then + if love.mouse.isDown(1) then self.clicked = true self.value = self.value + 1 - if self.value > #self.values then - self.value = 1 - end - self.target_variable = self.values[self.value] + print(self.value.." of "..self.target_value) + self:updateContents() end - else + elseif not love.mouse.isDown(1) then self.clicked = false end end @@ -82,12 +85,13 @@ function interfaceDialog:Draw() self.size.w*self.style.scale_x*self.style.scale_proportion, self.size.h*self.style.scale_y*self.style.scale_proportion) - if self.style.content ~= nil then - love.graphics.printf(self.style.content[1],self.pos.x+10,self.pos.y+(self.size.h/2),100,"left") - love.graphics.printf(self.style.content[2],self.pos.x+(self.size.w/2),self.pos.y+(self.size.h/2),100,"center") - love.graphics.printf(self.style.content[3],self.pos.x+(self.size.w)-10,self.pos.y+(self.size.h/2),100,"right") + if self.contents ~= nil then + love.graphics.printf(self.contents[1],self.pos.x+10,self.pos.y+(self.size.h/2),100,"left") + love.graphics.printf(self.contents[2],self.pos.x+(self.size.w/2),self.pos.y+(self.size.h/2),100,"center") + love.graphics.printf(self.contents[3],self.pos.x+(self.size.w)-10,self.pos.y+(self.size.h/2),100,"right") else love.graphics.printf("ERROR",self.pos.x+(self.size.w/2),self.pos.y+(self.size.h/2),100,"center") end + love.graphics.setColor(c1,c2,c3,a) end diff --git a/data/scripts/ui/dialogboxes.lua b/data/scripts/ui/dialogboxes.lua deleted file mode 100644 index 3d22e3c..0000000 --- a/data/scripts/ui/dialogboxes.lua +++ /dev/null @@ -1,9 +0,0 @@ -dialogboxes = { - - example = { - object = nil, - style = {content = {"test", "tested", "lol"}}, - exit = {example, 0} - } - -} diff --git a/main.lua b/main.lua index 6be1228..b979e84 100644 --- a/main.lua +++ b/main.lua @@ -3,8 +3,7 @@ function love.load() if logging then print("love: "..collectgarbage("count").." kB") end arrow = 0 - do_pause = false - + menu_type = "no" debug = false debug_collision = false editor_mode = false @@ -44,7 +43,7 @@ function love.load() LoadedParticles = {} LevelLoadTiles() - language = "ENG" + language = "HEON" LocaleLoad(language) main_Player = Player:New(75,50) @@ -90,13 +89,13 @@ function love.update(dt) if do_pause then do_pause = false else - menuPage = "pauseMenu" - MenuInit(menuPage) + menu_type = "pause" + MenuInit(menu_type) end end --MenuStep - if menuPage ~= nil then MenuStep(menuPage) end + if menu_type ~= nil then MenuStep(menu_type) end --editor if editor_mode then @@ -128,7 +127,7 @@ function love.draw() GameDraw() end - if menuPage ~= nil then MenuDraw(menuPage) end + if menu_type ~= nil then MenuDraw(menu_type) end love.graphics.print(game.scale,10,40) end