mirror of
https://github.com/NoelFB/tiny_link.git
synced 2025-07-01 19:45:27 +08:00
basic player movement
This commit is contained in:
@ -71,8 +71,20 @@ void Mover::stop()
|
||||
m_remainder.y = 0;
|
||||
}
|
||||
|
||||
bool Mover::on_ground(int dist) const
|
||||
{
|
||||
if (!collider)
|
||||
return false;
|
||||
|
||||
return collider->check(Mask::solid, Point(0, dist));
|
||||
}
|
||||
|
||||
void Mover::update()
|
||||
{
|
||||
// apply gravity
|
||||
if (gravity != 0 && (!collider || !collider->check(Mask::solid, Point(0, 1))))
|
||||
speed.y += gravity * Time::delta;
|
||||
|
||||
// get the amount we should move, including remainder from the previous frame
|
||||
Vec2 total = m_remainder + speed * Time::delta;
|
||||
|
||||
|
@ -13,8 +13,9 @@ namespace TL
|
||||
Vec2 m_remainder;
|
||||
|
||||
public:
|
||||
Collider* collider;
|
||||
Collider* collider = nullptr;
|
||||
Vec2 speed;
|
||||
float gravity = 0;
|
||||
|
||||
bool move_x(int amount);
|
||||
bool move_y(int amount);
|
||||
@ -23,6 +24,8 @@ namespace TL
|
||||
void stop_y();
|
||||
void stop();
|
||||
|
||||
bool on_ground(int dist = 1) const;
|
||||
|
||||
void update() override;
|
||||
};
|
||||
}
|
@ -0,0 +1,86 @@
|
||||
#include "player.h"
|
||||
#include "mover.h"
|
||||
|
||||
using namespace TL;
|
||||
|
||||
namespace
|
||||
{
|
||||
constexpr float max_ground_speed = 60;
|
||||
constexpr float max_air_speed = 70;
|
||||
constexpr float ground_accel = 500;
|
||||
constexpr float air_accel = 20;
|
||||
constexpr float friction = 800;
|
||||
constexpr float gravity = 450;
|
||||
constexpr float jump_force = -105;
|
||||
constexpr float jump_time = 0.18f;
|
||||
}
|
||||
|
||||
Player::Player()
|
||||
{
|
||||
input_move = VirtualStick()
|
||||
.add_keys(Key::Left, Key::Right, Key::Up, Key::Down)
|
||||
.add_buttons(0, Button::Left, Button::Right, Button::Up, Button::Down)
|
||||
.add_axes(0, Axis::LeftX, Axis::LeftY, 0.2f);
|
||||
|
||||
input_jump = VirtualButton()
|
||||
.press_buffer(0.15f)
|
||||
.add_key(Key::X)
|
||||
.add_button(0, Button::A);
|
||||
}
|
||||
|
||||
void Player::update()
|
||||
{
|
||||
input_move.update();
|
||||
input_jump.update();
|
||||
|
||||
auto mover = get<Mover>();
|
||||
auto on_ground = mover->on_ground();
|
||||
int input = input_move.value_i().x;
|
||||
|
||||
// Horizontal Movement
|
||||
{
|
||||
|
||||
// Acceleration
|
||||
mover->speed.x += input * (on_ground ? ground_accel : air_accel) * Time::delta;
|
||||
|
||||
// Maxspeed
|
||||
auto maxspd = (on_ground ? max_ground_speed : max_air_speed);
|
||||
if (Calc::abs(mover->speed.x) > maxspd)
|
||||
{
|
||||
mover->speed.x = Calc::approach(
|
||||
mover->speed.x,
|
||||
Calc::sign(mover->speed.x) * maxspd,
|
||||
2000 * Time::delta);
|
||||
}
|
||||
|
||||
if (input == 0 && on_ground)
|
||||
mover->speed.x = Calc::approach(mover->speed.x, 0, friction * Time::delta);
|
||||
}
|
||||
|
||||
// Gravity
|
||||
if (!on_ground)
|
||||
{
|
||||
float grav = gravity;
|
||||
if (Calc::abs(mover->speed.y) < 20 && input_jump.down())
|
||||
grav *= 0.4f;
|
||||
|
||||
mover->speed.y += grav * Time::delta;
|
||||
}
|
||||
|
||||
// Jumping
|
||||
{
|
||||
if (input_jump.pressed() && mover->on_ground())
|
||||
{
|
||||
mover->speed.x = input * max_air_speed;
|
||||
m_jump_timer = jump_time;
|
||||
}
|
||||
|
||||
if (m_jump_timer > 0)
|
||||
{
|
||||
mover->speed.y = -100;
|
||||
m_jump_timer -= Time::delta;
|
||||
if (!input_jump.down())
|
||||
m_jump_timer = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -9,6 +9,13 @@ namespace TL
|
||||
class Player : public Component
|
||||
{
|
||||
public:
|
||||
VirtualStick input_move;
|
||||
VirtualButton input_jump;
|
||||
|
||||
Player();
|
||||
void update() override;
|
||||
|
||||
private:
|
||||
float m_jump_timer = 0;
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user