Skip to content

Commit

Permalink
Grappling Hook mostly works; need to make the end where it moves the …
Browse files Browse the repository at this point in the history
…character on top of the hook target.
  • Loading branch information
JesterXL committed Jan 22, 2012
1 parent ed02448 commit 2de5f97
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 15 deletions.
8 changes: 7 additions & 1 deletion code/com/jxl/zombiestick/gamegui/LevelView.lua
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions code/com/jxl/zombiestick/gamegui/hud/HudControls.lua
Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions code/com/jxl/zombiestick/gamegui/levelviews/GrappleTarget.lua
Expand Up @@ -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

Expand Down
6 changes: 6 additions & 0 deletions code/com/jxl/zombiestick/states/FreemanAttackState.lua
Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand Down
43 changes: 29 additions & 14 deletions code/com/jxl/zombiestick/states/GrappleState.lua
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions code/com/jxl/zombiestick/states/ReadyState.lua
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 2de5f97

Please sign in to comment.