Skip to content

Commit

Permalink
Press the "E" key to make the character wave its right arm
Browse files Browse the repository at this point in the history
Also fix compile error
  • Loading branch information
Ravbug committed Nov 12, 2024
1 parent 153f12a commit c2fe3ca
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 8 deletions.
2 changes: 1 addition & 1 deletion RavEngine
4 changes: 4 additions & 0 deletions Samples/Animation/CameraEntity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions Samples/Animation/CameraEntity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
28 changes: 28 additions & 0 deletions Samples/Animation/Character.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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<CharacterScript>().Wave();
}

void Character::Create(Ref<MeshCollectionSkinned> mesh, Ref<MeshCollectionStatic> handMesh, Ref<PBRMaterialInstance> handMatInst, Ref<PBRMaterialInstance> material, Ref<SkeletonAsset> skeleton) {
GameObject::Create();
Expand Down Expand Up @@ -250,6 +276,7 @@ void Character::Create(Ref<MeshCollectionSkinned> mesh, Ref<MeshCollectionStatic
pound_begin_state.isLooping = false;
pound_end_state.isLooping = false;
pound_do_state.isLooping = false;
waveState.isLooping = false;

// adjust the speed of clips
walk_state.speed = 2.0;
Expand Down Expand Up @@ -332,6 +359,7 @@ void Character::Create(Ref<MeshCollectionSkinned> mesh, Ref<MeshCollectionStatic
layer->InsertState(pound_do_state);

auto waveLayer = animcomp.AddLayer();
waveLayer->SetWeight(0);
waveLayer->InsertState(waveState);

auto mask = RavEngine::New<SkeletonMask>(skeleton, 0); // we only want to enable the arm on this layer
Expand Down
8 changes: 7 additions & 1 deletion Samples/Animation/Character.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <RavEngine/AnimatorComponent.hpp>
#include <RavEngine/PhysicsBodyComponent.hpp>
#include <RavEngine/Ref.hpp>
#include <RavEngine/Tween.hpp>

namespace RavEngine {
struct MeshCollectionSkinned;
Expand All @@ -14,12 +15,14 @@ struct CharacterScript : public RavEngine::ComponentWithOwner {

RavEngine::ComponentHandle<RavEngine::AnimatorComponent> animator;
RavEngine::ComponentHandle<RavEngine::RigidBodyDynamicComponent> rigidBody;
RavEngine::Tween<decimalType> 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;

Expand All @@ -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();

};

Expand All @@ -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
Expand Down
8 changes: 6 additions & 2 deletions Samples/Animation/Level.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -89,7 +90,8 @@ Level::Level(){
EmplaceSystem<CameraScriptRunner>();
CreateDependency<CharacterScriptRunner, CameraScriptRunner>(); // character script runs after camera script
CreateDependency<AnimatorSystem, CharacterScriptRunner>(); // Animator system runs after the character script
CreateDependency<PhysicsLinkSystemWrite, CharacterScriptRunner>(); // physics writer runs afer
CreateDependency<PhysicsLinkSystemWrite, CharacterScriptRunner>(); // physics writer runs afer
CreateDependency<AnimatorSystem,CharacterScriptRunner>(); // CharacterScript modifies animation values

#if !NOCONTROLS
auto im = GetApp()->inputManager = RavEngine::New<InputManager>();
Expand All @@ -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);
Expand All @@ -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

Expand Down
8 changes: 4 additions & 4 deletions Samples/Flags/Flagpole.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c2fe3ca

Please sign in to comment.