The assignment was to write a combat simulator.
Assignment:
Spoiler:
main
Character.h
Character.cpp
Weapon.h
Weapon.cpp
Code: Select all
#include "Character.h"
#include "Weapon.h"
#pragma once
//Displays a message for when the combat is over
void Summary(Character& winner, Character& loser)
{
std::cout << loser.GetName() << " lost." << std::endl;
std::cout << winner.GetName() << " has " << winner.GetHealth() << " HP left." << std::endl;
}
int main()
{
//Variables
Weapon revolver(35, 70, 6, 1.5f, 0.5f);
Weapon scrapGun(20, 255, 4, 1.5f, 1.0f);
Character mcCree("McCree", 200, revolver);
Character roadHog("RoadHog", 600, scrapGun, 30.0f, true);
const float deltaTime = 0.1f;
bool running = true;
mcCree.SetOpponent(&roadHog);
roadHog.SetOpponent(&mcCree);
//Simulation loop
while (running)
{
mcCree.Update(deltaTime);
roadHog.Update(deltaTime);
//Exit the loop if either character is out of HP
if (mcCree.IsDead())
{
running = false;
Summary(roadHog, mcCree);
}
else if (roadHog.IsDead())
{
running = false;
Summary(mcCree, roadHog);
}
}
return 0;
}
Code: Select all
#include <string>
#include <iostream>
#include "Weapon.h"
#pragma once
class Character
{
public:
Character(const std::string name_in, const int health_in, Weapon& rWeapon_in);
Character(const std::string name_in, const int health_in, Weapon& rWeapon_in, const float hookCoolDown_in, const bool hasHookSkill_in);
void SetOpponent(Character* pOpponent_in);
void Update(const float deltaTime);
int GetHealth() const;
void IsCloseCombat();
void TakeDamage(const int damage);
std::string GetName() const;
void CombatMessage(const int damage) const;
bool IsDead() const;
private:
const std::string name;
int health;
bool closeCombat = false;
Weapon& rWeapon;
const float hookCoolodown = 0.0f;
float hookCooldownCounter = 0.0f;
const bool hasHookSkill = false;
Character* pOpponent = nullptr;
};
Code: Select all
#include "Character.h"
Character::Character(const std::string name_in, const int health_in, Weapon & rWeapon_in)
:
name(name_in),
health(health_in),
rWeapon(rWeapon_in)
{}
Character::Character(const std::string name_in, const int health_in, Weapon & rWeapon_in, const float hookCoolDown_in, const bool hasHookSkill_in)
:
name(name_in),
health(health_in),
rWeapon(rWeapon_in),
hookCoolodown(hookCoolDown_in),
hasHookSkill(hasHookSkill_in)
{}
void Character::SetOpponent(Character * pOpponent_in)
{
pOpponent = pOpponent_in;
}
void Character::Update(const float deltaTime)
{
if (!closeCombat && hasHookSkill)
{
if (hookCooldownCounter >= hookCoolodown)
{
std::cout << name << " used his special hook skill to get into close combat.\n" << std::endl;
closeCombat = true;
pOpponent->IsCloseCombat();
}
hookCooldownCounter += deltaTime;
}
if (rWeapon.Shoot(deltaTime))
{
if (closeCombat)
{
const int damageNear = rWeapon.GetDamageNear();
pOpponent->TakeDamage(damageNear);
CombatMessage(damageNear);
}
else
{
const int damageFar = rWeapon.GetDamageFar();
pOpponent->TakeDamage(damageFar);
CombatMessage(damageFar);
}
}
rWeapon.Update(deltaTime, name);
}
int Character::GetHealth() const
{
return health;
}
void Character::IsCloseCombat()
{
closeCombat = true;
}
void Character::TakeDamage(const int damage)
{
health -= damage;
if (health < 0)
{
health = 0;
}
}
std::string Character::GetName() const
{
return name;
}
void Character::CombatMessage(const int damage) const
{
const std::string opponentName = pOpponent->GetName();
std::cout << name << " shot " << opponentName << " and dealt " << damage << " damage." << std::endl;
std::cout << opponentName << " has " << pOpponent->GetHealth() << " HP left.\n" << std::endl;
}
bool Character::IsDead() const
{
return health <= 0;
}
Code: Select all
#include <iostream>
#include <string>
#pragma once
class Weapon
{
public:
Weapon(const int damageFar_in, const int damageNear_in, const int magazineSize_in, const float reloadTime_in, const float fireRate_in);
void Update(const float deltaTime, std::string name);
bool Shoot(const float deltaTime);
void Reload();
int GetDamageNear() const;
int GetDamageFar() const;
bool HasBullets() const;
private:
const int damageFar;
const int damageNear;
const int magazineSize;
int bulletsLeft;
const float reloadTime;
const float fireRate;
float reloadCounter = 0.0f;
float bulletCooldownCounter;
bool isReloaded = true;
};
Code: Select all
#include "Weapon.h"
Weapon::Weapon(const int damageFar_in, const int damageNear_in, const int magazineSize_in, const float reloadTime_in, const float fireRate_in)
:
damageFar(damageFar_in),
damageNear(damageNear_in),
magazineSize(magazineSize_in),
bulletsLeft(magazineSize_in),
reloadTime(reloadTime_in),
fireRate(fireRate_in),
bulletCooldownCounter(fireRate_in)
{}
void Weapon::Update(const float deltaTime, std::string name)
{
if (!HasBullets())
{
if ((reloadCounter += deltaTime) >= reloadTime)
{
Reload();
std::cout << name << " reloaded.\n" << std::endl;
}
}
}
bool Weapon::Shoot(const float deltaTime)
{
if ((bulletCooldownCounter += deltaTime) >= fireRate && HasBullets())
{
bulletCooldownCounter = 0.0f;
--bulletsLeft;
return true;
}
return false;
}
void Weapon::Reload()
{
reloadCounter = 0.0f;
bulletsLeft = magazineSize;
}
int Weapon::GetDamageNear() const
{
return damageNear;
}
int Weapon::GetDamageFar() const
{
return damageFar;
}
bool Weapon::HasBullets() const
{
return bulletsLeft > 0;
}
Tuesday is dedicated to finishing the assignment and there's no lectures. Since I'm already done, I'll spend that time researching a bit about some of the stuff on the list of course content.
btw, our programing teacher is great. Really liked him, he had a chili type sense of humor