updatemove ok
This commit is contained in:
parent
89887d287a
commit
1a519f0269
@ -78,12 +78,12 @@ void AndroidAI::DoMove()
|
||||
Human* hum = (Human*)owner;
|
||||
int speed = std::max(1, (int)hum->GetSpeed());
|
||||
for (int i = 0; i < speed; ++i) {
|
||||
Vector2D old_pos = owner->pos;
|
||||
owner->pos = owner->pos + hum->move_dir * (i + 1);
|
||||
if (IsCollision()) {
|
||||
owner->pos = old_pos;
|
||||
Vector2D old_pos = hum->pos;
|
||||
hum->pos = hum->pos + hum->move_dir * (i + 1);
|
||||
if (hum->IsCollision()) {
|
||||
hum->pos = old_pos;
|
||||
if (i == 0) {
|
||||
FindPath();
|
||||
hum->FindPath();
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -118,104 +118,3 @@ void AndroidAI::DoAttack()
|
||||
}
|
||||
}
|
||||
|
||||
bool AndroidAI::IsCollision()
|
||||
{
|
||||
//检查x轴
|
||||
if (owner->pos.x < 0.001f || owner->pos.x > owner->room->map_meta->i->width()) {
|
||||
return false;
|
||||
}
|
||||
//检查y轴
|
||||
if (owner->pos.y < 0.001f || owner->pos.x > owner->room->map_meta->i->height()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
int detection_flags = 0;
|
||||
a8::SetBitFlag(detection_flags, ET_Obstacle);
|
||||
a8::SetBitFlag(detection_flags, ET_Building);
|
||||
std::vector<Entity*> objects;
|
||||
owner->room->CollisionDetection(this->owner, detection_flags, objects);
|
||||
return !objects.empty();
|
||||
}
|
||||
|
||||
void AndroidAI::FindPath()
|
||||
{
|
||||
Human* hum = (Human*)owner;
|
||||
|
||||
Vector2D old_pos = owner->pos;
|
||||
{
|
||||
float dot = Vector2D::UP.Dot(hum->move_dir);
|
||||
if (std::abs(dot) <= 0.001f) { //相互垂直
|
||||
//向上
|
||||
owner->pos = owner->pos + Vector2D::UP;
|
||||
if (IsCollision()) {
|
||||
//向下
|
||||
owner->pos = old_pos;
|
||||
owner->pos = owner->pos + Vector2D::DOWN;
|
||||
if (IsCollision()) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
} else if (dot > 0.001f) { //基本相同
|
||||
//向右
|
||||
owner->pos = owner->pos + Vector2D::RIGHT;
|
||||
if (IsCollision()) {
|
||||
//向上
|
||||
owner->pos = old_pos;
|
||||
owner->pos = owner->pos + Vector2D::UP;
|
||||
if (IsCollision()) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
} else if (dot < 0.001f) { //基本相反
|
||||
//向右
|
||||
owner->pos = owner->pos + Vector2D::RIGHT;
|
||||
if (IsCollision()) {
|
||||
//向下
|
||||
owner->pos = old_pos;
|
||||
owner->pos = owner->pos + Vector2D::DOWN;
|
||||
if (IsCollision()) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
float dot = Vector2D::DOWN.Dot(hum->move_dir);
|
||||
if (std::abs(dot) <= 0.001f) { //相互垂直
|
||||
//向下
|
||||
owner->pos = owner->pos + Vector2D::DOWN;
|
||||
if (IsCollision()) {
|
||||
//向上
|
||||
owner->pos = old_pos;
|
||||
owner->pos = owner->pos + Vector2D::UP;
|
||||
if (IsCollision()) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
} else if (dot > 0.001f) { //基本相同
|
||||
//向左
|
||||
owner->pos = owner->pos + Vector2D::LEFT;
|
||||
if (IsCollision()) {
|
||||
//向下
|
||||
owner->pos = old_pos;
|
||||
owner->pos = owner->pos + Vector2D::DOWN;
|
||||
if (IsCollision()) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
} else if (dot < 0.001f) { //基本相反
|
||||
//向左
|
||||
owner->pos = owner->pos + Vector2D::LEFT;
|
||||
if (IsCollision()) {
|
||||
//向上
|
||||
owner->pos = old_pos;
|
||||
owner->pos = owner->pos + Vector2D::UP;
|
||||
if (IsCollision()) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -26,6 +26,4 @@ private:
|
||||
void DoMove();
|
||||
void DoAttack();
|
||||
|
||||
bool IsCollision();
|
||||
void FindPath();
|
||||
};
|
||||
|
@ -110,3 +110,102 @@ void Human::RecalcSelfCollider()
|
||||
self_collider_->pos = Vector2D();
|
||||
self_collider_->rad = meta->i->radius();
|
||||
}
|
||||
|
||||
bool Human::IsCollision()
|
||||
{
|
||||
//检查x轴
|
||||
if (pos.x < 0.001f || pos.x > room->map_meta->i->width()) {
|
||||
return false;
|
||||
}
|
||||
//检查y轴
|
||||
if (pos.y < 0.001f || pos.x > room->map_meta->i->height()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
int detection_flags = 0;
|
||||
a8::SetBitFlag(detection_flags, ET_Obstacle);
|
||||
a8::SetBitFlag(detection_flags, ET_Building);
|
||||
std::vector<Entity*> objects;
|
||||
room->CollisionDetection(this, detection_flags, objects);
|
||||
return !objects.empty();
|
||||
}
|
||||
|
||||
void Human::FindPath()
|
||||
{
|
||||
Vector2D old_pos = pos;
|
||||
{
|
||||
float dot = Vector2D::UP.Dot(move_dir);
|
||||
if (std::abs(dot) <= 0.001f) { //相互垂直
|
||||
//向上
|
||||
pos = pos + Vector2D::UP;
|
||||
if (IsCollision()) {
|
||||
//向下
|
||||
pos = old_pos;
|
||||
pos = pos + Vector2D::DOWN;
|
||||
if (IsCollision()) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
} else if (dot > 0.001f) { //基本相同
|
||||
//向右
|
||||
pos = pos + Vector2D::RIGHT;
|
||||
if (IsCollision()) {
|
||||
//向上
|
||||
pos = old_pos;
|
||||
pos = pos + Vector2D::UP;
|
||||
if (IsCollision()) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
} else if (dot < 0.001f) { //基本相反
|
||||
//向右
|
||||
pos = pos + Vector2D::RIGHT;
|
||||
if (IsCollision()) {
|
||||
//向下
|
||||
pos = old_pos;
|
||||
pos = pos + Vector2D::DOWN;
|
||||
if (IsCollision()) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
float dot = Vector2D::DOWN.Dot(move_dir);
|
||||
if (std::abs(dot) <= 0.001f) { //相互垂直
|
||||
//向下
|
||||
pos = pos + Vector2D::DOWN;
|
||||
if (IsCollision()) {
|
||||
//向上
|
||||
pos = old_pos;
|
||||
pos = pos + Vector2D::UP;
|
||||
if (IsCollision()) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
} else if (dot > 0.001f) { //基本相同
|
||||
//向左
|
||||
pos = pos + Vector2D::LEFT;
|
||||
if (IsCollision()) {
|
||||
//向下
|
||||
pos = old_pos;
|
||||
pos = pos + Vector2D::DOWN;
|
||||
if (IsCollision()) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
} else if (dot < 0.001f) { //基本相反
|
||||
//向左
|
||||
pos = pos + Vector2D::LEFT;
|
||||
if (IsCollision()) {
|
||||
//向上
|
||||
pos = old_pos;
|
||||
pos = pos + Vector2D::UP;
|
||||
if (IsCollision()) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -58,6 +58,8 @@ class Human : public Entity
|
||||
virtual void FillMFObjectFull(cs::MFObjectFull* full_data) override;
|
||||
void Shot(Vector2D& target_dir);
|
||||
void RecalcSelfCollider();
|
||||
bool IsCollision();
|
||||
void FindPath();
|
||||
|
||||
private:
|
||||
CircleCollider* self_collider_ = nullptr;
|
||||
|
@ -77,7 +77,19 @@ void Player::UpdateMove()
|
||||
moved_frames = 0;
|
||||
return;
|
||||
}
|
||||
}
|
||||
int speed = std::max(1, (int)GetSpeed());
|
||||
for (int i = 0; i < speed; ++i) {
|
||||
Vector2D old_pos = pos;
|
||||
pos = pos + move_dir * (i + 1);
|
||||
if (IsCollision()) {
|
||||
pos = old_pos;
|
||||
if (i == 0) {
|
||||
FindPath();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Player::UpdateShot()
|
||||
{
|
||||
|
Loading…
x
Reference in New Issue
Block a user