Line data Source code
1 : #include "core/animations/states/LegsRollBackAnimationState.hpp"
2 :
3 : #include "core/animations/states/LegsCrouchAnimationState.hpp"
4 : #include "core/animations/states/LegsGetUpAnimationState.hpp"
5 : #include "core/animations/states/LegsProneAnimationState.hpp"
6 : #include "core/animations/states/LegsJumpAnimationState.hpp"
7 : #include "core/animations/states/LegsStandAnimationState.hpp"
8 : #include "core/animations/states/LegsFallAnimationState.hpp"
9 :
10 : #include "core/animations/states/CommonAnimationStateTransitions.hpp"
11 :
12 : #include "core/entities/Soldier.hpp"
13 : #include "core/physics/Constants.hpp"
14 :
15 : namespace Soldank
16 : {
17 0 : LegsRollBackAnimationState::LegsRollBackAnimationState(
18 0 : const AnimationDataManager& animation_data_manager)
19 0 : : AnimationState(animation_data_manager.Get(AnimationType::RollBack))
20 0 : , animation_data_manager_(animation_data_manager)
21 : {
22 0 : }
23 :
24 0 : void LegsRollBackAnimationState::Enter(Soldier& soldier)
25 : {
26 0 : if (soldier.on_ground) {
27 0 : glm::vec2 particle_force = soldier.particle.GetForce();
28 0 : soldier.particle.SetForce(
29 0 : { -(float)soldier.direction * 2.0F * PhysicsConstants::CROUCHRUNSPEED,
30 : particle_force.y });
31 : }
32 0 : }
33 :
34 0 : std::optional<std::shared_ptr<AnimationState>> LegsRollBackAnimationState::HandleInput(
35 : Soldier& soldier)
36 : {
37 0 : if (soldier.control.prone) {
38 0 : return std::make_shared<LegsProneAnimationState>(animation_data_manager_);
39 : }
40 :
41 0 : if (GetFrame() == GetFramesCount()) {
42 : auto maybe_crouch_run_animation_state =
43 : CommonAnimationStateTransitions::TryTransitionToCrouchRunning(soldier,
44 0 : animation_data_manager_);
45 0 : if (maybe_crouch_run_animation_state.has_value()) {
46 0 : return *maybe_crouch_run_animation_state;
47 : }
48 :
49 0 : if (soldier.control.down) {
50 0 : return std::make_shared<LegsCrouchAnimationState>(animation_data_manager_);
51 : }
52 :
53 : auto maybe_running_animation_state =
54 0 : CommonAnimationStateTransitions::TryTransitionToRunning(soldier, animation_data_manager_);
55 0 : if (maybe_running_animation_state.has_value()) {
56 0 : return *maybe_running_animation_state;
57 : }
58 :
59 0 : if (soldier.on_ground) {
60 0 : if (soldier.control.up) {
61 0 : return std::make_shared<LegsJumpAnimationState>(animation_data_manager_);
62 : }
63 :
64 0 : if (soldier.control.down) {
65 0 : return std::make_shared<LegsCrouchAnimationState>(animation_data_manager_);
66 : }
67 :
68 0 : return std::make_shared<LegsStandAnimationState>(animation_data_manager_);
69 : }
70 :
71 0 : return std::make_shared<LegsFallAnimationState>(animation_data_manager_);
72 0 : }
73 :
74 0 : return std::nullopt;
75 : }
76 :
77 0 : void LegsRollBackAnimationState::Update(Soldier& soldier, const PhysicsEvents& physics_events)
78 : {
79 0 : soldier.stance = PhysicsConstants::STANCE_STAND;
80 :
81 0 : if (GetSpeed() > 1) {
82 0 : soldier.particle.velocity_.x /= (float)GetSpeed();
83 0 : soldier.particle.velocity_.y /= (float)GetSpeed();
84 : }
85 :
86 0 : if (soldier.on_ground) {
87 0 : glm::vec2 particle_force = soldier.particle.GetForce();
88 0 : soldier.particle.SetForce(
89 0 : { -(float)soldier.direction * PhysicsConstants::ROLLSPEED, particle_force.y });
90 : } else {
91 0 : glm::vec2 particle_force = soldier.particle.GetForce();
92 0 : soldier.particle.SetForce(
93 0 : { -(float)soldier.direction * 2.0F * PhysicsConstants::FLYSPEED, particle_force.y });
94 : }
95 :
96 0 : if ((GetFrame() > 1) && (GetFrame() < 8)) {
97 : // so apparently in soldat there's no difference between a RollBack and a backflip
98 : // if you press W during a Rollback in those frames then it pushes you up and therefore
99 : // it turns into a backflip. That's how also cannonballs works, if we release W fast enough,
100 : // it will turn into a RollBack
101 0 : if (soldier.control.up) {
102 0 : glm::vec2 particle_force = soldier.particle.GetForce();
103 0 : particle_force.y -= PhysicsConstants::JUMPDIRSPEED * 1.5F;
104 0 : particle_force.x *= 0.5;
105 0 : soldier.particle.SetForce(particle_force);
106 0 : soldier.particle.velocity_.x *= 0.8;
107 : }
108 : }
109 0 : }
110 : } // namespace Soldank
|