population counter is updating, ninja dude walking on the world

This commit is contained in:
Lendemor 2016-05-26 23:31:09 +02:00
parent ea3bd65bc7
commit 49d1ffb0c4
6 changed files with 20 additions and 7 deletions

View File

@ -6,5 +6,5 @@
extern "C" void think(Action *action, char *memory, const Info *info) extern "C" void think(Action *action, char *memory, const Info *info)
{ {
action->type = Action::MOVE; // the Dude will move action->type = Action::MOVE; // the Dude will move
action->dir = Dir( std::rand() % 4 ); // the direction of the movement will be random action->dir = Dir(0);//std::rand() % 4 ); // the direction of the movement will be random
} }

Binary file not shown.

View File

@ -42,6 +42,7 @@ void MainWindow::updateSimu()
{ {
p_simu->update(); p_simu->update();
ui->dateLabel->setText(QString::number(++m_date)); ui->dateLabel->setText(QString::number(++m_date));
ui->populationLabel->setText(QString::number(p_simu->getPopulation()));
ui->drawWidget->updateDudesBehavior(); ui->drawWidget->updateDudesBehavior();
ui->drawWidget->repaint(); ui->drawWidget->repaint();
} }

View File

@ -35,22 +35,33 @@ void Simulation::update()
{ {
//TODO: check if target pixel is walkable and has no dude //TODO: check if target pixel is walkable and has no dude
Coord spawnPos = p_map->team(i) + Coord(Dir(rand()%4)); Coord spawnPos = p_map->team(i) + Coord(Dir(rand()%4));
m_dudes.push_back(new Dude(spawnPos,p_map,i)); Dude *dude = new Dude(spawnPos,p_map,i);
(*p_map)[spawnPos].data.dudePtr = dude;
p_map->updatePixel(spawnPos);
m_dudes.push_back(dude);
} }
} }
} }
// TODO finish conversion of handleAction() // TODO finish conversion of handleAction()
void Simulation::handleAction(Action *action, Dude *dude){ void Simulation::handleAction(Action *action, Dude *dude){
Coord targetPos(dude->getPos() + Coord(action->dir)); Coord currentPos(dude->getPos());
Pixel target = p_map->getPixel(targetPos); Coord targetPos(currentPos + Coord(action->dir));
Pixel source = (*p_map)[currentPos];
Pixel target = (*p_map)[targetPos];
Dude* targetDude; Dude* targetDude;
dude->setSuccess(false); dude->setSuccess(false);
if(action == NULL) return; if(action == NULL) return;
switch(action->type){ switch(action->type){
case Action::MOVE: case Action::MOVE:
if (isWalkable(target.type)) if (isWalkable(target.type) && target.data.dudePtr == NULL){
NULL; // TODO: move action source.data.dudePtr = NULL;
target.data.dudePtr = dude;
dude->move(action->dir);
p_map->updatePixel(currentPos);
p_map->updatePixel(targetPos);
std::cout << "Moving to [" << targetPos.x << "," << targetPos.y << "]"<< std::endl;
}
break; break;
case Action::ATTACK: case Action::ATTACK:
if (isDestroyable(target.type)) if (isDestroyable(target.type))

View File

@ -16,6 +16,7 @@ private:
public: public:
Simulation(MapScene *_map, std::vector<BehaviorFunction> &_behaviors); Simulation(MapScene *_map, std::vector<BehaviorFunction> &_behaviors);
MapScene* getMap() { return (MapScene*) p_map; } MapScene* getMap() { return (MapScene*) p_map; }
int getPopulation(){return m_dudes.size();}
/** /**
* @brief update runs one step of simulation * @brief update runs one step of simulation

View File

@ -32,7 +32,7 @@ void Team::destroySpawn(){
Action* Team::update(Dude *dude) Action* Team::update(Dude *dude)
{ {
Action* action; Action* action = new Action();
m_behavior(action, dude->getMemory(),(Info*) dude); m_behavior(action, dude->getMemory(),(Info*) dude);
return action; return action;
} }