Skip to content

Commit

Permalink
fix: samples and unwanted logs
Browse files Browse the repository at this point in the history
Included composite animations samples and removed a Debug.Log message
that was logging when the animation
ended.
  • Loading branch information
indiegabo committed Nov 6, 2023
1 parent 2ece43a commit 579abe5
Show file tree
Hide file tree
Showing 13 changed files with 555 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,58 @@

using System;
using System.Collections.Generic;
using Codice.Client.BaseCommands.Download;
using UnityEngine;

namespace SpriteAnimations
{
public class SpriteAnimationComposite : SpriteAnimation
{
#region Static

/// <summary>
/// Creates a SpriteAnimationComposite animation on demand.
/// </summary>
/// <param name="fps">The frames per second of the animation.</param>
/// <param name="antecipationCycle">The list of sprites for the antecipation cycle.</param>
/// <param name="coreCycle">The list of sprites for the core cycle.</param>
/// <param name="recoveryCycle">The list of sprites for the recovery cycle.</param>
/// <returns>The created SpriteAnimationComposite object.</returns>
public static SpriteAnimationComposite OnDemand(int fps, List<Sprite> antecipationCycle, List<Sprite> coreCycle, List<Sprite> recoveryCycle)
{
// Create a new instance of SpriteAnimationComposite
var animation = CreateInstance<SpriteAnimationComposite>();

// Set the frames per second of the animation
animation.FPS = fps;

// Generate the cycles for the animation
animation.GenerateCycles();

// Add frames to the antecipation cycle
foreach (Sprite sprite in antecipationCycle)
{
animation.AntecipationCycle.AddFrame(sprite);
}

// Add frames to the core cycle
foreach (Sprite sprite in coreCycle)
{
animation.CoreCycle.AddFrame(sprite);
}

// Add frames to the recovery cycle
foreach (Sprite sprite in recoveryCycle)
{
animation.RecoveryCycle.AddFrame(sprite);
}

// Return the created animation
return animation;
}

#endregion

#region Inspector

[SerializeField]
Expand Down Expand Up @@ -47,6 +93,25 @@ public void GenerateCycles()
_recoveryCycle = new Cycle(this);
}

public void SetFrameID(CompositeCycle compositeCycle, int index, string id)
{
Cycle cycle = compositeCycle switch
{
CompositeCycle.Antecipation => _antecipationCycle,
CompositeCycle.Core => _coreCycle,
CompositeCycle.Recovery => _recoveryCycle,
_ => _antecipationCycle,
};

if (!cycle.TryGetFrame(index, out Frame frame))
{
Logger.LogError($"Trying to override the ID of frame {index} but index is out of range.", this);
return;
}

frame.Id = id;
}

#endregion

#region Calculations
Expand All @@ -57,5 +122,72 @@ public override int CalculateFramesCount()
}

#endregion

#region Templating

/// <summary>
/// Creates a new SpriteAnimationComposite instance using this animation as template.
/// </summary>
/// <param name="antecipationCycle">The antecipation cycle template.</param>
/// <param name="coreCycle">The core cycle template.</param>
/// <param name="recoveryCycle">The recovery cycle template.</param>
/// <returns>A new SpriteAnimationComposite instance.</returns>
public SpriteAnimationComposite UseAsTemplate(List<Sprite> antecipationCycle, List<Sprite> coreCycle, List<Sprite> recoveryCycle)
{
// Create a clone of this instance
SpriteAnimationComposite clone = Instantiate(this);

// Evaluate and adjust the size of the antecipation cycle
EvaluateCycleSize(CompositeCycle.Antecipation, antecipationCycle.Count, _antecipationCycle.Size);
for (int i = 0; i < clone.AntecipationCycle.Size; i++)
{
if (i > antecipationCycle.Count - 1) continue;
if (!clone.AntecipationCycle.TryGetFrame(i, out Frame frame)) continue;
frame.Sprite = antecipationCycle[i];
}

// Evaluate and adjust the size of the core cycle
EvaluateCycleSize(CompositeCycle.Core, coreCycle.Count, _coreCycle.Size);
for (int i = 0; i < clone.CoreCycle.Size; i++)
{
if (i > coreCycle.Count - 1) continue;
if (!clone.CoreCycle.TryGetFrame(i, out Frame frame)) continue;
frame.Sprite = coreCycle[i];
}

// Evaluate and adjust the size of the recovery cycle
EvaluateCycleSize(CompositeCycle.Recovery, recoveryCycle.Count, _recoveryCycle.Size);
for (int i = 0; i < clone.RecoveryCycle.Size; i++)
{
if (i > recoveryCycle.Count - 1) continue;
if (!clone.RecoveryCycle.TryGetFrame(i, out Frame frame)) continue;
frame.Sprite = recoveryCycle[i];
}
/// <summary>
/// Evaluates the size of the provided cycle and logs a warning if it doesn't match the original cycle size.
/// </summary>
/// <param name="compositeCycle">The composite cycle being evaluated.</param>
/// <param name="newCycleSize">The new cycle size.</param>
/// <param name="originalCycleSize">The original cycle size.</param>
void EvaluateCycleSize(CompositeCycle compositeCycle, int newCycleSize, int originalCycleSize)
{
if (newCycleSize != originalCycleSize)
{
Logger.LogWarning($"The number of frames for the Composite Cycle {compositeCycle} ({newCycleSize}) "
+ $"does not match the number of frames in the template's cycle ({originalCycleSize}). "
+ $"The animation might not work as expected.", this);
}
}
return clone;
}

#endregion
}

public enum CompositeCycle
{
Antecipation,
Core,
Recovery,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,6 @@ protected void EndCycle()
EndAnimation();

// Invoke the action when the overall cycle ends
Debug.Log("End Cycle");
_onEndAction?.Invoke();
}

Expand Down Expand Up @@ -287,11 +286,4 @@ public virtual CompositeAnimator SetOnRecoveryEnd(UnityAction onRecoveryEnd)

#endregion
}

public enum CompositeCycle
{
Antecipation,
Core,
Recovery,
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace SpriteAnimations.Sampling
{
public class CompositeAnimationOnDemand : MonoBehaviour
{
public SpriteAnimator _animator;
public int _fps;

public List<Sprite> _antecipationCycle;
public List<Sprite> _coreCycle;
public List<Sprite> _recoveryCycle;

private SpriteAnimationComposite _animation;

private void Awake()
{
_animation = SpriteAnimationComposite.OnDemand(_fps, _antecipationCycle, _coreCycle, _recoveryCycle);
}

private void Start()
{
_animator.Play<CompositeAnimator>(_animation).SetOnEnd(() =>
{
_animator.Play<CompositeAnimator>(_animation).FromStart();
});
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace SpriteAnimations.Sampling
{
public class CompositeAnimationTemplate : MonoBehaviour
{
public SpriteAnimator _animator;
public SpriteAnimationComposite _template;

public List<Sprite> _antecipationCycle;
public List<Sprite> _coreCycle;
public List<Sprite> _recoveryCycle;

private SpriteAnimationComposite _animation;

private void Awake()
{
_animation = _template.UseAsTemplate(_antecipationCycle, _coreCycle, _recoveryCycle);
}

private void Start()
{
_animator.Play<CompositeAnimator>(_animation).SetOnEnd(() =>
{
_animator.Play<CompositeAnimator>(_animation).FromStart();
});
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 8bed12526f9ecb043ba63c51bdace746, type: 3}
m_Name: Slide_Template
m_EditorClassIdentifier:
_animationName: Slide_Template
_fps: 8
_antecipationCycle:
_animation: {fileID: 11400000}
_frames:
- _sprite: {fileID: 21300000, guid: d107f48ed94e4f44a87ace124b74a53d, type: 3}
_id:
_identifiable: 1
_coreCycle:
_animation: {fileID: 11400000}
_frames:
- _sprite: {fileID: 21300000, guid: d107f48ed94e4f44a87ace124b74a53d, type: 3}
_id:
- _sprite: {fileID: 21300000, guid: d107f48ed94e4f44a87ace124b74a53d, type: 3}
_id:
- _sprite: {fileID: 21300000, guid: d107f48ed94e4f44a87ace124b74a53d, type: 3}
_id:
_identifiable: 1
_recoveryCycle:
_animation: {fileID: 11400000}
_frames:
- _sprite: {fileID: 21300000, guid: d107f48ed94e4f44a87ace124b74a53d, type: 3}
_id:
- _sprite: {fileID: 21300000, guid: d107f48ed94e4f44a87ace124b74a53d, type: 3}
_id:
- _sprite: {fileID: 21300000, guid: d107f48ed94e4f44a87ace124b74a53d, type: 3}
_id:
_identifiable: 1
_loopableCore: 1

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 579abe5

Please sign in to comment.