fix: improved tile collision

Fixes #75
Fixes #70
This commit is contained in:
Kazhnuz 2021-03-24 11:57:42 +01:00
parent d6995ecbba
commit 8a34c4f012
6 changed files with 23 additions and 10 deletions

View file

@ -43,6 +43,7 @@ function BaseActor:new(world, type, x, y, z, w, h, d, isSolid)
self:setDebugColor(1, 1, 1) self:setDebugColor(1, 1, 1)
self:register() self:register()
self.useTileCollision = true
end end
function BaseActor:setProperties(properties) function BaseActor:setProperties(properties)

View file

@ -104,11 +104,17 @@ function Hitbox2D:checkCollision(dx, dy, filter)
self:updatePosition() self:updatePosition()
local nx, ny = self.ox + dx, self.oy + dy local nx, ny = self.ox + dx, self.oy + dy
if (self.owner.useTileCollision and self.world.map.supportTileCollision) then
if (self:checkTileCollision(nx, self.y)) then if (self:checkTileCollision(nx, self.y)) then
nx = self.x nx = self.x + 1
nx = math.floor(nx / 16) * 16
self.owner.xsp = 0
end end
if (self:checkTileCollision(self.x, ny)) then if (self:checkTileCollision(self.x, ny)) then
ny = self.y ny = self.y + 1
ny = math.floor(ny / 16) * 16
self.owner.ysp = 0
end
end end
local x, y, cols, colNumber = self.world:checkCollision(self, nx, ny, filter) local x, y, cols, colNumber = self.world:checkCollision(self, nx, ny, filter)
local newx, newy = self:getNewOwnerPosition(x, y) local newx, newy = self:getNewOwnerPosition(x, y)
@ -117,7 +123,7 @@ function Hitbox2D:checkCollision(dx, dy, filter)
end end
function Hitbox2D:checkTileCollision(dx, dy) function Hitbox2D:checkTileCollision(dx, dy)
return self.world:haveTileTypeInRect(dx, dy, self.w, self.h, "solid") return self.world:haveTileTypeInRect(dx + 1, dy + 1, self.w - 2, self.h - 2, "solid")
end end
-- DRAW FUNCTIONS -- DRAW FUNCTIONS

View file

@ -11,6 +11,8 @@ function ParentMap:new(world, r, g, b)
local b = b or 128 local b = b or 128
self.backgroundColor = {r, g, b} self.backgroundColor = {r, g, b}
self.supportTileCollision = false
self:setPadding() self:setPadding()
self:register() self:register()
end end

View file

@ -9,6 +9,7 @@ TiledMap:implement(TiledMixins)
function TiledMap:new(world, mapfile) function TiledMap:new(world, mapfile)
self.wrapper = StiWrapper(self, mapfile, 0, 0) self.wrapper = StiWrapper(self, mapfile, 0, 0)
TiledMap.super.new(self, world) TiledMap.super.new(self, world)
self.supportTileCollision = true
self:setBackgroundColorFromTable(self.wrapper.sti.backgroundcolor) self:setBackgroundColorFromTable(self.wrapper.sti.backgroundcolor)
self.mapname = self:getMapName(mapfile) self.mapname = self:getMapName(mapfile)

View file

@ -38,18 +38,20 @@ end
function Parent:drawCharset(charset, charId) function Parent:drawCharset(charset, charId)
local x, y = utils.math.floorCoord(self.x, self.y) local x, y = utils.math.floorCoord(self.x, self.y)
y = y - 2
love.graphics.setColor(1,1,1,0.5) love.graphics.setColor(1,1,1,0.5)
self.assets.images["shadow"]:draw(x + 1, y + 10) self.assets.images["shadow"]:draw(x + 1, y + 11)
utils.graphics.resetColor() utils.graphics.resetColor()
if (not self.isTurning) then if (not self.isTurning) then
if (self:isMoving() and (not self.cantWalk)) then if (self:isMoving() and (not self.cantWalk)) then
self.charsetManager:draw(self.charset, self.charId, self.charDir, x, y - 1) self.charsetManager:draw(self.charset, self.charId, self.charDir, x, y)
else else
self.charsetManager:drawStanding(self.charset, self.charId, self.charDir, x, y - 1) self.charsetManager:drawStanding(self.charset, self.charId, self.charDir, x, y)
end end
else else
self.charsetManager:drawTurning(self.charset, self.charId, x, y - 1) self.charsetManager:drawTurning(self.charset, self.charId, x, y)
end end
end end

View file

@ -14,6 +14,7 @@ function TiledMultiMap:new(world, subworld, file)
self.w, self.h = 0, 0 self.w, self.h = 0, 0
self:loadMaps() self:loadMaps()
TiledMultiMap.super.new(self, world) TiledMultiMap.super.new(self, world)
self.supportTileCollision = true
self:setBackgroundColorFromTable(self.data.color) self:setBackgroundColorFromTable(self.data.color)
self.mapname = self.data.areaName self.mapname = self.data.areaName