Skip to content
This repository has been archived by the owner on Jul 1, 2022. It is now read-only.

Commit

Permalink
Readd the attack action to use the new player action system
Browse files Browse the repository at this point in the history
We add a generic use action, so we can choose between attack, harvest,
etc.. depending on context, like the other RTS games do
  • Loading branch information
arthurmco committed Apr 18, 2020
1 parent 2e78812 commit 867102b
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 16 deletions.
31 changes: 21 additions & 10 deletions src/client/HumanPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ bool zoom_mouse = false;
bool build_something = false;
bool build_tent = false, build_tower = false;

bool move_something = false;
bool do_something = false;

std::weak_ptr<GameObject> attacker, attackee;

Expand All @@ -48,8 +48,6 @@ HumanPlayer::HumanPlayer(PlayerManager &pm, const char *name, int code)
if (std::holds_alternative<KeyAction>(hia.type)) {
auto event = std::get<KeyAction>(hia.type);

printf("<%s>\n", event.isPressed ? "pressed" : "released");

switch (event.keycode) {
case SDLK_w:
if (event.isPressed)
Expand Down Expand Up @@ -162,10 +160,10 @@ HumanPlayer::HumanPlayer(PlayerManager &pm, const char *name, int code)

if (event.buttonCode == SDL_BUTTON_RIGHT) {
if (event.isPressed) {
move_something = true;
do_something = true;

} else {
move_something = false;
do_something = false;
}

return true;
Expand Down Expand Up @@ -231,11 +229,11 @@ void HumanPlayer::generateInput()
{
double camera_speed = 0.1;
double zoom_speed = 0.01;

glm::vec2 cameraSpeedVec = glm::vec2(0, 0);
bool isCameraMove = front || back || left || right || zoom_mouse;
double zoom_val = 0;

if (front) {
cameraSpeedVec.y -= camera_speed;
}
Expand Down Expand Up @@ -304,13 +302,26 @@ void HumanPlayer::generateInput()
mouse_click = false;
}

if (selected_.size() > 0 && move_something) {
if (selected_.size() > 0 && do_something) {
// Requested to move a selected object
glm::vec2 to = _ip->GetGameProjectedPosition();
this->pushAction(SelectedObjectMoveAction{to.x, to.y});

move_something = false;
if (has_selection) {
// Requested to run the default action on the selected object
// Can be harvest or attack.
auto l = _ip->GetIntersectedObject().lock();
this->pushAction(ObjectUseAction{l->getID()});

// TODO: how to treat things when the object is too far from the
// destination to perform an action?
} else {
this->pushAction(SelectedObjectMoveAction{to.x, to.y});
}


do_something = false;
}

}


Expand Down
2 changes: 1 addition & 1 deletion src/client/graphical/gl_renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ VertexHandle* GLRenderer::createVertex(VertexData& vd, VertexInfo& vi)
auto vhandle = std::make_unique<GLVertexHandle>(vao, *this, vi);
vhandle->vsize = vd.position.size();

log->write("gl-renderer", LogType::Info,
log->write("gl-renderer", LogType::Debug,
"created vertex handle: vao=%#x, vsize=%zu",
vao, vhandle->vsize);

Expand Down
8 changes: 4 additions & 4 deletions src/common/logic/object_components.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ std::optional<double> AttackComponent::doDirectAttack(const AttackComponent& def

if (validAngle && this->atkDistance > defDistance) {

//printf("sin (atk/def): %.2f %.2f\t", sin(arcUpper), sin_defAngle);
//printf("cos (atk/def): %.2f %.2f\t", cos(arcUpper), cos_defAngle);
//printf("distance (atk/def): %.2f %.2f\n", this->atkDistance, defDistance);

const double factor = (1 - std::abs(sin_defAngle / arcUpper));

// TODO: Occasionally, the armor points will not be considered
Expand All @@ -83,10 +87,6 @@ std::optional<double> AttackComponent::doDirectAttack(const AttackComponent& def
- defender.armor;
return std::make_optional(std::max(0.0, damage));
}

printf("sin (atk/def): %.2f %.2f\t", sin(arcUpper), sin_defAngle);
printf("cos (atk/def): %.2f %.2f\t", cos(arcUpper), cos_defAngle);
printf("distance (atk/def): %.2f %.2f\n", this->atkDistance, defDistance);

return std::nullopt;

Expand Down
29 changes: 28 additions & 1 deletion src/common/logic/player_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,36 @@ void PlayerManager::processAction(const PlayerInputAction& pia, ObjectManager& o
void operator()(ObjectUseAction a) {
auto& log = LoggerService::getLogger();
log->write("player-manager", LogType::Debug,
"%s type: ObjectUseAction: make selected objects do action on object %ld",
"%s type: ObjectUseAction: make selected objects do default action on object %ld",
fmt::to_string(out).data(),
a.useWhat);

if (this->pl.has_value()) {
auto player = (*this->pl);

/// We only implement the attack action, but other actions will be implemented
/// when the action system is good.
std::string default_action = std::string{"attack"};

if (default_action == std::string{"attack"}) {
auto attacker_w = (*pl)->getSelections().at(0);
auto attackee_o = om.get(a.useWhat);

if (!attacker_w.expired() && attackee_o.has_value()) {
auto attacker = attacker_w.lock();
auto attackee = (*attackee_o);

auto& atkManager = LogicService::getAttackManager();
atkManager->doRegister(
attacker->getID(), attacker->getAttackComponent().value());
atkManager->doRegister(
attackee->getID(), attackee->getAttackComponent().value());
atkManager->startAttack(
attacker->getID(), attackee->getID());
}

}
}
}
void operator()(ObjectRunAction a) {
auto& log = LoggerService::getLogger();
Expand Down

0 comments on commit 867102b

Please sign in to comment.