From f04d0c6b4ea9779f3c85dd4b02e6f9be31c0aafc Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Thu, 2 May 2019 19:51:43 +0200 Subject: [PATCH] modules/world: initial gravity system --- examples/gameplay/plateform/actors/player.lua | 1 + gamecore/modules/world/actors/actor2D.lua | 28 +++++++++++++++++++ gamecore/modules/world/baseworld.lua | 11 ++++++++ 3 files changed, 40 insertions(+) diff --git a/examples/gameplay/plateform/actors/player.lua b/examples/gameplay/plateform/actors/player.lua index 5a38e40..862bba9 100644 --- a/examples/gameplay/plateform/actors/player.lua +++ b/examples/gameplay/plateform/actors/player.lua @@ -4,6 +4,7 @@ local Player = Base:extend() function Player:new(world, x, y, id) Player.super.new(self, world, "player", x, y, 16, 24, true) self:setSprite("player", 8, 12) + self:setYGravity(480) end function Player:update(dt) diff --git a/gamecore/modules/world/actors/actor2D.lua b/gamecore/modules/world/actors/actor2D.lua index 1c64f0a..c4d8836 100644 --- a/gamecore/modules/world/actors/actor2D.lua +++ b/gamecore/modules/world/actors/actor2D.lua @@ -66,6 +66,7 @@ function Actor2D:initPhysics(x, y, w, h, isSolid) self.yfrc = 0 self.bounceFactor = 0 + self:initGravity() self.isSolid = isSolid or false @@ -115,6 +116,8 @@ function Actor2D:setFilter() end function Actor2D:autoMove(dt) + self:applyGravity(dt) + local newx, newy, cols, colNumber = self:move(self.x + self.xsp * dt, self.y + self.ysp * dt) -- apply after the movement the friction, until the player stop @@ -160,6 +163,31 @@ function Actor2D:move(dx, dy) return self.x, self.y, cols, colNumber end +function Actor2D:initGravity() + local xgrav, ygrav + + if (self.world.gravity.isDefault) then + self.xgrav = self.world.gravity.xgrav + self.ygrav = self.world.gravity.ygrav + else + self.xgrav = 0 + self.ygrav = 0 + end +end + +function Actor2D:setXGravity(grav) + self.xgrav = grav +end + +function Actor2D:setYGravity(grav) + self.ygrav = grav +end + +function Actor2D:applyGravity(dt) + self.xsp = self.xsp + self.xgrav * dt + self.ysp = self.ysp + self.ygrav * dt +end + -- DRAW FUNCTIONS -- Draw the actors. diff --git a/gamecore/modules/world/baseworld.lua b/gamecore/modules/world/baseworld.lua index 41ba0fa..844fb17 100644 --- a/gamecore/modules/world/baseworld.lua +++ b/gamecore/modules/world/baseworld.lua @@ -41,6 +41,7 @@ function BaseWorld:new(scene, actorlist, mapfile) self:initPlayers() self:setActorList(actorlist) self:initMap(mapfile) + self:setGravity() end function BaseWorld:setActorList(actorlist) @@ -58,6 +59,16 @@ function BaseWorld:initMap(mapfile) self.mapfile = mapfile end +function BaseWorld:setGravity(xgrav, ygrav, isDefault) + local xgrav = xgrav or 0 + local ygrav = ygrav or 0 + local isDefault = isDefault or 0 + + self.gravity = {} + self.gravity.xgrav, self.gravity.ygrav = xgrav, ygrav + self.gravity.isDefault = isDefault +end + -- ACTOR MANAGEMENT FUNCTIONS -- Basic function to handle actors