TGB/Tutorials/Asteroids/Section2
From TDN
[edit] Asteroids TutorialWritten for TGB Version: 1.6 |
|
[edit] Playing Around
[edit] Fixing Things
function AsteroidsControlsBehavior::onUpdate(%this)
{
if (%this.owner.control == 0)
return;
%this.owner.setImpulseForcePolar(%this.owner.rotation, (%this.up - %this.down) * %this.acceleration);
%this.owner.setAngularVelocity((%this.right - %this.left) * %this.turnSpeed);
}
Now we've added a way that tells this function if %this.owner.control (which is our player ship) is 0, it will return out and not apply any forces or velocity to the player ship. Save the changes and close this file.
function ShootsBehavior::fire(%this, %val)
{
if (%this.owner.control == 0)
return;
if (%val == 0)
{
cancel(%this.fireSchedule);
return;
}
if (!isObject(%this.projectile))
return;
%projectile = %this.projectile.cloneWithBehaviors();
%projectile.setPosition(%this.owner.position);
%projectile.setRotation(%this.owner.rotation);
%projectile.setLinearVelocityPolar(%this.owner.rotation, %this.projectileSpeed);
if (!isEventPending(%this.fireSchedule))
%this.fireSchedule = %this.schedule(%this.fireRate * 1000, "fire", 1);
}
Save and close the file. Our if statements are set up to ignore player input when dead, so let's set the toggle switching in the kill and spawn functions for the player ship. They can be found in takesDamage.cs. In the kill function we want to set the control to 0 and in the spawn function we'll set the control to 1. The kill function also has a setAtRest() command to ensure the player respawns at the same spot he was killed at.
function TakesDamageBehavior::kill(%this)
{
%this.lives--;
if (%this.lives <= 0)
{
%this.owner.safeDelete();
return;
}
%this.invincible = true;
%this.owner.visible = false;
%this.owner.control = 0;
%this.owner.setAtRest();
%this.owner.collisionActiveReceive = false;
%this.schedule(%this.respawnTime * 1000, "spawn");
}
function TakesDamageBehavior::spawn(%this)
{
%this.owner.collisionActiveReceive = true;
%this.schedule(%this.invincibleTime * 1000, "setVincible");
%this.health = %this.startHealth;
%this.owner.setBlendColor(1, 1, 1, 1);
%this.owner.visible = true;
%this.owner.control = 1;
%this.owner.setFrame(%this.startFrame);
if (isObject(%this.respawnEffect))
{
%explosion = %this.respawnEffect.cloneWithBehaviors();
%explosion.position = %this.owner.position;
%explosion.setEffectLifeMode("Kill", 1.0);
%explosion.playEffect();
}
}
Save the takesDamage file and play the scene. The player can now no longer shoot or move while dead. |



