room transition + attack anim

This commit is contained in:
Noel Berry
2021-01-03 14:08:22 -08:00
parent a8b78214f9
commit fb9cebd8ff
15 changed files with 249 additions and 54 deletions

View File

@ -9,7 +9,12 @@ Animator::Animator(const String& sprite)
m_animation_index = 0;
}
void Animator::play(const String& animation)
const Sprite* Animator::sprite() const
{
return m_sprite;
}
void Animator::play(const String& animation, bool restart)
{
BLAH_ASSERT(m_sprite, "No Sprite Assigned!");
@ -17,8 +22,13 @@ void Animator::play(const String& animation)
{
if (m_sprite->animations[i].name == animation)
{
m_animation_index = i;
m_frame_index = 0;
if (m_animation_index != i || restart)
{
m_animation_index = i;
m_frame_index = 0;
m_frame_counter = 0;
}
break;
}
}

View File

@ -21,8 +21,8 @@ namespace TL
Animator() = default;
Animator(const String& sprite);
void play(const String& animation);
const Sprite* sprite() const;
void play(const String& animation, bool restart = false);
void update() override;
void render(Batch& batch) override;

View File

@ -27,12 +27,18 @@ Player::Player()
.press_buffer(0.15f)
.add_key(Key::X)
.add_button(0, Button::A);
input_attack = VirtualButton()
.press_buffer(0.15f)
.add_key(Key::C)
.add_button(0, Button::X);
}
void Player::update()
{
input_move.update();
input_jump.update();
input_attack.update();
auto mover = get<Mover>();
auto anim = get<Animator>();
@ -44,37 +50,99 @@ void Player::update()
{
// land squish
if (!was_on_ground && m_on_ground)
anim->scale = Vec2(facing * 1.5f, 0.7f);
anim->scale = Vec2(m_facing * 1.5f, 0.7f);
// lerp scale back to one
anim->scale = Calc::approach(anim->scale, Vec2(facing, 1.0f), Time::delta * 4);
anim->scale = Calc::approach(anim->scale, Vec2(m_facing, 1.0f), Time::delta * 4);
// set m_facing
anim->scale.x = Calc::abs(anim->scale.x) * m_facing;
// set facing
anim->scale.x = Calc::abs(anim->scale.x) * facing;
}
// Horizontal Movement
// NORMAL STATE
if (m_state == st_normal)
{
// Acceleration
mover->speed.x += input * (m_on_ground ? ground_accel : air_accel) * Time::delta;
// Maxspeed
auto maxspd = (m_on_ground ? max_ground_speed : max_air_speed);
if (Calc::abs(mover->speed.x) > maxspd)
// Current Animation
if (m_on_ground)
{
mover->speed.x = Calc::approach(
mover->speed.x,
Calc::sign(mover->speed.x) * maxspd,
2000 * Time::delta);
if (input != 0)
anim->play("run");
else
anim->play("idle");
}
else
{
anim->play("jump");
}
// Friction
if (input == 0 && m_on_ground)
mover->speed.x = Calc::approach(mover->speed.x, 0, friction * Time::delta);
// Horizontal Movement
{
// Acceleration
mover->speed.x += input * (m_on_ground ? ground_accel : air_accel) * Time::delta;
// Facing Direction
if (input != 0 && m_on_ground)
facing = input;
// Maxspeed
auto maxspd = (m_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);
}
// Friction
if (input == 0 && m_on_ground)
mover->speed.x = Calc::approach(mover->speed.x, 0, friction * Time::delta);
// Facing Direction
if (input != 0 && m_on_ground)
m_facing = input;
}
// Invoke Jumping
{
if (input_jump.pressed() && mover->on_ground())
{
input_jump.clear_press_buffer();
anim->scale = Vec2(m_facing * 0.65f, 1.4f);
mover->speed.x = input * max_air_speed;
m_jump_timer = jump_time;
}
}
// Begin Attack
if (input_attack.pressed())
{
input_attack.clear_press_buffer();
m_state = st_attack;
m_attack_timer = anim->sprite()->get_animation("attack")->duration();
if (m_on_ground)
mover->speed.x = 0;
}
}
// ATTACK STATE
else if (m_state == st_attack)
{
anim->play("attack");
m_attack_timer -= Time::delta;
if (m_attack_timer <= 0)
{
anim->play("idle");
m_state = st_normal;
}
}
// Variable Jumping
if (m_jump_timer > 0)
{
mover->speed.y = -100;
m_jump_timer -= Time::delta;
if (!input_jump.down())
m_jump_timer = 0;
}
// Gravity
@ -86,22 +154,4 @@ void Player::update()
mover->speed.y += grav * Time::delta;
}
// Jumping
{
if (input_jump.pressed() && mover->on_ground())
{
anim->scale = Vec2(facing * 0.65f, 1.4f);
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;
}
}
}

View File

@ -9,15 +9,21 @@ namespace TL
class Player : public Component
{
public:
static constexpr int st_normal = 0;
static constexpr int st_attack = 1;
VirtualStick input_move;
VirtualButton input_jump;
VirtualButton input_attack;
Player();
void update() override;
private:
int facing = 1;
int m_state = st_normal;
int m_facing = 1;
float m_jump_timer = 0;
float m_attack_timer = 0;
bool m_on_ground;
};
}

View File

@ -52,10 +52,12 @@ void Tilemap::set_cells(int x, int y, int w, int h, const Subtexture* tex)
void Tilemap::render(Batch& batch)
{
batch.push_matrix(Mat3x2::create_translation(entity()->position));
for (int x = 0; x < m_columns; x ++)
for (int y = 0; y < m_rows; y ++)
if (m_grid[x + y * m_columns].texture)
{
batch.tex(m_grid[x + y * m_columns], Vec2(x * m_tile_width, y * m_tile_height));
}
batch.pop_matrix();
}