1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
|
/* ResidualVM - A 3D game interpreter
*
* ResidualVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the AUTHORS
* file distributed with this source distribution.
*
* 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
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "engines/stark/movement/walk.h"
#include "engines/stark/movement/shortestpath.h"
#include "engines/stark/movement/stringpullingpath.h"
#include "engines/stark/services/global.h"
#include "engines/stark/services/services.h"
#include "engines/stark/services/stateprovider.h"
#include "engines/stark/resources/anim.h"
#include "engines/stark/resources/floor.h"
#include "engines/stark/resources/floorface.h"
#include "engines/stark/resources/item.h"
#include "engines/stark/resources/location.h"
namespace Stark {
Walk::Walk(Resources::FloorPositionedItem *item) :
Movement(item),
_item3D(item),
_running(false),
_reachedDestination(false),
_turnDirection(kTurnNone) {
_path = new StringPullingPath();
}
Walk::~Walk() {
delete _path;
}
void Walk::start() {
Movement::start();
updatePath();
changeItemAnim();
Resources::Location *location = StarkGlobal->getCurrent()->getLocation();
location->startFollowingCharacter();
}
void Walk::stop() {
Movement::stop();
changeItemAnim();
}
void Walk::updatePath() const {
_path->reset();
Resources::Floor *floor = StarkGlobal->getCurrent()->getFloor();
Math::Vector3d startPosition = _item3D->getPosition3D();
int32 startFloorFaceIndex = floor->findFaceContainingPoint(startPosition);
if (startFloorFaceIndex == -1) {
startFloorFaceIndex = 0;
}
Resources::FloorFace *startFloorFace = floor->getFace(startFloorFaceIndex);
Resources::FloorEdge *startFloorEdge = startFloorFace->findNearestEdge(startPosition);
if (!startFloorEdge) {
// Unable to find enabled start edge
return;
}
int32 destinationFloorFaceIndex = floor->findFaceContainingPoint(_destination);
if (destinationFloorFaceIndex < 0) {
// Unable to find the destination's face
return;
}
Resources::FloorFace *destinationFloorFace = floor->getFace(destinationFloorFaceIndex);
Resources::FloorEdge *destinationFloorEdge = destinationFloorFace->findNearestEdge(_destination);
if (!destinationFloorEdge) {
// Unable to find enabled destination edge
return;
}
ShortestPath pathSearch;
ShortestPath::NodeList edgePath = pathSearch.search(startFloorEdge, destinationFloorEdge);
for (ShortestPath::NodeList::const_iterator it = edgePath.begin(); it != edgePath.end(); it++) {
_path->addStep((*it)->getPosition());
}
_path->addStep(_destination);
}
void Walk::onGameLoop() {
if (!_path->hasSteps()) {
// There is no path to the destination
stop();
return;
}
Resources::Floor *floor = StarkGlobal->getCurrent()->getFloor();
// Get the target to walk to
Math::Vector3d currentPosition = _item3D->getPosition3D();
Math::Vector3d target = _path->computeWalkTarget(currentPosition);
// Compute the direction to walk into
Math::Vector3d direction = target - currentPosition;
direction.z() = 0;
direction.normalize();
// Compute the angle with the current character direction
Math::Vector3d currentDirection = _item3D->getDirectionVector();
float directionDeltaAngle = computeAngleBetweenVectorsXYPlane(currentDirection, direction);
// If the angle between the current direction and the new one is too high,
// make the character turn on itself until the angle is low enough
if (ABS(directionDeltaAngle) > getAngularSpeed() + 0.1f) {
_turnDirection = directionDeltaAngle < 0 ? kTurnLeft : kTurnRight;
} else {
_turnDirection = kTurnNone;
}
float distancePerGameloop = computeDistancePerGameLoop();
Math::Vector3d newPosition;
if (_turnDirection == kTurnNone) {
// Compute the new position using the distance per gameloop
if (currentPosition.getDistanceTo(target) > distancePerGameloop) {
newPosition = currentPosition + direction * distancePerGameloop;
} else {
newPosition = target;
}
} else {
// The character does not change position when it is turning
newPosition = currentPosition;
direction = currentDirection;
Math::Matrix3 rot;
rot.buildAroundZ(_turnDirection == kTurnLeft ? -getAngularSpeed() : getAngularSpeed());
rot.transformVector(&direction);
}
// Some scripts expect the character position to be the exact destination
if (newPosition == _destination) {
_reachedDestination = true;
stop();
}
// Update the new position's height according to the floor
int32 newFloorFaceIndex = floor->findFaceContainingPoint(newPosition);
if (newFloorFaceIndex >= 0) {
floor->computePointHeightInFace(newPosition, newFloorFaceIndex);
} else {
warning("Item %s is walking off the floor", _item->getName().c_str());
}
// Update the item's properties
_item3D->setPosition3D(newPosition);
if (direction.getMagnitude() != 0.0) {
_item3D->setDirection(computeAngleBetweenVectorsXYPlane(direction, Math::Vector3d(1.0, 0.0, 0.0)));
}
if (newFloorFaceIndex >= 0) {
// When unable to find the face containing the new position, keep the previous one
// to prevent draw order glitches.
_item3D->setFloorFaceIndex(newFloorFaceIndex);
}
changeItemAnim();
}
float Walk::getAngularSpeed() const {
return _defaultTurnAngleSpeed * StarkGlobal->getMillisecondsPerGameloop();
}
float Walk::computeDistancePerGameLoop() const {
Resources::Anim *anim = _item->getAnim();
float distancePerGameloop = anim->getMovementSpeed() * StarkGlobal->getMillisecondsPerGameloop() / 1000.0;
return distancePerGameloop;
}
void Walk::setDestination(const Math::Vector3d &destination) {
_destination = destination;
}
void Walk::setRunning() {
_running = true;
changeItemAnim();
}
void Walk::changeItemAnim() {
if (_ended) {
_item->setAnimKind(Resources::Anim::kActorUsageIdle);
} else if (_turnDirection != kTurnNone) {
_item->setAnimKind(Resources::Anim::kActorUsageIdle);
} else if (_running) {
_item->setAnimKind(Resources::Anim::kActorUsageRun);
} else {
_item->setAnimKind(Resources::Anim::kActorUsageWalk);
}
}
void Walk::changeDestination(const Math::Vector3d &destination) {
setDestination(destination);
updatePath();
}
bool Walk::hasReachedDestination() const {
return _reachedDestination;
}
uint32 Walk::getType() const {
return kTypeWalk;
}
void Walk::saveLoad(ResourceSerializer *serializer) {
serializer->syncAsVector3d(_destination);
serializer->syncAsUint32LE(_running);
}
} // End of namespace Stark
|