modules/world: initial gravity system

This commit is contained in:
Kazhnuz 2019-05-02 19:51:43 +02:00
parent adfa147b72
commit f04d0c6b4e
3 changed files with 40 additions and 0 deletions

View file

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

View file

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

View file

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