Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
#include "combat.h"
<<<<<<< HEAD
#include "inputTest.h"
=======
>>>>>>> eea4137c94972ad5ffc5c09eab0ecae6ee4071ba
/** NICE TO HAVE : A COMBO SYSTEM */
Combat::Combat( int areaID, int howManyEnemies):Area()
{
/**int numberOfEnemies = howManyEnemies;*/
numberOfEnemies = rand() % 5+1;
int nameNumber = rand() % 3;
areaLocation = areaID;
/**
areaName = arrayName[nameNumber];
*/
for(int i=0; i<numberOfEnemies; i++){ //This for loop creates enemies using the "Enemy" class and puts them into a vector.
Enemy opponentInList(5,5,5,5,6,5,5);
enemyList.push_back(opponentInList);
}
}
/**
This method will take the "character" object and the vector of "enemy" objects and then run a loop where the "fight" method will be called
for each "enemy" object, making the player character fight each enemy 1 by 1.
*/
char Combat::battle(Character& hero){
for( int i=0; i<enemyList.size();i++ ) //Combat is initialize, for loop goes over each enemy in the enemyList vector.
{
if(hero.current_HP>0){ //TO DO: ADD A BREAK IN BETWEEN FIGHTS
std::cout<<"There are "<<numberOfEnemies<<" enemies in this area. What do you want to do? \n 1.Fight them \n 2.Escape"<<std::endl;
int playerChoice = inputTest<int>();
if(playerChoice == 1)
Combat::fight(hero, enemyList[i]);
else if (playerChoice == 2)
{
std::system("cls");
std::cout<<"Hero escaped from the battle."<<std::endl;
return 2; //Return 2 means the hero escaped from battle
}
else
{
std::cout<<"Choose a correct action to take."<<std::endl;
i--; //If the player doesn't make a correct action choice, the loop doesn't go to the next enemy, but stays at the same one
}
}
else
{
std::cout<<"Hero lost the battle."<<std::endl;
return 0; //Return 0 means hero lost the battle
}
}
std::system("cls");
if(hero.current_HP>0) /** This if statement is here in case the player dies to the last enemy in the area,
since then the next iteration of for loop wouldn't start to check if they're dead.
Not the most ellegant solution, will fix if I have some spare time */
{
std::cout<<"Hero won the battle."<<std::endl;
return 1; //Return 1 means hero won the battle
}
else
{
std::cout<<"Hero lost the battle."<<std::endl;
return 0; //Return 0 means hero lost the battle
}
}
/**
* This method will make the "character" object fight a single "enemy" object.
*/
char Combat::fight(Character& hero, Enemy& opponent){
int opponentName = rand() % 213; //TO DO: TAKE NAME FROM THE ENEMY OBJECT, WHEN THE NAMES ARE ACTUALLY ADDED IN ANY FORM
int heroHitDamage = hero.strength-opponent.defense; //The hero's base attack damage is calculated once at the beginning of the fight.
int opponentHitDamage = opponent.strength - hero.defense; //The opponent's base attack damage is calculated once at the beginning of the fight
while(hero.current_HP>0 and opponent.current_HP>0)
{
std::cout<<"The enemy"<<opponentName<<" stands before you. \n What action will you take? \n 1. Attack \n 2. Defend"<<std::endl;
int heroAction = inputTest<int>();
if(heroAction == 1) //If player chooses 1:
{
char fightOutcome = hero_attack(hero, opponent, heroHitDamage, opponentHitDamage, opponentName);
if(fightOutcome == 4)
continue;
else
return fightOutcome;
}
//if (heroAction == 2) //If player chooses 2:
/**HERE BE
* THE CODE FOR
* DEFENDING
*
*
* */
}
return 3; //Something went wrong!
}
/*
This method is called by the "fight" method when an attack is performed
*/
char Combat::hero_attack(Character& hero, Enemy& opponent, int heroHitDamage, int opponentHitDamage, int opponentName)
{
if(typing_result()==true) //If the player types the word in fast enough
{
opponent.current_HP -=heroHitDamage; //The hero attacks
std::cout<<"The hero dealt "<<heroHitDamage<<" damage \n"<<" Enemy"<<opponentName<<"'s current HP:"<<opponent.current_HP<<"\n"<<std::endl; //The damage is printed
if(opponent.current_HP<=0) //If the enemy dies to the attack
{
std::cout<<"Hero won the fight."<<std::endl;
numberOfEnemies --; // After enemy is killed, decrease the number of enemies by 1
std::system("cls"); //Clears the terminal
return 1; //The hero won the fight
}
}
hero.current_HP -= opponentHitDamage; //The enemy also attacks the hero
std::cout<<"The enemy"<<opponentName<<" dealt "<<opponentHitDamage<<" damage \n"<<" Hero's current HP:"<<hero.current_HP<<"\n"<<std::endl; //The damage is printed
if(hero.current_HP<=0) //If the hero's health reaches 0
{
std::system("cls");
std::cout<<"Hero lost the fight."<<std::endl;
return 0; //The hero died
}
return 4; //The battle should continue as normal
}
/**
A getter function to return the number of enemies in this combat area, overrides the description function in area.cpp
*/
std::string Combat::get_area_description()
{
std::string description = "There are " + std::to_string(numberOfEnemies) + " enemies here.";
return description;
}