Skip to content

Commit

Permalink
enhancing pnj manager
Browse files Browse the repository at this point in the history
  • Loading branch information
Loodoor committed Nov 27, 2016
1 parent 4a0410d commit 2d19b90
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 13 deletions.
File renamed without changes.
1 change: 1 addition & 0 deletions src/entities/animatedentity.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "animatedentity.hpp"
9 changes: 9 additions & 0 deletions src/entities/animatedentity.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef DEF_ANIMATED_ENTITY
#define DEF_ANIMATED_ENTITY

class AnimatedEntity
{

};

#endif // DEF_ANIMATED_ENTITY
14 changes: 13 additions & 1 deletion src/entities/pnjmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ void PNJManager::add_pnj_on_map(int mid, PNJ pnj, std::string dname)
pnj.setDisplayName(dname);
if (!pnj.load())
std::cout << "Unable to load " << dname << " PNJ" << std::endl;
this->pnjs[mid].push_back(pnj);
this->pnjs[mid].emplace(pnj);
}

int PNJManager::countPNJonMap(int mid)
Expand All @@ -30,3 +30,15 @@ PNJ& PNJManager::getPNJonMap(int mid, int pid)
{
return this->pnjs[mid][pid];
}

void PNJManager::update(int mid, sf::RenderWindow& window, sf::Time elapsed)
{
if (this->pnjs.find(mid) == this->pnjs.end())
goto _end;

for (int i=0; i < this->pnjs[mid].size(); i++)
{
this->pnjs[mid][i].update(window, elapsed);
}
_end:;
}
2 changes: 2 additions & 0 deletions src/entities/pnjmanager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <map>
#include <vector>
#include <string>
#include <SFML/Graphics.hpp>

#include "pnj.hpp"

Expand All @@ -17,6 +18,7 @@ class PNJManager
void add_pnj_on_map(int, PNJ, std::string);
int countPNJonMap(int);
PNJ& getPNJonMap(int, int);
void update(int, sf::RenderWindow&, sf::Time);
};

#endif // DEF_PNJMANAGER
6 changes: 6 additions & 0 deletions src/map/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Map::Map(std::string path) :
std::cout << path << std::endl;

this->map_data_path = path;
this->id = int(this->map_data_path.substr("assets/map/".size(), this->map_data_path.size() - ".umd".size()));
}

int Map::load()
Expand Down Expand Up @@ -60,6 +61,11 @@ int Map::getHeight()
return this->map_height;
}

int Map::getId()
{
return this->id;
}

bool Map::colliding_at(int tx, int ty)
{
int rpos = tx + ty * this->map_width;
Expand Down
2 changes: 2 additions & 0 deletions src/map/map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class Map
std::string tileset_path;
std::string map_data_path;
Point pos;
int id;
// default values for the moment
int map_width;
int map_height;
Expand All @@ -37,6 +38,7 @@ class Map
void update(sf::RenderWindow&, sf::Time);
int getWidth();
int getHeight();
int getId();
bool colliding_at(int, int);
};

Expand Down
21 changes: 10 additions & 11 deletions src/views/default.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
// public
DefaultView::DefaultView() :
View(DEFAULT_VIEW_ID)
, pnj("vader", "Je suis Vader, un commander d'El Padrino !", PNJkind::special)
, level("assets/map/1-1441.umd")
, level("assets/map/1.umd")
{
}

Expand All @@ -20,10 +19,8 @@ bool DefaultView::load()
return false;
}

this->pnj.setDisplayName("Vader");
if (!this->pnj.load())
if (!this->pnjmgr.add_pnj_on_map(this->level.getId(), pnj("vader", "Je suis Vader, un commander d'El Padrino !", PNJkind::special), "Vader"))
{
std::cout << "An error occured while loading a test pnj" <<std::endl;
return false;
}

Expand All @@ -41,7 +38,13 @@ void DefaultView::render(sf::RenderWindow& window)
{
this->level.render(window);
this->level.render_chara(this->player.getCurrentSprite(), this->player.getPos(), window);
this->pnj.render(window); // testing
for (int i=0; i < this->pnjmgr.countPNJonMap(this->level.getId()); i++)
{
this->level.render_chara(
this->pnjmgr.getPNJonMap(this->level.getId(), i).getCurrentSprite()
, this->pnjmgr.getPNJonMap(this->level.getId(), i).getPos()
, window);
}
this->level.render_top(window);
this->menu_hud.render(window);
}
Expand All @@ -50,7 +53,7 @@ void DefaultView::update(sf::RenderWindow& window, sf::Time elapsed)
{
this->player.update(window, elapsed);
this->level.update(window, elapsed);
this->pnj.update(window, elapsed); // testing
this->pnjmgr.update(this->level.getId(), window, elapsed);
this->menu_hud.update(window, elapsed);
}

Expand Down Expand Up @@ -87,10 +90,6 @@ int DefaultView::process_event(sf::Event& event, sf::Time elapsed)
this->player.move(DIRECTION::right, this->level, elapsed);
break;

case sf::Keyboard::Space:
this->pnj.speak();
break;

default:
break;
}
Expand Down
3 changes: 2 additions & 1 deletion src/views/default.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@
#include "../entities/pnj.hpp"
#include "huds/menu.hpp"
#include "../constants.hpp"
#include "../entities/pnjmanager.hpp"

class DefaultView : public View
{
private:
// variables
Character player;
PNJ pnj;
Map level;
PNJManager pnjmgr;
MenuHUD menu_hud;

public:
Expand Down

0 comments on commit 2d19b90

Please sign in to comment.