127 lines
2.8 KiB
C++
127 lines
2.8 KiB
C++
#include "player.hpp"
|
|
#include "../../logging/easylogging++.h"
|
|
|
|
#include <utility>
|
|
|
|
Player::Player(std::shared_ptr<InputIdentity> assignedInput, const std::string &skinRessourceName,
|
|
GridCoordinates initCoordinates)
|
|
: spawnPosition(initCoordinates), skinName(skinRessourceName)
|
|
{
|
|
playerId = playerCreationCounter++;
|
|
coordinates->setTranslated(spawnPosition);
|
|
input = std::move(assignedInput);
|
|
|
|
skinSprite = std::make_shared<VersatileSprite>(skinRessourceName, getIsoSize());
|
|
addChildScreenOffset(skinSprite, IsometricCoordinates(-getIsoSize() / 2.f));
|
|
|
|
updateRadiusBasedOnPoints();
|
|
|
|
LOG(INFO) << "Player " << playerId << " created.";
|
|
}
|
|
|
|
sf::Vector2f Player::getTrackablePosition() const
|
|
{
|
|
return coordinates->isometric().toScreen();
|
|
}
|
|
|
|
sf::Vector2f Player::getTrackableSize() const
|
|
{
|
|
return getIsoSize();
|
|
}
|
|
|
|
void Player::update()
|
|
{
|
|
if (!input->isActive)
|
|
{
|
|
setActive(false);
|
|
return;
|
|
}
|
|
|
|
if (!passiveMode) {
|
|
performInteractiveUpdates();
|
|
}
|
|
|
|
GameObject::update();
|
|
}
|
|
|
|
TrackableState Player::getTrackableState() const
|
|
{
|
|
if (getActive())
|
|
{
|
|
return TrackableState::TRACKING;
|
|
} else
|
|
{
|
|
return TrackableState::END_TRACKING;
|
|
}
|
|
}
|
|
|
|
int Player::getPlayerId() const
|
|
{
|
|
return playerId;
|
|
}
|
|
|
|
float Player::getWorldRadius() const
|
|
{
|
|
return radiusInWorld;
|
|
}
|
|
|
|
sf::Vector2f Player::getIsoSize() const
|
|
{
|
|
// TODO: For some reason, the player is a little to narrow. This fixes it.
|
|
const float fixFactor = sqrt(2);
|
|
float width = radiusInWorld * 2.f * WORLD_TO_ISO_SCALE * fixFactor;
|
|
return {width, width * ISOMETRIC_SKEW};
|
|
}
|
|
|
|
void Player::setWorldRadius(float newWorldRadius)
|
|
{
|
|
radiusInWorld = newWorldRadius;
|
|
|
|
// Update skin
|
|
auto newSize = getIsoSize();
|
|
skinSprite->setSize(newSize);
|
|
skinSprite->coordinates->setScreenOffset(IsometricCoordinates(-newSize / 2.f));
|
|
}
|
|
|
|
long Player::getPoints() const
|
|
{
|
|
return points;
|
|
}
|
|
|
|
void Player::updateRadiusBasedOnPoints()
|
|
{
|
|
long points = getPoints();
|
|
float newWorldRadius = PLAYER_MIN_RADIUS + PLAYER_RADIUS_PER_LEVEL * points / 10.f;
|
|
|
|
setWorldRadius(newWorldRadius);
|
|
}
|
|
|
|
void Player::consume(int points)
|
|
{
|
|
this->points += points;
|
|
LOG(INFO) << "Player " << playerId << " consumed " << points << " points. Total: " << this->points;
|
|
|
|
updateRadiusBasedOnPoints();
|
|
}
|
|
|
|
std::string Player::getSkinName() const
|
|
{
|
|
return skinName;
|
|
}
|
|
|
|
std::shared_ptr<InputIdentity> Player::getInput() const
|
|
{
|
|
return input;
|
|
}
|
|
|
|
void Player::setPassiveMode(bool newPassiveMode)
|
|
{
|
|
passiveMode = newPassiveMode;
|
|
}
|
|
|
|
void Player::performInteractiveUpdates()
|
|
{
|
|
auto moveDirection = input->direction.asIsometricVector();
|
|
auto moveDelta = moveDirection * speed * FRAME_TIME.asSeconds();
|
|
coordinates->move(moveDelta);
|
|
}
|