Large refactoring. Also, reimplemented resource manager to use parsers. Changed from json to xml (it allows comments!!!).
This commit is contained in:
162
src/controller/player/PlayerController.cpp
Normal file
162
src/controller/player/PlayerController.cpp
Normal file
@ -0,0 +1,162 @@
|
||||
/*
|
||||
* PlayerController.cpp
|
||||
*
|
||||
* Created on: Nov 27, 2016
|
||||
* Author: tibi
|
||||
*/
|
||||
|
||||
#include <GameState.h>
|
||||
#include <controller/player/PlayerController.h>
|
||||
#include <graphics/SpriteRenderer.h>
|
||||
#include <input/Input.h>
|
||||
#include <utils/Assert.h>
|
||||
|
||||
using namespace farmlands::input;
|
||||
using namespace farmlands::graphics;
|
||||
using namespace farmlands::model;
|
||||
|
||||
namespace farmlands {
|
||||
namespace controller {
|
||||
namespace player {
|
||||
|
||||
static const float PlayerWalkVelocity = 2.0f; // The default velocity of the player when walking (units/sec).
|
||||
static const float PlayerRunVelocity = 4.0f; // The default velocity of the player when running (units/sec).
|
||||
static const float PlayerAttackVelocity = 0.1f; // Movement speed when attacking.
|
||||
|
||||
/**
|
||||
* Direction enum based on sign of velocity on x and y
|
||||
*/
|
||||
static const Direction VelocitySignDirections[3][3] =
|
||||
{
|
||||
{ Direction::NorthWest, Direction::West, Direction::SouthWest },
|
||||
{ Direction::North, Direction::None, Direction::South },
|
||||
{ Direction::NorthEast, Direction::East, Direction::SouthEast },
|
||||
};
|
||||
|
||||
PlayerController::PlayerController()
|
||||
: m_transform(nullptr),
|
||||
m_attackTimeLeft(0),
|
||||
m_facingDirection(Direction::South),
|
||||
m_walking(false),
|
||||
m_running(false)
|
||||
{
|
||||
}
|
||||
|
||||
PlayerController::~PlayerController()
|
||||
{
|
||||
}
|
||||
|
||||
void PlayerController::onInitialize()
|
||||
{
|
||||
m_transform = gameObject->component<base::Transform>();
|
||||
}
|
||||
|
||||
bool PlayerController::onEvent(SDL_Event& event)
|
||||
{
|
||||
bool attack1 = (Input::instance().down(GameKey::Action, event));
|
||||
bool attack2 = (Input::instance().down(GameKey::Action2, event));
|
||||
|
||||
if (m_attackTimeLeft == 0 && (attack1 || attack2))
|
||||
{
|
||||
attack();
|
||||
m_attackTimeLeft = 20 + attack2 * 20;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void PlayerController::onUpdateLogic()
|
||||
{
|
||||
m_running = false;
|
||||
m_walking = false;
|
||||
|
||||
// Compute movement velocity
|
||||
float velMultiplier = PlayerWalkVelocity;
|
||||
|
||||
if (m_attackTimeLeft)
|
||||
{
|
||||
velMultiplier = PlayerAttackVelocity;
|
||||
--m_attackTimeLeft;
|
||||
}
|
||||
|
||||
else if (Input::instance().pressed(GameKey::Run))
|
||||
{
|
||||
velMultiplier = PlayerRunVelocity;
|
||||
m_running = true;
|
||||
}
|
||||
|
||||
|
||||
// Make movement time independent
|
||||
velMultiplier *= GameState::current().elapsedTime;
|
||||
|
||||
// Get velocity of axes
|
||||
float vx = Input::instance().getX() * velMultiplier;
|
||||
float vy = Input::instance().getY() * velMultiplier;
|
||||
|
||||
// Check if we can move to the new position
|
||||
float newX = m_transform->x + vx;
|
||||
float newY = m_transform->y + vy;
|
||||
if ((vx || vy) && canMove(newX, newY))
|
||||
{
|
||||
m_walking = true;
|
||||
m_transform->x = newX;
|
||||
m_transform->y = newY;
|
||||
|
||||
m_facingDirection = getDirection(vx, vy);
|
||||
}
|
||||
}
|
||||
|
||||
void PlayerController::onPreRender()
|
||||
{
|
||||
// Get sprite
|
||||
SpriteRenderer* spriteRenderer = gameObject->component<SpriteRenderer>();
|
||||
|
||||
// Compute current state
|
||||
std::string stateName = (m_walking) ? "Walking " : "Idle ";
|
||||
|
||||
if (m_facingDirection & Direction::East)
|
||||
stateName += "right";
|
||||
else if (m_facingDirection & Direction::West)
|
||||
stateName += "left";
|
||||
else if (m_facingDirection & Direction::North)
|
||||
stateName += "up";
|
||||
else
|
||||
stateName += "down";
|
||||
|
||||
spriteRenderer->sprite->setState(stateName);
|
||||
|
||||
// Set animation velocity
|
||||
float animVelocity = (m_running) ? 1.0f : 0.7f;
|
||||
if (m_attackTimeLeft)
|
||||
animVelocity = 0.1f;
|
||||
|
||||
spriteRenderer->sprite->setAnimationVelocity(animVelocity);
|
||||
|
||||
// Set camera
|
||||
base::Transform* cam = GameState::current().renderContext.cameraTransform();
|
||||
cam->x = m_transform->x;
|
||||
cam->y = m_transform->y - 1;
|
||||
}
|
||||
|
||||
bool PlayerController::canMove(float x, float y)
|
||||
{
|
||||
// TODO: check collisions & stuff. For now, nothing
|
||||
return true;
|
||||
}
|
||||
|
||||
Direction PlayerController::getDirection(float vx, float vy)
|
||||
{
|
||||
int xx = (0 < vx) - (vx < 0);
|
||||
int yy = (0 < vy) - (vy < 0);
|
||||
|
||||
return VelocitySignDirections[xx + 1][yy + 1];
|
||||
}
|
||||
|
||||
void PlayerController::attack()
|
||||
{
|
||||
// For now - nothing
|
||||
}
|
||||
|
||||
}
|
||||
} /* namespace controller */
|
||||
} /* namespace farmlands */
|
Reference in New Issue
Block a user