Commit ea0499fb authored by louiz’'s avatar louiz’

Implement following a Unit with a right click on it

parent 07b3ce2e
......@@ -45,12 +45,22 @@ void Game::move_callback(Message* message)
for (const auto& id: srl.entity_id())
ids.push_back(id);
Position pos;
pos.x.raw() = srl.mutable_pos()->x();
pos.y.raw() = srl.mutable_pos()->y();
if (srl.has_pos())
{
Position pos;
pos.x.raw() = srl.mutable_pos()->x();
pos.y.raw() = srl.mutable_pos()->y();
this->turn_handler.insert_action(std::bind(&World::do_move, &this->world, ids, pos, srl.queue()),
srl.turn());
this->turn_handler.insert_action(
std::bind(&World::do_move, &this->world, ids, pos, srl.queue()),
srl.turn());
}
else
{
this->turn_handler.insert_action(
std::bind(&World::do_follow, &this->world, ids, srl.target(), srl.queue()),
srl.turn());
}
}
void Game::cast_callback(Message* message)
......
......@@ -268,6 +268,20 @@ bool GameClient::action_move(const std::vector<EntityId>& ids, const Position& p
return true;
}
bool GameClient::action_follow(const std::vector<EntityId>& ids,
const EntityId& target_id, const bool queue)
{
ser::request::Move srl;
if (queue)
srl.set_queue(queue);
srl.set_target(target_id);
for (const EntityId id: ids)
srl.add_entity_id(id);
this->send_message("MOVE", srl);
this->sounds_handler.play(SoundType::DefaultOk, true);
return true;
}
bool GameClient::action_cast(const std::vector<EntityId>& ids,
const Position& pos, const AbilityType& type,
const bool queue)
......
......@@ -96,6 +96,8 @@ public:
*/
bool action_move(const std::vector<EntityId>& ids, const Position& pos,
const bool queue);
bool action_follow(const std::vector<EntityId>& ids,
const EntityId& target_id, const bool queue);
/**
* Send, to the server, a request to make the list of entities cast the
* given spell. There are three versions, each for each type of possible
......
......@@ -247,10 +247,15 @@ void GameServer::on_move_request(Message* message)
std::vector<EntityId> ids;
for (const auto& id: srl.entity_id())
ids.push_back(id);
Position pos;
pos.x.raw() = srl.pos().x();
pos.y.raw() = srl.pos().y();
this->send_move_order(ids, pos, srl.queue());
if (srl.has_pos())
{
Position pos;
pos.x.raw() = srl.pos().x();
pos.y.raw() = srl.pos().y();
this->send_move_order(ids, pos, srl.queue());
}
else
this->send_follow_order(ids, srl.target(), srl.queue());
}
void GameServer::on_cast_request(Message* message)
......@@ -347,3 +352,16 @@ void GameServer::send_move_order(const std::vector<EntityId> ids, const Position
this->turn_handler.insert_action(std::bind(&World::do_move, &this->world, ids, pos, queue),
srl.turn());
}
void GameServer::send_follow_order(const std::vector<EntityId> ids, const EntityId target, const bool queue)
{
ser::order::Move srl;
srl.set_turn(this->turn_handler.get_current_turn() + 2);
for (const EntityId id: ids)
srl.add_entity_id(id);
srl.set_queue(queue);
srl.set_target(target);
this->send_order_to_all("MOVE", srl);
this->turn_handler.insert_action(std::bind(&World::do_follow, &this->world, ids, target, queue),
srl.turn());
}
......@@ -75,6 +75,7 @@ public:
void send_new_entity_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);
void send_follow_order(const std::vector<EntityId> ids, const EntityId target, const bool queue);
/**
* Send an Order to each client, to make each of these entities cast the
* given ability.
......
......@@ -124,7 +124,12 @@ void Camera::handle_right_click(const sf::Event& event)
std::vector<EntityId> ids;
for (const auto& entity: this->game->get_selection().get_entities())
ids.push_back(entity->get_id());
if (!ids.empty())
if (ids.empty())
return ;
const Entity* entity_under_mouse = this->get_entity_under_mouse();
if (entity_under_mouse)
this->game->action_follow(ids, entity_under_mouse->get_id(), queue);
else
this->game->action_move(ids, pos, queue);
}
}
......
......@@ -26,7 +26,8 @@ message Move
required uint32 turn = 1;
optional bool queue = 2 [default = false];
repeated uint32 entity_id = 3;
required ser.game.Position pos = 4;
optional ser.game.Position pos = 4;
optional uint32 target = 5;
}
message Cast
......
......@@ -11,7 +11,8 @@ message Move
{
repeated uint32 entity_id = 1;
optional bool queue = 2 [default = false];
required ser.game.Position pos = 3;
optional ser.game.Position pos = 3;
optional uint32 target = 4;
}
message Cast
......
#ifndef FOLLOW_WORK_HPP_INCLUDED
#define FOLLOW_WORK_HPP_INCLUDED
#include <world/works/single_task_work.hpp>
#include <world/tasks/follow_task.hpp>
using FollowWork = SingleTaskWork<FollowTask>;
#endif /* FOLLOW_WORK_HPP_INCLUDED */
......@@ -5,6 +5,7 @@
#include <world/health.hpp>
#include <world/team.hpp>
#include <world/works/path_work.hpp>
#include <world/works/follow_work.hpp>
#include <world/location.hpp>
#include <world/team.hpp>
#include <world/vision.hpp>
......@@ -143,9 +144,32 @@ void World::do_move(const std::vector<EntityId>& ids, const Position& pos, const
if (!entity)
continue;
if (queue)
entity->queue_work(std::make_unique<PathWork>(entity, pos));
entity->queue_work(std::make_unique<PathWork>(entity, this, pos));
else
entity->set_work(std::make_unique<PathWork>(entity, pos));
entity->set_work(std::make_unique<PathWork>(entity, this, pos));
}
}
void World::do_follow(const std::vector<EntityId>& ids, const EntityId& target_id, const bool queue)
{
Entity* entity;
std::shared_ptr<Entity> target = this->get_shared_entity_by_id(target_id);
if (!target)
{
log_debug("Could not follow entity " << target_id << ", it does not exist");
return;
}
for (const EntityId id: ids)
{
log_debug("Entity " << id << " follows entity " << target_id);
entity = this->get_entity_by_id(id);
if (!entity)
continue;
if (queue)
entity->queue_work(std::make_unique<FollowWork>(entity, this, target));
else
entity->set_work(std::make_unique<FollowWork>(entity, this, target));
}
}
......
......@@ -70,6 +70,8 @@ public:
const uint16_t team);
void do_move(const std::vector<EntityId>& ids, const Position& pos,
const bool queue);
void do_follow(const std::vector<EntityId>& ids, const EntityId& target_id,
const bool queue);
void do_cast_on_pos(const std::vector<EntityId>& ids, const Position& pos,
const AbilityType& type, const bool queue);
void do_cast_on_target(const std::vector<EntityId>& ids,
......
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