From bdd837d4da440cb8950df24a070c1ae6390a13d5 Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Sun, 7 Apr 2019 17:15:27 +0200 Subject: [PATCH] modules/world: basic player system --- gamecore/modules/world/actors/actor2D.lua | 4 +++ gamecore/modules/world/baseworld.lua | 33 +++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/gamecore/modules/world/actors/actor2D.lua b/gamecore/modules/world/actors/actor2D.lua index f987bad..a967c92 100644 --- a/gamecore/modules/world/actors/actor2D.lua +++ b/gamecore/modules/world/actors/actor2D.lua @@ -20,6 +20,10 @@ function Actor2D:register() self.world:registerActor(self) end +function Actor2D:getInput(keys) + self.keys = keys or core.input.fakekeys +end + function Actor2D:update(dt) -- here will be update actions end diff --git a/gamecore/modules/world/baseworld.lua b/gamecore/modules/world/baseworld.lua index c2168f6..6d671da 100644 --- a/gamecore/modules/world/baseworld.lua +++ b/gamecore/modules/world/baseworld.lua @@ -9,6 +9,8 @@ local Sti = require(cwd .. "libs.sti") function BaseWorld:new(scene, actorlist, mapfile) self.scene = scene + + self:initPlayers() self:setActorList(actorlist) self:setMap(mapfile) self.actors = {} @@ -84,6 +86,36 @@ function BaseWorld:getActors() return self.actors end +-- PLAYER MANAGEMENT FUNCTIONS +-- Basic function to handle player actors + +function BaseWorld:initPlayers() + self.players = {} +end + +function BaseWorld:addPlayer(actor, sourceid) + local player = {} + player.actor = actor + player.sourceid = sourceid or 1 + + table.insert(self.players, player) +end + +function BaseWorld:sendInputToPlayers(actor) + for i,v in ipairs(self.players) do + --TODO: make the player get from a selected source inputs + v.actor:getInput(self.scene.keys) + end +end + +function BaseWorld:removePlayer(actor) + for i,v in ipairs(self.players) do + if (v.actor == actor) then + table.remove(self.players, i) + end + end +end + -- MAP FUNCTIONS -- All map wrappers @@ -114,6 +146,7 @@ end function BaseWorld:update(dt) self:updateMap(dt) + self:sendInputToPlayers(dt) self:updateActors(dt) end