96 lines
2.4 KiB
C++
96 lines
2.4 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)
|
|
{
|
|
playerId = playerCreationCounter++;
|
|
coordinates->setTranslated(spawnPosition);
|
|
|
|
input = std::move(assignedInput);
|
|
|
|
skinSprite = std::make_shared<VersatileSprite>(skinRessourceName, getIsoSize());
|
|
addChildScreenOffset(skinSprite, IsometricCoordinates(-getIsoSize() / 2.f));
|
|
|
|
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;
|
|
}
|
|
|
|
auto moveDirection = input->direction.asIsometricVector();
|
|
auto moveDelta = moveDirection * speed * FRAME_TIME.asSeconds();
|
|
coordinates->move(moveDelta);
|
|
|
|
if (input->isPerformingAction(GameAction::GROW))
|
|
{
|
|
setWorldRadius(radiusInWorld * (1 + PLAYER_PROPORTIONAL_SIZE_CHANGE_SPEED * FRAME_TIME.asSeconds()));
|
|
} else if (input->isPerformingAction(GameAction::SHRINK))
|
|
{
|
|
auto newRadius = radiusInWorld * (1 - PLAYER_PROPORTIONAL_SIZE_CHANGE_SPEED * FRAME_TIME.asSeconds());
|
|
if (newRadius <= DEFAULT_PLAYER_RADIUS)
|
|
{
|
|
newRadius = DEFAULT_PLAYER_RADIUS;
|
|
}
|
|
setWorldRadius(newRadius);
|
|
}
|
|
|
|
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 = 1.4f;
|
|
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));
|
|
}
|