float AIController::GetDistanceSqr(const Vector3 &pos1,const Vector3 &pos2)const { Vector2 distance ;//since the board gemeotry is mostly flat,we can use 2D vector distance.x = pos1.x - pos2.x; distance.y = pos1.z - pos2.z; return distance.LengthSqr(); }
void AIController::Control(const Board *board,const MovableObject *player,bool playerInv) { const Vector3 & pos1 = object->GetPosition();//ghost position in world space float velocity = object->GetVelocity(); //get ghost location in board board->GetTileInfo(pos1,currentTile.row,currentTile.col); float diff; switch (object->GetDirection()) { case LEFT: diff = pos1.x - destination.x; if(diff <= FLOAT_DELTA && diff >=- FLOAT_DELTA) break; if (diff > velocity + FLOAT_DELTA) { this->object->Update(); return; } else { posOffset = velocity - diff; this->object->SetPosition(destination); return; } break; case RIGHT: diff = destination.x - pos1.x; if(diff <= FLOAT_DELTA && diff >=- FLOAT_DELTA) break; if (diff > velocity + FLOAT_DELTA) { this->object->Update(); return; } else { posOffset = velocity - diff; this->object->SetPosition(destination); return; } break; case UP: diff = pos1.z - destination.y; if(diff <= FLOAT_DELTA && diff >=- FLOAT_DELTA) break; if (diff > velocity + FLOAT_DELTA) { this->object->Update(); return; } else { posOffset = velocity - diff; this->object->SetPosition(destination); return; } break; case DOWN: diff = destination.y - pos1.z; if(diff <= FLOAT_DELTA && diff >=- FLOAT_DELTA) break; if (diff > velocity + FLOAT_DELTA) { this->object->Update(); return; } else { posOffset = velocity - diff; this->object->SetPosition(destination); return; } break; } if(playerInv)//player is invisible { Wandering(board); return; } Vector2 distance ;//since the board gemeotry is mostly flat,we can use 2D vector float distSqr; const Vector3 & pos2 = player->GetPosition(); distance.x = pos1.x - pos2.x; distance.y = pos1.z - pos2.z; distSqr = distance.LengthSqr(); if (distSqr <= rosSqr)//player is in ghost's range of sight Chasing(board,player); else { Wandering(board); } }