diff --git a/examples/gameplay/moveplayer3D/actors/wall.lua b/examples/gameplay/moveplayer3D/actors/wall.lua index 1706f85..6de23b0 100644 --- a/examples/gameplay/moveplayer3D/actors/wall.lua +++ b/examples/gameplay/moveplayer3D/actors/wall.lua @@ -4,11 +4,7 @@ local Wall = Base:extend() function Wall:new(world, x, y, z, w, h, d) Wall.super.new(self, world, "wall", x, y, z, w, h, d, true) self:setDebugColor(0,0,0) -end - -function Wall:draw() - self:drawMainHitbox() - utils.graphics.resetColor( ) + self.boxes.Base(self, w, h, d) end return Wall diff --git a/gamecore/modules/world/actors/actor3D.lua b/gamecore/modules/world/actors/actor3D.lua index 541dbc9..21da23f 100644 --- a/gamecore/modules/world/actors/actor3D.lua +++ b/gamecore/modules/world/actors/actor3D.lua @@ -27,6 +27,7 @@ local BaseActor = require(cwd .. "baseactor") local Actor3D = BaseActor:extend() local Hitbox = require(cwd .. "utils.hitbox3D") +local Boxes = require(cwd .. "utils.boxes") -- INIT FUNCTIONS -- Initialise the actor and its base functions @@ -35,6 +36,7 @@ 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) + self.boxes = Boxes end function Actor3D:destroy() @@ -208,8 +210,12 @@ end function Actor3D:draw() self:drawStart() - local x, y = math.floor(self.x), math.floor(self.y - self.z - self.d + (self.h/2)) - self:drawSprite(x, y) + 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 self:drawEnd() end diff --git a/gamecore/modules/world/actors/utils/boxes/init.lua b/gamecore/modules/world/actors/utils/boxes/init.lua new file mode 100644 index 0000000..624e60f --- /dev/null +++ b/gamecore/modules/world/actors/utils/boxes/init.lua @@ -0,0 +1,30 @@ +-- box3D :: drawable box with shadow handling for fake3D 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('%.init$', '') .. "." + +local Boxes = {} + +Boxes.Base = require(cwd .. "parent") + +return Boxes diff --git a/gamecore/modules/world/actors/utils/boxes/parent.lua b/gamecore/modules/world/actors/utils/boxes/parent.lua new file mode 100644 index 0000000..0da34e3 --- /dev/null +++ b/gamecore/modules/world/actors/utils/boxes/parent.lua @@ -0,0 +1,93 @@ +-- box3D :: drawable box with shadow handling for fake3D 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 Box3D = Object:extend() + +function Box3D:new(owner, w, h, d) + self.owner = owner + + self.w = w + self.h = h + self.d = d + + self.haveLine = true + + self.texture = {} + self:setTopTexture() + self:setBottomTexture() + + self:register() +end + +function Box3D:register() + self.owner.box = self +end + +function Box3D:setSizeFromOwner() + self:setSize(self.owner.w, self.owner.h, self.owner.d) +end + +function Box3D:setSize() + self.w = w + self.h = h + self.d = d + + self:regenerate() +end + +function Box3D:setTopTexture() + local canvas = love.graphics.newCanvas(self.w, self.h) + love.graphics.setCanvas( canvas ) + utils.graphics.resetColor() + love.graphics.rectangle("fill", 0, 0, self.w, self.h) + love.graphics.setCanvas( ) + local imagedata = canvas:newImageData() + self.texture.top = love.graphics.newImage( imagedata ) + imagedata:release() + canvas:release() +end + +function Box3D:setBottomTexture() + local canvas = love.graphics.newCanvas(self.w, self.d) + love.graphics.setCanvas( canvas ) + love.graphics.setColor(0.9, 0.9, 0.9, 1) + love.graphics.rectangle("fill", 0, 0, self.w, self.d) + utils.graphics.resetColor() + love.graphics.setCanvas( ) + local imagedata = canvas:newImageData() + self.texture.bottom = love.graphics.newImage( imagedata ) + imagedata:release() + canvas:release() +end + +function Box3D:draw(x, y, z) + love.graphics.setColor(0, 0, 0, 1) + if (self.haveLine) then + love.graphics.rectangle("line", x, (y-z) - (self.d), self.w, self.d + self.h) + end + utils.graphics.resetColor() + love.graphics.draw(self.texture.top, x, (y-z) - (self.d)) + love.graphics.draw(self.texture.bottom, x, (y-z) - (self.d) + (self.h)) +end + +return Box3D