void Guards::Update(int worldWidth, int worldHeight, int tileSize, double dt) { if(GetUpdate() == true) { if (stun && checkTimer < stunTimer) { checkTimer += dt; Character::changeAni(StateMachine::IDLE_STATE); this->changeAni(Guards_StateMachine::IDLE_STATE); int dir = Math::RandIntMinMax(1, 4); switch (dir) { case 1: { this->dir.Set(1, 0); break; } case 2: { this->dir.Set(-1, 0); break; } case 3: { this->dir.Set(0, 1); break; } case 4: { this->dir.Set(0, -1); break; } } if (checkTimer >= stunTimer) { stun = false; chase = false; checkTimer = 0; } } else { if (chase == false) { Patrolling(worldWidth, worldHeight, tileSize, dt); } else { Chasing(worldWidth, worldHeight, tileSize, dt); } } AI::Update(dt); } }
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); } }