feat(world): add a way to automatically load hitbox from a file

This commit is contained in:
Kazhnuz 2019-06-23 15:45:34 +02:00
parent 3fd8d2fecc
commit 89b2ffe483
4 changed files with 63 additions and 3 deletions

View file

@ -21,6 +21,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- **assets:** Add a new getCurrentAnimation function
- **world:** Add a way to automatically load hitbox from a file
### Changed
- **world2D:** Use a list for bodies (hitboxes, etc) and one other for actors

View file

@ -11,6 +11,7 @@ function Player:new(world, x, y, id)
self.direction = 1
self.punchName = ""
self:setHitboxFile("examples.gameplay.plateform.assets.playerhitbox")
end
function Player:updateStart(dt)
@ -33,14 +34,12 @@ function Player:updateStart(dt)
if self.keys["A"].isDown then
self.isPunching = true
self.punchName = self:addHitboxFromFrameData({"punch", 16, 6, 12, 12, false})
else
self.isPunching = false
self:removeHitbox(self.punchName)
end
if (self.isPunching) then
self:checkHitboxCollisions(self.punchName)
--self:checkHitboxCollisions(self.punchName)
end
if self.keys["start"].isPressed then

View file

@ -0,0 +1,13 @@
return {
["idle"] = {
{
{"main", 0, 0, 16, 24, true}
}
},
["punch"] = {
{
{"main", 0, 0, 16, 24, true},
{"punch", 16, 6, 12, 12, false}
}
}
}

View file

@ -55,6 +55,7 @@ function Actor2D:initMovement()
end
function Actor2D:autoMove(dt)
self:updateHitboxes()
self.onGround = false
self:applyGravity(dt)
@ -185,6 +186,51 @@ function Actor2D:initHitboxes(w, h)
self:initMainHitbox()
self.hitboxes = {}
self.hitboxListFile = ""
self.hitboxList = nil
end
function Actor2D:setHitboxFile(file)
self.hitboxList = require(file)
self.hitboxListFile = file
end
function Actor2D:getAutomaticHitboxLoading()
return (self.hitboxList ~= nil)
end
function Actor2D:getHitboxFile()
return self.hitboxListFile
end
function Actor2D:getHitboxList(animation, frame)
if (animation == nil) or (self.hitboxList == nil) then
return self.hitboxList
else
local list = self.hitboxList[animation]
if (frame == nil) or (list == nil) then
return list
else
return list[frame]
end
end
end
function Actor2D:updateHitboxes()
if (self.hitboxList ~= nil) then
self:purgeHitbox()
local animation, frame
animation = self:getCurrentAnimation()
frame = self:getRelativeFrame()
local hitboxList = self:getHitboxList(animation, frame)
if (hitboxList ~= nil) then
for i,v in ipairs(hitboxList) do
self:addHitboxFromFrameData(v, animation, frame, i)
end
end
end
end
function Actor2D:addHitboxFromFrameData(framedata, animationID, frameID, hitboxID)