2019-04-08 19:58:41 +02:00
|
|
|
-- actor2D.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.
|
|
|
|
]]
|
|
|
|
|
2019-05-05 20:13:19 +02:00
|
|
|
local cwd = (...):gsub('%.actor2D$', '') .. "."
|
2019-05-05 20:30:39 +02:00
|
|
|
local BaseActor = require(cwd .. "baseactor")
|
|
|
|
local Actor2D = BaseActor:extend()
|
2019-05-05 20:13:19 +02:00
|
|
|
|
2019-06-22 19:27:31 +02:00
|
|
|
local Hitbox = require(cwd .. "utils.hitbox2D")
|
|
|
|
|
2019-04-08 19:58:41 +02:00
|
|
|
-- INIT FUNCTIONS
|
|
|
|
-- Initialise the actor and its base functions
|
|
|
|
|
2019-04-25 17:54:51 +02:00
|
|
|
function Actor2D:new(world, type, x, y, w, h, isSolid)
|
2019-06-27 21:56:02 +02:00
|
|
|
Actor2D.super.new(self, world, type, x, y, 0, w, h, 0, isSolid)
|
|
|
|
self:initHitboxes()
|
2019-06-22 19:27:31 +02:00
|
|
|
end
|
|
|
|
|
2019-06-27 20:38:15 +02:00
|
|
|
function Actor2D:destroy()
|
2019-06-22 19:27:31 +02:00
|
|
|
self.world:removeActor(self)
|
2019-06-27 20:46:21 +02:00
|
|
|
self.mainHitbox:destroy()
|
2019-06-22 19:27:31 +02:00
|
|
|
self.isDestroyed = true
|
2019-04-22 12:13:01 +02:00
|
|
|
end
|
|
|
|
|
2019-06-27 21:56:02 +02:00
|
|
|
-- PHYSICS FUNCTIONS
|
|
|
|
-- Handle movement and collisions.
|
2019-05-05 21:25:25 +02:00
|
|
|
|
2019-04-22 12:02:52 +02:00
|
|
|
function Actor2D:autoMove(dt)
|
2019-06-23 15:45:34 +02:00
|
|
|
self:updateHitboxes()
|
2019-05-02 20:10:31 +02:00
|
|
|
self.onGround = false
|
2019-05-02 19:51:43 +02:00
|
|
|
self:applyGravity(dt)
|
|
|
|
|
2019-06-27 21:29:49 +02:00
|
|
|
local dx, dy = self:getFuturePosition(dt)
|
2019-06-27 22:14:12 +02:00
|
|
|
local newx, newy, cols, colNumber = self:move(dx, dy)
|
2019-04-28 09:31:10 +02:00
|
|
|
|
|
|
|
-- 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
|
|
|
|
|
2019-04-28 11:06:07 +02:00
|
|
|
self:solveAllCollisions(cols)
|
|
|
|
|
2019-06-27 21:29:49 +02:00
|
|
|
self:applyFriction(dt)
|
|
|
|
end
|
|
|
|
|
2019-06-27 22:14:12 +02:00
|
|
|
function Actor2D:changeSpeedToCollisionNormal(normal)
|
2019-04-28 11:09:55 +02:00
|
|
|
local xsp, ysp = self.xsp, self.ysp
|
2019-06-27 22:14:12 +02:00
|
|
|
local nx, ny = normal.x, normal.y
|
2019-04-28 11:09:55 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
self.xsp, self.ysp = xsp, ysp
|
|
|
|
end
|
|
|
|
|
2019-04-22 12:09:28 +02:00
|
|
|
function Actor2D:move(dx, dy)
|
2019-05-05 19:50:33 +02:00
|
|
|
local cols, colNumber = {}, 0
|
|
|
|
if (self.isDestroyed == false) then
|
2019-06-22 19:27:31 +02:00
|
|
|
self.x, self.y, cols, colNumber = self.mainHitbox:checkCollision(dx, dy, self.filter)
|
|
|
|
self.mainHitbox:updatePosition()
|
2019-05-05 19:50:33 +02:00
|
|
|
end
|
|
|
|
return self.x, self.y, cols, colNumber
|
|
|
|
end
|
|
|
|
|
|
|
|
function Actor2D:checkCollision(dx, dy)
|
|
|
|
local x, y, cols, colNumber = dx, dy, {}, 0
|
|
|
|
if (self.isDestroyed == false) then
|
2019-06-22 19:27:31 +02:00
|
|
|
x, y, cols, colNumber = self.mainHitbox:checkCollision(dx, dy, self.filter)
|
2019-05-05 19:50:33 +02:00
|
|
|
end
|
2019-04-22 12:09:28 +02:00
|
|
|
return self.x, self.y, cols, colNumber
|
2019-04-07 18:33:41 +02:00
|
|
|
end
|
|
|
|
|
2019-06-27 21:05:23 +02:00
|
|
|
-- GRAVITY SYSTEM FUNCTIONS
|
|
|
|
-- All functions related to gravity
|
|
|
|
|
2019-05-02 19:51:43 +02:00
|
|
|
function Actor2D:applyGravity(dt)
|
2019-06-27 21:03:34 +02:00
|
|
|
self.ysp = self.ysp + self.grav * dt
|
2019-05-05 13:01:25 +02:00
|
|
|
|
2019-06-27 21:03:34 +02:00
|
|
|
if utils.math.sign(self.ysp) == utils.math.sign(self.grav) then
|
|
|
|
self:checkGround( )
|
2019-05-05 13:01:25 +02:00
|
|
|
end
|
2019-05-02 19:51:43 +02:00
|
|
|
end
|
|
|
|
|
2019-06-27 21:05:23 +02:00
|
|
|
function Actor2D:checkGround()
|
|
|
|
local dx, dy = self.x, self.y + utils.math.sign(self.grav)
|
|
|
|
local newx, newy, cols, colNumber = self:checkCollision(dx, dy)
|
|
|
|
|
|
|
|
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
|
|
|
|
if col.normal.y ~= utils.math.sign(self.grav) then self.onGround = true end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-27 21:56:02 +02:00
|
|
|
-- COORDINATE/MOVEMENT FUNCTIONS
|
|
|
|
-- Handle coordinate
|
|
|
|
|
|
|
|
function Actor2D:getViewCenter()
|
|
|
|
local x, y = self:getCenter()
|
|
|
|
return x, y
|
|
|
|
end
|
|
|
|
|
2019-06-22 17:27:36 +02:00
|
|
|
-- HITBOXES FUNCTIONS
|
|
|
|
-- Functions related to actor hitboxes
|
2019-05-05 21:22:41 +02:00
|
|
|
|
2019-06-23 10:19:01 +02:00
|
|
|
function Actor2D:addHitboxFromFrameData(framedata, animationID, frameID, hitboxID)
|
2020-05-10 11:38:50 +02:00
|
|
|
local sx, sy = self.sprite:getScalling()
|
2019-06-23 10:19:01 +02:00
|
|
|
local type = framedata[1]
|
|
|
|
local ox = framedata[2]
|
|
|
|
local oy = framedata[3]
|
|
|
|
local w = framedata[4]
|
|
|
|
local h = framedata[5]
|
|
|
|
local isSolid = framedata[6] or false
|
|
|
|
local anim = animationID or "null"
|
|
|
|
local frame = frameID or 0
|
|
|
|
local id = hitboxID or 0
|
|
|
|
if (sx < 0) then
|
|
|
|
ox = self.w - ox - w
|
|
|
|
end
|
|
|
|
if (sy < 0) then
|
|
|
|
oy = self.h - oy - h
|
|
|
|
end
|
|
|
|
|
|
|
|
if (type == "main") then
|
|
|
|
self.mainHitbox:modify(ox, oy, w, h)
|
|
|
|
else
|
|
|
|
local hitboxName = anim .. frame .. type .. id
|
|
|
|
self:addHitbox(hitboxName, type, ox, oy, w, h, isSolid)
|
|
|
|
return hitboxName
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-22 22:11:22 +02:00
|
|
|
function Actor2D:initMainHitbox()
|
2019-06-22 19:27:31 +02:00
|
|
|
self.mainHitbox = Hitbox(self, self.type, 0, 0, self.w, self.h, self.isSolid)
|
2019-06-27 21:15:57 +02:00
|
|
|
self.mainHitbox:advertiseAsMainHitbox()
|
2019-05-05 21:22:41 +02:00
|
|
|
end
|
2019-04-08 19:58:41 +02:00
|
|
|
|
2019-06-22 22:11:22 +02:00
|
|
|
function Actor2D:addHitbox(name, type, ox, oy, w, h, isSolid)
|
|
|
|
if (self.hitboxes[name] ~= nil) then
|
2020-05-10 14:06:20 +02:00
|
|
|
core.debug:logWarn("actor2D", "the hitbox " .. name .. " already exists")
|
2019-06-22 22:11:22 +02:00
|
|
|
else
|
|
|
|
local hitbox = Hitbox(self, type, ox, oy, w, h, isSolid)
|
|
|
|
self.hitboxes[name] = hitbox
|
|
|
|
return hitbox
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function Actor2D:checkHitboxCollisions(name, filter)
|
|
|
|
self:checkHitboxCollisionsAtPoint(name, self.x, self.y, filter)
|
|
|
|
end
|
|
|
|
|
|
|
|
function Actor2D:checkHitboxCollisionsAtPoint(name, dx, dy, filter)
|
|
|
|
local x, y, cols, colNumber = dx, dy, {}, 0
|
|
|
|
local filter = filter or self.filter
|
|
|
|
if (self.isDestroyed == false) and (self.hitboxes[name] ~= nil) then
|
|
|
|
x, y, cols, colNumber = self.hitboxes[name]:checkCollision(dx, dy, filter)
|
|
|
|
local type = self.hitboxes[name].type
|
|
|
|
|
|
|
|
for i, col in ipairs(cols) do
|
|
|
|
self:hitboxResponse(name, type, col)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return x, y, cols, colNumber
|
|
|
|
end
|
|
|
|
|
2019-05-05 21:22:41 +02:00
|
|
|
-- DRAW FUNCTIONS
|
|
|
|
-- Draw the actors.
|
|
|
|
|
2019-07-12 23:18:01 +02:00
|
|
|
function Actor2D:getShape()
|
2019-07-01 14:13:26 +02:00
|
|
|
return (self.x), (self.y), self.w, (self.h)
|
|
|
|
end
|
|
|
|
|
2019-05-05 21:22:41 +02:00
|
|
|
function Actor2D:draw()
|
2019-06-13 18:39:09 +02:00
|
|
|
self:drawStart()
|
2019-05-05 21:22:41 +02:00
|
|
|
local x, y = math.floor(self.x), math.floor(self.y)
|
|
|
|
self:drawSprite(x, y)
|
2019-06-13 18:39:09 +02:00
|
|
|
self:drawEnd()
|
2019-05-05 21:22:41 +02:00
|
|
|
end
|
|
|
|
|
2019-04-01 08:08:54 +02:00
|
|
|
return Actor2D
|