Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //------------------------------------------------------------------------------
- // getTargetFromList Behavior for TGB
- // programmed by Deozaan
- //
- // The purpose of this behavior is to allow any object to easily pick one target
- // from a list of many objects based on varying and different criteria.
- //
- // %this.targetList is a SimSet list of targets to choose from. Make it easy on
- // yourself and use my getTargetList behavior to create this list.
- // %this.method is a string and is the method to use to decide which object to
- // target from the list. Details about each method are documented above each
- // method's corresponding function.
- // %this.useRange is a boolean and determines whether or not the object needs
- // to be within a certain range of the owner of this behavior.
- // %this.range is a float and is the range (radius) the object needs to be
- // within to be considered in the choosing of a target.
- // %this.target is an object, and stores the ID of the current target. This
- // value is later stored in %this.owner.target so that it can be accessed by
- // other behaviors.
- //
- //------------------------------------------------------------------------------
- if (!isObject(GetTargetFromListBehavior))
- {
- %template = new BehaviorTemplate(GetTargetFromListBehavior);
- %template.friendlyName = "Get Target from List";
- %template.behaviorType = "AI";
- %template.description = "Gets a specific target from a list";
- %template.addBehaviorField(targetList, "SimSet list to use", Default, %this.owner.targetList);
- %template.addBehaviorField(method, "Method to use to determine target", Enum, "Nearest", "Nearest" NL "Farthest"); // NL "Most Life" NL "Least Life" NL "Fastest" NL "Slowest" NL "Strongest" NL "Weakest");
- %template.addBehaviorField(useRange, "Use Range?", Bool, true);
- %template.addBehaviorField(range, "Range within which to get target", Float, 0.00);
- }
- function GetTargetFromListBehavior::onBehaviorAdd(%this)
- {
- %this.owner.enableUpdateCallback();
- // if this behavior was added with code, it won't have targetList or method
- // defined yet, so we deactivate it until we get some values passed in
- if (!%this.targetList || !%this.method)
- { %this.activate = false; }
- if (%this.range <= 0) // if we have a 0 or negative range, don't use range
- { %this.useRange = false; }
- }
- function GetTargetFromListBehavior::onUpdate(%this)
- {
- %this.getTarget();
- }
- function GetTargetFromListBehavior::settings(%this, %list, %method, %useRange, %range)
- //------------------------------------------------------------------------------
- // settings() makes it easy to change any of the settings through
- // script at any time. It is also very useful for adding behaviors manually,
- // which, depending on how you do it, they may not be initialized with settings
- // like they would if you enter everything in with the Level Builder.
- //------------------------------------------------------------------------------
- {
- %this.targetList = %list;
- %this.method = %method;
- %this.useRange = %useRange;
- %this.range = %range;
- if (%this.range <= 0) { %this.useRange = false; }
- %this.activate = true;
- }
- function GetTargetFromListBehavior::getTarget(%this)
- //------------------------------------------------------------------------------
- // getTarget() is merely a redirector that makes sure we it's supposed to be
- // running, and if so, redirects to the appropriate function for getting the
- // target according to the designated method.
- //------------------------------------------------------------------------------
- {
- if (%this.activate == false) // if we don't have settings, don't do anything
- { return; }
- switch$(%this.method)
- {
- case "Nearest":
- // targets closest object
- %this.getNearest();
- case "Farthest":
- // targets farthest object
- %this.getFarthest();
- case "Most Life":
- // tagets object with most life
- %this.getMostLife();
- case "Least Life":
- // targets object with least life
- %this.getLeastLife();
- case "Fastest":
- // targets fastest object
- %this.getFastest();
- case "Slowest":
- // targets slowest object
- %this.getSlowest();
- case "Strongest":
- // targets object with most defense
- %this.getStrongest();
- case "Weakest":
- // tagets object with least defense
- %this.getWeakest();
- }
- %this.owner.target = %this.target;
- }
- function GetTargetFromListBehavior::getNearest(%this)
- //------------------------------------------------------------------------------
- // getNearest() chooses a target based on least distance to the object's owner.
- //------------------------------------------------------------------------------
- {
- %target = %this.targetList.getObject(0);
- %dist = t2dVectorDistance(%target.position, %this.owner.position);
- %nearest = %dist;
- for( %i = 1; %i < %this.targetList.getCount(); %i++ )
- {
- %obj = %this.targetList.getObject(%i);
- %dist = t2dVectorDistance(%obj.position, %this.owner.position);
- if (%this.useRange == true)
- { // if we're using range to validate this
- if (%dist > %this.range) // and if the distance is outside the range
- { continue; } // try the next one in the list
- }
- if (%dist < %nearest)
- {
- %nearest = %dist;
- %target = %this.targetList.getObject(%i);
- }
- }
- %this.target = %target;
- }
- function GetTargetFromListBehavior::getFarthest(%this)
- //------------------------------------------------------------------------------
- // getFarthest() chooses a target based on most distance from the object's owner
- //------------------------------------------------------------------------------
- {
- %target = "None";
- %farthest = 0;
- for( %i = 0; %i < %this.targetList.getCount(); %i++ )
- {
- %obj = %this.targetList.getObject(%i);
- %dist = t2dVectorDistance(%obj.position, %this.owner.position);
- if (%this.useRange == true)
- { // if we're using range to validate this
- if (%dist > %this.range) // and if the distance is outside the range
- { continue; }// try the next one in the list
- }
- if (%dist > %farthest)
- {
- %farthest = %dist;
- %target = %this.targetList.getObject(%i);
- }
- }
- %this.target = %target;
- }
- function GetTargetFromListBehavior::getMostLife(%this)
- //------------------------------------------------------------------------------
- // getMostLife()
- //------------------------------------------------------------------------------
- {
- echo("getMostLife is not yet defined, getting Nearest instead");
- %this.getNearest();
- }
- function GetTargetFromListBehavior::getLeastLife(%this)
- //------------------------------------------------------------------------------
- // getLeastLife()
- //------------------------------------------------------------------------------
- {
- echo("getLeastLife is not yet defined, getting Nearest instead");
- %this.getNearest();
- }
- function GetTargetFromListBehavior::getFastest(%this)
- //------------------------------------------------------------------------------
- // getFastest()
- //------------------------------------------------------------------------------
- {
- echo("getFastest is not yet defined, getting Nearest instead");
- %this.getNearest();
- }
- function GetTargetFromListBehavior::getSlowest(%this)
- //------------------------------------------------------------------------------
- // getSlowest
- //------------------------------------------------------------------------------
- {
- echo("getSlowest is not yet defined, getting Nearest instead");
- %this.getNearest();
- }
- function GetTargetFromListBehavior::getStrongest(%this)
- //------------------------------------------------------------------------------
- // getStrongest
- //------------------------------------------------------------------------------
- {
- echo("getStrongest is not yet defined, getting Nearest instead");
- %this.getNearest();
- }
- function GetTargetFromListBehavior::getWeakest(%this)
- //------------------------------------------------------------------------------
- // getWeakest
- //------------------------------------------------------------------------------
- {
- echo("getWeakest is not yet defined, getting Nearest instead");
- %this.getNearest();
- }
Advertisement
Add Comment
Please, Sign In to add comment