feat(actor3D): add a basic 3D box drawing system for 3D actors

This commit is contained in:
Kazhnuz 2019-07-01 17:00:55 +02:00
parent cdddc79973
commit 5bdb275b8a
4 changed files with 132 additions and 7 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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