//----------------------------------------------------------------------------- // // Addapted from the MoveToward and FollowMouse behaviors in Torque Game Builder // Last modified 17 November 2008 // By Andrew Holifield // //----------------------------------------------------------------------------- if (!isObject(DarkDeedAIBehavior)) { // Template Name %template = new BehaviorTemplate(DarkDeedAIBehavior); %template.friendlyName = "Dark Deed Enemy AI"; %template.behaviorType = "Dark Deed"; %template.description = "Set the object to have enemy properties"; // Follow Variable %template.addBehaviorField(targetObject, "The object to move toward", object, "", t2dSceneObject); // Rotation Variable %template.addBehaviorField(rotationOffset, "The rotation offset (degrees)", float, 90.0); // Boolean variables to indicated if the enemy is speedy, strong, or retarded. Default is super average %template.addBehaviorField(b_isSpeedy, "Is the enemy a Speedy Sperm", bool, false); %template.addBehaviorField(b_isStrong, "Is the enemy a Strong Sperm", bool, false); %template.addBehaviorField(b_isTarded, "Is the enemy a Retarded Sperm", bool, false); // Starting Rotation %template.addBehaviorField(startRotation, "What angle should the enemy start at (in degrees)", float, 0.0); // Default damping value $damping = 1.0; } function DarkDeedAIBehavior::onAddToScene(%this) { // Enable Update and CollisionCallBack %this.owner.enableUpdateCallback(); %this.Owner.setCollisionCallback(1); // Set damping %this.owner.setDamping($damping); // Set Rotation %this.owner.setRotation(%this.startRotation); // Set Unit values %this.setUnitValues(); // When the player comes within range, the player class will change this value %this.owner.b_isDormant = true; } // Set the default enemy unit values based on unit type function DarkDeedAIBehavior::setUnitValues(%this) { // If more than one boolean for enemy type is selected, all booleans after the first are ignored if(%this.b_isSpeedy) { // Set default parameters for the speedy enemy sperm %this.turnSpeed = 45.0; %this.acceleration = 2.5; %this.attackAngle = 15.0; %this.range = 56.0; %this.Owner.setCollisionPhysics(0,1); %this.enemyNumber = 2; } else if(%this.b_isStrong) { // Set default parameters for the heavy enemy sperm %this.turnSpeed = 120.0; %this.acceleration = 0.5; %this.attackAngle = 45.0; %this.range = 64.0; %this.Owner.setCollisionPhysics(1,0); %this.enemyNumber = 1; } else if(%this.b_isTarded) { // Set default parameters for the retarded enemy sperm %direction = getRandom(-360000, 360000); %speed = getRandom(5000, 10000); %rotation = getRandom(-45000, 45000); %this.owner.setLinearVelocityPolar(%direction * 0.001, %speed * 0.001); %this.owner.setAngularVelocity(%rotation * 0.001); %this.range = 32.0; %this.owner.setDamping(0.0); %this.enemyNumber = 3; } else { // Set default parameters for the standard enemy sperm %this.turnSpeed = 90.0; %this.acceleration = 1.0; %this.attackAngle = 30.0; %this.range = 48.0; %this.Owner.setCollisionPhysics(0,1); %this.enemyNumber = 0; } } // On every update make the enemy sperm move toward the player. If I'm too far from the player do nothing. function DarkDeedAIBehavior::onUpdate(%this) { if(%this.owner.b_isDormant) return; // find the distance between the enemy and the player %distance = t2dVectorDistance(%this.targetObject.position, %this.owner.position); // If the actor is in range, rotate and move toward it if(%distance <= %this.range) { // if player is using push-back ability, go backward if(($b_isPushBack) && (!%this.b_isTarded)) { %this.owner.setImpulseForcePolar(%this.owner.rotation, -1.0); return; } // If this is a retarded sperm it doesn't move toward the player so do nothing if(%this.b_isTarded) return; // find how far to rotate in order to be facing the player %vector = t2dVectorSub(%this.targetObject.position, %this.owner.position); %targetRotation = mRadToDeg(mAtan(%vector.y, %vector.x)) + %this.rotationOffset; // Rotate toward the player %this.owner.rotateTo(%targetRotation, %this.turnSpeed, true, false, true, 0.1); // Move toward the player if(mAbs(%this.owner.getRotation() - %targetRotation) <= (%this.attackAngle)) %this.owner.setImpulseForcePolar(%this.owner.rotation, %this.acceleration); } } // This function checks if a heavy sperm has collided with another sperm // and if so, sets an impuse force on them to knock them back function DarkDeedAIBehavior::onCollision(%this, %dstObj, %srcRef, %dstRef, %time, %normal, %contactCount, %contacts) { // If the enemy is dormant, do nothing if(%this.owner.b_isDormant) return; // If the enemy is not a strong enemy return because it doesn't need to do anything special on a collision if(!%this.b_isStrong) return; // If a heavy sperm collided with something that is not another enemy or the player (like a wall), do nothing if (!isObject(%dstObj.getBehavior("DarkDeedAI")) && !isObject(%dstObj.getBehavior("DarkDeedControlsBehavior"))) return; // Set an impulse on the object the heavy sperm collided with in the direction that the heavy is going %dstObj.setImpulseForcePolar(%this.owner.rotation, 7.0); }