TargetMovementEntrySpline.cpp

Go to the documentation of this file.
00001 ////////////////////////////////////////////////////////////////////////////////
00002 //    Scorched3D (c) 2000-2009
00003 //
00004 //    This file is part of Scorched3D.
00005 //
00006 //    Scorched3D is free software; you can redistribute it and/or modify
00007 //    it under the terms of the GNU General Public License as published by
00008 //    the Free Software Foundation; either version 2 of the License, or
00009 //    (at your option) any later version.
00010 //
00011 //    Scorched3D is distributed in the hope that it will be useful,
00012 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //    GNU General Public License for more details.
00015 //
00016 //    You should have received a copy of the GNU General Public License
00017 //    along with Scorched3D; if not, write to the Free Software
00018 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 ////////////////////////////////////////////////////////////////////////////////
00020 
00021 #include <movement/TargetMovementEntrySpline.h>
00022 #include <common/Defines.h>
00023 #include <common/RandomGenerator.h>
00024 #include <engine/ScorchedContext.h>
00025 #include <target/Target.h>
00026 #include <target/TargetLife.h>
00027 #include <target/TargetState.h>
00028 #include <landscapemap/LandscapeMaps.h>
00029 #include <landscapedef/LandscapeTex.h>
00030 #include <landscapedef/LandscapeMovement.h>
00031 
00032 TargetMovementEntrySpline::TargetMovementEntrySpline() : context_(0)
00033 {
00034 }
00035 
00036 TargetMovementEntrySpline::~TargetMovementEntrySpline()
00037 {
00038 }
00039 
00040 void TargetMovementEntrySpline::generate(ScorchedContext &context, 
00041         RandomGenerator &random, LandscapeMovementType *movementType)
00042 {
00043         // Create the spline path the target will move on
00044         // Do this from the set of control points specified in the xml file
00045         LandscapeMovementTypeSpline *splineGroup = 
00046                 (LandscapeMovementTypeSpline *) movementType;
00047         context_ = &context;
00048         groundOnly_ = splineGroup->groundonly;
00049 
00050         // Create the control points from those specified
00051         std::vector<FixedVector> controlPoints;
00052         controlPoints.push_back(FixedVector::getNullVector());
00053         controlPoints.insert(controlPoints.end(), splineGroup->points.begin(), splineGroup->points.end());
00054 
00055         // Add a control point at the end to join the loop
00056         FixedVector midPt = (controlPoints[1] + controlPoints.back()) / 2;
00057         controlPoints.push_back(midPt);
00058         controlPoints.front() = midPt;
00059 
00060         // This is done mainly so they are draw correctly for debug
00061         if (groundOnly_) 
00062         {
00063                 std::vector<FixedVector>::iterator itor;
00064                 for (itor = controlPoints.begin();
00065                         itor != controlPoints.end();
00066                         itor++)
00067                 {
00068                         FixedVector &point = (*itor);
00069                         point[2] = context.getLandscapeMaps().getGroundMaps().getInterpHeight(
00070                                 point[0], point[1]);
00071                 }
00072         }
00073 
00074         // Generate the spline path
00075         path_.generate(controlPoints, 200, 3, splineGroup->speed);
00076         path_.simulate(splineGroup->starttime);
00077 
00078         // Find the group to move the objects in
00079         groupEntry_ = context.getLandscapeMaps().getGroundMaps().getGroups().
00080                 getGroup(splineGroup->groupname.c_str());
00081         if (!groupEntry_)
00082         {
00083                 S3D::dialogExit("TargetMovementEntrySpline", 
00084                         S3D::formatStringBuffer("Group entry %s has no objects defined for it", 
00085                         splineGroup->groupname.c_str()));
00086         }
00087 
00088         // Generate the list of offsets for all of the targets in the group
00089         std::map<unsigned int, TargetGroup *> &objects = groupEntry_->getObjects();
00090         std::map<unsigned int, TargetGroup *>::iterator itor;
00091         for (itor = objects.begin();
00092                 itor != objects.end();
00093                 itor++)
00094         {
00095                 unsigned int playerId = (*itor).first;
00096                 TargetGroup *entry = (*itor).second;
00097 
00098                 if (!entry->getTarget()->isTarget() ||
00099                         entry->getTarget()->getPlayerId() >= TargetID::MIN_TARGET_TRANSIENT_ID)
00100                 {
00101                         S3D::dialogExit("TargetMovementEntrySpline",
00102                                 "Movement can be assigned to level targets only (no tanks)");
00103                 }
00104                 if (entry->getTarget()->getTargetState().getMovement())
00105                 {
00106                         S3D::dialogExit("TargetMovementEntryBoids",
00107                                 "Only one movement can be assigned to each target");
00108                 }
00109 
00110                 // Set this target as moving
00111                 entry->getTarget()->getTargetState().setMovement(new TargetStateMovement());
00112         }
00113 }
00114 
00115 void TargetMovementEntrySpline::simulate(ScorchedContext &context, fixed frameTime)
00116 {
00117         // Update the position of all of the targets along the path
00118         path_.simulate(frameTime);
00119 
00120         // Get the position and direction along the current path
00121         FixedVector position;
00122         FixedVector direction;
00123         path_.getPathAttrs(position, direction);
00124         FixedVector directionPerp = direction.get2DPerp();
00125 
00126         // Move the position to the ground if set
00127         if (groundOnly_)
00128         {
00129                 position[2] = context_->getLandscapeMaps().getGroundMaps().getInterpHeight(
00130                         position[0], position[1]);
00131         }
00132 
00133         // For each target set position and rotation based on its offset
00134         std::map<unsigned int, TargetGroup *> &objects = groupEntry_->getObjects();
00135         std::map<unsigned int, TargetGroup *>::iterator itor;
00136         for (itor = objects.begin();
00137                 itor != objects.end();
00138                 itor++)
00139         {
00140                 unsigned int playerId = (*itor).first;
00141                 TargetGroup *groupEntry = (*itor).second;
00142                 
00143                 fixed angle = atan2x(direction[1], direction[0]);
00144                 fixed angleDegs = (angle / fixed::XPI) * 180 - 90;
00145 
00146                 // Update target
00147                 groupEntry->getTarget()->getLife().setTargetPositionAndRotation(
00148                         position, angleDegs);
00149                 groupEntry->getTarget()->getLife().setVelocity(direction);
00150         }
00151 }
00152 
00153 bool TargetMovementEntrySpline::writeMessage(NetBuffer &buffer)
00154 {
00155         fixed pathTime = path_.getPathTime();
00156         buffer.addToBuffer(pathTime);
00157         return true;
00158 }
00159 
00160 bool TargetMovementEntrySpline::readMessage(NetBufferReader &reader)
00161 {
00162         fixed pathTime = 0;
00163         if (!reader.getFromBuffer(pathTime)) return false;
00164         path_.setPathTime(pathTime);
00165         return true;
00166 }
00167 
00168 void TargetMovementEntrySpline::draw()
00169 {
00170 #ifndef S3D_SERVER
00171         path_.draw();
00172 #endif
00173 }

Generated on Mon Feb 16 15:14:51 2009 for Scorched3D by  doxygen 1.5.3