diff --git a/gamecore/modules/world/actors/actor2D.lua b/gamecore/modules/world/actors/actor2D.lua index 292c06d..04aa065 100644 --- a/gamecore/modules/world/actors/actor2D.lua +++ b/gamecore/modules/world/actors/actor2D.lua @@ -193,6 +193,10 @@ end -- DRAW FUNCTIONS -- Draw the actors. +function Actor3D:getShape() + return (self.x), (self.y), self.w, (self.h) +end + function Actor2D:draw() self:drawStart() local x, y = math.floor(self.x), math.floor(self.y) diff --git a/gamecore/modules/world/actors/actor3D.lua b/gamecore/modules/world/actors/actor3D.lua index 0f255ab..541dbc9 100644 --- a/gamecore/modules/world/actors/actor3D.lua +++ b/gamecore/modules/world/actors/actor3D.lua @@ -34,11 +34,13 @@ local Hitbox = require(cwd .. "utils.hitbox3D") 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) self:initHitboxes() + self.world:registerShape(self) end function Actor3D:destroy() self.world:removeActor(self) self.mainHitbox:destroy() + self.world:removeShape(self) self.isDestroyed = true end @@ -86,6 +88,7 @@ function Actor3D:move(dx, dy, dz) if (self.isDestroyed == false) then self.x, self.y, self.z, cols, colNumber = self.mainHitbox:checkCollision(dx, dy, dz, self.filter) self.mainHitbox:updatePosition() + self.world:updateShape(self) end return self.x, self.y, self.z, cols, colNumber end @@ -199,6 +202,10 @@ end -- DRAW FUNCTIONS -- 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() self:drawStart() local x, y = math.floor(self.x), math.floor(self.y - self.z - self.d + (self.h/2)) diff --git a/gamecore/modules/world/world3D.lua b/gamecore/modules/world/world3D.lua index 4a68d9c..06813db 100644 --- a/gamecore/modules/world/world3D.lua +++ b/gamecore/modules/world/world3D.lua @@ -31,6 +31,8 @@ local Bump = require(cwd .. "libs.bump") local Bump3D = require(cwd .. "libs.bump-3dpd") local CameraSystem = require(cwd .. "camera") +local PADDING_VALUE = 10/100 + function World3D:new(scene, actorlist, mapfile) World3D.super.new(self, scene, actorlist, mapfile) end @@ -42,6 +44,7 @@ function World3D:initActors() self.currentCreationID = 0 self.actors = {} self.bodies = Bump3D.newWorld(50) + self:initShapes() end function World3D:newActor(name, x, y, z) @@ -57,15 +60,41 @@ function World3D:moveActor(actor, x, y, z, filter) end function World3D:getActorsInRect(x, y, w, h) - -- Just a placeholder before adding a better algorythm - World3D.super.getActorsInRect(x, y, w, h) + return self:getShapeInRect(x, y, w, h) 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 + + 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 -- Load player stuff @@ -141,4 +170,33 @@ function World3D:getBodiesInRect(x, y, w, h) return {} --self.bodies:queryRect(x, y, w, h) 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