From 89b2ffe483ca962a8aca80c2a0eb0750439249fb Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Sun, 23 Jun 2019 15:45:34 +0200 Subject: [PATCH] feat(world): add a way to automatically load hitbox from a file --- CHANGELOG.md | 2 + examples/gameplay/plateform/actors/player.lua | 5 +- .../plateform/assets/playerhitbox.lua | 13 ++++++ gamecore/modules/world/actors/actor2D.lua | 46 +++++++++++++++++++ 4 files changed, 63 insertions(+), 3 deletions(-) create mode 100644 examples/gameplay/plateform/assets/playerhitbox.lua diff --git a/CHANGELOG.md b/CHANGELOG.md index 18e70d3..7ed7e67 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/examples/gameplay/plateform/actors/player.lua b/examples/gameplay/plateform/actors/player.lua index 0c93e62..84e473a 100644 --- a/examples/gameplay/plateform/actors/player.lua +++ b/examples/gameplay/plateform/actors/player.lua @@ -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 diff --git a/examples/gameplay/plateform/assets/playerhitbox.lua b/examples/gameplay/plateform/assets/playerhitbox.lua new file mode 100644 index 0000000..d60c018 --- /dev/null +++ b/examples/gameplay/plateform/assets/playerhitbox.lua @@ -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} + } + } +} diff --git a/gamecore/modules/world/actors/actor2D.lua b/gamecore/modules/world/actors/actor2D.lua index 268af8a..b9fd140 100644 --- a/gamecore/modules/world/actors/actor2D.lua +++ b/gamecore/modules/world/actors/actor2D.lua @@ -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)