Sync Tutorial - Adding Lasers
We are going to add link points on the eyes of the ship. These link points will determine where the bullets will fire out from.
Now that you have the ship selected. Click on the two big eyes on our player's ship to create the two link points.
Now that that is done we can start to script the guns.
function SceneWindow2D::onMouseDown(%this, %modifier, %worldPosition, %mouseClicks)
{
playerShip.setTimerOn(100); //used playerShip instead of %this because it would be the SceneWindow2D not our playerShip
}
function SceneWindow2D::onMouseUp(%this, %modifier, %worldPosition, %mouseClicks)
{
playerShip.setTimerOff();
}
function PlayerClass::OnTimer(%this)
{
if(playerShip.getlayer() == 5) //when the player dies it will be in layer 31 and can't shoot
{
%this.FindShootRotation();
%this.ShootLaser();
}
}
When the mouse is pressed down a timer is created. When the mouse lifts up the timer stops. This timer calculates which direction our laser should travel, and then shoot out that laser.
function PlayerClass::FindShootRotation(%this)
{
%vec = t2dVectorSub(%this.getPosition(), sceneWindow2D.getMousePosition());
%this.mouseRotation = -mRadToDeg(mATan(getWord(%vec,0),getWord(%vec,1)));
}
The code above finds the angle that the bullets need to travel in based off of the position of the mouse.
function PlayerClass::ShootLaser(%this)
{
//%this.FireLink is a variable used to determine which main laser fires
if(%this.FireLink == 0) //to do equal signs in an if statement you have to use ==
{
%this.FireLink += 1;
%this.FireLink1();
}
else
{
%this.FireLink -= 1;
%this.FireLink2();
}
//add other X 2 lasers here
}
function PlayerClass::FireLink1(%this)
{
%this.playerLaser = new t2dStaticSprite()
{
scenegraph = %this.scenegraph;
class = playerLaser;
};
%this.playerLaser.fire1();
}
function playerLaser::fire1(%this)
{
%this.setWorldLimit( kill, "-55 -42 55 42" );
%this.setLinearVelocityPolar(playerShip.mouseRotation,150);
%this.setRotation(playerShip.mouseRotation);
%this.setPosition(playerShip.getLinkPoint(1));
%this.setImageMap(Green_Blaster_BoltImageMap);
%this.setSize(1.25, 2);
%this.setLayer(5);
%this.setCollisionActive( true, true );
%this.setCollisionPhysics(false, false);
%this.setCollisionCallback(true);
%this.setCollisionLayers(8); // the enemies will be in layer 8
}
function PlayerClass::FireLink2(%this)
{
%this.playerLaser = new t2dStaticSprite()
{
scenegraph = %this.scenegraph;
class = playerLaser;
};
%this.playerLaser.fire2();
}
function playerLaser::fire2(%this)
{
%this.setWorldLimit( kill, "-55 -42 55 42" );
%this.setLinearVelocityPolar(playerShip.mouseRotation,150);
%this.setRotation(playerShip.mouseRotation);
%this.setPosition(playerShip.getLinkPoint(2));
%this.setImageMap(Green_Blaster_BoltImageMap);
%this.setSize(1.25, 2);
%this.setLayer(5);
%this.setCollisionActive( true, true );
%this.setCollisionPhysics(false, false);
%this.setCollisionCallback(true);
%this.setCollisionLayers(8); // the enemies will be in layer 8
}
This code shoots out a laser from a specified link point, and also sets the parameters for the lasers. You can now run the game and see
function playerLaser::onCollision( %srcObj, %dstObj, %srcRef, %dstRef, %time, %normal, %contactCount, %contacts )
{
//play particle effect for when the laser hits an object
%srcObj.safedelete(); //this object would be our laser
%dstObj.OnLaserHit(); //this object is whatever the laser hit
}
When our laser hits something in layer 8, which would be our enemy, the laser will be deleted by Torque, and the enemy will receive a %dstObj.OnLaserHit() function which will result in the enemy being damaged.
Now its time to get the actual laser into Torque. Save the image and import it into Torque Game Builder.