Link to github: https://github.com/coffee-enthusiast/CatchEmAll
A micro-version of Pokemon made in C++!!
In Order the player to travel to different locations with different actions available there I implemented a State Machine with each state representing each location(ie. Arena, Doctor, Gym, etc.)
class State
{
public:
State(StateMachine* sM, Map* m);
State();
~State();
StateMachine* stateM;
Map* map;
virtual void EnterState() = 0;
virtual void HandleInput() = 0;
virtual void Update() = 0;
virtual void ExitState() = 0;
};
When player changes location the following code gets executed:
void StateMachine::ChangeState(State* newState)
{
if(currentState != nullptr)
currentState->ExitState();
currentState = newState;
currentState->EnterState();
}
Here is what the Home location state looks like:
/*-- HomeState.h ----*/
class HomeState : public State
{
private:
Player* p_Player;
public:
HomeState(StateMachine* sM, Map* m, Player* p);
HomeState();
~HomeState();
void EnterState();
void HandleInput();
void Update();
void ExitState();
bool gameEnds;
void PrintHelp();
private:
};
/*-- HomeState.cpp ----*/
void HomeState::HandleInput()
{
int input = getchar();
if (input == 'h')
{
PrintHelp();
}
else if (input == 'a')
{
stateM->ChangeState(map->arenaLocation);
}
else if (input == 'd')
{
stateM->ChangeState(map->doctorLocation);
}
else if (input == 'w')
{
stateM->ChangeState(map->walkLocation);
}
else if (input == 'e')
{
gameEnds = true;
}
else if (input == 'p') // Print Creatures command
{
p_Player->PrintMyCreatures();
}
}
Finally here is a glimpse of battle between player and ai:
void Battle::rivalAttacks()
{
// If chosenEntity is effective againt opponent then Use a Move
if (aiChooseAction() == 0)
{
// Choose a random move
Move myMove;
do
{
myMove = rivalChosenEntity->myMovesQuad.GetMove(rand() % rivalChosenEntity->myMovesQuad.size);
} while (myMove.name == "");
if(myMove.category == "Physical")
{
// Calculate and Take damage from the rival entity
cout << rival->getName() << " used " << myMove.name << "!" << endl;
float damage = rivalChosenEntity->getAttack() + myMove.power - playerChosenEntity->getDefence();
damage *= (float)myMove.accuracy / (float)100;
damage *= effectiveAgainstType(rivalChosenEntity->getType(), playerChosenEntity->getType());
damage /= (float)5;
playerChosenEntity->TakeDamage(damage);
rivalChosenEntity->changePowerPoints(myMove.powerPoints);
cout << playerChosenEntity->getName() << " lost " << to_string(damage) << " HP!" << endl;
if (playerChosenEntity->getHealth() <= 0)
{
cout << playerChosenEntity->getName() << " fainted!" << endl;
rivalChosenEntity->addExpPoints(8);
playerAliveLeft--;
if (playerAliveLeft == 0)
{
battleEnded = true;
whoWon = rival;
}
}
}
}
}