merge
This commit is contained in:
commit
cfee60d71a
14 files changed
+975
-15
No files matched your search
+57
-4
@@ -33,8 +33,46 @@ void Character::loadBinaryAnimation(AnimationState state, const std::string& fil
|
||||
|
||||
void Character::setTarget(const Eigen::Vector3f& target,
|
||||
std::function<void()> onArrived) {
|
||||
walkTarget = target;
|
||||
Eigen::Vector3f normalizedTarget(target.x(), 0.f, target.z());
|
||||
|
||||
const bool sameRequestedTarget =
|
||||
(normalizedTarget - requestedWalkTarget).norm() <= TARGET_REPLAN_THRESHOLD;
|
||||
const bool alreadyMovingToTarget =
|
||||
!pathWaypoints.empty() || (walkTarget - normalizedTarget).norm() <= TARGET_REPLAN_THRESHOLD;
|
||||
const bool stoppedAfterFailedPath =
|
||||
pathWaypoints.empty() && (walkTarget - position).norm() <= TARGET_REPLAN_THRESHOLD;
|
||||
|
||||
if (!onArrived && sameRequestedTarget && (alreadyMovingToTarget || stoppedAfterFailedPath)) {
|
||||
return;
|
||||
}
|
||||
|
||||
requestedWalkTarget = normalizedTarget;
|
||||
onArrivedCallback = std::move(onArrived);
|
||||
|
||||
if (pathPlanner) {
|
||||
pathWaypoints = pathPlanner(position, normalizedTarget);
|
||||
currentWaypointIndex = 0;
|
||||
|
||||
if (!pathWaypoints.empty()) {
|
||||
for (Eigen::Vector3f& waypoint : pathWaypoints) {
|
||||
waypoint.y() = 0.f;
|
||||
}
|
||||
walkTarget = pathWaypoints.back();
|
||||
return;
|
||||
}
|
||||
|
||||
walkTarget = Eigen::Vector3f(position.x(), 0.f, position.z());
|
||||
onArrivedCallback = nullptr;
|
||||
return;
|
||||
}
|
||||
|
||||
pathWaypoints.clear();
|
||||
currentWaypointIndex = 0;
|
||||
walkTarget = normalizedTarget;
|
||||
}
|
||||
|
||||
void Character::setPathPlanner(PathPlanner planner) {
|
||||
pathPlanner = std::move(planner);
|
||||
}
|
||||
|
||||
AnimationState Character::resolveActiveState() const {
|
||||
@@ -46,7 +84,12 @@ AnimationState Character::resolveActiveState() const {
|
||||
void Character::update(int64_t deltaMs) {
|
||||
//std::cout << "update called with deltaMs: " << deltaMs << std::endl;
|
||||
// Move toward walk target on the XZ plane
|
||||
Eigen::Vector3f toTarget = walkTarget - position;
|
||||
Eigen::Vector3f activeTarget = walkTarget;
|
||||
if (!pathWaypoints.empty() && currentWaypointIndex < pathWaypoints.size()) {
|
||||
activeTarget = pathWaypoints[currentWaypointIndex];
|
||||
}
|
||||
|
||||
Eigen::Vector3f toTarget = activeTarget - position;
|
||||
toTarget.y() = 0.f;
|
||||
float dist = toTarget.norm();
|
||||
|
||||
@@ -54,7 +97,7 @@ void Character::update(int64_t deltaMs) {
|
||||
Eigen::Vector3f dir = toTarget / dist;
|
||||
float moveAmount = walkSpeed * static_cast<float>(deltaMs) / 1000.f;
|
||||
if (moveAmount >= dist) {
|
||||
position = walkTarget;
|
||||
position = activeTarget;
|
||||
position.y() = 0.f;
|
||||
} else {
|
||||
position += dir * moveAmount;
|
||||
@@ -71,7 +114,17 @@ void Character::update(int64_t deltaMs) {
|
||||
{
|
||||
currentState = AnimationState::STAND;
|
||||
}
|
||||
if (onArrivedCallback) {
|
||||
|
||||
const bool hasNextWaypoint = !pathWaypoints.empty() && currentWaypointIndex + 1 < pathWaypoints.size();
|
||||
if (hasNextWaypoint) {
|
||||
++currentWaypointIndex;
|
||||
}
|
||||
else {
|
||||
pathWaypoints.clear();
|
||||
currentWaypointIndex = 0;
|
||||
}
|
||||
|
||||
if (!hasNextWaypoint && onArrivedCallback) {
|
||||
auto cb = std::move(onArrivedCallback);
|
||||
onArrivedCallback = nullptr;
|
||||
cb();
|
||||
|
||||
Reference in new issue
Block a user