43dadcdbb8
I just _had_ to get rid of that horrible libs/ directory. Now most of the graphical stuff is in client/, but I'll create more subdirectories so that they're not getting in the way.
115 lines
2.7 KiB
C++
115 lines
2.7 KiB
C++
/* Copyright: (c) Kayne Ruse 2013
|
|
*
|
|
* This software is provided 'as-is', without any express or implied
|
|
* warranty. In no event will the authors be held liable for any damages
|
|
* arising from the use of this software.
|
|
*
|
|
* Permission is granted to anyone to use this software for any purpose,
|
|
* including commercial applications, and to alter it and redistribute it
|
|
* freely, subject to the following restrictions:
|
|
*
|
|
* 1. The origin of this software must not be misrepresented; you must not
|
|
* claim that you wrote the original software. If you use this software
|
|
* in a product, an acknowledgment in the product documentation would be
|
|
* appreciated but is not required.
|
|
*
|
|
* 2. Altered source versions must be plainly marked as such, and must not be
|
|
* misrepresented as being the original software.
|
|
*
|
|
* 3. This notice may not be removed or altered from any source
|
|
* distribution.
|
|
*/
|
|
#include "player_character.hpp"
|
|
|
|
void PlayerCharacter::Update(double delta) {
|
|
if (limitSpeed) {
|
|
constexpr double d = 1.0/sqrt(2);
|
|
position += motion * delta * d;
|
|
}
|
|
else {
|
|
position += motion * delta;
|
|
}
|
|
sprite.Update(delta);
|
|
}
|
|
|
|
void PlayerCharacter::MoveDirection(CardinalDirection cd) {
|
|
//shift the movement in this direction
|
|
switch(cd) {
|
|
case CardinalDirection::NORTH:
|
|
if (motion.y >= 0) {
|
|
motion.y -= WALKING_SPEED;
|
|
}
|
|
break;
|
|
case CardinalDirection::SOUTH:
|
|
if (motion.y <= 0) {
|
|
motion.y += WALKING_SPEED;
|
|
}
|
|
break;
|
|
case CardinalDirection::WEST:
|
|
if (motion.x >= 0) {
|
|
motion.x -= WALKING_SPEED;
|
|
}
|
|
break;
|
|
case CardinalDirection::EAST:
|
|
if (motion.x <= 0) {
|
|
motion.x += WALKING_SPEED;
|
|
}
|
|
break;
|
|
}
|
|
//face the correct direction
|
|
FaceDirection();
|
|
}
|
|
|
|
void PlayerCharacter::FaceDirection(CardinalDirection cd) {
|
|
switch(cd) {
|
|
case CardinalDirection::NORTH:
|
|
sprite.SetCurrentStrip(1);
|
|
break;
|
|
case CardinalDirection::SOUTH:
|
|
sprite.SetCurrentStrip(0);
|
|
break;
|
|
case CardinalDirection::WEST:
|
|
sprite.SetCurrentStrip(2);
|
|
break;
|
|
case CardinalDirection::EAST:
|
|
sprite.SetCurrentStrip(3);
|
|
break;
|
|
}
|
|
}
|
|
|
|
void PlayerCharacter::FaceDirection() {
|
|
//base the direction on the character's movement
|
|
if (motion.y < 0) {
|
|
FaceDirection(CardinalDirection::NORTH);
|
|
}
|
|
if (motion.y > 0) {
|
|
FaceDirection(CardinalDirection::SOUTH);
|
|
}
|
|
if (motion.x < 0) {
|
|
FaceDirection(CardinalDirection::WEST);
|
|
}
|
|
if (motion.x > 0) {
|
|
FaceDirection(CardinalDirection::EAST);
|
|
}
|
|
CheckSpeed();
|
|
}
|
|
|
|
void PlayerCharacter::CheckSpeed() {
|
|
//diagonal
|
|
if (motion.x != 0 && motion.y != 0) {
|
|
sprite.SetDelay(0.1);
|
|
limitSpeed = true;
|
|
}
|
|
//cardinal
|
|
else if (motion != 0) {
|
|
sprite.SetDelay(0.1);
|
|
limitSpeed = false;
|
|
}
|
|
//not moving
|
|
else {
|
|
sprite.SetDelay(0);
|
|
sprite.SetCurrentFrame(0);
|
|
limitSpeed = false;
|
|
}
|
|
}
|