From 2083374adc2ded636f17946b320a30011501bb86 Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Sat, 2 Nov 2024 09:08:04 +0100 Subject: [PATCH] feat: add a very basic shadow system --- framework/scenes/world/actors/init.lua | 17 +++++++++++++++ .../scenes/world/actors/physics/init.lua | 21 +++++++++++++++++++ .../scenes/world/actors/visuals/init.lua | 2 +- 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/framework/scenes/world/actors/init.lua b/framework/scenes/world/actors/init.lua index d480dc8..604b391 100644 --- a/framework/scenes/world/actors/init.lua +++ b/framework/scenes/world/actors/init.lua @@ -68,12 +68,29 @@ function Actor:destroy() end function Actor:draw() + self:drawShadow() self:_drawVisual() if (self.def.drawHitboxes == true) then self:_drawHitboxes() end end +function Actor:drawShadow() + if (self.world.type ~= "3D" and self.world.drawShadow ~= true) then + return + end + local shadowZ = self:getGroundZ() + if (shadowZ == nil) then + return + end + + local r, g, b, a = love.graphics.getColor( ) + local x, y = self.position.x + (self.dimensions.w / 2), self.position.y + (3 * self.dimensions.h / 4) - shadowZ + love.graphics.setColor(0, 0, 0, .3) + love.graphics.ellipse( "fill", math.floor(x), math.floor(y), self.dimensions.w / 2, self.dimensions.w / 4 ) + love.graphics.setColor(r, g, b, a) +end + -- Callbacks function Actor:onInit() diff --git a/framework/scenes/world/actors/physics/init.lua b/framework/scenes/world/actors/physics/init.lua index cdada6a..bbb5391 100644 --- a/framework/scenes/world/actors/physics/init.lua +++ b/framework/scenes/world/actors/physics/init.lua @@ -184,6 +184,27 @@ function Physics:checkGround() end end +function Physics:getGroundZ() + if (self.world.type ~= "3D") then + return 0 + end + -- On check to les acteurs qui pourraient ĂȘtre en dessous + local bodiesBehind = self.world:getBodiesInCube(self.position.x, self.position.y, self.position.z - 10000, self.dimensions.w, self.dimensions.h, 10000) + + local z = nil + for _, body in ipairs(bodiesBehind) do + if (body.type ~= "main" and body.isSolid and body.owner ~= self) then + local zToCheck = body.position.z + body.dimensions.d + if (z == nil) then + z = zToCheck + else + z = math.max(z, zToCheck) + end + end + end + return z +end + function Physics:_applyCollisionResponses(col) if (self.type == "player") and (col.other.owner ~= nil) and (col.other.owner.onPlayerCollision ~= nil) then col.other.owner:onPlayerCollision(self, "main", col) diff --git a/framework/scenes/world/actors/visuals/init.lua b/framework/scenes/world/actors/visuals/init.lua index 2f4f859..4336da7 100644 --- a/framework/scenes/world/actors/visuals/init.lua +++ b/framework/scenes/world/actors/visuals/init.lua @@ -41,7 +41,7 @@ function Visual:_getShapeData() if (self.world.type == "3D") then -- TODO: vĂ©rifier si l'algo est bon - position.y = position.y - self.dimensions.d - position.z + (self.dimensions.h) + position.y = position.y - self.dimensions.d - position.z + (3 * self.dimensions.h / 4) dimensions = { w = self.dimensions.w, h = self.dimensions.h + self.dimensions.d,