This commit is contained in:
aozhiwei 2022-12-07 15:04:01 +08:00
parent 31c819ee66
commit 8e54caefc2
2 changed files with 70 additions and 74 deletions

View File

@ -10,15 +10,6 @@
#include "creature.h"
#include "metamgr.h"
struct MovePathPoint
{
glm::vec3 src_pos;
glm::vec3 tar_pos;
glm::vec3 curr_pos;
glm::vec3 dir;
float distance = 0.0f;
};
MoveHelper::MoveHelper(Creature* owner)
{
owner_ = owner;
@ -34,35 +25,35 @@ bool MoveHelper::GetMovePosition(glm::vec3& out_pos)
a8::Vec2 dir;
auto& point = paths_[path_index_];
src_pos.x = point->src_pos.x;
src_pos.y = point->src_pos.z;
src_pos.x = point.src_pos.x;
src_pos.y = point.src_pos.z;
curr_pos.x = point->curr_pos.x;
curr_pos.y = point->curr_pos.z;
curr_pos.x = point.curr_pos.x;
curr_pos.y = point.curr_pos.z;
tar_pos.x = point->tar_pos.x;
tar_pos.y = point->tar_pos.z;
tar_pos.x = point.tar_pos.x;
tar_pos.y = point.tar_pos.z;
dir.x = point->dir.x;
dir.y = point->dir.z;
dir.x = point.dir.x;
dir.y = point.dir.z;
curr_pos = (curr_pos + dir * owner_->GetSpeed() * 1);
point->curr_pos.x = curr_pos.x;
point->curr_pos.z = curr_pos.y;
point.curr_pos.x = curr_pos.x;
point.curr_pos.z = curr_pos.y;
owner_->SetPos(curr_pos);
if (owner_->GetPos().Distance(src_pos) - point->distance >= 0.0001f) {
if (owner_->GetPos().Distance(src_pos) - point.distance >= 0.0001f) {
owner_->SetPos(tar_pos);
++path_index_;
if (path_index_ < paths_.size()) {
auto& next_point = paths_[path_index_];
a8::Vec2 v2;
v2.x = next_point->tar_pos.x;
v2.y = next_point->tar_pos.z;
v2.x = next_point.tar_pos.x;
v2.y = next_point.tar_pos.z;
a8::Vec2 dir = (v2 - owner_->GetPos());
dir.Normalize();
next_point->dir.x = dir.x;
next_point->dir.z = dir.y;
next_point.dir.x = dir.x;
next_point.dir.z = dir.y;
}
}
if (owner_->GetPos().x < 0 ||
@ -99,52 +90,52 @@ void MoveHelper::CalcTargetPos(float distance)
end.z = target_pos2d.y / 10.0f;
bool is_hit = false;
MovePathPoint* point = new MovePathPoint();
MovePathPoint point;
if (c->HasBuffEffect(kBET_ThroughWall) ||
c->HasBuffEffect(kBET_Fly) ||
c->HasBuffEffect(kBET_Jump)) {
point->tar_pos = end * 10.f;
if (point->tar_pos.x > owner_->room->GetMapMeta()->i->map_width() + 10) {
point->tar_pos.x = owner_->room->GetMapMeta()->i->map_width() - 10;
point.tar_pos = end * 10.f;
if (point.tar_pos.x > owner_->room->GetMapMeta()->i->map_width() + 10) {
point.tar_pos.x = owner_->room->GetMapMeta()->i->map_width() - 10;
}
if (point->tar_pos.z > owner_->room->GetMapMeta()->i->map_height() + 10) {
point->tar_pos.z = owner_->room->GetMapMeta()->i->map_height() - 10;
if (point.tar_pos.z > owner_->room->GetMapMeta()->i->map_height() + 10) {
point.tar_pos.z = owner_->room->GetMapMeta()->i->map_height() - 10;
}
if (point->tar_pos.x < 10) {
point->tar_pos.x = 10;
if (point.tar_pos.x < 10) {
point.tar_pos.x = 10;
}
if (point->tar_pos.z < 10) {
point->tar_pos.z = 10;
if (point.tar_pos.z < 10) {
point.tar_pos.z = 10;
}
} else {
int ret = owner_->room->map_instance->Raycast(0, start, end, hit_point);
if (ret > 0) {
is_hit = true;
point->tar_pos = hit_point * 10.f;
point.tar_pos = hit_point * 10.f;
} else {
point->tar_pos = end * 10.f;
point.tar_pos = end * 10.f;
}
}
a8::Vec2 v2;
v2.x = point->tar_pos.x;
v2.y = point->tar_pos.z;
point->distance = v2.Distance(owner_->GetPos());
point->src_pos.x = owner_->GetPos().x;
point->src_pos.z = owner_->GetPos().y;
point->curr_pos.x = owner_->GetPos().x;
point->curr_pos.z = owner_->GetPos().y;
v2.x = point.tar_pos.x;
v2.y = point.tar_pos.z;
point.distance = v2.Distance(owner_->GetPos());
point.src_pos.x = owner_->GetPos().x;
point.src_pos.z = owner_->GetPos().y;
point.curr_pos.x = owner_->GetPos().x;
point.curr_pos.z = owner_->GetPos().y;
if (point->tar_pos.x < 0 ||
point->tar_pos.z < 0) {
if (point.tar_pos.x < 0 ||
point.tar_pos.z < 0) {
abort();
}
#ifdef DEBUG
a8::XPrintf("CalcTargetPos src_pos:%f,%f tar_pos:%f,%f is_hit:%d start:%f,%f,%f end:%f,%f,%f distance:%f\n",
{
point->src_pos.x,
point->src_pos.z,
point->tar_pos.x,
point->tar_pos.z,
point.src_pos.x,
point.src_pos.z,
point.tar_pos.x,
point.tar_pos.z,
is_hit ? 1 : 0,
start.x,
@ -154,7 +145,7 @@ void MoveHelper::CalcTargetPos(float distance)
end.x,
end.y,
end.z,
point->distance
point.distance
});
#endif
@ -164,8 +155,8 @@ void MoveHelper::CalcTargetPos(float distance)
a8::Vec2 dir = (v2 - owner_->GetPos());
dir.Normalize();
point->dir.x = dir.x;
point->dir.z = dir.y;
point.dir.x = dir.x;
point.dir.z = dir.y;
paths_.push_back(point);
}
@ -173,9 +164,6 @@ void MoveHelper::CalcTargetPos(float distance)
void MoveHelper::ClearPath()
{
path_index_ = 0;
for (size_t i = 0; i < paths_.size(); ++i) {
delete paths_[i];
}
paths_.clear();
}
@ -199,26 +187,26 @@ void MoveHelper::AddPaths(const a8::Vec3& start, std::vector<a8::Vec3>& paths)
++i;
}
for (; i < paths.size(); ++i) {
MovePathPoint* p = new MovePathPoint();
p->src_pos = last_pos;
p->src_pos.x *= 10.f;
p->src_pos.y *= 10.f;
p->src_pos.z *= 10.f;
MovePathPoint p;
p.src_pos = last_pos;
p.src_pos.x *= 10.f;
p.src_pos.y *= 10.f;
p.src_pos.z *= 10.f;
p->tar_pos.x = paths[i].x * 10.f;
p->tar_pos.y = paths[i].y * 10.f;
p->tar_pos.z = paths[i].z * 10.f;
p.tar_pos.x = paths[i].x * 10.f;
p.tar_pos.y = paths[i].y * 10.f;
p.tar_pos.z = paths[i].z * 10.f;
p->curr_pos = p->src_pos;
p.curr_pos = p.src_pos;
glm::vec2 src_pos;
glm::vec2 tar_pos;
src_pos.x = p->src_pos.x;
src_pos.y = p->src_pos.z;
src_pos.x = p.src_pos.x;
src_pos.y = p.src_pos.z;
tar_pos.x = p->tar_pos.x;
tar_pos.y = p->tar_pos.z;
tar_pos.x = p.tar_pos.x;
tar_pos.y = p.tar_pos.z;
{
a8::Vec2 v1;
@ -229,7 +217,7 @@ void MoveHelper::AddPaths(const a8::Vec3& start, std::vector<a8::Vec3>& paths)
v2.x = src_pos.x;
v2.y = src_pos.y;
p->distance = v1.Distance(v2);
p.distance = v1.Distance(v2);
}
glm::vec2 dir = tar_pos - src_pos;
dir = glm::normalize(dir);
@ -241,9 +229,9 @@ void MoveHelper::AddPaths(const a8::Vec3& start, std::vector<a8::Vec3>& paths)
abort();
}
p->dir.x = dir.x;
p->dir.y = 0;
p->dir.z = dir.y;
p.dir.x = dir.x;
p.dir.y = 0;
p.dir.z = dir.y;
last_pos.x = paths[i].x;
last_pos.y = paths[i].y;

View File

@ -1,6 +1,14 @@
#pragma once
struct MovePathPoint;
struct MovePathPoint
{
glm::vec3 src_pos;
glm::vec3 tar_pos;
glm::vec3 curr_pos;
glm::vec3 dir;
float distance = 0.0f;
};
class Creature;
class MoveHelper
{
@ -14,7 +22,7 @@ class MoveHelper
size_t GetPathSize();
private:
std::vector<MovePathPoint*> paths_;
std::vector<MovePathPoint> paths_;
int path_index_ = 0;
Creature* owner_ = nullptr;
};