Commit f254e423 authored by louiz’'s avatar louiz’

Implement a way to cast on a target, in addition to cast on a position

parent 5a736198
......@@ -67,13 +67,22 @@ void Game::cast_callback(Message* message)
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();
AbilityType type = static_cast<AbilityType>(srl.type());
uint32_t type = srl.type();
if (srl.has_target())
{
this->turn_handler.insert_action(
std::bind(&World::do_cast_on_target, &this->world, ids, srl.target(), type, srl.queue()),
srl.turn());
}
else if (srl.has_pos())
{
Position pos;
pos.x.raw() = srl.pos().x();
pos.y.raw() = srl.pos().y();
this->turn_handler.insert_action(std::bind(&World::do_cast, &this->world, ids, pos,
static_cast<AbilityType>(type), srl.queue()),
srl.turn());
this->turn_handler.insert_action(
std::bind(&World::do_cast_on_pos, &this->world, ids, pos, type, srl.queue()),
srl.turn());
}
}
......@@ -268,8 +268,9 @@ bool GameClient::action_move(const std::vector<EntityId>& ids, const Position& p
return true;
}
bool GameClient::action_cast(const std::vector<EntityId>& ids, const Position& pos,
const AbilityType& type, const bool queue)
bool GameClient::action_cast(const std::vector<EntityId>& ids,
const Position& pos, const AbilityType& type,
const bool queue)
{
ser::request::Cast srl;
if (queue)
......@@ -283,6 +284,21 @@ bool GameClient::action_cast(const std::vector<EntityId>& ids, const Position& p
return true;
}
bool GameClient::action_cast(const std::vector<EntityId>& ids,
const EntityId target_id, const AbilityType& type,
const bool queue)
{
ser::request::Cast srl;
if (queue)
srl.set_queue(queue);
srl.set_target(target_id);
for (const EntityId id: ids)
srl.add_entity_id(id);
srl.set_type(static_cast<uint32_t>(type));
this->send_message("CAST", srl);
return true;
}
void GameClient::select_entity(const Entity* entity)
{
this->current_selection.add_to_selection(entity);
......
......@@ -271,6 +271,8 @@ void GameServer::on_cast_request(Message* message)
pos.y.raw() = srl.pos().y();
this->send_cast_order(ids, pos, srl.type(), srl.queue());
}
else if (srl.has_target())
this->send_cast_order(ids, srl.target(), srl.type(), srl.queue());
}
void GameServer::send_cast_order(const std::vector<EntityId>& ids, const Position& pos,
......@@ -286,7 +288,24 @@ void GameServer::send_cast_order(const std::vector<EntityId>& ids, const Positio
srl.mutable_pos()->set_x(pos.x.raw());
srl.mutable_pos()->set_y(pos.y.raw());
this->send_order_to_all("CAST", srl);
this->turn_handler.insert_action(std::bind(&World::do_cast, &this->world, ids, pos,
this->turn_handler.insert_action(std::bind(&World::do_cast_on_pos, &this->world, ids, pos,
static_cast<AbilityType>(type), queue),
srl.turn());
}
void GameServer::send_cast_order(const std::vector<EntityId>& ids, const EntityId target_id,
const uint32_t type, const bool queue)
{
ser::order::Cast srl;
srl.set_turn(this->turn_handler.get_current_turn() + 2);
if (queue)
srl.set_queue(queue);
srl.set_target(target_id);
for (const EntityId id: ids)
srl.add_entity_id(id);
srl.set_type(type);
this->send_order_to_all("CAST", srl);
this->turn_handler.insert_action(std::bind(&World::do_cast_on_target, &this->world, ids, target_id,
static_cast<AbilityType>(type), queue),
srl.turn());
}
......
......@@ -81,7 +81,7 @@ public:
*/
void send_cast_order(const std::vector<EntityId>& ids, const Position& pos,
const uint32_t type, const bool queue);
void send_cast_order(const std::vector<EntityId>& ids, const EntityId pos,
void send_cast_order(const std::vector<EntityId>& ids, const EntityId target_id,
const uint32_t type, const bool queue);
void send_cast_order(const std::vector<EntityId>& ids,
const uint32_t type, const bool queue);
......
......@@ -35,5 +35,6 @@ message Cast
optional bool queue = 2 [default = false];
repeated uint32 entity_id = 3;
optional ser.game.Position pos = 4;
required uint32 type = 5;
optional uint32 target = 5;
required uint32 type = 6;
}
\ No newline at end of file
......@@ -19,5 +19,6 @@ message Cast
repeated uint32 entity_id = 1;
optional bool queue = 2 [default = false];
optional ser.game.Position pos = 3;
required uint32 type = 4;
optional uint32 target = 4;
required uint32 type = 5;
}
\ No newline at end of file
#ifndef ABILITY_HPP_INCLUDED
#define ABILITY_HPP_INCLUDED
#include <memory>
#include <string>
class World;
......@@ -26,7 +27,7 @@ public:
virtual ~Ability() = default;
virtual void tick(Entity*, World*) {}
virtual void cast(Entity*, World*, const Position&, const bool) {}
virtual void cast(Entity*, World*, Entity*, const bool) {}
virtual void cast(Entity*, World*, const std::shared_ptr<Entity>&, const bool) {}
virtual const std::string& get_name() const = 0;
virtual AbilityType get_type() const = 0;
......
......@@ -149,7 +149,30 @@ void World::do_move(const std::vector<EntityId>& ids, const Position& pos, const
}
}
void World::do_cast(const std::vector<EntityId>& ids, const Position& pos, const AbilityType& type, const bool queue)
void World::do_cast_on_target(const std::vector<EntityId>& ids, const EntityId target_id, const AbilityType& type, const bool queue)
{
std::shared_ptr<Entity> target = this->get_shared_entity_by_id(target_id);
if (!target)
{
log_warning("Could not cast ability on target " << target_id);
return ;
}
for (const EntityId id: ids)
{
Entity* entity = this->get_entity_by_id(id);
if (!entity)
continue;
Ability* ability = get_ability(entity, type);
if (ability)
ability->cast(entity, this, target, queue);
else
{
log_warning("Received a cast order, but entity " << id << " does not have ability: " << static_cast<int>(type));
}
}
}
void World::do_cast_on_pos(const std::vector<EntityId>& ids, const Position& pos, const AbilityType& type, const bool queue)
{
for (const EntityId id: ids)
{
......
......@@ -68,8 +68,13 @@ public:
Path calculate_path(Position endpos, Location* location);
Entity* do_new_entity(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_cast(const std::vector<EntityId>& ids, const Position& pos, const AbilityType& type, const bool queue);
void do_move(const std::vector<EntityId>& ids, const Position& pos,
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,
const EntityId target_id, const AbilityType& type,
const bool queue);
Entity* get_entity_by_id(EntityId id);
/**
* Sends a message to the server saying that we confirm that action.
......
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