2019-06-30 17:07:58 +02:00
|
|
|
-- actor3D.lua :: the implementation of a 2D actor. It contain every element
|
|
|
|
-- needed to create your own 2D actors.
|
|
|
|
|
|
|
|
--[[
|
|
|
|
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 cwd = (...):gsub('%.actor3D$', '') .. "."
|
2020-11-25 13:30:10 +01:00
|
|
|
local BaseActor = require("birb.modules.world.actors.mixins.base")
|
2020-11-26 18:40:25 +01:00
|
|
|
local SpritedActor = require("birb.modules.world.actors.mixins.sprites")
|
|
|
|
local TimedActor = require("birb.modules.world.actors.mixins.timers")
|
|
|
|
local InputActor = require("birb.modules.world.actors.mixins.inputs")
|
2020-11-26 19:16:15 +01:00
|
|
|
local PhysicalActor = require("birb.modules.world.actors.mixins.physics")
|
2020-11-26 18:40:25 +01:00
|
|
|
|
2020-11-25 13:30:10 +01:00
|
|
|
local Actor3D = Object:extend()
|
|
|
|
Actor3D:implement(BaseActor)
|
2020-11-26 18:40:25 +01:00
|
|
|
Actor3D:implement(SpritedActor)
|
|
|
|
Actor3D:implement(TimedActor)
|
|
|
|
Actor3D:implement(InputActor)
|
2020-11-26 19:16:15 +01:00
|
|
|
Actor3D:implement(PhysicalActor)
|
2019-06-30 17:07:58 +02:00
|
|
|
|
|
|
|
local Hitbox = require(cwd .. "utils.hitbox3D")
|
2019-07-01 17:00:55 +02:00
|
|
|
local Boxes = require(cwd .. "utils.boxes")
|
2019-06-30 17:07:58 +02:00
|
|
|
|
|
|
|
-- INIT FUNCTIONS
|
|
|
|
-- Initialise the actor and its base functions
|
|
|
|
|
|
|
|
function Actor3D:new(world, type, x, y, z, w, h, d, isSolid)
|
2020-11-26 19:16:15 +01:00
|
|
|
self:init(world, type)
|
2020-11-26 20:29:59 +01:00
|
|
|
self:initPhysics(Hitbox, x, y, z, w, h, d, isSolid)
|
2020-11-26 18:40:25 +01:00
|
|
|
self:initTimers()
|
|
|
|
self:initSprite()
|
2019-07-01 14:13:26 +02:00
|
|
|
self.world:registerShape(self)
|
2019-07-01 17:00:55 +02:00
|
|
|
self.boxes = Boxes
|
2019-07-06 18:00:00 +02:00
|
|
|
self.doCastShadows = true
|
2019-06-30 17:07:58 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function Actor3D:destroy()
|
2019-07-06 18:00:00 +02:00
|
|
|
self:removeOldShadowTargets()
|
|
|
|
if self.box ~= nil then
|
|
|
|
self.world:removeTerrain(self)
|
|
|
|
end
|
2019-06-30 17:07:58 +02:00
|
|
|
self.world:removeActor(self)
|
|
|
|
self.mainHitbox:destroy()
|
2019-07-01 14:13:26 +02:00
|
|
|
self.world:removeShape(self)
|
2019-06-30 17:07:58 +02:00
|
|
|
self.isDestroyed = true
|
|
|
|
end
|
|
|
|
|
2020-11-26 20:29:59 +01:00
|
|
|
function Actor3D:packForHitbox()
|
|
|
|
return {0, 0, 0, self.w, self.h, self.d}
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2019-06-30 17:07:58 +02:00
|
|
|
-- PHYSICS FUNCTIONS
|
|
|
|
-- Handle movement and collisions.
|
|
|
|
|
|
|
|
function Actor3D:autoMove(dt)
|
|
|
|
self:updateHitboxes()
|
|
|
|
self.onGround = false
|
|
|
|
self:applyGravity(dt)
|
|
|
|
|
|
|
|
local dx, dy, dz = self:getFuturePosition(dt)
|
|
|
|
local newx, newy, newz, cols, colNumber = self:move(dx, dy, dz)
|
|
|
|
|
|
|
|
-- apply after the movement the friction, until the player stop
|
|
|
|
-- note: the friction is applied according to the delta time,
|
|
|
|
-- thus the friction should be how much speed is substracted in 1 second
|
|
|
|
|
|
|
|
self:solveAllCollisions(cols)
|
|
|
|
|
|
|
|
self:applyFriction(dt)
|
|
|
|
end
|
|
|
|
|
|
|
|
function Actor3D:changeSpeedToCollisionNormal(normal)
|
|
|
|
local xsp, ysp, zsp = self.xsp, self.ysp, self.zsp
|
|
|
|
local nx, ny, nz = normal.x, normal.y, normal.z
|
|
|
|
|
|
|
|
if (nx < 0 and xsp > 0) or (nx > 0 and xsp < 0) then
|
|
|
|
xsp = -xsp * self.bounceFactor
|
|
|
|
end
|
|
|
|
|
|
|
|
if (ny < 0 and ysp > 0) or (ny > 0 and ysp < 0) then
|
|
|
|
ysp = -ysp * self.bounceFactor
|
|
|
|
end
|
|
|
|
|
|
|
|
if (nz < 0 and zsp > 0) or (nz > 0 and zsp < 0) then
|
|
|
|
zsp = -zsp * self.bounceFactor
|
|
|
|
end
|
|
|
|
|
|
|
|
self.xsp, self.ysp, self.zsp = xsp, ysp, zsp
|
|
|
|
end
|
|
|
|
|
|
|
|
function Actor3D:move(dx, dy, dz)
|
|
|
|
local cols, colNumber = {}, 0
|
2019-07-06 18:00:00 +02:00
|
|
|
local oldx, oldy, oldz = self.x, self.y, self.z
|
2019-06-30 17:07:58 +02:00
|
|
|
if (self.isDestroyed == false) then
|
2020-11-26 20:41:02 +01:00
|
|
|
self.x, self.y, self.z, cols, colNumber = self.mainHitbox:checkCollisionAtPoint(dx, dy, dz, self.filter)
|
2019-06-30 17:07:58 +02:00
|
|
|
self.mainHitbox:updatePosition()
|
2019-07-01 14:13:26 +02:00
|
|
|
self.world:updateShape(self)
|
2019-06-30 17:07:58 +02:00
|
|
|
end
|
2019-07-06 18:00:00 +02:00
|
|
|
|
|
|
|
if (oldx ~= self.x) or (oldy ~= self.y) or (oldz ~= self.z) or (self.shadowTargetsPrevious == nil) then
|
|
|
|
if (self.doCastShadows) then
|
|
|
|
self:castShadow()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-30 17:07:58 +02:00
|
|
|
return self.x, self.y, self.z, cols, colNumber
|
|
|
|
end
|
|
|
|
|
2020-11-26 20:41:02 +01:00
|
|
|
function Actor3D:checkCollisionAtPoint(dx, dy, dz)
|
2019-06-30 17:07:58 +02:00
|
|
|
local x, y, z, cols, colNumber = dx, dy, dz, {}, 0
|
|
|
|
if (self.isDestroyed == false) then
|
2020-11-26 20:41:02 +01:00
|
|
|
x, y, z, cols, colNumber = self.mainHitbox:checkCollisionAtPoint(dx, dy, dz, self.filter)
|
2019-06-30 17:07:58 +02:00
|
|
|
end
|
|
|
|
return self.x, self.y, self.z, cols, colNumber
|
|
|
|
end
|
|
|
|
|
|
|
|
-- GRAVITY SYSTEM FUNCTIONS
|
|
|
|
-- All functions related to gravity
|
|
|
|
|
|
|
|
function Actor3D:applyGravity(dt)
|
2019-06-30 22:04:30 +02:00
|
|
|
local grav = self.grav * -1
|
|
|
|
self.zsp = self.zsp + (grav * dt)
|
2019-06-30 17:07:58 +02:00
|
|
|
|
2019-06-30 22:04:30 +02:00
|
|
|
if utils.math.sign(self.zsp) == utils.math.sign(grav) then
|
2019-06-30 17:07:58 +02:00
|
|
|
self:checkGround( )
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function Actor3D:checkGround()
|
2019-06-30 22:04:30 +02:00
|
|
|
local dx, dy, dz = self.x, self.y, self.z - utils.math.sign(self.grav)
|
2020-11-26 20:41:02 +01:00
|
|
|
local newx, newy, newz, cols, colNumber = self:checkCollisionAtPoint(dx, dy, dz)
|
2019-06-30 17:07:58 +02:00
|
|
|
for i, col in ipairs(cols) do
|
|
|
|
if (col.type == "touch") or (col.type == "bounce") or (col.type == "slide") then
|
|
|
|
if not (self.grav == 0) then
|
2019-06-30 22:04:30 +02:00
|
|
|
if col.normal.z == utils.math.sign(self.grav) then self.onGround = true end
|
2019-06-30 17:07:58 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- COORDINATE/MOVEMENT FUNCTIONS
|
|
|
|
-- Handle coordinate
|
|
|
|
|
|
|
|
function Actor3D:getViewCenter()
|
|
|
|
local x, y, z = self:getCenter()
|
|
|
|
return x, y - (self.d/2)
|
|
|
|
end
|
|
|
|
|
2019-07-06 18:00:00 +02:00
|
|
|
-- SHADOW FUNCTIONS
|
|
|
|
-- Handle everything related to shadow
|
|
|
|
|
|
|
|
function Actor3D:castShadow()
|
|
|
|
local shadowTargets = self.world:getTerrainInRect(self.x, self.y, self.w, self.d)
|
|
|
|
-- initialize the shadowTargetsPrevious variable if it doesn't exist
|
|
|
|
if (self.shadowTargetsPrevious == nil) then
|
|
|
|
self.shadowTargetsPrevious = {}
|
|
|
|
end
|
|
|
|
|
|
|
|
for i, target in ipairs(shadowTargets) do
|
|
|
|
-- We test if the actor is below the current actor
|
|
|
|
if (target ~= self) and (target.box ~= nil) then
|
|
|
|
|
|
|
|
if (target.z + target.d <= self.z + self.d) then
|
|
|
|
-- Remove the target of the list of item targeted last update,
|
|
|
|
-- in order to only have object no longer shadowed after the
|
|
|
|
-- end of the loop
|
|
|
|
for j, oldtarget in ipairs(self.shadowTargetsPrevious) do
|
|
|
|
if (target == oldtarget) then
|
|
|
|
table.remove(self.shadowTargetsPrevious, j)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- We update the shadow source
|
|
|
|
local x, y = math.floor(self.x - target.x), math.floor(self.y - target.y)
|
|
|
|
target.box:setShadowSource(self, x, y)
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
-- At this point, if a target is still in the shadowTargetsPrevious list,
|
|
|
|
-- it mean that it's not shadowed. So we can simply remove the shadow.
|
|
|
|
self:removeOldShadowTargets()
|
|
|
|
|
|
|
|
self.shadowTargetsPrevious = shadowTargets
|
|
|
|
end
|
|
|
|
|
|
|
|
function Actor3D:removeOldShadowTargets()
|
|
|
|
if (self.shadowTargetsPrevious ~= nil) then
|
|
|
|
for i, target in ipairs(self.shadowTargetsPrevious) do
|
|
|
|
if (target.box ~= nil) then
|
|
|
|
target.box:removeShadowSource(self)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function Actor3D:redrawShadowCanvas()
|
|
|
|
if (self.box ~= nil) then
|
|
|
|
self.box:redrawShadowCanvas()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-30 17:07:58 +02:00
|
|
|
-- DRAW FUNCTIONS
|
|
|
|
-- Draw the actors.
|
|
|
|
|
2019-07-06 18:00:00 +02:00
|
|
|
function Actor3D:drawShadow(x, y)
|
|
|
|
love.graphics.setColor(0, 0, 0, 1)
|
|
|
|
love.graphics.rectangle("fill", x, y, self.w, self.h)
|
|
|
|
utils.graphics.resetColor()
|
|
|
|
end
|
|
|
|
|
2019-07-01 14:13:26 +02:00
|
|
|
function Actor3D:getShape()
|
|
|
|
return (self.x), (self.y - self.z - self.d), self.w, (self.h + self.d)
|
|
|
|
end
|
|
|
|
|
2019-06-30 17:07:58 +02:00
|
|
|
function Actor3D:draw()
|
|
|
|
self:drawStart()
|
2019-07-01 17:00:55 +02:00
|
|
|
if (self.box ~= nil) then
|
|
|
|
self.box:draw(self.x, self.y, self.z)
|
|
|
|
else
|
|
|
|
local x, y = math.floor(self.x), math.floor(self.y - self.z - self.d + (self.h/2))
|
|
|
|
self:drawSprite(x, y)
|
|
|
|
end
|
2019-06-30 17:07:58 +02:00
|
|
|
self:drawEnd()
|
|
|
|
end
|
|
|
|
|
|
|
|
return Actor3D
|