From 816c6fee225ec529608526643235878eee1d12ca Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Thu, 31 Oct 2024 09:54:13 +0100 Subject: [PATCH] feat(exemples): port the plateformer exemple to the new world system --- examples/assets/sprites/monkey_lad.lua | 3 + .../scenes/gameplay/plateform/actors/coin.lua | 30 ++--- .../plateform/actors/hitboxes/player.lua | 13 -- .../scenes/gameplay/plateform/actors/init.lua | 16 --- .../gameplay/plateform/actors/player.lua | 127 +++++++----------- .../scenes/gameplay/plateform/actors/wall.lua | 8 -- examples/scenes/gameplay/plateform/assets.lua | 19 --- examples/scenes/gameplay/plateform/init.lua | 42 +----- examples/scenes/gameplay/plateform/pause.lua | 60 --------- 9 files changed, 77 insertions(+), 241 deletions(-) delete mode 100644 examples/scenes/gameplay/plateform/actors/hitboxes/player.lua delete mode 100644 examples/scenes/gameplay/plateform/actors/init.lua delete mode 100644 examples/scenes/gameplay/plateform/actors/wall.lua delete mode 100644 examples/scenes/gameplay/plateform/assets.lua delete mode 100644 examples/scenes/gameplay/plateform/pause.lua diff --git a/examples/assets/sprites/monkey_lad.lua b/examples/assets/sprites/monkey_lad.lua index 89bced3..2239951 100644 --- a/examples/assets/sprites/monkey_lad.lua +++ b/examples/assets/sprites/monkey_lad.lua @@ -48,6 +48,9 @@ return { loop = 1, speed = 0, pauseAtEnd = false, + areas = { + ["frame1"] = {{type = "punch", box = {position = {x = 16, y = 6}, dimensions = {w = 12, h = 12}}, isSolid = false}} + } }, } } diff --git a/examples/scenes/gameplay/plateform/actors/coin.lua b/examples/scenes/gameplay/plateform/actors/coin.lua index 35501a4..de36cda 100644 --- a/examples/scenes/gameplay/plateform/actors/coin.lua +++ b/examples/scenes/gameplay/plateform/actors/coin.lua @@ -1,15 +1,15 @@ -local Base = require "framework.scenes.world.actors.actor2D" -local Coin = Base:extend() - -function Coin:new(world, x, y) - Coin.super.new(self, world, "coin", x, y, 16, 16, false) - self:setSprite("coin") -end - -function Coin:takeCoin(other) - self.obj.GFX(self.world, self.x + 8, self.y + 8, "gfx.sparkle") - assets:playSFX("gameplay.collectcoin") - self:destroy( ) -end - -return Coin +return actor { + type = "coin", + dimensions = { w = 16, h = 16 }, + isSolid = false, + visuals = { + mode = "sprite", + assetName = "coin" + }, + onPlayerCollision = function (self, player) + self:destroy() + assets:playSFX("gameplay.collectcoin") + self.world:showGFX("gfx.sparkle", self.position) + player.coin = player.coin + 1 + end +} diff --git a/examples/scenes/gameplay/plateform/actors/hitboxes/player.lua b/examples/scenes/gameplay/plateform/actors/hitboxes/player.lua deleted file mode 100644 index bf1c63c..0000000 --- a/examples/scenes/gameplay/plateform/actors/hitboxes/player.lua +++ /dev/null @@ -1,13 +0,0 @@ -return { - ["idle"] = { - { - {"main", {0, 0, 16, 24}, true} - } - }, - ["punch"] = { - { - {"main", {0, 0, 16, 24}, true}, - {"punch", {16, 6, 12, 12}, false} - } - } -} diff --git a/examples/scenes/gameplay/plateform/actors/init.lua b/examples/scenes/gameplay/plateform/actors/init.lua deleted file mode 100644 index 7a7ff0c..0000000 --- a/examples/scenes/gameplay/plateform/actors/init.lua +++ /dev/null @@ -1,16 +0,0 @@ -local Obj = {} - --- On charge toutes les différentes types d'acteurs -local cwd = (...):gsub('%.init$', '') .. "." - -Obj.Player = require(cwd .. "player") -Obj.GFX = require("framework.scenes.world.actors.gfx2D") - -Obj.index = {} -Obj.index["player"] = require(cwd .. "player") -Obj.index["coin"] = require(cwd .. "coin") - -Obj.collisions = {} -Obj.collisions["wall"] = require(cwd .. "wall") - -return Obj diff --git a/examples/scenes/gameplay/plateform/actors/player.lua b/examples/scenes/gameplay/plateform/actors/player.lua index a51fa40..97c4229 100644 --- a/examples/scenes/gameplay/plateform/actors/player.lua +++ b/examples/scenes/gameplay/plateform/actors/player.lua @@ -1,83 +1,79 @@ -local Base = require "framework.scenes.world.actors.actor2D" -local Player = Base:extend() +local Player = actor { + type = "player", + dimensions = { w = 16, h = 24 }, + friction = { x = 480 * 3 }, + isSolid = true, + visuals = { + mode = "sprite", + assetName = "monkey_lad", + clone = true, + origin = { + x = 8, + y = 12 + }, + getHitboxes = true + }, + gravity = { axis = "y", value = 480 }, + drawHitboxes = true +} -function Player:new(world, x, y, id) - Player.super.new(self, world, "player", x, y, 16, 24, true) - self:setSprite("monkey_lad", true, 8, 12) - self:setGravity(480) - - self.isPunching = false - self.direction = 1 - self.startx, self.starty = self.x, self.y +function Player:onInit() self.isDead = false - + self.start = self.position:clone() + self.isPunching = false + self.direction = 1 + self.coin = 0 self.punchName = "" - self:setHitboxFile("scenes.gameplay.plateform.actors.hitboxes.player") end -function Player:updateStart(dt) - self.xfrc = 480*3 - - if self.keys["up"].isPressed and (self.onGround) then - self.ysp = -280 +function Player:update(dt) + if love.keyboard.isDown("up") and (self.onGround) then + self.speed.y = -280 assets:playSFX("gameplay.jump") end - if self.keys["down"].isDown then - self.mainHitbox:modify(0, 8, 16, 16) + if love.keyboard.isDown("down") then + self.mainHitbox:modify({x = 0, y = 8}, {w = 16, h = 16}) else - self.mainHitbox:modify(0, 0, 16, 24) + self.mainHitbox:modify({x = 0, y = 0}, {w = 16, h = 24}) end - if self.keys["left"].isDown and (not self.isPunching) then - self.xsp = -120 + if love.keyboard.isDown("left") and (not self.isPunching) then + self.speed.x = -120 end - if self.keys["right"].isDown and (not self.isPunching) then - self.xsp = 120 + if love.keyboard.isDown("right") and (not self.isPunching) then + self.speed.x = 120 end - if self.keys["A"].isDown then + if love.keyboard.isDown("a") then self.isPunching = true else self.isPunching = false end - if (self.isPunching) then - self:applyHitboxesCollisions() - end - - if self.keys["start"].isPressed then - self.scene.menusystem:activate() - self.scene.menusystem:switchMenu("PauseMenu") - self.scene:flushKeys() - end - - self:setDirection(self.xsp) -end - -function Player:updateEnd(dt) + self:setDirection(self.speed.x) self:setAnimation() - local width, height = self.world:getDimensions() + local _, dimensions = self.world:getBox() - if (self.y > height + self.h) and (self.isDead == false) then - self:addTimer("respawn", 1) + if (self.position.y > (dimensions.h + self.dimensions.h)) and (self.isDead == false) then + self.timers:newTimer(1, "respawn") self.isDead = true end end function Player:setAnimation() - self.sprite:setCustomSpeed(math.abs(self.xsp) / 12) + self.visual:setCustomSpeed(math.abs(self.speed.x) / 12) if (self.isPunching) then - self.sprite:changeAnimation("punch", false) + self.visual:changeAnimation("punch", false) else - if (self.onGround) then - if math.abs(self.xsp) > 0 then - self.sprite:changeAnimation("walk", false) + if (self.onGround) then + if math.abs(self.speed.x) > 0 then + self.visual:changeAnimation("walk", false) + else + self.visual:changeAnimation("idle", true) + end else - self.sprite:changeAnimation("idle", true) + self.visual:changeAnimation("jump", true) end - else - self.sprite:changeAnimation("jump", true) - end end end @@ -86,39 +82,20 @@ function Player:setDirection(direction) if direction ~= 0 then direction = utils.math.sign(direction) self.direction = direction - self.sprite:setScallingX(direction) + self.visual:setScalling({x = direction}) end end function Player:timerResponse(timer) if timer == "respawn" then - self.x, self.y = self.startx, self.starty - self.xspeed = 0 - self.yspeed = 0 + self.position.x, self.position.y = self.start.x, self.start.y + self.speed.x, self.speed.y = 0, 0 self.isDead = false end end -function Player:collisionResponse(collision) - if collision.other.type == "coin" then - collision.other.owner:takeCoin(self) - end -end - -function Player:hitboxResponse(name, type, collision) - if (collision.other.type == "coin") and (type == "punch") then - collision.other.owner:takeCoin(self) - end -end - -function Player:draw() - Player.super.draw(self) - self:drawHitboxes() - utils.graphics.resetColor() -end - -function Player:drawHUD(id) - love.graphics.print(id .. " test", 4, 4) +function Player:drawHUD() + assets:print("medium", "Coins : " .. self.coin, 8, 8) end return Player diff --git a/examples/scenes/gameplay/plateform/actors/wall.lua b/examples/scenes/gameplay/plateform/actors/wall.lua deleted file mode 100644 index 20893c2..0000000 --- a/examples/scenes/gameplay/plateform/actors/wall.lua +++ /dev/null @@ -1,8 +0,0 @@ -local Base = require "framework.scenes.world.actors.actor2D" -local Wall = Base:extend() - -function Wall:new(world, x, y, w, h) - Wall.super.new(self, world, "wall", x, y, w, h, true) -end - -return Wall diff --git a/examples/scenes/gameplay/plateform/assets.lua b/examples/scenes/gameplay/plateform/assets.lua deleted file mode 100644 index 94ee4f5..0000000 --- a/examples/scenes/gameplay/plateform/assets.lua +++ /dev/null @@ -1,19 +0,0 @@ -return { - ["textures"] = { - {"box", "assets/sprites/box.png"}, - {"boxtop", "assets/sprites/boxtop.png"} - }, - ["sprites"] = { - {"player", "assets/sprites/monkey_lad"}, - {"coin", "assets/sprites/coin"}, - {"sparkle", "assets/sprites/gfx/sparkle"} - }, - ["imagefonts"] = { - {"medium", "assets/fonts/medium"} - }, - ["sfx"] = { - {"navigate", "assets/sfx/menu_move.mp3"}, - {"confirm", "assets/sfx/menu_confirm.mp3"}, - {"cancel", "assets/sfx/menu_error.mp3"}, - } -} diff --git a/examples/scenes/gameplay/plateform/init.lua b/examples/scenes/gameplay/plateform/init.lua index c52fb5c..4d425b6 100644 --- a/examples/scenes/gameplay/plateform/init.lua +++ b/examples/scenes/gameplay/plateform/init.lua @@ -21,46 +21,18 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ]] -local Scene = require "framework.scenes" -local Plateformer = Scene:extend() -local World = require "framework.scenes.world.world2D" -local Pause = require "scenes.gameplay.plateform.pause" - -function Plateformer:new() - Plateformer.super.new(self) - - local folder = "scenes.gameplay.plateform" - - World(self, folder .. ".actors", "datas/maps/plateformer/platformer.lua") - - --Pause(self) - --self.menusystem:deactivate() - --self.menusystem:lockWorldWhenActive(true) - --self.menusystem:lockAssetsWhenActive(true) - - self.world:loadMap() -end +local Plateformer = world { + actorPath = "scenes.gameplay.plateform.actors", + defaultMap = "datas/maps/plateformer/platformer.lua", + collisions = { + wall = {isSolid = true, type = "wall"} + } +} function Plateformer:restart() - --self.menusystem:deactivate() collectgarbage() self.world:reset() end -function Plateformer:update(dt) - --if (self.menusystem.isActive == true) and self.sources[1].keys["start"].isPressed then - --self.menusystem:deactivate() - --end -end - -function Plateformer:draw() - if (self.world.isActive == false) then - local w, h = core.screen:getDimensions() - love.graphics.setColor(1, 1, 0, 1) - love.graphics.printf("PAUSE", 0, 24*3, w, "center") - utils.graphics.resetColor() - end -end - return Plateformer diff --git a/examples/scenes/gameplay/plateform/pause.lua b/examples/scenes/gameplay/plateform/pause.lua deleted file mode 100644 index bd9df37..0000000 --- a/examples/scenes/gameplay/plateform/pause.lua +++ /dev/null @@ -1,60 +0,0 @@ -local ListMenu = require "framework.scenes.gui.menus.listbox" -local Widget = require "framework.scenes.gui.menus.widgets" - -local PauseMenu = ListMenu:extend() - -local ResumeWidget = Widget.Text:extend() -local RestartWidget = Widget.Text:extend() -local ExitWidget = Widget.Text:extend() - -local MENU_NAME = "PauseMenu" - -function PauseMenu:new(scene) - self.scene = scene - local screenHeight, screenWidth = core.screen:getDimensions() - local w, h = 424/4, 240 - 48*4 - local x, y = 3*w/2, 24*4 - PauseMenu.super.new(self, scene.menusystem, MENU_NAME, x, y, w, h, 3) - - ResumeWidget() - RestartWidget() - ExitWidget() -end - --- WIDGETS --- All widgets used by the pause menu - -function ResumeWidget:new() - self.scene = core.scenemanager:getScene() - local label = "resume" - ResumeWidget.super.new(self, MENU_NAME, "medium", label) -end - -function ResumeWidget:action() - assets:playSFX(self.sfx) - self.scene.menusystem:deactivate() -end - -function RestartWidget:new() - self.scene = core.scenemanager:getScene() - local label = "restart" - RestartWidget.super.new(self, MENU_NAME, "medium", label) -end - -function RestartWidget:action() - assets:playSFX(self.sfx) - self.scene:restart() -end - -function ExitWidget:new() - self.scene = core.scenemanager:getScene() - local label = "exit" - ExitWidget.super.new(self, MENU_NAME, "medium", label) -end - -function ExitWidget:action() - assets:playSFX(self.sfx) - core.scenemanager:setStoredScene("mainmenu") -end - -return PauseMenu