Select Git revision
Unit.cpp 6.16 KiB
/**
* Unit.cpp
*
* @authors Max Körschen
* @author Frederik Keens
* @author Lorenz Diel
*/
#include "Unit.hpp"
#include "../core/Config.hpp"
#include "../physics/PhysicsBody.hpp"
#include "../physics/PhysicsEngine.hpp"
#include <memory>
namespace advanced_wars
{
Unit::Unit(
int unitId, b2World* world, int tileX, int tileY, Faction faction, UnitTypeId unitTypeId,
UnitState state, Config& config)
: m_unitId(unitId), m_tileX(tileX), m_tileY(tileY), m_health(100),
m_cost(config.getUnitCost(unitTypeId)), m_ammo(config.getUnitAmmo(unitTypeId)),
m_minRange(config.getUnitMinRange(unitTypeId)),
m_maxRange(config.getUnitMaxRange(unitTypeId)), m_maxHealth(100),
m_movementPoints(config.getUnitMovementPoints(unitTypeId)),
m_movementType(config.getUnitMovementType(unitTypeId)), m_faction(faction),
m_unitTypeId(unitTypeId), m_state(state), m_world(world), m_animX(tileX), m_animY(tileY)
{
m_physicsBody = std::make_unique<PhysicsBody>(
m_world, tileX, tileY, 8.0F, 8.0F, 1.0, 0.3, true, BodyType::UNIT);
if (m_physicsBody && (m_physicsBody->getBody() != nullptr))
{
BodyUserData* bud = new BodyUserData();
bud->type = BodyUserData::Type::UNIT;
bud->id = m_unitId;
bud->data = this;
m_physicsBody->getBody()->GetUserData().pointer = reinterpret_cast<uintptr_t>(bud);
}
// Initialisieren der Primär- und Sekundärwaffe
std::unordered_map<UnitTypeId, int> primaryDamage;
std::unordered_map<UnitTypeId, int> secondaryDamage;
for (int targetIt = static_cast<int>(UnitTypeId::FIRST);
targetIt <= static_cast<int>(UnitTypeId::LAST); ++targetIt)
{
auto targetId = static_cast<UnitTypeId>(targetIt);
if (auto damage = config.getUnitPrimaryWeaponDamage(m_unitTypeId, targetId))
{
primaryDamage[targetId] = *damage;
}
if (auto damage = config.getUnitSecondaryWeaponDamage(m_unitTypeId, targetId))
{
secondaryDamage[targetId] = *damage;
}
}
m_primaryWeapon =
std::make_unique<Weapon>(Weapon(config.getUnitPrimaryWeapon(m_unitTypeId), primaryDamage));
m_secondaryWeapon = std::make_unique<Weapon>(
Weapon(config.getUnitSecondaryWeapon(m_unitTypeId), secondaryDamage));
}
void Unit::update(float deltaTime)
{
if (!m_physicsBody)
{