Skip to content

Instantly share code, notes, and snippets.

@zaphire
Created April 2, 2011 00:19
Show Gist options
  • Save zaphire/899094 to your computer and use it in GitHub Desktop.
Save zaphire/899094 to your computer and use it in GitHub Desktop.
void Player::Update()
{
Entity::Update();
Vector2 lastPosition = position;
const float walkSpeed = 150;
const float runSpeed = 220;
const float maxFallSpeed = 50.0f;
const float gravity = 980.0f;
// fall, cap at maxFallSpeed
velocity.y += MIN(gravity * Monocle::deltaTime, maxFallSpeed);
position += velocity * Monocle::deltaTime;
float useSpeed = isWalking?walkSpeed:runSpeed;
const float turnThresh = 0.25f;
if (moveDir.x > turnThresh)
scale.x = 1;
else if (moveDir.x < -turnThresh)
scale.x = -1;
if (fabs(moveDir.x) < 0.5f)
{
moveDir.x = 0;
}
position += moveDir * useSpeed * Monocle::deltaTime;
int y = 0;
int max = 16;
for (y = 0; y < max; y++)
{
CollisionData collisionData;
if (Collide("Obstruction", &collisionData))
{
position.y = lastPosition.y;
velocity.y = 0;
}
else
{
break;
}
position.y = lastPosition.y - y;
}
if (y == max)
{
position = lastPosition;
}
moveDir = Vector2::zero;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment