From 54d56675a9c9460dc64eda5a0881b2dcd873ef61 Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Sun, 7 Apr 2019 17:00:47 +0200 Subject: [PATCH] modules/world: prepare structure for movable player example --- examples/gameplay/moveplayer/actors/init.lua | 12 +++++ .../gameplay/moveplayer/actors/parent.lua | 13 ++++++ .../gameplay/moveplayer/actors/player.lua | 27 +++++++++++ examples/gameplay/moveplayer/init.lua | 45 +++++++++++++++++++ examples/init.lua | 3 +- examples/mainmenu/init.lua | 2 + 6 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 examples/gameplay/moveplayer/actors/init.lua create mode 100644 examples/gameplay/moveplayer/actors/parent.lua create mode 100644 examples/gameplay/moveplayer/actors/player.lua create mode 100644 examples/gameplay/moveplayer/init.lua diff --git a/examples/gameplay/moveplayer/actors/init.lua b/examples/gameplay/moveplayer/actors/init.lua new file mode 100644 index 0000000..3770609 --- /dev/null +++ b/examples/gameplay/moveplayer/actors/init.lua @@ -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 diff --git a/examples/gameplay/moveplayer/actors/parent.lua b/examples/gameplay/moveplayer/actors/parent.lua new file mode 100644 index 0000000..13f85af --- /dev/null +++ b/examples/gameplay/moveplayer/actors/parent.lua @@ -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 diff --git a/examples/gameplay/moveplayer/actors/player.lua b/examples/gameplay/moveplayer/actors/player.lua new file mode 100644 index 0000000..d4bd7f4 --- /dev/null +++ b/examples/gameplay/moveplayer/actors/player.lua @@ -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 diff --git a/examples/gameplay/moveplayer/init.lua b/examples/gameplay/moveplayer/init.lua new file mode 100644 index 0000000..5db44ca --- /dev/null +++ b/examples/gameplay/moveplayer/init.lua @@ -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 diff --git a/examples/init.lua b/examples/init.lua index f8e4064..08ed0db 100644 --- a/examples/init.lua +++ b/examples/init.lua @@ -3,5 +3,6 @@ return { Test = require "examples.basic.test_scene", Test2 = require "examples.basic.test_scene2", TestMenu = require "examples.basic.test_menus", - Inventory = require "examples.menus.inventory" + Inventory = require "examples.menus.inventory", + MovePlayer = require "examples.gameplay.moveplayer", } diff --git a/examples/mainmenu/init.lua b/examples/mainmenu/init.lua index ef97f2f..3210127 100644 --- a/examples/mainmenu/init.lua +++ b/examples/mainmenu/init.lua @@ -42,6 +42,8 @@ function MainMenu:new() self:addScene("basic", examples.TestMenu, "Basic Test Menu") self:addSubMenu("menus", "Menus Tests") self:addScene("menus", examples.Inventory, "Inventory") + self:addSubMenu("gameplay", "Games Examples") + self:addScene("gameplay", examples.MovePlayer, "Movable Player") ExitWidget(self, "main") self.menusystem:switchMenu("main")