improvement(actor): merge most physics initialisation
This commit is contained in:
parent
049213000a
commit
10bbd5fd84
2 changed files with 50 additions and 45 deletions
|
@ -32,9 +32,8 @@ local Hitbox = require(cwd .. "utils.hitbox2D")
|
|||
-- Initialise the actor and its base functions
|
||||
|
||||
function Actor2D:new(world, type, x, y, w, h, isSolid)
|
||||
self:setCoordinate(x, y)
|
||||
Actor2D.super.new(self, world, type, isSolid)
|
||||
self:initHitboxes(w, h)
|
||||
Actor2D.super.new(self, world, type, x, y, 0, w, h, 0, isSolid)
|
||||
self:initHitboxes()
|
||||
end
|
||||
|
||||
function Actor2D:destroy()
|
||||
|
@ -43,16 +42,8 @@ function Actor2D:destroy()
|
|||
self.isDestroyed = true
|
||||
end
|
||||
|
||||
-- MOVEMENT FUNCTIONS
|
||||
-- Basic functions from the movement.
|
||||
|
||||
function Actor2D:initMovement()
|
||||
self.xsp = 0
|
||||
self.ysp = 0
|
||||
|
||||
self.xfrc = 0
|
||||
self.yfrc = 0
|
||||
end
|
||||
-- PHYSICS FUNCTIONS
|
||||
-- Handle movement and collisions.
|
||||
|
||||
function Actor2D:autoMove(dt)
|
||||
self:updateHitboxes()
|
||||
|
@ -168,13 +159,18 @@ function Actor2D:checkGround()
|
|||
end
|
||||
end
|
||||
|
||||
-- COORDINATE/MOVEMENT FUNCTIONS
|
||||
-- Handle coordinate
|
||||
|
||||
function Actor2D:getViewCenter()
|
||||
local x, y = self:getCenter()
|
||||
return x, y
|
||||
end
|
||||
|
||||
-- HITBOXES FUNCTIONS
|
||||
-- Functions related to actor hitboxes
|
||||
|
||||
function Actor2D:initHitboxes(w, h)
|
||||
self.w = w or 0
|
||||
self.h = h or 0
|
||||
|
||||
function Actor2D:initHitboxes()
|
||||
self:initMainHitbox()
|
||||
|
||||
self.hitboxes = {}
|
||||
|
@ -322,22 +318,6 @@ function Actor2D:drawMainHitbox()
|
|||
self.mainHitbox:draw()
|
||||
end
|
||||
|
||||
-- COORDINATE FUNCTION
|
||||
-- Handle the coordinate system
|
||||
|
||||
function Actor2D:setCoordinate(x, y)
|
||||
self.x = x or self.x
|
||||
self.y = y or self.y
|
||||
end
|
||||
|
||||
function Actor2D:getCenter()
|
||||
return (self.x + (self.w / 2)), (self.y + (self.h / 2))
|
||||
end
|
||||
|
||||
function Actor2D:getViewCenter()
|
||||
return self:getCenter()
|
||||
end
|
||||
|
||||
-- DRAW FUNCTIONS
|
||||
-- Draw the actors.
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
-- actor2D.lua :: the global implementation of an actor. Basically, it abstract
|
||||
-- everything that isn't 2D or 3D related to the actor system.
|
||||
-- BaseActor.lua :: the global implementation of an actor. Basically, it abstract
|
||||
-- everything that isn't only 2D or 3D related to the actor system.
|
||||
|
||||
--[[
|
||||
Copyright © 2019 Kazhnuz
|
||||
|
@ -30,7 +30,7 @@ local Timer = require(cwd .. "utils.timer")
|
|||
-- INIT FUNCTIONS
|
||||
-- Initialise the actor and its base functions
|
||||
|
||||
function BaseActor:new(world, type, isSolid)
|
||||
function BaseActor:new(world, type, x, y, z, w, h, d, isSolid)
|
||||
self.type = type or ""
|
||||
self.isSolid = isSolid or false
|
||||
self.depth = 0
|
||||
|
@ -39,7 +39,7 @@ function BaseActor:new(world, type, isSolid)
|
|||
self:initKeys()
|
||||
self:initTimers()
|
||||
self:setSprite()
|
||||
self:initPhysics()
|
||||
self:initPhysics(x, y, z, w, h, d)
|
||||
|
||||
self:setDebugColor(1, 1, 1)
|
||||
self:register()
|
||||
|
@ -69,17 +69,36 @@ function BaseActor:destroy()
|
|||
self.isDestroyed = true
|
||||
end
|
||||
|
||||
-- PHYSICS INITIALISATION
|
||||
-- Basic initialization of the physic systems
|
||||
-- PHYSICS FUNCTIONS
|
||||
-- Raw implementation of everything common in physics
|
||||
|
||||
function BaseActor:initPhysics(x, y, z, w, h, d)
|
||||
self:setCoordinate(x, y, z)
|
||||
|
||||
self.w = w or 0
|
||||
self.h = h or 0
|
||||
self.d = d or 0
|
||||
|
||||
self.xsp = 0
|
||||
self.ysp = 0
|
||||
self.zsp = 0
|
||||
|
||||
self.xfrc = 0
|
||||
self.yfrc = 0
|
||||
self.zfrc = 0
|
||||
|
||||
function BaseActor:initPhysics()
|
||||
self:initMovement()
|
||||
self:initGravity()
|
||||
|
||||
self:setBounceFactor()
|
||||
self:setFilter()
|
||||
end
|
||||
|
||||
function BaseActor:setCoordinate(x, y, z, w, h, d)
|
||||
self.x = x or self.x
|
||||
self.y = y or self.y
|
||||
self.z = z or self.z
|
||||
end
|
||||
|
||||
function BaseActor:setBounceFactor(newBounceFactor)
|
||||
self.bounceFactor = newBounceFactor or 0
|
||||
end
|
||||
|
@ -98,14 +117,20 @@ function BaseActor:setFilter()
|
|||
end
|
||||
end
|
||||
|
||||
function BaseActor:initMovement( )
|
||||
-- Empty placeholder function
|
||||
end
|
||||
|
||||
function BaseActor:initGravity( )
|
||||
-- Empty placeholder function
|
||||
end
|
||||
|
||||
-- COORDINATE/MOVEMENT FUNCTIONS
|
||||
-- Handle coordinate
|
||||
|
||||
function BaseActor:getCenter()
|
||||
return (self.x + (self.w / 2)), (self.y + (self.h / 2)), (self.z + (self.d / 2))
|
||||
end
|
||||
|
||||
function BaseActor:getViewCenter()
|
||||
return self:getCenter()
|
||||
end
|
||||
|
||||
-- UPDATE FUNCTIONS
|
||||
-- Theses functions are activated every steps
|
||||
|
|
Loading…
Reference in a new issue