diff --git a/game/main.lua b/game/main.lua index 96def3f..53bfae1 100644 --- a/game/main.lua +++ b/game/main.lua @@ -1,9 +1,15 @@ local world = { - require("src.world.race")(), - require("src.world.train")() + ["main_menu"] = require("src.world.main_menu")(), + ["race"] = require("src.world.race")(), + ["train"] = require("src.world.train")(), }; -local world_i = 1 +local world_i = "main_menu" + +function load_world(world_to_load) + world_i = world_to_load + world[world_i]:reload() +end function love.load() world[world_i]:load() @@ -18,13 +24,6 @@ function love.draw() end function love.keyreleased(key, scancode) - if (key == "right") then - world_i = world_i + 1 - if (world_i > 2) then - world_i = 1 - end - world[world_i]:reload() - end world[world_i]:keyreleased(key, scancode) end diff --git a/game/src/world/common/component/button.lua b/game/src/world/common/component/button.lua new file mode 100644 index 0000000..e299f6d --- /dev/null +++ b/game/src/world/common/component/button.lua @@ -0,0 +1,30 @@ +local components = {} + +components.dict = { + collider = "button.collider", + func = "button.func", + label = "button.label" +} + +function components.collider (c, x, y, w, h) + c.data = { + x = x or 0, + y = y or 0, + w = w or 0, + h = h or 0 + } +end + +function default() + print("click") +end + +function components.func (c, func) + c.data = func or default +end + +function components.label (c, label) + c.data = label or "" +end + +return components diff --git a/game/src/world/common/system/trigger_button.lua b/game/src/world/common/system/trigger_button.lua new file mode 100644 index 0000000..a90ccd0 --- /dev/null +++ b/game/src/world/common/system/trigger_button.lua @@ -0,0 +1,81 @@ +local system_constructor = require("wrapper.Concord.system") +local c_button = require("src.world.common.component.button") + +local system = {} + +system.__index = system + +system.pool = { + pool = { + c_button.dict.collider, + c_button.dict.func, + c_button.dict.label + } +} + +system.components = { + [c_button.dict.collider] = c_button.collider, + [c_button.dict.func] = c_button.func, + [c_button.dict.label] = c_button.label +} + +function system.new() + local new_system = system_constructor.new("trigger_button", system.pool) + if (new_system) then + for k, v in pairs(system) do + new_system[k] = v + end + return new_system + else + return nil + end +end + +-- x1 y1 -- (x1 + w1) y1 +-- x2 y2 +-- x1 (y1 + h1) -- (x1 + w1) (y1 + h1) +local function is_inside(x1,y1,w1,h1, x2,y2) + return x1 < x2 and + x2 < x1+w1 and + y1 < y2 and + y2 < y1+h1 +end + +function system:mousereleased(x, y, button, istouch, presses) + for _, e in ipairs(self.pool) do + local x1 = e[c_button.dict.collider].data.x + local y1 = e[c_button.dict.collider].data.y + local w1 = e[c_button.dict.collider].data.w + local h1 = e[c_button.dict.collider].data.h + local func = e[c_button.dict.func].data + if (is_inside(x1, y1, w1, h1, x, y) and button == 1) then + func() + end + end +end + +local function draw(text, x, y, w, h) + love.graphics.push() + love.graphics.line( + x, y, + x + w, y, + x + w, y + h, + x, y + h, + x, y + ) + love.graphics.print(text, x, y) + love.graphics.pop() +end + +function system:draw() + for _, e in ipairs(self.pool) do + local x = e[c_button.dict.collider].data.x + local y = e[c_button.dict.collider].data.y + local w = e[c_button.dict.collider].data.w + local h = e[c_button.dict.collider].data.h + local label = e[c_button.dict.label].data + draw(label, x, y, w, h) + end +end + +return system diff --git a/game/src/world/common/template/button.lua b/game/src/world/common/template/button.lua new file mode 100644 index 0000000..8c19399 --- /dev/null +++ b/game/src/world/common/template/button.lua @@ -0,0 +1,24 @@ +local button = require("src.world.common.component.button") + +local template = {} + +local function default() +end + +template.default_data = { + collider = { + x = 0, + y = 0, + w = 20, + h = 20 + }, + func = default, + label = "debug" +} +function template.assemble(e, data) + e:give(button.dict.collider, data.collider.x, data.collider.y, data.collider.w, data.collider.h) + e:give(button.dict.func, data.func) + e:give(button.dict.label, data.label) +end + +return template diff --git a/game/src/world/main_menu/init.lua b/game/src/world/main_menu/init.lua new file mode 100644 index 0000000..392863c --- /dev/null +++ b/game/src/world/main_menu/init.lua @@ -0,0 +1,47 @@ +local reap = require("lib.reap") + +local BASE = reap.base_path(...) + +local world = require("wrapper.Concord.world") + +local debug_entity = require("src.world.common.template.debug_entity") +local button = require("src.world.common.template.button") + +local wrapper = world:extend() + +local function button_func() + load_world("race") +end + +function wrapper:new() + wrapper.super.new(self, BASE, ".main_menu") +end + +function wrapper:load(_args) + wrapper.super.load(self, { + "src/world/common/system/" + }, { + { + assemblage = debug_entity.assembleDebug, + data = { + position = {0, 0}, + label = "main_menu" + } + }, + { + assemblage = button.assemble, + data = { + collider = { + x = 20, + y = 20, + w = 20, + h = 20 + }, + func = button_func, + label = "race" + } + } + }) +end + +return wrapper