Permalink
Browse files

Grappling Hook mostly works; need to make the end where it moves the …

…character on top of the hook target.
  • Loading branch information...
JesterXL committed Jan 22, 2012
1 parent ed02448 commit 2de5f975bd0dea60fcd678a933d25cc50f3d2b02
@@ -276,7 +276,13 @@ function LevelView:new(x, y, width, height)
elseif terrainType == "Grapple Target" then
params.name = "Grapple Target"
terrain = GrappleTarget:new(params.x, params.y)
- print("ZOMG GRAPPLETARGET ZOMG")
+ function terrain:touch(event)
+ if event.phase == "began" then
+ level.player.lastGrappleTarget = self
+ Runtime:dispatchEvent({name="onGrappleTargetTouched", target=self})
+ end
+ end
+ terrain:addEventListener("touch", terrain)
end
self:insertChild(terrain)
end
@@ -76,9 +76,12 @@ function HudControls:new(width, height)
end
local function onTouch(event)
+ print("HudControls::onTouch")
if event.phase == "began" then
+ if gunButton.alpha == 1 then
controls:dispatchEvent({name="onAttackButtonTouch", target=controls,
phase=event.phase, button=clickRect, x=event.x, y=event.y})
+ end
end
end
@@ -7,6 +7,10 @@ function GrappleTarget:new(x, y)
grappleTarget.classType = "GrappleTarget"
grappleTarget.x = x
grappleTarget.y = y
+
+ physics.addBody( grappleTarget, {isSensor = true } )
+ grappleTarget.bodyType = "kinematic"
+
return grappleTarget
end
@@ -21,6 +21,11 @@ function FreemanAttackState:new()
function state:attack(event)
local player = self.entity
+ if player.selectedWeapon ~= "gun" then
+ player.fsm:changeStateToAtNextTick("ready")
+ return
+ end
+
player.attacking = true
player:showSprite("attack")
@@ -51,6 +56,7 @@ function FreemanAttackState:new()
levelView.gameLoop:addLoop(bullet)
player.fsm:changeStateToAtNextTick("ready")
elseif player.selectedWeapon == "grapple" then
+ -- TODO: remove this
player.fsm:changeStateToAtNextTick("grapple")
end
end
@@ -9,58 +9,73 @@ function GrappleState:new()
function state:onEnterState(event)
print("GrappleState::onEnterState")
local player = self.entity
-
+ player.isFixedRotation = false
player.attacking = true
player:showSprite("attack")
player:performedAction("attack")
local levelView = LevelView.instance
local lc = levelView.levelChildren
+ local grappleTarget = player.lastGrappleTarget
local targetX, targetY
--print("player.lastAttackX: ", player.lastAttackX)
- local localX, localY = levelView:localToContent(player.lastAttackX, player.lastAttackY)
- local variance = 80
- targetX = localX + (lc.x * -1) + (math.random() * variance) - (variance / 2)
- targetY = localY + (lc.y * -1) + (math.random() * variance) - (variance / 2)
- -- SWALLOWS exception in here somewhere.....
- local touchJoint = physics.newJoint( "touch", player, targetX, targetY )
+ local localX, localY = levelView:localToContent(grappleTarget.x, grappleTarget.x)
+ local touchJoint = physics.newJoint( "distance", player, grappleTarget, player.x,player.y, grappleTarget.x, grappleTarget.y)
+ if touchJoint == nil then
+ error("Failed to create touchJoint")
+ return false
+ end
self.touchJoint = touchJoint
+ --touchJoint.frequency = 0.5
+ --touchJoint.dampingRatio = 0
+ player:setLinearVelocity( 0, 6)
end
function state:onExitState(event)
print("GrappleState::onExitState")
+ local player = self.entity
local grappleLine = self.grappleLine
if grappleLine ~= nil then
self.grappleLine = nil
grappleLine:removeSelf()
end
self.touchJoint:removeSelf()
+
+ player:setLinearVelocity( 0, 0 )
+ player.angularVelocity = 0
+ player.isFixedRotation = true
+ player.rotation = 0
+ player:applyLinearImpulse(0, -10, player.width / 2, player.height / 2)
end
function state:tick(time)
-
local touchJoint = self.touchJoint
if touchJoint == nil then
return
end
- local minX, minY = touchJoint:getAnchorA()
- local maxX, maxY = touchJoint:getAnchorB()
+ local minX, minY = touchJoint:getAnchorB()
local player = self.entity
- local levelView = LevelView.instance
local grappleLine = self.grappleLine
if grappleLine ~= nil then
grappleLine:removeSelf()
end
-
- grappleLine = display.newLine(levelView, minX, minY, maxX, maxY)
- grappleLine.width = 2
+ grappleLine = display.newLine(player.x + (player.width / 2), player.y + (player.height / 2), minX, minY)
+ grappleLine.width = 3
grappleLine:setColor(0, 0, 0)
self.grappleLine = grappleLine
+ --LevelView.instance:insertChild(grappleLine)
+
+ touchJoint.length = touchJoint.length - 3
+ if touchJoint.length < 2 then
+ self.stateMachine:changeStateToAtNextTick("ready")
+ return
+ end
+ print("touchJoint.length: ", touchJoint.length)
end
return state
@@ -20,6 +20,7 @@ function ReadyState:new()
Runtime:addEventListener("onMoveLeftStarted", self)
Runtime:addEventListener("onMoveRightStarted", self)
Runtime:addEventListener("onAttackStarted", self)
+ Runtime:addEventListener("onGrappleTargetTouched", self)
Runtime:addEventListener("onJumpStarted", self)
Runtime:addEventListener("onJumpLeftStarted", self)
Runtime:addEventListener("onJumpRightStarted", self)
@@ -31,6 +32,7 @@ function ReadyState:new()
Runtime:removeEventListener("onMoveLeftStarted", self)
Runtime:removeEventListener("onMoveRightStarted", self)
Runtime:removeEventListener("onAttackStarted", self)
+ Runtime:removeEventListener("onGrappleTargetTouched", self)
Runtime:removeEventListener("onJumpStarted", self)
Runtime:removeEventListener("onJumpLeftStarted", self)
Runtime:removeEventListener("onJumpRightStarted", self)
@@ -64,9 +66,15 @@ function ReadyState:new()
end
function state:onAttackStarted(event)
+ print("ReadyState::onAttackStarted")
self.stateMachine:changeStateToAtNextTick("attack")
end
+ function state:onGrappleTargetTouched(event)
+ print("ReadyState::onGrappleTargetTouched")
+ self.stateMachine:changeStateToAtNextTick("grapple")
+ end
+
function state:onJumpStarted(event)
self.stateMachine:changeStateToAtNextTick("jump")
end

0 comments on commit 2de5f97

Please sign in to comment.