Skip to content

Commit

Permalink
Reworked UnitAnimation.cs. Animatinos now work on server&clients.
Browse files Browse the repository at this point in the history
So far only random gestures implemented, but the framework is now there to just start adding a bunch of animations
  • Loading branch information
mariuskilian committed May 7, 2020
1 parent f32e16c commit 7f58d4f
Show file tree
Hide file tree
Showing 25 changed files with 79 additions and 88 deletions.
152 changes: 71 additions & 81 deletions Assets/Scripts/Other/UnitBehaviours/Animation/UnitAnimation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,108 +7,98 @@

public abstract class UnitAnimation : UnitBehaviour {

/*protected static readonly Action // Gestures
ReactiveGesture, NonReactiveGesture;
protected static readonly Action
ComeHere, Distracted, LookThere, NoThanks, Name, Doze, Sleep;
protected static readonly Action
Excited, Shake;
private const int
MaxNumVersions = 5 // Max. number of versions any unit has for a single animation
;
private static readonly float[]
SleepRange = { 3f, 15f },
DozeRange = { 1f, 5f }
;
private readonly Dictionary<string, float[]> TimedGestureLengths = new Dictionary<string, float[]> { { Sleep, SleepRange }, { Doze, DozeRange } };
// Type of gesture
protected const string Reactive = "ReactiveGesture", NonReactive = "NonReactiveGesture";

// Clip names
protected const string ComeHere = "ComeHere", Distracted = "Distracted", LookThere = "LookThere", NoThanks = "NoThanks",
Name = "Name", Doze = "Doze", Sleep = "Sleep", Excited = "Excited", Shake = "Shake";

// Other parameter names
protected const string PickedUp = "PickedUp", Carried = "Carried", CarryPreClipSpeed = "CarryPreClipSpeedNormalizer",
CarryPostClipSpeed = "CarryPostClipSpeedNormalizer";

protected bool TriggerAsReactive { set { if (value) unit.state.ReactiveGesture(); else unit.state.NonReactiveGesture(); } }

// Dictionaries map clip names to the Actions of accessing and changing the correct parameters
protected Dictionary<string, Action> TriggerOnlyGestures, TriggerOnlyAnimations;
protected Dictionary<string, Action<bool>> TimedGestures, BoolOnlyAnimations;
protected Dictionary<string, Action<int>> TriggerGesturesWithVersions;
private void InitDictionaries() {
// Animations that are specific to an action. All units have these animations
TriggerOnlyAnimations = new Dictionary<string, Action> {
{ PickedUp, () => unit.state.PickedUp() } };
BoolOnlyAnimations = new Dictionary<string, Action<bool>> {
{ Carried, b => unit.state.Carried = b} };

// Animations that can be randomly triggered, but not all units have all these animations
TriggerOnlyGestures = new Dictionary<string, Action> {
{ ComeHere, () => unit.state.ComeHere() },
{ Distracted, () => unit.state.Distracted() },
{ LookThere, () => unit.state.LookThere() },
{ NoThanks, () => unit.state.NoThanks() },
{ Name, () => unit.state.Name() } };
TimedGestures = new Dictionary<string, Action<bool>> {
{ Doze, b => unit.state.Doze = b},
{ Sleep, b => unit.state.Sleep = b} };
TriggerGesturesWithVersions = new Dictionary<string, Action<int>> {
{ Excited, v => { unit.state.Excited(); unit.state.ExcitedIndex = v; } },
{ Shake, v => { unit.state.Shake(); unit.state.ShakeIndex = v; } } };
}

private readonly List<string>
TriggerOnlyGestures = new List<Action> { ComeHere, Distracted, LookThere, NoThanks, Name },
BoolOnlyGestures = new List<Action> { Doze, Sleep },
TriggerGesturesWithVersions = new List<Action> { Excited, Shake }
;
protected readonly Dictionary<string, float[]> TimedLengths = new Dictionary<string, float[]> {
{"Doze", new float[]{ 1f, 5f } }, {"Sleep", new float[]{ 3f, 15f } } };

protected Dictionary<string, Action<bool>> AvailableAnimations;
protected Animator animator;
protected Dictionary<string, Action<bool>> AvailableGestures;

protected Animator anim;
private static readonly int MaxNumVersions = 5;

protected new void Awake() {
base.Awake();
anim = unit.state.Animator;
Initialization();
}
protected new void Awake() { base.Awake(); Initialization(); }

private void Initialization() {
// Get list of all overridden clips
var aoc = anim.runtimeAnimatorController as AnimatorOverrideController;
animator = unit.state.Animator;
InitDictionaries();
AvailableGestures = new Dictionary<string, Action<bool>>();

var aoc = animator.runtimeAnimatorController as AnimatorOverrideController;
var overrideClips = new List<KeyValuePair<AnimationClip, AnimationClip>>(aoc.overridesCount);
aoc.GetOverrides(overrideClips);

AvailableAnimations = new Dictionary<string, Action<bool>>();
Dictionary<string, string> overrideClipNames = overrideClips.ToDictionary(
var overrideClipNames = overrideClips.ToDictionary(
pair => pair.Key.name,
pair => { if (pair.Value != null) return pair.Value.name; else return null; }
);

foreach (string clip in TriggerOnlyGestures) {
if (!overrideClipNames.TryGetValue(clip, out var newClip)) continue;
var copy = clip;
if (newClip != null) AvailableAnimations.Add(copy, isReactive => TriggerGesture(copy, isReactive));
foreach (var g in TriggerOnlyGestures) {
if (!overrideClipNames.TryGetValue(g.Key, out var newClip) || newClip == null) continue;
AvailableGestures.Add(g.Key, b => { TriggerAsReactive = b; g.Value?.Invoke(); });
}

foreach (string clip in BoolOnlyGestures) {
if (!overrideClipNames.TryGetValue(clip, out var newClip)) continue;
var copy = clip; var range = TimedGestureLengths[copy];
if (newClip != null) AvailableAnimations.Add(copy, isReactive => StartCoroutine(TimedGesture(copy, range[0], range[1], isReactive)));
foreach (var g in TimedGestures) {
if (!overrideClipNames.TryGetValue(g.Key, out var newClip) || newClip == null) continue;
var l = TimedLengths[g.Key];
AvailableGestures.Add(g.Key, b => { TriggerAsReactive = b; StartCoroutine(TimedGesture(g.Value, l[0], l[1])); });
}
foreach (string clip in TriggerGesturesWithVersions) {
foreach (var g in TriggerGesturesWithVersions) {
int numVersions = 0;
for (int i = 1; i < MaxNumVersions; i++) {
if (!overrideClipNames.TryGetValue(clip + "V" + i, out var newClip)) continue;
if (newClip != null) numVersions++;
}
if (numVersions > 0) {
var copy = clip;
AvailableAnimations.Add(copy, isReactive => {
int version = RNG.Next(numVersions);
unit.state.Animator.SetInteger(copy + Index, version + 1);
TriggerGesture(copy, isReactive);
});
}
}
}
private void TriggerGesture(string gestureName, bool isReactive) {
if (isReactive) { ResetAllTriggers(); unit.state.ReactiveGesture(); } else unit.state.NonReactiveGesture();
for (int v = 1; v <= MaxNumVersions; v++)
if (overrideClipNames.TryGetValue(g.Key + "V" + v, out var newClip) && newClip != null) numVersions++;

anim.SetTrigger(gestureName);
if (numVersions == 0) continue;
AvailableGestures.Add(g.Key, b => { TriggerAsReactive = b; g.Value?.Invoke(RNG.Next(numVersions) + 1); });
}
}

private IEnumerator TimedGesture(string gestureName, float minLength, float maxLength, bool isReactive) {
string gestureType = (isReactive) ? ReactiveGesture : NonReactiveGesture;
anim.SetTrigger(gestureName);
anim.SetBool(gestureName, true);
yield return new WaitForSeconds(((float)RNG.NextDouble() * (maxLength - minLength)) + minLength);
anim.SetBool(gestureName, false);
private IEnumerator TimedGesture(Action<bool> action, float minLength, float maxLength) {
action?.Invoke(true);
yield return new WaitForSeconds((float)RNG.NextDouble() * (maxLength - minLength) + minLength);
action?.Invoke(false);
}

private void ResetAllTriggers() {
foreach (var par in unit.state.Animator.parameters)
if (par.type == AnimatorControllerParameterType.Trigger) anim.ResetTrigger(par.name);
protected void TryPerformGesture(string name, bool isReactive) {
AvailableGestures.TryGetValue(name, out Action<bool> action);
action?.Invoke(isReactive);
}
protected void TryPerformAnimation(string name, bool isReactive) { AvailableAnimations.TryGetValue(name, out var func); func?.Invoke(isReactive); }
}
public class AnimationClass {
public string Name { get; private set; }
public Action*/
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@

public class UnitRandomAnimation : UnitAnimation {

/*private bool isDoingRandomGesture = false;
private bool isDoingRandomGesture = false;

private void Update() { if (!isDoingRandomGesture) StartCoroutine(RandomGesture()); }

private IEnumerator RandomGesture() {
isDoingRandomGesture = true;
float random = (float)RNG.NextDouble();
yield return new WaitForSeconds((random * random * 20f) + 5f);
if (!anim.GetBool(NonReactiveGesture)) {
int index = RNG.Next(AvailableAnimations.Count);
var key = new List<string>(AvailableAnimations.Keys)[index];
TryPerformAnimation(key, false);
if (!animator.GetBool(NonReactive)) {
int index = RNG.Next(AvailableGestures.Count);
var key = new List<string>(AvailableGestures.Keys)[index];
TryPerformGesture(key, false);
}
isDoingRandomGesture = false;
}*/
}
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
player-connection-mode=Listen
player-connection-guid=2892303552
player-connection-guid=1386670960
player-connection-debug=0
player-connection-project-name=Poke-Arena URP
player-connection-ip=192.168.2.122
Expand Down
Binary file not shown.
Binary file modified Bolt_DebugStart_Build/Bolt_DebugStart_Build_Data/level0
Binary file not shown.
Binary file modified Bolt_DebugStart_Build/Bolt_DebugStart_Build_Data/level1
Binary file not shown.
1 change: 1 addition & 0 deletions ProjectSettings/ProjectSettings.asset
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ PlayerSettings:
- {fileID: 0}
- {fileID: 0}
- {fileID: 0}
- {fileID: 0}
metroInputSource: 0
wsaTransparentSwapchain: 0
m_HolographicPauseOnTrackingLoss: 1
Expand Down

0 comments on commit 7f58d4f

Please sign in to comment.