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

Add actual fighting mechanics

Modifie Level.hpp with eventhandling (bare-bones)
Fix potential bugs in unit.cpp
parent c3aeb3d7
Branches
No related tags found
1 merge request!15Merge units into main
......@@ -22,11 +22,50 @@ Level::Level(std::string name, int width, int height, std::vector<Tile> tiles,
}
};
void Level::render(Engine &engine, std::vector<SDL_Event> &events) {
const int RENDERING_SCALE = 3;
bool Level::clickCheck(int mouseX, int mouseY) {
int tileX = mouseX/(16*RENDERING_SCALE);
int tileY = mouseY/(16*RENDERING_SCALE);
for (auto& unit : units) {
if(unit.x == tileX && unit.y == tileY) {
//std::cout << "X:" << unit.x << "Y:" << unit.y << std::endl;
selectedUnit = &unit;
return true;
}
}
}
void Level::handleEvent(Engine &engine, SDL_Event &event) {
//handle following events:
//clicks/mouseDown
//escape (esc)
switch (event.type)
{
case SDL_MOUSEBUTTONDOWN:
if(clickCheck(event.button.x, event.button.y)) {
selectedUnit->onClick(event, units);
}
break;
default:
break;
}
}
void Level::render(Engine &engine, std::vector<SDL_Event> &events) {
// Iterate over all events
while (!events.empty()) {
//events.erase(events.begin());
handleEvent(engine, events.at(0));
events.erase(events.begin());
}
......
......@@ -23,6 +23,8 @@ public:
void render(Engine &engine, std::vector<SDL_Event> &events);
void handleEvent(Engine &engine, SDL_Event &event);
private:
std::string name;
int width;
......@@ -31,6 +33,9 @@ private:
std::vector<Building> buildings;
std::vector<Unit> units;
std::vector<Effect> effects;
Unit* selectedUnit;
bool clickCheck(int mouseX, int mouseY);
};
} // namespace advanced_wars
......@@ -82,7 +82,7 @@ int main() {
engine.set_scene(level);
Spritesheet spritesheet("/media/data/rust/sprite-extractor/spritesheet.h5",
Spritesheet spritesheet("./spritesheet.h5",
engine);
engine.set_spritesheet(spritesheet);
......
......@@ -6,7 +6,7 @@ namespace advanced_wars
{
Unit::Unit(int x, int y, UnitFaction faction, UnitId id, UnitState state)
: x(x), y(y), faction(faction), id(id), state(state)
: x(x), y(y), faction(faction), id(id), state(state), max_health(100)
{
health = max_health;
};
......@@ -106,7 +106,7 @@ namespace advanced_wars
this->y = posY;
}
void Unit::onClick(SDL_Event event)
void Unit::onClick(SDL_Event event, std::vector<Unit> &unitVector)
{
Unit *defender = nullptr;
......@@ -118,45 +118,50 @@ namespace advanced_wars
// we have to re-initialize the unit.state (probably to idle)
this->is_selected = true;
std::cout << "I am selected!!" << std::endl;
/*
for (Unit *unit : units)
{
if (inRange(unit))
if (!inRange(unit))
{
unit->state = advanced_wars::UnitState::UNAVAILABLE;
};
}
*/
break;
case SDL_BUTTON_RIGHT:
this->is_targeted = true;
std::cout << "I am targeted!!" << std::endl;
for (Unit *unit : units)
for (Unit unit : unitVector)
{
if (unit->state == advanced_wars::UnitState::UNAVAILABLE)
if (unit.state == advanced_wars::UnitState::UNAVAILABLE)
{
continue;
}
if (unit->is_selected)
if (unit.is_selected)
{
attacker = unit;
attacker = &unit;
}
if (unit->is_targeted)
if (unit.is_targeted)
{
defender = unit;
defender = &unit;
}
}
if (attacker && defender)
if (attacker != nullptr && defender != nullptr)
{
attack(attacker, defender);
std::cout << "We are fighting!!" << std::endl;
break;
}
else
{
std::cerr << "Fehler beim Laden der XML-Datei!" << std::endl;
std::cerr << "Angriff konnte nicht gestartet werden!" << std::endl;
break;
}
}
......
......@@ -60,6 +60,10 @@ using MatchupTabel = std::unordered_map<u_int8_t, std::unordered_map<u_int8_t, i
class Unit {
public:
int x;
int y;
Unit(int x, int y, UnitFaction faction, UnitId id, UnitState state);
~Unit() {
//Assuming that the destruktion of a unit triggers events
......@@ -109,12 +113,9 @@ void loadXML(const char* filename);
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
*/
void onClick(SDL_Event event);
void onClick(SDL_Event event, std::vector<Unit> &unitVector);
private:
int x;
int y;
UnitFaction faction;
UnitId id;
UnitState state;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment