From c2fe3cabac6545fd798ba2899d525b2f44c1bdcc Mon Sep 17 00:00:00 2001 From: Ravbug Date: Tue, 12 Nov 2024 15:50:42 -0500 Subject: [PATCH] Press the "E" key to make the character wave its right arm Also fix compile error --- RavEngine | 2 +- Samples/Animation/CameraEntity.cpp | 4 ++++ Samples/Animation/CameraEntity.hpp | 1 + Samples/Animation/Character.cpp | 28 ++++++++++++++++++++++++++++ Samples/Animation/Character.hpp | 8 +++++++- Samples/Animation/Level.cpp | 8 ++++++-- Samples/Flags/Flagpole.cpp | 8 ++++---- 7 files changed, 51 insertions(+), 8 deletions(-) diff --git a/RavEngine b/RavEngine index 36d2a61..4ec0999 160000 --- a/RavEngine +++ b/RavEngine @@ -1 +1 @@ -Subproject commit 36d2a615fcebda310fae58009e51ed70b132c4f5 +Subproject commit 4ec099902aae827c1b693389d7e858eda67dee0c diff --git a/Samples/Animation/CameraEntity.cpp b/Samples/Animation/CameraEntity.cpp index fa8eb11..a9e3a15 100644 --- a/Samples/Animation/CameraEntity.cpp +++ b/Samples/Animation/CameraEntity.cpp @@ -76,6 +76,10 @@ void CameraEntity::Pound() cameraScript->target.Pound(); } +void CameraEntity::Wave(){ + cameraScript->target.Wave(); +} + void CameraScriptRunner::operator()(CameraScript& script) { const auto fpsScale = GetApp()->GetCurrentFPSScale(); diff --git a/Samples/Animation/CameraEntity.hpp b/Samples/Animation/CameraEntity.hpp index f90edd9..4043dc1 100644 --- a/Samples/Animation/CameraEntity.hpp +++ b/Samples/Animation/CameraEntity.hpp @@ -21,6 +21,7 @@ struct CameraEntity : public RavEngine::GameObject { void SpeedIncrement(float); void Jump(); void Pound(); + void Wave(); inline auto get_id( )const{ return id; diff --git a/Samples/Animation/Character.cpp b/Samples/Animation/Character.cpp index 4e95a72..7798cce 100644 --- a/Samples/Animation/Character.cpp +++ b/Samples/Animation/Character.cpp @@ -89,6 +89,13 @@ void CharacterScript::Tick(float fpsScale) { rigidBody->setDynamicsWorldPose(vector3(0, 5, 0), GetOwner().GetTransform().GetLocalRotation()); //GetTransform().SetWorldPosition(vector3(0, 5, 0)); } + + if (tweenEnabled){ + waveInfluenceTween.Step(fpsScale); + if (waveInfluenceTween.GetProgress() >= 1){ + tweenEnabled = false; + } + } } void CharacterScript::Move(const vector3& dir, decimalType speedMultiplier) { @@ -165,6 +172,25 @@ void CharacterScript::StartPounding() { rigidBody->SetLinearVelocity(vector3(0,-5,0),false); } +CharacterScript::CharacterScript(RavEngine::Entity owner, decltype(animator) a, decltype(rigidBody) r) : animator(a), rigidBody(r), ComponentWithOwner(owner) { + waveInfluenceTween = {[a](decimalType d) mutable{ + a->GetLayerAtIndex(1)->SetWeight(d); + },0}; + waveInfluenceTween.AddKeyframe(0.1, RavEngine::TweenCurves::LinearCurve, 1); + waveInfluenceTween.AddKeyframe(0.4, RavEngine::TweenCurves::LinearCurve, 1); + waveInfluenceTween.AddKeyframe(0.5, RavEngine::TweenCurves::LinearCurve, 0); +} + +void CharacterScript::Wave(){ + waveInfluenceTween.Seek(0); + tweenEnabled = true; + animator->GetLayerAtIndex(1)->Play(true); +} + + +void Character::Wave(){ + GetComponent().Wave(); +} void Character::Create(Ref mesh, Ref handMesh, Ref handMatInst, Ref material, Ref skeleton) { GameObject::Create(); @@ -250,6 +276,7 @@ void Character::Create(Ref mesh, Ref mesh, RefInsertState(pound_do_state); auto waveLayer = animcomp.AddLayer(); + waveLayer->SetWeight(0); waveLayer->InsertState(waveState); auto mask = RavEngine::New(skeleton, 0); // we only want to enable the arm on this layer diff --git a/Samples/Animation/Character.hpp b/Samples/Animation/Character.hpp index f14b28a..1ee0595 100644 --- a/Samples/Animation/Character.hpp +++ b/Samples/Animation/Character.hpp @@ -4,6 +4,7 @@ #include #include #include +#include namespace RavEngine { struct MeshCollectionSkinned; @@ -14,12 +15,14 @@ struct CharacterScript : public RavEngine::ComponentWithOwner { RavEngine::ComponentHandle animator; RavEngine::ComponentHandle rigidBody; + RavEngine::Tween waveInfluenceTween; + bool tweenEnabled = false; bool controlsEnabled = true; constexpr static decimalType sprintSpeed = 2.5, walkSpeed = 2; int16_t groundCounter = 0; - CharacterScript(RavEngine::Entity owner, decltype(animator) a, decltype(rigidBody) r) : animator(a), rigidBody(r), ComponentWithOwner(owner) {} + CharacterScript(RavEngine::Entity owner, decltype(animator) a, decltype(rigidBody) r); bool OnGround() const; @@ -36,6 +39,8 @@ struct CharacterScript : public RavEngine::ComponentWithOwner { void OnColliderExit(RavEngine::PhysicsBodyComponent& other, const RavEngine::ContactPairPoint* contactPoints, size_t numContactPoints); void StartPounding(); + + void Wave(); }; @@ -51,6 +56,7 @@ struct Character : public RavEngine::GameObject { void Move(const vector3&, decimalType speedMultiplier = 0); void Jump(); void Pound(); + void Wave(); // anything that has these 2 calls // can be a target for input manager diff --git a/Samples/Animation/Level.cpp b/Samples/Animation/Level.cpp index 0516fe1..e30683e 100644 --- a/Samples/Animation/Level.cpp +++ b/Samples/Animation/Level.cpp @@ -27,7 +27,8 @@ struct InputNames{ * Sprint = "Sprint", * Jump = "Jump", * Pound = "Pound", - * ChangeChar = "ChangeChar"; + * ChangeChar = "ChangeChar", + * Wave = "Wave"; }; constexpr static RavEngine::CacheBase::unique_key_t lvl_wall_key = 1; @@ -89,7 +90,8 @@ Level::Level(){ EmplaceSystem(); CreateDependency(); // character script runs after camera script CreateDependency(); // Animator system runs after the character script - CreateDependency(); // physics writer runs afer + CreateDependency(); // physics writer runs afer + CreateDependency(); // CharacterScript modifies animation values #if !NOCONTROLS auto im = GetApp()->inputManager = RavEngine::New(); @@ -102,6 +104,7 @@ Level::Level(){ im->AddActionMap(InputNames::Jump, SDL_SCANCODE_SPACE); im->AddActionMap(InputNames::Pound, SDL_SCANCODE_LCTRL); im->AddActionMap(InputNames::ChangeChar, SDL_SCANCODE_C); + im->AddActionMap(InputNames::Wave, SDL_SCANCODE_E); // AppleTV remote im->AddAxisMap(InputNames::Sprint, SDL_SCANCODE_PAUSE); @@ -122,6 +125,7 @@ Level::Level(){ im->BindAction(InputNames::Jump, camera, &CameraEntity::Jump, ActionState::Pressed, CID::ANY); im->BindAction(InputNames::Pound, camera, &CameraEntity::Pound, ActionState::Pressed, CID::ANY); im->BindAction(InputNames::ChangeChar, GetInput(this),&Level::ChangeChar, ActionState::Pressed, CID::ANY); + im->BindAction(InputNames::Wave, camera, &CameraEntity::Wave, ActionState::Pressed, CID::ANY); #else #endif diff --git a/Samples/Flags/Flagpole.cpp b/Samples/Flags/Flagpole.cpp index 3ab58a5..4f6f9e0 100644 --- a/Samples/Flags/Flagpole.cpp +++ b/Samples/Flags/Flagpole.cpp @@ -59,10 +59,10 @@ void Flagpole::Create(){ AnimatorComponent::State all_anim{ 0, clips }; all_anim.isLooping = true; - auto& layer = animcomp.AddLayer(); - layer.InsertState(all_anim); - layer.Goto(0,true); - layer.Play(); + auto layer = animcomp.AddLayer(); + layer->InsertState(all_anim); + layer->Goto(0,true); + layer->Play(); animcomp.debugEnabled = true; // load shaders