holesome/src/game/player/player.cpp

106 lines
2.4 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++;
2023-06-17 19:48:51 +02:00
coordinates->setTranslated(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));
2023-07-09 21:24:50 +02:00
updateRadiusBasedOnPoints();
2023-06-28 01:13:10 +02:00
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
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.
2023-07-06 00:21:41 +02:00
const float fixFactor = sqrt(2);
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));
}
2023-06-28 01:13:10 +02:00
long Player::getPoints() const
{
return points;
}
2023-07-09 21:24:50 +02:00
void Player::updateRadiusBasedOnPoints()
2023-06-28 01:13:10 +02:00
{
long points = getPoints();
2023-07-09 21:24:50 +02:00
float newWorldRadius = PLAYER_MIN_RADIUS + PLAYER_RADIUS_PER_LEVEL * points / 10.f;
2023-06-28 01:13:10 +02:00
setWorldRadius(newWorldRadius);
}
2023-07-09 21:24:50 +02:00
void Player::consume(int points)
{
this->points += points;
LOG(INFO) << "Player " << playerId << " consumed " << points << " points. Total: " << this->points;
updateRadiusBasedOnPoints();
}