--[[ This file is the property of the SMU Guildhall. It may not be reproduced or distributed in any form without the express permission of the SMU Guildhall. Please contact Ron Jenkins (jenkinsr@smu.edu) for further information. (c)SMU Guildhall, 2012. Notes: --]] --MD: File load and execution priority priority = 100 --MD: UI defaults active = true sizeScale = 1.0 jumpSpeed = 10.0 jumpSoundName = "silence" collisions = true bTopOnly = true --MD: Local variables local triggered = false local configuredCorrectly = true local debugBox = {} --[[ GuildEd UI Settings Created by : Morgan Davis, 2012 Last Updated by : Notes : User Interface Use JumpSpeed to change the height of the jump bTopOnly is used if you want the player to interact with the Jump Pad from only the top part of the box --]] ui = { active = { type = "bool", label = "Active at level start", default = true }, bTopOnly = { type = "bool", label = "Jump only on Top to Activate", default = true }, sizeScale = { type = "number", label = "Size multiplier", default = 1.0 }, jumpSpeed = { type = "number", label = "Jump Speed"}, idleName = { type = "anim", label = "Idle"}, jumpLName = { type = "anim", label = "Jump left"}, jumpRName = { type = "anim", label = "Jump right"}, jumpSoundName = { type = "string", label = "Jump sound" }, collisions = { type = "bool", label = "Collision" }, } --[[ Function Name : init Arguments : Description : Called by GameCore when actor is spawned in the world Created by : Morgan Davis, 2012 Last Updated by : Notes : Initiliazes animations and sound. The debugBox is only used with Debug program (not the actual collision). --]] function init() triggered = false iconVisible = true configuredCorrectly = true idle = ImageAnim( idleName ); jumpL = ImageAnim( jumpLName ); jumpR = ImageAnim( jumpRName ); jumpSound = Sound( jumpSoundName..".wav" ) if ( configuredCorrectly ) then g.physics:remove( rigidBody ) rigidBody = g.physics:createBox( x, y, RigidBody_SENSOR, sizeScale*iconW/pixelsPerUnit, sizeScale*iconH/pixelsPerUnit, 1 ) --JS: Calculate the bounding box of the trigger so we can draw it in debug mode local halfBox = ( sizeScale*iconW/pixelsPerUnit ) / 2 debugBox[1] = { x - halfBox, y - halfBox } debugBox[2] = { x + halfBox, y - halfBox } debugBox[3] = { x + halfBox, y + halfBox } debugBox[4] = { x - halfBox, y + halfBox } end anim = idle; end --[[ Function Name : beginContact Arguments : collidedWith, the actor that collided with me Description : Called by Box2D when a collision is detected with me Created by : Morgan Davis, 2012 Last Updated by : Notes : This checks to see: 1. Is it configured correctly? 2. Is the Jump pad activated on the Top only or any sides? 3. Does the box have collisions checked? 4. Is the linear Velocity Y of the player not nil? --]] function beginContact( o ) if ( configuredCorrectly ) then --Sets collision to activate only on top if ( bTopOnly == true ) then --note: you can toggle this in editor --Check type of interaction isType( o ) --Player only one who can interact (letter "o") if ( not g.player.rigidBody:isOnGround() and g.player.rigidBody:linearVelocityY() > 0 and o == g.player and collisions ) then if ( active ) and ( not triggered ) then triggered = true active = true end end --Allows jumping even if the player simply walks into the JumpPad else isType( o ) if ( g.player.rigidBody:linearVelocityY() ~= nil and o == g.player and collisions ) then if ( active ) and ( not triggered ) then triggered = true active = true end end end end end --[[ Function Name : update Arguments : dt, time passed since last update Description : Called by GameCore on the rendering schedule Created by : Morgan Davis, 2012 Last Updated by : Notes : Animations activate here. --]] function update( dt ) anim = idle; if ( configuredCorrectly ) then --JS: Has trigger EVENT been received? if ( active ) and ( not triggered ) then if ( triggered ) then debugPrint( "JumpPad Event received on " .. self.name ) if( not g.player.rigidBody:isOnGround() ) then if ( rigidBody:linearVelocityX() < 0 ) then anim = jumpL; else anim = jumpR; end end anim:draw( stateTime, x, y, 0, Image_COORDS_GAME ); end end end end --[[ Function Name : fixedUpdate Arguments : dt, time passed since last update Description : Called by GameCore on the physics schedule Created by : Morgan Davis, 2012 Last Updated by : Notes : This is where all the magic occurs. Linear velocity is set to g.player instead of the box's rigidbody data. Vy is reset each time so the jump is consistent every time. --]] function fixedUpdate( dt ) --takes rigid body from g.player vx = g.player.rigidBody:linearVelocityX() vy = g.player.rigidBody:linearVelocityY() if ( configuredCorrectly ) then --JS: Draw debug collision box if ( active ) then debugDrawCollision( debugBox, {0,255,0} ) end --MD: Did it Trigger? if (g.player.rigidBody ~= nil) then if ( triggered ) then --MD: Then make player go up vy = 0 debugPrint( vx, vy ) vy = vy - jumpSpeed jumpSound:play() triggered = false end g.player.rigidBody:setLinearVelocity( vx, vy ) end end end --[[ Function Name : activate() Arguments : Description : Define the actor's behavior when it is activated (i.e. triggered) Created by : Morgan Davis 05/8/2012 Last Updated by : Notes : MUST EXIST ON ALL ENTITIES since we can activate *anything* with a name --]] function activate() active = not active debugPrint( "Jump Pad Activated On " .. this.name ) end