Demo combination experiment
by Ryan Cabaruvas · in Technical Issues · 07/21/2008 (8:32 pm) · 1 replies
Hello, I have been learning a lot about scripting through torque and I have recently tried combining the shooter tutorial and the fish game tutorial to give me a unique game. However, I have run into some problems combining the script here and there and I was wondering if you guys could help me out? Here is the scripts I am changing:
***player.cs***
function playerShip::onLevelLoaded(%this, %scenegraph)
{
$pShip = %this;
%this.isDead = false;
%this.startX = %this.getPositionX();
%this.startY = %this.getPositionY();
moveMap.bindCmd(keyboard, "w", "pShipUp();", "pShipUpStop();");
moveMap.bindCmd(keyboard, "s", "pShipDown();", "pShipDownStop();");
moveMap.bindCmd(keyboard, "a", "pShipLeft();", "pShipLeftStop();");
moveMap.bindCmd(keyboard, "d", "pShipRight();", "pShipRightStop();");
moveMap.bindCmd(keyboard, "space", "$pShip.createMissile();", "");
%this.lifeDrain = 2;
%this.lowerLife();
}
function playerShip::updateMovement(%this)
{
if(%this.isDead)
return;
if(%this.moveLeft)
{
%this.setLinearVelocityX( -$pShip.hSpeed );
}
if(%this.moveRight)
{
%this.setLinearVelocityX( $pShip.hSpeed );
}
if(%this.moveUp)
{
%this.setLinearVelocityY( -$pShip.vSpeed );
}
if(%this.moveDown)
{
%this.setLinearVelocityY( $pShip.vSpeed );
}
if(!%this.moveLeft && !%this.moveRight)
{
%this.setLinearVelocityX( 0 );
}
if(!%this.moveUp && !%this.moveDown)
{
%this.setLinearVelocityY( 0 );
}
}
function pShipUp()
{
$pShip.moveUp = true;
$pShip.updateMovement();
}
function pShipDown()
{
$pShip.moveDown = true;
$pShip.updateMovement();
}
function pShipLeft()
{
$pShip.moveLeft = true;
$pShip.updateMovement();
}
function pShipRight()
{
$pShip.moveRight = true;
$pShip.updateMovement();
}
function pShipLeftStop()
{
$pShip.moveLeft = false;
$pShip.updateMovement();
}
function pShipRightStop()
{
$pShip.moveRight = false;
$pShip.updateMovement();
}
function pShipUpStop()
{
$pShip.moveUp = false;
$pShip.updateMovement();
}
function pShipDownStop()
{
$pShip.moveDown = false;
$pShip.updateMovement();
}
function playerShip::spawn(%this)
{
%this.isDead = false;
%this.setPosition(%this.startX, %this.startY);
%this.setEnabled(true);
}
function playerShip::explode(%this)
{
%this.isDead = true;
%explosion = new t2dParticleEffect()
{
scenegraph = %this.scenegraph;
};
%explosion.loadEffect("~/data/particles/big_explosion.eff");
%explosion.setEffectLifeMode("KILL", 1);
%explosion.setPosition(%this.getPosition());
%explosion.playEffect();
%this.setEnabled(false);
%this.schedule(5000, "spawn");
}
function playerShip::createMissile(%this, %isDead)
{
if(!%this.isDead)
{
%this.playerMissile = new t2dStaticSprite()
{
scenegraph = %this.scenegraph;
class = playerMissile;
missileSpeed=%this.missileSpeed;
player = %this;
};
%this.playerMissile.fire();
}
}
function playerShip::onCollision( %srcObj, %dstObj, %srcRef, %dstRef, %time,
%normal, %contactCount, %contacts )
{
if(%this.isDead)
return;
if(%dstObj.class $= "enemyShip")
{
%srcObj.explode();
%dstObj.explode();
}
}
function playerShip::modifyLife(%this, %dmg)
{
%this.life += %dmg;
if(%this.life > 100)
{
%this.life = 100;
} else if (%this.life < 0)
{
%this.life = 0;
}
if(%this.life <= 30)
{
%this.explode();
}
}
function playerShip::lowerLife(%this)
{
%this.modifyLife(%this.lifeDrain);
if(!%this.dead)
{
%this.schedule(500, "lowerLife");
}
}
***game.cs***
//---------------------------------------------------------------------------------------------
// Torque Game Builder
// Copyright (C) GarageGames.com, Inc.
//---------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------
// startGame
// All game logic should be set up here. This will be called by the level builder when you
// select "Run Game" or by the startup process of your game to load the first level.
//---------------------------------------------------------------------------------------------
function startGame(%level)
{
exec("./fish.cs");
exec("./fishfood.cs");
exec("./player.cs");
exec("./playerMissile.cs");
Canvas.setContent(mainScreenGui);
Canvas.setCursor(DefaultCursor);
new ActionMap(moveMap);
moveMap.push();
$enableDirectInput = true;
activateDirectInput();
enableJoystick();
sceneWindow2D.loadLevel(%level);
}
//---------------------------------------------------------------------------------------------
// endGame
// Game cleanup should be done here.
//---------------------------------------------------------------------------------------------
function endGame()
{
sceneWindow2D.endLevel();
moveMap.pop();
moveMap.delete();
}
***meter.cs***
function meter::onLevelLoaded(%this, %scenegraph)
{
$hBar = %this;
%this.startX = 0;
%this.startY = 0;
%this.updateHeath();
}
function meter::updateHeath(%this)
{
%this.hp = $pShip.life;
echo("The new health is %this.hp");
// Redraw the Bar
%this.schedule(50, "updateHealth");
}
(sorry I couldn't figure out how to load up the actual text documents even with the learn how, if you could explain this I would greatly appreciate it.)
Ok, so here are my goals with these scripts.
The player.cs can move properly as well as fire so the scripting for this is fine, but I want to do more.
(1) I want the missiles to be able to fire both left and right (I think if than functions would work but I'm still learning so don't know how to write them successfully yet.)
(2) For a meter to be generated that shrinks instead of the actual player.cs and when it reaches its limit of shrinking the player.cs dies. The fish game demo had the fish shrink to its limit and then die, however I am doing a plane game so that would look rather funny to see the ship shrink. This is why I created a new .cs file called meter.cs.
(3) To link the function explode() to the death of the player.cs so when the meter reaches its limit and the player dies it produces the big_explosion.eff then spawns again in 5000 milliseconds.
(4) Last issue that is rather recent through trying to get this script to work is all my .eff files have suddenly grayed out. I have no idea what caused this, but no matter what I try they are all now gray appear blank on torque when I add them to the scenegraph. Interestingly enough my last project still can use .eff files that were loaded on it as well as new ones (however some were already grayed from the start). I have tried re-adding them to the particles folder, refreshing the object library, and closing and reloading torque, but the .eff files still remain grayed and invisible on the scenegraph. I also made sure they are visible when loaded so I am out of ideas on this one.
***currently what happens with this script is the playerShip class of the player.cs will shrink and flip upside down then go to the top of the screen. It will then respond in a few seconds where I originally placed it, but will be unmovable, however the create missile function will still work. So just one more thing I am currently trying to fix on a back up copy of this script.***
Thank you for your help,
Faytesp
***player.cs***
function playerShip::onLevelLoaded(%this, %scenegraph)
{
$pShip = %this;
%this.isDead = false;
%this.startX = %this.getPositionX();
%this.startY = %this.getPositionY();
moveMap.bindCmd(keyboard, "w", "pShipUp();", "pShipUpStop();");
moveMap.bindCmd(keyboard, "s", "pShipDown();", "pShipDownStop();");
moveMap.bindCmd(keyboard, "a", "pShipLeft();", "pShipLeftStop();");
moveMap.bindCmd(keyboard, "d", "pShipRight();", "pShipRightStop();");
moveMap.bindCmd(keyboard, "space", "$pShip.createMissile();", "");
%this.lifeDrain = 2;
%this.lowerLife();
}
function playerShip::updateMovement(%this)
{
if(%this.isDead)
return;
if(%this.moveLeft)
{
%this.setLinearVelocityX( -$pShip.hSpeed );
}
if(%this.moveRight)
{
%this.setLinearVelocityX( $pShip.hSpeed );
}
if(%this.moveUp)
{
%this.setLinearVelocityY( -$pShip.vSpeed );
}
if(%this.moveDown)
{
%this.setLinearVelocityY( $pShip.vSpeed );
}
if(!%this.moveLeft && !%this.moveRight)
{
%this.setLinearVelocityX( 0 );
}
if(!%this.moveUp && !%this.moveDown)
{
%this.setLinearVelocityY( 0 );
}
}
function pShipUp()
{
$pShip.moveUp = true;
$pShip.updateMovement();
}
function pShipDown()
{
$pShip.moveDown = true;
$pShip.updateMovement();
}
function pShipLeft()
{
$pShip.moveLeft = true;
$pShip.updateMovement();
}
function pShipRight()
{
$pShip.moveRight = true;
$pShip.updateMovement();
}
function pShipLeftStop()
{
$pShip.moveLeft = false;
$pShip.updateMovement();
}
function pShipRightStop()
{
$pShip.moveRight = false;
$pShip.updateMovement();
}
function pShipUpStop()
{
$pShip.moveUp = false;
$pShip.updateMovement();
}
function pShipDownStop()
{
$pShip.moveDown = false;
$pShip.updateMovement();
}
function playerShip::spawn(%this)
{
%this.isDead = false;
%this.setPosition(%this.startX, %this.startY);
%this.setEnabled(true);
}
function playerShip::explode(%this)
{
%this.isDead = true;
%explosion = new t2dParticleEffect()
{
scenegraph = %this.scenegraph;
};
%explosion.loadEffect("~/data/particles/big_explosion.eff");
%explosion.setEffectLifeMode("KILL", 1);
%explosion.setPosition(%this.getPosition());
%explosion.playEffect();
%this.setEnabled(false);
%this.schedule(5000, "spawn");
}
function playerShip::createMissile(%this, %isDead)
{
if(!%this.isDead)
{
%this.playerMissile = new t2dStaticSprite()
{
scenegraph = %this.scenegraph;
class = playerMissile;
missileSpeed=%this.missileSpeed;
player = %this;
};
%this.playerMissile.fire();
}
}
function playerShip::onCollision( %srcObj, %dstObj, %srcRef, %dstRef, %time,
%normal, %contactCount, %contacts )
{
if(%this.isDead)
return;
if(%dstObj.class $= "enemyShip")
{
%srcObj.explode();
%dstObj.explode();
}
}
function playerShip::modifyLife(%this, %dmg)
{
%this.life += %dmg;
if(%this.life > 100)
{
%this.life = 100;
} else if (%this.life < 0)
{
%this.life = 0;
}
if(%this.life <= 30)
{
%this.explode();
}
}
function playerShip::lowerLife(%this)
{
%this.modifyLife(%this.lifeDrain);
if(!%this.dead)
{
%this.schedule(500, "lowerLife");
}
}
***game.cs***
//---------------------------------------------------------------------------------------------
// Torque Game Builder
// Copyright (C) GarageGames.com, Inc.
//---------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------
// startGame
// All game logic should be set up here. This will be called by the level builder when you
// select "Run Game" or by the startup process of your game to load the first level.
//---------------------------------------------------------------------------------------------
function startGame(%level)
{
exec("./fish.cs");
exec("./fishfood.cs");
exec("./player.cs");
exec("./playerMissile.cs");
Canvas.setContent(mainScreenGui);
Canvas.setCursor(DefaultCursor);
new ActionMap(moveMap);
moveMap.push();
$enableDirectInput = true;
activateDirectInput();
enableJoystick();
sceneWindow2D.loadLevel(%level);
}
//---------------------------------------------------------------------------------------------
// endGame
// Game cleanup should be done here.
//---------------------------------------------------------------------------------------------
function endGame()
{
sceneWindow2D.endLevel();
moveMap.pop();
moveMap.delete();
}
***meter.cs***
function meter::onLevelLoaded(%this, %scenegraph)
{
$hBar = %this;
%this.startX = 0;
%this.startY = 0;
%this.updateHeath();
}
function meter::updateHeath(%this)
{
%this.hp = $pShip.life;
echo("The new health is %this.hp");
// Redraw the Bar
%this.schedule(50, "updateHealth");
}
(sorry I couldn't figure out how to load up the actual text documents even with the learn how, if you could explain this I would greatly appreciate it.)
Ok, so here are my goals with these scripts.
The player.cs can move properly as well as fire so the scripting for this is fine, but I want to do more.
(1) I want the missiles to be able to fire both left and right (I think if than functions would work but I'm still learning so don't know how to write them successfully yet.)
(2) For a meter to be generated that shrinks instead of the actual player.cs and when it reaches its limit of shrinking the player.cs dies. The fish game demo had the fish shrink to its limit and then die, however I am doing a plane game so that would look rather funny to see the ship shrink. This is why I created a new .cs file called meter.cs.
(3) To link the function explode() to the death of the player.cs so when the meter reaches its limit and the player dies it produces the big_explosion.eff then spawns again in 5000 milliseconds.
(4) Last issue that is rather recent through trying to get this script to work is all my .eff files have suddenly grayed out. I have no idea what caused this, but no matter what I try they are all now gray appear blank on torque when I add them to the scenegraph. Interestingly enough my last project still can use .eff files that were loaded on it as well as new ones (however some were already grayed from the start). I have tried re-adding them to the particles folder, refreshing the object library, and closing and reloading torque, but the .eff files still remain grayed and invisible on the scenegraph. I also made sure they are visible when loaded so I am out of ideas on this one.
***currently what happens with this script is the playerShip class of the player.cs will shrink and flip upside down then go to the top of the screen. It will then respond in a few seconds where I originally placed it, but will be unmovable, however the create missile function will still work. So just one more thing I am currently trying to fix on a back up copy of this script.***
Thank you for your help,
Faytesp
Ryan Cabaruvas