AI Tutorial - Setting Up the UFO
 
The Alien Invasion will consist of two parts. The first will be making the UFO move and making our game switch from day to night. The UFO will move side to side and up and down. When it moves left and right it will rotate to make a cool effect in our video game. Let's begin by creating a new project named Alien Invasion Tutorial.
Next Page
Previous Page
TGB Collisions
Download the AlienInvasionImagesPart1.zip and copy all of the images into your images folder. Go back to Torque Game Builder and click the create new ImageMap button. Load the UFO and set it's size to about 3.5 by 9. Set its layer to 10 and check the send and receive collision. Now we need to edit the collision poly. Make its collision to be about the same as the picture.
Now we can start making the UFO behavior. Make a new script file and add the following code.
We will be using the collision to check for people we abduct.
AI Tutorial - Coding the UFO
if (!isObject(UFOMovement))
{
%template =
new BehaviorTemplate(UFOMovement);
  
%template.friendlyName =
"UFO Controls";
%template.behaviorType =
"Input";
%template.description  =
"UFO movement control";
  
%template.addBehaviorField(upKey,
"Key to bind to ascend", keybind, "keyboard up");
%template.addBehaviorField(downKey,
"Key to bind to decend", keybind, "keyboard down");
%template.addBehaviorField(leftKey,
"Key to bind to move left", keybind, "keyboard left");
%template.addBehaviorField(rightKey,
"Key to bind to move right", keybind, "keyboard right");
  
%template.addBehaviorField(VerticalSpeed,
"Vertical acceleration", float, 7.5);
%template.addBehaviorField(HorizontalSpeed,
"Horizontal acceleration", float, 15.0);
%template.addBehaviorField(damping,
"Damp movement", float, 5.0);
}

function UFOMovement::onBehaviorAdd(%
this)
{
if (!isObject(moveMap))
return;

%
this.owner.enableUpdateCallback();
%
this.owner.setDamping(%this.damping);


moveMap.bindObj(getWord(%
this.upKey, 0), getWord(%this.upKey, 1), "moveUp", %this);
moveMap.bindObj(getWord(%
this.downKey, 0), getWord(%this.downKey, 1), "moveDown", %this);
moveMap.bindObj(getWord(%
this.leftKey, 0), getWord(%this.leftKey, 1), "moveLeft", %this);
moveMap.bindObj(getWord(%
this.rightKey, 0), getWord(%this.rightKey, 1), "moveRight", %this);
  
%
this.up = 0;
%
this.down = 0;
%
this.left = 0;
%
this.right = 0;
}

function UFOMovement::onBehaviorRemove(%
this)
{
if (!isObject(moveMap))
return;

%
this.owner.disableUpdateCallback();
  
moveMap.unbindObj(getWord(%
this.upKey, 0), getWord(%this.upKey, 1), %this);
moveMap.unbindObj(getWord(%
this.downKey, 0), getWord(%this.downKey, 1), %this);
moveMap.unbindObj(getWord(%
this.leftKey, 0), getWord(%this.leftKey, 1), %this);
moveMap.unbindObj(getWord(%
this.rightKey, 0), getWord(%this.rightKey, 1), %this);
  
%
this.up = 0;
%
this.down = 0;
%
this.left = 0;
%
this.right = 0;
}

function UFOMovement::moveUp(%
this, %val)
{
%
this.up = %val;
}

function UFOMovement::moveDown(%
this, %val)
{
%
this.down = %val;
}

function UFOMovement::moveLeft(%
this, %val)
{
%
this.left = %val;
}

function UFOMovement::moveRight(%
this, %val)
{
%
this.right = %val;
}

function UFOMovement::onUpdate(%
this)
{
if(%this.right == 1)
{
%
this.owner.setImpulseForce(%this.HorizontalSpeed,0);
%
this.TargetRotation = 15;
}
if(%this.left == 1)
{
%
this.owner.setImpulseForce(%this.HorizontalSpeed * -1,0);
%
this.TargetRotation = -15;
}
if(%this.right == 0 && %this.left == 0)
{
%
this.TargetRotation = 0;
}
if(%this.right == 1 && %this.left == 1)
{
%
this.TargetRotation = 0;
}
if(%this.up == 1)
{
%
this.owner.setImpulseForce(0 , %this.VerticalSpeed * -1);
}
if(%this.down == 1)
{
%
this.owner.setImpulseForce(0 , %this.VerticalSpeed);
}
%
this.owner.rotateTo(%this.TargetRotation, 60);
}

Save the file as UFOBehavior.cs. Before you can test out your UFO you have to reload the project in Torque Game Builder so the engine can recognize it. Now that that is done select your UFO and add the UFO Controls by clicking the green plus. Test out your UFO controls.

Let's look at this behavior. We have our standard arrow key which we can use by %this.up, %this.down, %this.left and %this.right. When you press the arrow keys the UFO moves to that direction according to the vertical acceleriation and horizontal acceleration. Now let
fs look at the TargetRotation. When the ship moves left and right it is rotated towards the TargetRotation by the line of code that reads %this.owner.rotateTo(%this.TargetRotation, 60);
AIImagesPart1.Zip