/* 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 "space_trigger.h" #include "debug.h" ObjectListDefinition(SpaceTrigger); CREATE_FACTORY(SpaceTrigger); CREATE_SCRIPTABLE_CLASS(SpaceTrigger, // 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(&SpaceTrigger::setTarget)) ->addMethod("setTriggerParent", Executor1(&SpaceTrigger::setTriggerParent)) ->addMethod("setTriggerRemains", Executor1(&SpaceTrigger::setTriggerRemains)) ->addMethod("setInvert", Executor1(&SpaceTrigger::setInvert)) ->addMethod("setRadius", Executor1(&SpaceTrigger::setRadius)) ->addMethod("setScript", Executor1(&SpaceTrigger::setScript)) ->addMethod("setFunction", Executor1(&SpaceTrigger::setFunction)) ->addMethod("setDebugDraw", Executor1(&SpaceTrigger::setDebugDraw)) ->addMethod("setAddToScript", Executor1(&SpaceTrigger::setAddToScript)) ); /** * Constructs a new SpaceTrigger. * @param root the xml element to load the parameters from. * */ SpaceTrigger::SpaceTrigger(const TiXmlElement* root) { this->registerObject(this, SpaceTrigger::_objectList); this->toList(OM_COMMON); radius = 10; invert = false; triggerRemains = true; } /** * Deletes the SpaceTrigger. * */ SpaceTrigger::~SpaceTrigger() { } /** * Reads the values from the tml element and sets them. * @param root the xml element to load the parameters from. * */ void SpaceTrigger::loadParams(const TiXmlElement* root) { ScriptTrigger ::loadParams(root); LoadParam(root, "radius", this, SpaceTrigger, setRadius) .describe("the fileName of the script, that should be triggered by this script trigger") .defaultValues(0); LoadParam(root, "worldentity", this, SpaceTrigger, setTarget) .describe("The name of the target as it is in the *.oxw file") .defaultValues(""); LoadParam(root, "invert", this, SpaceTrigger, setInvert) .describe("") .defaultValues(false); LoadParam(root, "triggerRemains", this, SpaceTrigger, setTriggerRemains) .describe("") .defaultValues(true); } /** * Sets the target(a world entity) of the SpaceTrigger. 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 SpaceTrigger::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 SpaceTrigger::tick(float timestep) { if( scriptFinished ) return; if(triggerRemains && scriptCalled ) { executeAction(timestep); return; } if( this->target != NULL) { if( !invert && this->distance(target) < radius) { //printf("Distance is %f \n", this->distance(target)); executeAction(timestep); scriptCalled = true; return; } else if( invert && this->distance(target) > radius) { executeAction(timestep); scriptCalled = true; return; } } }