Skip to content

Commit

Permalink
feat(flappy): animations
Browse files Browse the repository at this point in the history
  • Loading branch information
naezith committed Nov 30, 2019
1 parent 96697f5 commit 43044de
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions tutorials/flappy-bird/step_9/flappy-bird.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ struct flappy_bird_constants {
const float gravity{2000.f};
const float jump_force{650.f};
const float rotate_speed{100.f};
const float fall_angle{20.f};
const float max_angle{60.f};
const float jump_rotation{-40.f};

// Pipes
const float gap_height{265.f};
Expand Down Expand Up @@ -454,9 +456,11 @@ class player_logic final : public ecs::logic_update_system<player_logic> {
if (jump_key_tapped) {
movement_speed_.set_y(-constants.jump_force);

auto &animation = entity_registry_.get<animation2d::anim_component>(player_);

animation.animation_id = "dragon_fall";
entity_registry_.replace<animation2d::anim_component>(player_,
animation2d::anim_component{.animation_id = "dragon_jump",
.current_status = animation2d::anim_component::status::playing,
.speed = animation2d::anim_component::seconds(0.13f),
.loop = true});
}

// Add movement speed to position to make the character move, but apply over time with delta time
Expand All @@ -481,10 +485,22 @@ class player_logic final : public ecs::logic_update_system<player_logic> {
// If jump button is tapped, reset rotation,
// If rotation is higher than the max angle, set it to max angle
if (jump_key_tapped)
props.rotation = 0.f;
props.rotation = constants.jump_rotation;
else if (props.rotation > constants.max_angle)
props.rotation = constants.max_angle;

// Change to falling animation when angle is down enough
if(props.rotation > constants.fall_angle) {
auto &animation = entity_registry_.get<animation2d::anim_component>(player_);
if(animation.animation_id != "dragon_fall") {
entity_registry_.replace<animation2d::anim_component>(player_,
animation2d::anim_component{.animation_id = "dragon_fall",
.current_status = animation2d::anim_component::status::playing,
.speed = animation2d::anim_component::seconds(0.13f),
.loop = true});
}
}

// Set the properties
registry.replace<transform::properties>(player_, props);
}
Expand Down Expand Up @@ -526,6 +542,10 @@ class collision_logic final : public ecs::logic_update_system<collision_logic> {
player_died_ = true;
auto &animation = entity_registry_.get<animation2d::anim_component>(player_);
animation.current_status = animation2d::anim_component::stopped;

entity_registry_.replace<animation2d::anim_component>(player_,
animation2d::anim_component{.animation_id = "dragon_hurt",
.current_status = animation2d::anim_component::status::stopped});
}
}
}
Expand Down

0 comments on commit 43044de

Please sign in to comment.