Skip to content

Commit

Permalink
Implement animation logic for AnimatedObjects
Browse files Browse the repository at this point in the history
Implement animation logic for AnimatedObjects
  • Loading branch information
0x7c13 committed Apr 2, 2022
1 parent 2b0568f commit 6c0cfd5
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
6 changes: 5 additions & 1 deletion Assets/Scripts/Pal3/Actor/ActorActionController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ namespace Pal3.Actor
using Command.SceCommands;
using Core.DataLoader;
using Core.DataReader.Mv3;
using Core.Extensions;
using Core.Services;
using Data;
using MetaData;
Expand Down Expand Up @@ -344,6 +343,11 @@ public void Execute(ActorStopActionCommand command)
animationRenderer.StopAnimation();
}
_animationLoopPointWaiter?.CancelWait();

if (_autoStand)
{
PerformAction(_actor.GetIdleAction());
}
}
}

Expand Down
5 changes: 5 additions & 0 deletions Assets/Scripts/Pal3/Renderer/CvdModelRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ public void Init(CvdFile cvdFile, ITextureResourceProvider textureProvider, Colo
root.transform.SetParent(gameObject.transform, false);
}

public float GetAnimationDuration()
{
return _animationDuration;
}

private void BuildTextureCache(CvdGeometryNode node,
ITextureResourceProvider textureProvider,
Dictionary<string, Texture2D> textureCache)
Expand Down
49 changes: 49 additions & 0 deletions Assets/Scripts/Pal3/Scene/SceneObjects/StaticOrAnimatedObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@

namespace Pal3.Scene.SceneObjects
{
using System.Collections;
using Core.DataReader.Scn;
using Data;
using Effect;
using MetaData;
using Renderer;
using UnityEngine;

[ScnSceneObject(ScnSceneObjectType.StaticOrAnimated)]
Expand Down Expand Up @@ -53,6 +55,53 @@ public void Init(GameResourceProvider resourceProvider, StaticOrAnimatedObject s
(_effectComponent as IEffect)!.Init(resourceProvider, sceneObject.Info.EffectModelType);
}

private float _startYPosition;
private void Start()
{
_startYPosition = transform.localPosition.y;

// Randomly play animation if Parameters[1] == 0 for Cvd modeled objects.
if (_sceneObject.Info.Parameters[1] == 0)
{
if (gameObject.GetComponent<CvdModelRenderer>() is {} cvdModelRenderer)
{
StartCoroutine(PlayAnimationRandomly(cvdModelRenderer));
}
}
}

// Play animation with random wait time.
private IEnumerator PlayAnimationRandomly(CvdModelRenderer cvdModelRenderer)
{
var animationWaiter = new WaitForSeconds(cvdModelRenderer.GetAnimationDuration());
while (isActiveAndEnabled)
{
yield return new WaitForSeconds(UnityEngine.Random.Range(1.5f, 5f));
cvdModelRenderer.PlayAnimation(1f, 1);
yield return animationWaiter;
}
}

void LateUpdate()
{
var currentTransform = transform;

// Parameters[2] describes animated object's default animation.
// 0 means no animation. 1 means the object is animated up and down (sine curve).
// 2 means the object is animated with constant rotation.
if (_sceneObject.Info.Parameters[2] == 1)
{
var currentPosition = currentTransform.localPosition;
transform.localPosition = new Vector3(currentPosition.x,
_startYPosition + Mathf.Sin(Time.time) / 2f,
currentPosition.z);
}
else if (_sceneObject.Info.Parameters[2] == 2)
{
transform.RotateAround(currentTransform.position, currentTransform.up, Time.deltaTime * 80f);
}
}

private void OnDisable()
{
Destroy(_effectComponent);
Expand Down

0 comments on commit 6c0cfd5

Please sign in to comment.