#include "player.hpp" #include "../../logging/easylogging++.h" #include Player::Player(std::shared_ptr assignedInput, const std::string &skinRessourceName, GridCoordinates initCoordinates) : spawnPosition(initCoordinates) { playerId = playerCreationCounter++; coordinates->setTranslated(spawnPosition); input = std::move(assignedInput); skinSprite = std::make_shared(skinRessourceName, getIsoSize()); addChildScreenOffset(skinSprite, IsometricCoordinates(-getIsoSize() / 2.f)); updateRadiusBasedOnLevel(); 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)) { points = (points + 1) * (1 + 1 * FRAME_TIME.asSeconds()); } else if (input->isPerformingAction(GameAction::SHRINK)) { points *= 1 - 1 * FRAME_TIME.asSeconds(); if (points < 0) { points = 0; } } updateRadiusBasedOnLevel(); 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::updateRadiusBasedOnLevel() { long points = getPoints(); float newWorldRadius = PLAYER_MIN_RADIUS + PLAYER_RADIUS_PER_LEVEL * pow(points / 100.f, 2); setWorldRadius(newWorldRadius); }