Skip to content
Snippets Groups Projects
Commit 3528a3d6 authored by Max Cherris's avatar Max Cherris
Browse files

Refactor left_click

Why does the clicked unit dissappear after moving it?
parent af6c8e06
Branches
No related tags found
1 merge request!15Merge units into main
......@@ -113,7 +113,7 @@ void Level::handleEvent(Engine &engine, SDL_Event &event) {
if(click_check_left(event.button.x, event.button.y)) {
if(selectedUnit) {
selectedUnit->onClick(event, units);
selectedUnit->on_left_click(event, units);
}
if(selectedBuilding) {
......@@ -146,8 +146,6 @@ void Level::handleEvent(Engine &engine, SDL_Event &event) {
} else {
selectedUnit->update_position(tileX, tileY);
}
......
......@@ -175,60 +175,11 @@ Features:
- show context menu
*/
void Unit::onClick(SDL_Event event, std::vector<Unit> &unitVector)
void Unit::on_left_click(SDL_Event event, std::vector<Unit> &unitVector)
{
Unit *defender = nullptr;
Unit *attacker = nullptr;
std::cout << "Left-button pressed on unit: " << this->health << std::endl;
switch (event.button.button)
{
case SDL_BUTTON_LEFT:
// we have to re-initialize the unit.state (probably to idle)
this->is_selected = true;
std::cout << "I am selected!!" << std::endl;
std::cout << "And my position is:" << this->x << " " << this->y << std::endl;
//make move range calc
break;
case SDL_BUTTON_RIGHT:
this->is_targeted = true;
std::cout << "I am targeted!!" << std::endl;
std::cout << "And my position is:" << this->x << " " << this->y << std::endl;
for (Unit unit : unitVector)
{
if (unit.state == advanced_wars::UnitState::UNAVAILABLE)
{
continue;
}
if (unit.is_selected)
{
attacker = &unit;
}
if (unit.is_targeted)
{
defender = &unit;
}
}
if (attacker != nullptr && defender != nullptr)
{
//attack(attacker, defender);
std::cout << "We are fighting!!" << std::endl;
break;
}
else
{
std::cerr << "Angriff konnte nicht gestartet werden!" << std::endl;
break;
}
}
}
bool Unit::inRange(Unit *enemy)
......
......@@ -112,9 +112,9 @@ public:
/*
This function will be called by an external event-handler, eventually.
Currently, it should be called if a Unit is interacted with and the resulting SDL_EVENT is passed through, and then decided upon
It should start displaying standard unit information, such as UI and move_range
*/
void onClick(SDL_Event event, std::vector<Unit> &unitVector);
void on_left_click(SDL_Event event, std::vector<Unit> &unitVector);
private:
UnitFaction faction;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment