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

Update with move position and format unit.cpp

parent 85ba7225
No related branches found
No related tags found
1 merge request!15Merge units into main
#include "unit.hpp"
#include <tinyxml2.h>
namespace advanced_wars {
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)
{
health = max_health;
};
void Unit::render(Engine &engine, int scale) {
void Unit::render(Engine &engine, int scale)
{
Spritesheet *spritesheet = engine.get_spritesheet();
while (engine.events not empty)
{
handle_event(events.pop_front());
}
int step = engine.get_stage() % spritesheet->get_unit_textures()
......@@ -23,7 +25,8 @@ void Unit::render(Engine &engine, int scale) {
.at(static_cast<int>(state))
.second;
if (state == UnitState::IDLE || state == UnitState::UNAVAILABLE) {
if (state == UnitState::IDLE || state == UnitState::UNAVAILABLE)
{
SDL_Rect src;
src.x = step * spritesheet->get_unit_width();
......@@ -44,7 +47,9 @@ void Unit::render(Engine &engine, int scale) {
.at(static_cast<int>(state))
.first,
&src, &dst, 0, NULL, SDL_FLIP_NONE);
} else {
}
else
{
// The moving states have a resolution of 24x24 instead of 16x16 and need to
// be handled separately
SDL_Rect src;
......@@ -69,9 +74,11 @@ void Unit::render(Engine &engine, int scale) {
}
}
void Unit::attack(Unit &ally, Unit &enemy) {
void Unit::attack(Unit &ally, Unit &enemy)
{
if (ally->has_attacked) {
if (ally->has_attacked)
{
// display the unit as not able to attack (maybe greyscale?)
}
......@@ -80,29 +87,43 @@ void Unit::attack(Unit &ally, Unit &enemy) {
int defDamage = damageMatrix[ally->id][enemy->id] * ((enemy->health) / (enemy->max_health));
enemy->health = enemy->health - offDamage;
if(enemy->health > 0) {
if (enemy->health > 0)
{
ally->health = ally->health - defDamage;
if(ally->health <= 0) {
if (ally->health <= 0)
{
ally->~Unit();
}
} else {
}
else
{
enemy->~Unit();
}
}
void Unit::onClick(SDL_Event event) {
void Unit::update_position(int posX, int posY)
{
this->x = posX;
this->y = posY;
}
void Unit::onClick(SDL_EVENT event)
{
Unit & defender;
Unit & attacker;
switch (event.button.button) {
switch (event.button.button)
{
case SDL_BUTTON_LEFT:
// we have to re-initialize the unit.state (probably to idle)
this->is_selected = true;
for (Unit &unit : units) {
if(inRange(unit)) {
for (Unit &unit : units)
{
if (inRange(unit))
{
unit.state = UNAVAILABLE;
};
}
......@@ -111,44 +132,56 @@ void Unit::onClick(SDL_Event event) {
this->is_targeted = true;
for (Unit &unit : units) {
if(unit.state = UNAVAILABLE) {
for (Unit &unit : units)
{
if (unit.state = UNAVAILABLE)
{
continue;
}
if(unit.is_selected) {
if (unit.is_selected)
{
attacker = &unit;
}
if(unit.is_targeted) {
if (unit.is_targeted)
{
defender = &unit;
}
}
if(attacker && defender) {
if (attacker && defender)
{
attack(attacker, defender);
break;
} else {
}
else
{
stderr("Could not init the attack!");
break;
}
}
}
bool Unit::inRange(Unit &enemy) {
if (this->x == enemy.x) {
bool Unit::inRange(Unit &enemy)
{
if (this->x == enemy.x)
{
return abs(this->y - enemy.y) <= this->range;
} else if (this->y == enemy.y) {
}
else if (this->y == enemy.y)
{
return abs(this->x - enemy.x) <= this->range;
}
return false;
}
void Unit::loadXML(const char* filename) {
void Unit::loadXML(const char *filename)
{
tinyxml2::XMLDocument doc;
if (doc.LoadFile(filename) != tinyxml2::XML_SUCCESS) {
if (doc.LoadFile(filename) != tinyxml2::XML_SUCCESS)
{
std::cerr << "Fehler beim Laden der XML-Datei!" << std::endl;
return;
}
......@@ -157,7 +190,8 @@ void Unit::loadXML(const char* filename) {
tinyxml2::XMLElement *unitElement = doc.FirstChildElement("Units")->FirstChildElement("Unit");
// get all Units
while(unitElement) {
while (unitElement)
{
const u_int8_t UnitId = unitElement->Attribute("id");
......@@ -165,11 +199,13 @@ void Unit::loadXML(const char* filename) {
tinyxml2::XMLElement *attackElement = unitElement->FirstChildElement("Attack");
// get all attack-values
while(attackElement) {
while (attackElement)
{
tinyxml2::XMLElement *attackTypeElement = attackElement->FirstChildElement();
while(attackTypeElement) {
while (attackTypeElement)
{
UnitId Unit_Id = static_cast<UnitId> attackTypeElement->Name(); // wenn das geht kauf ich maggus sein kochbuch
int attackValue = attackTypeElement->IntText();
......@@ -183,6 +219,5 @@ void Unit::loadXML(const char* filename) {
damageMatrix[unitElement][attackValues];
unitElement = unitElement->NextSiblingElement("Unit");
}
}
} // namespace advanced_wars
\ No newline at end of file
......@@ -81,11 +81,12 @@ public:
Will Update the health for both units
Attacker deals damage to the defender first
*/
void attack(Unit &enemy);
void attack(Unit &ally ,Unit &enemy);
/*
@params Takes the desired position of the unit and updates its values
This will teleport the unit, there is no smooth transition between tiles
*/
void update_position(int posX, int posY);
......@@ -102,6 +103,11 @@ public:
*/
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);
private:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment