Commit f8eac096 authored by louiz’'s avatar louiz’

Implement the concept of entity team

parent b50535a3
......@@ -26,7 +26,7 @@ void Game::new_unit_callback(Message* message)
Position pos;
pos.x.raw() = srl.pos().x();
pos.y.raw() = srl.pos().y();
this->turn_handler.insert_action(std::bind(&Game::do_new_unit, this, srl.type_id(), pos),
this->turn_handler.insert_action(std::bind(&Game::do_new_unit, this, srl.type_id(), pos, srl.team()),
srl.turn());
}
......@@ -52,8 +52,8 @@ void Game::move_callback(Message* message)
srl.turn());
}
void Game::do_new_unit(const EntityType type, const Position& pos)
void Game::do_new_unit(const EntityType type, const Position& pos, const uint16_t team)
{
log_debug("Game::do_new_unit");
this->world.do_new_unit(type, pos);
this->world.do_new_unit(type, pos, team);
}
......@@ -18,7 +18,8 @@ public:
void new_unit_callback(Message* msg);
void move_callback(Message* msg);
virtual void do_new_unit(const EntityType type, const Position& pos);
virtual void do_new_unit(const EntityType type, const Position& pos,
const uint16_t team);
protected:
World world;
......
......@@ -211,6 +211,12 @@ void GameClient::handle_start_message(Message* message)
// this->world.start();
}
void GameClient::set_self_team(const uint16_t team)
{
log_debug("Our team is number: " << team);
this->team = team;
}
void GameClient::turn_callback(Message*)
{
log_debug("GameClient::turn_callback");
......@@ -279,9 +285,9 @@ void GameClient::spawn_callback(Message* message)
{
}
void GameClient::do_new_unit(const EntityType type, const Position& pos)
void GameClient::do_new_unit(const EntityType type, const Position& pos, const uint16_t team)
{
Unit* unit = this->world.do_new_unit(type, pos);
Unit* unit = this->world.do_new_unit(type, pos, team);
this->camera.on_new_unit(unit);
}
......
......@@ -29,7 +29,7 @@ public:
void add_new_occupant(std::unique_ptr<Occupant>&& occupant);
void do_new_unit(const EntityType type, const Position& pos) override final;
void do_new_unit(const EntityType type, const Position& pos, const uint16_t team) override final;
void send_message(const char* name, const google::protobuf::Message& msg);
void send_message(const char* name, const std::string& archive);
......@@ -122,6 +122,7 @@ public:
return &this->current_selection;
}
void add_selection_change_callback(const t_selection_changed_callback);
void set_self_team(const uint16_t team);
private:
/**
......@@ -138,6 +139,7 @@ private:
Camera camera;
Hud hud;
DebugHud debug_hud;
uint16_t team;
GameClient(const GameClient&) = delete;
GameClient(GameClient&&) = delete;
......
......@@ -32,6 +32,22 @@ void GameServer::on_new_client(RemoteGameClient* new_client)
message->set_name("NEW_OCCUPANT");
auto new_occupant = std::make_unique<Occupant>(new_client->get_id(), "coucou");
// Get the first team not already taken by an other occupant
uint16_t team;
for (team = 1;; team++)
{
bool res = std::none_of(this->occupants_handler.begin(),
this->occupants_handler.end(),
[team](const auto& occupant)
{
return occupant->get_team() == team;
});
if (res)
break;
}
new_occupant->set_team(team);
message->set_body(new_occupant->serialize());
this->send_to_all_clients(message);
......@@ -135,10 +151,10 @@ void GameServer::tick()
void GameServer::start_game()
{
this->send_new_unit_order(0, {300, 300});
this->send_new_unit_order(0, {400, 800.12});
this->send_new_unit_order(0, {500, 500});
this->send_new_unit_order(0, {320, 610});
this->send_new_unit_order(0, {300, 300}, 1);
this->send_new_unit_order(0, {400, 800.12}, 1);
this->send_new_unit_order(0, {500, 500}, 2);
this->send_new_unit_order(0, {320, 610}, 2);
this->turn_handler.mark_turn_as_ready();
}
......@@ -217,15 +233,17 @@ void GameServer::on_move_request(Message* message)
this->send_move_order(ids, pos, srl.queue());
}
void GameServer::send_new_unit_order(const EntityType type, const Position& pos)
void GameServer::send_new_unit_order(const EntityType type, const Position& pos,
const uint16_t team)
{
ser::order::NewUnit srl;
srl.set_turn(this->turn_handler.get_current_turn() + 2);
srl.set_type_id(type);
srl.mutable_pos()->set_x(pos.x.raw());
srl.mutable_pos()->set_y(pos.y.raw());
srl.set_team(team);
this->send_order_to_all("NEW_UNIT", srl);
this->turn_handler.insert_action(std::bind(&World::do_new_unit, &this->world, type, pos),
this->turn_handler.insert_action(std::bind(&World::do_new_unit, &this->world, type, pos, team),
srl.turn());
}
......
......@@ -67,7 +67,7 @@ public:
*/
void on_next_turn(const TurnNb nb);
void send_new_unit_order(const EntityType type, const Position& pos);
void send_new_unit_order(const EntityType type, const Position& pos, const uint16_t team);
void send_move_order(const std::vector<EntityId> ids, const Position& pos, const bool queue);
/**
* This will create the initial game
......
......@@ -8,7 +8,8 @@ Occupant::Occupant(uint32_t id, const std::string& name):
Occupant::Occupant(const ser::game::Occupant& srl):
id(srl.id()),
name(srl.name())
name(srl.name()),
team(srl.team())
{
}
......@@ -16,11 +17,22 @@ Occupant::~Occupant()
{
}
void Occupant::set_team(const uint16_t team)
{
this->team = team;
}
uint16_t Occupant::get_team() const
{
return this->team;
}
ser::game::Occupant Occupant::serialize() const
{
ser::game::Occupant srlzed;
srlzed.set_id(this->id);
srlzed.set_name(this->name);
srlzed.set_team(this->team);
return srlzed;
}
......@@ -18,10 +18,13 @@ public:
Occupant(const ser::game::Occupant& srl);
~Occupant();
void set_team(const uint16_t team);
uint16_t get_team() const;
ser::game::Occupant serialize() const;
uint32_t id;
int team;
uint16_t team;
std::string name;
private:
......
......@@ -5,6 +5,7 @@
#include <world/unit.hpp>
#include <world/health.hpp>
#include <world/mana.hpp>
#include <world/team.hpp>
PicpicSprite::PicpicSprite(const Unit* const unit):
UnitSprite(unit),
......@@ -23,7 +24,9 @@ PicpicSprite::PicpicSprite(const Unit* const unit):
void PicpicSprite::draw(GameClient* game) const
{
this->draw_shadow(game->get_camera());
Team* team = this->unit->get<Team>();
assert(team);
this->draw_shadow(game->get_camera(), this->team_colors[team->get()]);
const auto entpos = game->get_camera().world_to_camera_position(this->unit->pos);
const int x = entpos.x - game->get_camera().x;
......
......@@ -3,6 +3,9 @@
sf::Texture UnitSprite::shadow_texture;
bool UnitSprite::init = false;
const std::vector<sf::Color> UnitSprite::team_colors = {sf::Color::White,
sf::Color::Red,
sf::Color::Blue};
UnitSprite::UnitSprite(const Unit* const unit):
unit(unit)
......@@ -15,7 +18,7 @@ UnitSprite::UnitSprite(const Unit* const unit):
}
}
void UnitSprite::draw_shadow(Camera& camera) const
void UnitSprite::draw_shadow(Camera& camera, const sf::Color color) const
{
const auto entpos = camera.world_to_camera_position(this->unit->pos);
const int x = entpos.x - camera.x;
......@@ -24,7 +27,6 @@ void UnitSprite::draw_shadow(Camera& camera) const
sf::Sprite sprite(UnitSprite::shadow_texture);
sprite.setPosition(x - size.x/2, y - size.y/2);
const sf::Color color(255, 255, 255, 122);
sprite.setColor(color);
camera.draw(sprite);
}
......
......@@ -4,6 +4,8 @@
#include <gui/sprites/world_sprite.hpp>
#include <gui/camera/camera.hpp>
#include <vector>
class Unit;
class UnitSprite: public WorldSprite
......@@ -16,11 +18,12 @@ public:
protected:
const Unit* const unit;
void draw_shadow(Camera& camera) const;
void draw_shadow(Camera& camera, const sf::Color color) const;
static const std::vector<sf::Color> team_colors;
private:
static sf::Texture shadow_texture;
static bool init;
static sf::Texture shadow_texture;
UnitSprite(const UnitSprite&);
UnitSprite& operator=(const UnitSprite&);
......
......@@ -11,6 +11,7 @@ enum class ComponentType
Health,
Mana,
Vision,
Team,
};
class Component
......
......@@ -2,6 +2,7 @@
#include <world/health.hpp>
#include <world/mana.hpp>
#include <world/vision.hpp>
#include <world/team.hpp>
EntityFactory::EntityFactory()
{
......@@ -26,6 +27,9 @@ std::unique_ptr<Unit> EntityFactory::make_unit(const EntityType type)
res->add_component(std::make_unique<Health>(100));
res->add_component(std::make_unique<Mana>(300));
res->add_component(std::make_unique<Vision>(700, false));
res->add_component(std::make_unique<Team>());
// return std::make_unique<Unit>(this->unit_models[type]);
return res;
}
#include <world/team.hpp>
const ComponentType Team::component_type;
Team::Team(const Team::type number):
number(number)
{
}
Team::Team():
number(0)
{
}
Team::type Team::get() const
{
return this->number;
}
void Team::set(const Team::type number)
{
this->number = number;
}
void Team::tick(Entity* entity, World* world)
{
}
#ifndef TEAM_COMPONENT_HPP_INCLUDED
#define TEAM_COMPONENT_HPP_INCLUDED
#include <cstdint>
#include <world/components.hpp>
class Team: public Component
{
public:
static const ComponentType component_type = ComponentType::Team;
using type = uint8_t;
Team(const type number);
Team();
~Team() = default;
type get() const;
void set(const type number);
void tick(Entity* entity, World* world) override final;
private:
type number;
Team(const Team&) = delete;
Team(Team&&) = delete;
Team& operator=(const Team&) = delete;
Team& operator=(Team&&) = delete;
};
#endif /* TEAM_COMPONENT_HPP_INCLUDED */
......@@ -3,6 +3,7 @@
#include <world/path.hpp>
#include <world/work.hpp>
#include <world/health.hpp>
#include <world/team.hpp>
World::World()
{
......@@ -57,10 +58,14 @@ Path World::calculate_path(Position endpos, Unit* unit)
return {};
}
Unit* World::do_new_unit(const EntityType type, const Position& pos)
Unit* World::do_new_unit(const EntityType type, const Position& pos, const uint16_t team_value)
{
auto entity = this->entity_factory.make_unit(type);
entity->pos = pos;
Team* team = entity->get<Team>();
assert(team);
team->set(team_value);
auto res = entity.get();
this->insert_unit(std::move(entity));
return res;
......
......@@ -69,7 +69,7 @@ public:
void pause();
void unpause();
Path calculate_path(Position, Unit*);
Unit* do_new_unit(const EntityType type, const Position& pos);
Unit* do_new_unit(const EntityType type, const Position& pos, const uint16_t team);
void do_move(const std::vector<EntityId> ids, const Position& pos, const bool queue);
// void do_path(ActionEvent*);
// void do_new_unit(ActionEvent*);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment