modules/world: prepare structure for movable player example

This commit is contained in:
Kazhnuz 2019-04-07 17:00:47 +02:00
parent 064d5bd2a8
commit 54d56675a9
6 changed files with 101 additions and 1 deletions

View file

@ -0,0 +1,12 @@
local Obj = {}
-- On charge toutes les différentes types d'acteurs
local cwd = (...):gsub('%.init$', '') .. "."
Obj.Player = require(cwd .. "player")
Obj.index = {}
Obj.index["player"] = Obj.Explosion
Obj.collisions = {}
return Obj

View file

@ -0,0 +1,13 @@
local Base = require "gamecore.modules.world.actors.actor2D"
local Parent = Base:extend()
function Parent:new(world, type, x, y, w, h)
self.scene = world.scene
Parent.super.new(self, world, type, x, y, w, h)
end
function Parent:draw()
utils.graphics.drawBox(self.x, self.y, self.w, self.h)
end
return Parent

View file

@ -0,0 +1,27 @@
local cwd = (...):gsub('%.player$', '') .. "."
local Parent = require(cwd .. "parent")
local Player = Parent:extend()
function Player:new(world, x, y)
Player.super.new(self, world, "player", x, y, 16, 16)
end
function Player:update(dt)
local dx, dy
if self.keys["up"].isPressed then
dy = -120 * dt
end
if self.keys["down"].isPressed then
dy = 120 * dt
end
if self.keys["left"].isPressed then
dx = -120 * dt
end
if self.keys["right"].isPressed then
dx = 120 * dt
end
self:move(self.x + dx, self.y + dy)
end
return Player

View file

@ -0,0 +1,45 @@
-- scenes/moveplayer :: a basic player movement example
--[[
Copyright © 2019 Kazhnuz
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
]]
local Scene = require "gamecore.modules.scenes"
local MovePlayer = Scene:extend()
local World = require "gamecore.modules.world.baseworld"
function MovePlayer:new()
MovePlayer.super.new(self)
self.world = World(self, "examples.gameplay.moveplayer.actors")
self.world.obj.Player(self.world, 16, 16)
end
function MovePlayer:update(dt)
self.world:update(dt)
end
function MovePlayer:draw()
self.world:draw()
end
return MovePlayer

View file

@ -3,5 +3,6 @@ return {
Test = require "examples.basic.test_scene", Test = require "examples.basic.test_scene",
Test2 = require "examples.basic.test_scene2", Test2 = require "examples.basic.test_scene2",
TestMenu = require "examples.basic.test_menus", TestMenu = require "examples.basic.test_menus",
Inventory = require "examples.menus.inventory" Inventory = require "examples.menus.inventory",
MovePlayer = require "examples.gameplay.moveplayer",
} }

View file

@ -42,6 +42,8 @@ function MainMenu:new()
self:addScene("basic", examples.TestMenu, "Basic Test Menu") self:addScene("basic", examples.TestMenu, "Basic Test Menu")
self:addSubMenu("menus", "Menus Tests") self:addSubMenu("menus", "Menus Tests")
self:addScene("menus", examples.Inventory, "Inventory") self:addScene("menus", examples.Inventory, "Inventory")
self:addSubMenu("gameplay", "Games Examples")
self:addScene("gameplay", examples.MovePlayer, "Movable Player")
ExitWidget(self, "main") ExitWidget(self, "main")
self.menusystem:switchMenu("main") self.menusystem:switchMenu("main")