holesome/src/game/player/player.cpp

102 lines
2.5 KiB
C++
Raw Normal View History

2023-05-08 18:20:55 +02:00
#include "player.hpp"
#include "../../logging/easylogging++.h"
2023-05-17 23:22:45 +02:00
#include <utility>
Player::Player(std::shared_ptr<InputIdentity> assignedInput, const std::string &skinRessourceName,
2023-06-11 15:54:05 +02:00
GridCoordinates initCoordinates)
: spawnPosition(initCoordinates)
{
playerId = playerCreationCounter++;
coordinates->set(spawnPosition);
input = std::move(assignedInput);
2023-06-11 15:18:22 +02:00
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();
}
2023-05-24 14:00:51 +02:00
void Player::update()
{
if (!input->isActive)
{
setActive(false);
return;
}
auto moveDirection = input->direction.asIsometricVector();
2023-05-29 00:22:36 +02:00
auto moveDelta = moveDirection * speed * FRAME_TIME.asSeconds();
coordinates->move(moveDelta);
2023-06-04 20:55:14 +02:00
2023-06-11 15:18:22 +02:00
if (input->isPerformingAction(GameAction::GROW))
{
2023-06-11 15:18:22 +02:00
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);
}
2023-06-04 20:55:14 +02:00
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};
}
2023-06-11 15:18:22 +02:00
void Player::setWorldRadius(float newWorldRadius)
{
radiusInWorld = newWorldRadius;
// Update skin
auto newSize = getIsoSize();
skinSprite->setSize(newSize);
skinSprite->coordinates->setScreenOffset(IsometricCoordinates(-newSize / 2.f));
}
Player::~Player()
{
LOG(INFO) << "Player " << playerId << " destroyed.";
}