/* orxonox - the future of 3D-vertical-scrollers Copyright (C) 2004 orx This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. ### File Specific: main-programmer: Silvan Nellen co-programmer: ... */ #include "util/loading/factory.h" #include "action_trigger.h" #include "debug.h" ObjectListDefinition(ActionTrigger); CREATE_FACTORY(ActionTrigger); CREATE_SCRIPTABLE_CLASS(ActionTrigger, // Coordinates addMethod("setAbsCoor", Executor3(&PNode::setAbsCoor)) ->addMethod("getAbsCoorX", Executor0ret(&PNode::getAbsCoorX)) ->addMethod("getAbsCoorY", Executor0ret(&PNode::getAbsCoorY)) ->addMethod("getAbsCoorZ", Executor0ret(&PNode::getAbsCoorZ)) //Properties ->addMethod("setName", Executor1(&BaseObject::setName)) ->addMethod("setTarget", Executor1(&ActionTrigger::setTarget)) ->addMethod("setTriggerParent", Executor1(&ScriptTrigger::setTriggerParent)) ->addMethod("setInvert", Executor1(&ActionTrigger::setInvert)) ->addMethod("setRadius", Executor1(&ActionTrigger::setRadius)) ->addMethod("setScript", Executor1(&ScriptTrigger::setScript)) ->addMethod("setFunction", Executor1(&ScriptTrigger::setFunction)) ->addMethod("setDebugDraw", Executor1(&ScriptTrigger::setDebugDraw)) ->addMethod("setAddToScript", Executor1(&ScriptTrigger::setAddToScript)) ); /** * Constructs a new ActionTrigger. * @param root the xml element to load the parameters from. * */ ActionTrigger::ActionTrigger(const TiXmlElement* root) { this->registerObject(this, ActionTrigger::_objectList); radius = 10; invert = false; if(root != NULL) { loadParams(root); if(addToScript && scriptIsOk) { script->addObject( "ActionTrigger", this->getName()); } } } /** * Deletes the ActionTrigger. * */ ActionTrigger::~ActionTrigger() { } /** * Reads the values from the tml element and sets them. * @param root the xml element to load the parameters from. * */ void ActionTrigger::loadParams(const TiXmlElement* root) { ScriptTrigger ::loadParams(root); LoadParam(root, "radius", this, ActionTrigger, setRadius) .describe("the fileName of the script, that should be triggered by this script trigger") .defaultValues(0); LoadParam(root, "worldentity", this, ActionTrigger, setTarget) .describe("The name of the target as it is in the *.oxw file") .defaultValues(""); LoadParam(root, "invert", this, ActionTrigger, setInvert) .describe("") .defaultValues(false); } /** * Sets the target(a world entity) of the ActionTrigger. If the distance between the target and this trigger is smaller than the radius, the script gets triggered. * @param target The worldentity that the script supervises. */ void ActionTrigger::setTarget(const std::string& target) { WorldEntity* targetEntity = WorldEntity::objectList().getObject(target); if (targetEntity != NULL) { this->setTarget(targetEntity); } else { PRINTF(2)("ERROR SCRTIPTTRIGGER : Target %s for %s::%s does not Exist\n", target.c_str(), this->getClassCName(), this->getCName()); } } void ActionTrigger::tick(float timestep) { if( scriptFinished ) return; if( this->target != NULL) { if( !invert && this->distance(target) < radius && actionScheduled) { //printf("Distance is %f \n", this->distance(target)); executeAction(timestep); scriptCalled = true; return; } else if( invert && this->distance(target) > radius && actionScheduled) { executeAction(timestep); scriptCalled = true; return; } } }