feat(world3D): add a visible shape system to handle better visiblity

Actor2D also have a shape, in order to use them in other functions.
This commit is contained in:
Kazhnuz 2019-07-01 14:13:26 +02:00
parent ddebdba2e8
commit c06f1c49aa
3 changed files with 73 additions and 4 deletions

View file

@ -193,6 +193,10 @@ end
-- DRAW FUNCTIONS -- DRAW FUNCTIONS
-- Draw the actors. -- Draw the actors.
function Actor3D:getShape()
return (self.x), (self.y), self.w, (self.h)
end
function Actor2D:draw() function Actor2D:draw()
self:drawStart() self:drawStart()
local x, y = math.floor(self.x), math.floor(self.y) local x, y = math.floor(self.x), math.floor(self.y)

View file

@ -34,11 +34,13 @@ local Hitbox = require(cwd .. "utils.hitbox3D")
function Actor3D:new(world, type, x, y, z, w, h, d, isSolid) function Actor3D:new(world, type, x, y, z, w, h, d, isSolid)
Actor3D.super.new(self, world, type, x, y, z, w, h, d, isSolid) Actor3D.super.new(self, world, type, x, y, z, w, h, d, isSolid)
self:initHitboxes() self:initHitboxes()
self.world:registerShape(self)
end end
function Actor3D:destroy() function Actor3D:destroy()
self.world:removeActor(self) self.world:removeActor(self)
self.mainHitbox:destroy() self.mainHitbox:destroy()
self.world:removeShape(self)
self.isDestroyed = true self.isDestroyed = true
end end
@ -86,6 +88,7 @@ function Actor3D:move(dx, dy, dz)
if (self.isDestroyed == false) then if (self.isDestroyed == false) then
self.x, self.y, self.z, cols, colNumber = self.mainHitbox:checkCollision(dx, dy, dz, self.filter) self.x, self.y, self.z, cols, colNumber = self.mainHitbox:checkCollision(dx, dy, dz, self.filter)
self.mainHitbox:updatePosition() self.mainHitbox:updatePosition()
self.world:updateShape(self)
end end
return self.x, self.y, self.z, cols, colNumber return self.x, self.y, self.z, cols, colNumber
end end
@ -199,6 +202,10 @@ end
-- DRAW FUNCTIONS -- DRAW FUNCTIONS
-- Draw the actors. -- Draw the actors.
function Actor3D:getShape()
return (self.x), (self.y - self.z - self.d), self.w, (self.h + self.d)
end
function Actor3D:draw() function Actor3D:draw()
self:drawStart() self:drawStart()
local x, y = math.floor(self.x), math.floor(self.y - self.z - self.d + (self.h/2)) local x, y = math.floor(self.x), math.floor(self.y - self.z - self.d + (self.h/2))

View file

@ -31,6 +31,8 @@ local Bump = require(cwd .. "libs.bump")
local Bump3D = require(cwd .. "libs.bump-3dpd") local Bump3D = require(cwd .. "libs.bump-3dpd")
local CameraSystem = require(cwd .. "camera") local CameraSystem = require(cwd .. "camera")
local PADDING_VALUE = 10/100
function World3D:new(scene, actorlist, mapfile) function World3D:new(scene, actorlist, mapfile)
World3D.super.new(self, scene, actorlist, mapfile) World3D.super.new(self, scene, actorlist, mapfile)
end end
@ -42,6 +44,7 @@ function World3D:initActors()
self.currentCreationID = 0 self.currentCreationID = 0
self.actors = {} self.actors = {}
self.bodies = Bump3D.newWorld(50) self.bodies = Bump3D.newWorld(50)
self:initShapes()
end end
function World3D:newActor(name, x, y, z) function World3D:newActor(name, x, y, z)
@ -57,15 +60,41 @@ function World3D:moveActor(actor, x, y, z, filter)
end end
function World3D:getActorsInRect(x, y, w, h) function World3D:getActorsInRect(x, y, w, h)
-- Just a placeholder before adding a better algorythm return self:getShapeInRect(x, y, w, h)
World3D.super.getActorsInRect(x, y, w, h)
end end
function World3D:getVisibleActors(id) function BaseWorld:getVisibleActors(id)
local actors = {}
if (id ~= nil) then
local camx, camy, camw, camh = self.cameras:getViewCoordinate(id)
local paddingw = camw * PADDING_VALUE
local paddingh = camh * PADDING_VALUE
local x = camx - paddingw
local y = camy - paddingh
local w = camw + paddingw * 2
local h = camh + paddingh * 2
return self.actors actors = self:getActorsInRect(x, y, w, h)
else
actors = self:getActors()
end end
table.sort(actors, function(a,b)
if (a.y == b.y) then
if (a.depth == b.depth) then
return a.creationID < b.creationID
else
return a.depth > b.depth
end
else
return a.y < b.y
end
end)
return actors
end
-- PLAYER FUNCTIONS -- PLAYER FUNCTIONS
-- Load player stuff -- Load player stuff
@ -141,4 +170,33 @@ function World3D:getBodiesInRect(x, y, w, h)
return {} --self.bodies:queryRect(x, y, w, h) return {} --self.bodies:queryRect(x, y, w, h)
end end
-- SHAPE SYSTEM
-- Handle onscreen shapes
function World3D:initShapes()
self.shapes = Bump.newWorld(50)
end
function World3D:registerShape(actor)
local x, y, w, h = actor:getShape()
return self.shapes:add(actor, x, y, w, h)
end
function World3D:updateShape(actor)
local x, y, w, h = actor:getShape()
return self.shapes:update(actor, x, y, w, h)
end
function World3D:removeShape(actor)
return self.shapes:remove(actor)
end
function World3D:checkShapeIntersection(actor, x, y)
return self.shapes:check(actor, x, y)
end
function World3D:getShapeInRect(x, y, w, h)
return self.shapes:queryRect(x, y, w, h)
end
return World3D return World3D