From 44f94f0752063852ededa73c0bb7eada50b08f33 Mon Sep 17 00:00:00 2001 From: Aki Date: Sat, 14 Jan 2017 20:45:08 +0100 Subject: Working hits; I really need to move it from world to player --- player.lua | 27 ++++++++++++++------------- world.lua | 6 ++++++ 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/player.lua b/player.lua index 5f95027..ba6e838 100644 --- a/player.lua +++ b/player.lua @@ -424,20 +424,21 @@ function Player:hit(direction) self:playSound(4) end --- Hittest --- Should be replaced with actual sensor; after moving collision callbacks into Player -function Player:testHit(target, ox, oy, sx, sy) - local x, y = self.body:getPosition() - for v=0,2 do - for h=0,2,1+v%2 do - if target.fixture:testPoint(x+ox+h*sx, y+oy+v*sy) then return true end - end - end - return false -end - -- Taking damage of `Player` by successful hit test -function Player:damage(horizontal, vertical) +function Player:damage(direction) + local horizontal, vertical = 0, 0 + if direction == "left" then + horizontal = -1 + end + if direction == "right" then + horizontal = 1 + end + if direction == "up" then + vertical = -1 + end + if direction == "down" then + vertical = 1 + end self:createEffect("hit") local x,y = self.body:getLinearVelocity() self.body:setLinearVelocity(x,0) diff --git a/world.lua b/world.lua index 57b8cd7..b01948e 100644 --- a/world.lua +++ b/world.lua @@ -379,6 +379,12 @@ function World.beginContact(a, b, coll) b:getUserData():playSound(3) end end + if a:getCategory() == 3 then + b:getUserData():damage(a:getUserData()[2]) + end + if b:getCategory() == 3 then + a:getUserData():damage(b:getUserData()[2]) + end end -- endContact function World.endContact(a, b, coll) -- cgit v1.1