feat(exemples): port the plateformer exemple to the new world system

This commit is contained in:
Kazhnuz 2024-10-31 09:54:13 +01:00
parent e40683a6b0
commit 816c6fee22
9 changed files with 77 additions and 241 deletions

View file

@ -48,6 +48,9 @@ return {
loop = 1, loop = 1,
speed = 0, speed = 0,
pauseAtEnd = false, pauseAtEnd = false,
areas = {
["frame1"] = {{type = "punch", box = {position = {x = 16, y = 6}, dimensions = {w = 12, h = 12}}, isSolid = false}}
}
}, },
} }
} }

View file

@ -1,15 +1,15 @@
local Base = require "framework.scenes.world.actors.actor2D" return actor {
local Coin = Base:extend() type = "coin",
dimensions = { w = 16, h = 16 },
function Coin:new(world, x, y) isSolid = false,
Coin.super.new(self, world, "coin", x, y, 16, 16, false) visuals = {
self:setSprite("coin") mode = "sprite",
end assetName = "coin"
},
function Coin:takeCoin(other) onPlayerCollision = function (self, player)
self.obj.GFX(self.world, self.x + 8, self.y + 8, "gfx.sparkle") self:destroy()
assets:playSFX("gameplay.collectcoin") assets:playSFX("gameplay.collectcoin")
self:destroy( ) self.world:showGFX("gfx.sparkle", self.position)
end player.coin = player.coin + 1
end
return Coin }

View file

@ -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}
}
}
}

View file

@ -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

View file

@ -1,83 +1,79 @@
local Base = require "framework.scenes.world.actors.actor2D" local Player = actor {
local Player = Base:extend() 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) function Player:onInit()
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
self.isDead = false self.isDead = false
self.start = self.position:clone()
self.isPunching = false
self.direction = 1
self.coin = 0
self.punchName = "" self.punchName = ""
self:setHitboxFile("scenes.gameplay.plateform.actors.hitboxes.player")
end end
function Player:updateStart(dt) function Player:update(dt)
self.xfrc = 480*3 if love.keyboard.isDown("up") and (self.onGround) then
self.speed.y = -280
if self.keys["up"].isPressed and (self.onGround) then
self.ysp = -280
assets:playSFX("gameplay.jump") assets:playSFX("gameplay.jump")
end end
if self.keys["down"].isDown then if love.keyboard.isDown("down") then
self.mainHitbox:modify(0, 8, 16, 16) self.mainHitbox:modify({x = 0, y = 8}, {w = 16, h = 16})
else else
self.mainHitbox:modify(0, 0, 16, 24) self.mainHitbox:modify({x = 0, y = 0}, {w = 16, h = 24})
end end
if self.keys["left"].isDown and (not self.isPunching) then if love.keyboard.isDown("left") and (not self.isPunching) then
self.xsp = -120 self.speed.x = -120
end end
if self.keys["right"].isDown and (not self.isPunching) then if love.keyboard.isDown("right") and (not self.isPunching) then
self.xsp = 120 self.speed.x = 120
end end
if self.keys["A"].isDown then if love.keyboard.isDown("a") then
self.isPunching = true self.isPunching = true
else else
self.isPunching = false self.isPunching = false
end end
if (self.isPunching) then self:setDirection(self.speed.x)
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:setAnimation() self:setAnimation()
local width, height = self.world:getDimensions() local _, dimensions = self.world:getBox()
if (self.y > height + self.h) and (self.isDead == false) then if (self.position.y > (dimensions.h + self.dimensions.h)) and (self.isDead == false) then
self:addTimer("respawn", 1) self.timers:newTimer(1, "respawn")
self.isDead = true self.isDead = true
end end
end end
function Player:setAnimation() function Player:setAnimation()
self.sprite:setCustomSpeed(math.abs(self.xsp) / 12) self.visual:setCustomSpeed(math.abs(self.speed.x) / 12)
if (self.isPunching) then if (self.isPunching) then
self.sprite:changeAnimation("punch", false) self.visual:changeAnimation("punch", false)
else else
if (self.onGround) then if (self.onGround) then
if math.abs(self.xsp) > 0 then if math.abs(self.speed.x) > 0 then
self.sprite:changeAnimation("walk", false) self.visual:changeAnimation("walk", false)
else
self.visual:changeAnimation("idle", true)
end
else else
self.sprite:changeAnimation("idle", true) self.visual:changeAnimation("jump", true)
end end
else
self.sprite:changeAnimation("jump", true)
end
end end
end end
@ -86,39 +82,20 @@ function Player:setDirection(direction)
if direction ~= 0 then if direction ~= 0 then
direction = utils.math.sign(direction) direction = utils.math.sign(direction)
self.direction = direction self.direction = direction
self.sprite:setScallingX(direction) self.visual:setScalling({x = direction})
end end
end end
function Player:timerResponse(timer) function Player:timerResponse(timer)
if timer == "respawn" then if timer == "respawn" then
self.x, self.y = self.startx, self.starty self.position.x, self.position.y = self.start.x, self.start.y
self.xspeed = 0 self.speed.x, self.speed.y = 0, 0
self.yspeed = 0
self.isDead = false self.isDead = false
end end
end end
function Player:collisionResponse(collision) function Player:drawHUD()
if collision.other.type == "coin" then assets:print("medium", "Coins : " .. self.coin, 8, 8)
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)
end end
return Player return Player

View file

@ -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

View file

@ -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"},
}
}

View file

@ -21,46 +21,18 @@
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 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 Plateformer = world {
local Pause = require "scenes.gameplay.plateform.pause" actorPath = "scenes.gameplay.plateform.actors",
defaultMap = "datas/maps/plateformer/platformer.lua",
function Plateformer:new() collisions = {
Plateformer.super.new(self) wall = {isSolid = true, type = "wall"}
}
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
function Plateformer:restart() function Plateformer:restart()
--self.menusystem:deactivate()
collectgarbage() collectgarbage()
self.world:reset() self.world:reset()
end 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 return Plateformer

View file

@ -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