added battle resolution and verification that dude is alive before thinking

This commit is contained in:
Lendemor 2016-06-04 02:36:03 +02:00
parent 4dbec52004
commit 7ddde5e2ec
5 changed files with 56 additions and 7 deletions

View File

@ -75,7 +75,7 @@ struct Coord
};
struct CoordHash{
size_t operator()(const Coord& val) const{
std::size_t operator()(const Coord& val) const{
return val.x+10000*val.y;
}
};

View File

@ -25,6 +25,7 @@ public:
int getTeam(){ return m_team; }
void setSuccess(bool success) { m_success = success; }
void setInventory(PixelType item) { m_inventory = item; }
bool isAlive() { return !m_dead; }
void perish() { m_dead = true; }
const Coord& getPos() { return m_pos; }
void move(Dir d);

View File

@ -34,9 +34,14 @@ void Simulation::update()
std::random_shuffle(m_dudes.begin(), m_dudes.end());
for (int i=0; i<m_dudes.size(); ++i){
Dude *dude = m_dudes[i];
if (dude->isAlive()){
m_teams[dude->getTeam()].update(dude); //get action for this dude from behavior function in team
handleAction(dude);
}
}
resolveBattles();
// for each team, spawn dude if condition met
for(int i=0; i<m_teams.size(); ++i){
Team &t = m_teams[i];
@ -77,8 +82,8 @@ void Simulation::handleAction(Dude *dude)
if(PixelProperty::isDestructible(target.type))
{
dude->setSuccess(true);
if (target.type == DUDE) // TODO: add fight between dude and targetDude
NULL;
if (target.type == DUDE) //DONE
m_battles.insert(Battle(dude,target.data.dudePtr));
else // DONE
{
if(target.type == SPAWN)
@ -211,3 +216,33 @@ void Simulation::handleAction(Dude *dude)
}
}
}
// TODO: verify if battle resolution work
void Simulation::resolveBattles(){
for (Battle battle : m_battles){
Dude *attacker = battle.first, *defender = battle.second;
if (defender->isAlive() && defender->getAction().type == Action::ATTACK){
if (attacker->getPos() == defender->getPos() + Coord(defender->getAction().dir)){
bool armedAttacker = attacker->getInventory() == PixelType::SWORD, armedDefender = defender->getInventory() == PixelType::SWORD;
if (armedAttacker == armedDefender){
if ((rand()%100 + 1) > 50)
attacker->perish();
else
defender->perish();
}else if(armedAttacker){
if ((rand()%100 + 1) > 80)
attacker->perish();
else
defender->perish();
}else if(armedDefender){
if ((rand()%100 + 1) > 20)
attacker->perish();
else
defender->perish();
}
}else
defender->perish();
}else
defender->perish();
}
}

View File

@ -2,6 +2,8 @@
#define SIMULATION_H
#include <vector>
#include <set>
#include <tuple>
#include "behavior.h"
#include "team.h"
@ -10,9 +12,21 @@ class MapScene;
class Simulation
{
private:
typedef std::pair<Dude*,Dude*> Battle;
struct battleComparator{
bool operator() (Battle a, Battle b){
return ((a == b) || (a.first == b.second && b.first == a.second));
}
};
MapScene *p_map;
std::vector<Dude*> m_dudes;
std::vector<Team> m_teams;
std::set<Battle,battleComparator> m_battles;
void handleAction(Dude* dude);
void resolveBattles();
public:
Simulation(MapScene *_map, std::vector<BehaviorFunction> &_behaviors);
~Simulation();
@ -23,7 +37,6 @@ public:
* @brief update runs one step of simulation
*/
void update();
void handleAction(Dude* dude);
};
#endif // SIMULATION_H