Repository navigation
feat: mobs play their own animations, sounds and particles while they are used - the smithy rings, sparks and steams #578
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
5e903d6
refactor: animation sound/particle events and bone sampling are reusa…
BoroBongo f855023
refactor: an NPC's mob usage ends in one place (NpcService.ReleaseMob…
BoroBongo ddfbb62
feat: mob state animations are looked up and baked while the world lo…
BoroBongo 313465c
feat: a mob can play its own state animation with its sounds and part…
BoroBongo ce77fa6
feat: mobs play their own state animations, sounds and particles whil…
BoroBongo 36ec125
docs: mobs - who writes the mob state, who uses a mob, saving, the mo…
BoroBongo a1d6059
chore: meta files of the new mob animation scripts (generated by Unit…
BoroBongo 7744022
fix: a mob's sound stops with it when the last user leaves - the forg…
BoroBongo e7054cc
fix: a smith killed or stood up at the anvil leaves it free - the las…
BoroBongo 34923ac
docs: why the hero's mob use starts only after the mob dialog is prep…
BoroBongo c8eaf5c
fix: a skipped mob animation is logged, and the hero's mob is forgott…
BoroBongo 8a17233
fix: animation sounds play at Gothic's own volume of each sound - the…
BoroBongo 4746ca7
fix: the options' sound effects slider works - world sounds, NPC and …
BoroBongo 46b9e74
fix: the sound effects volume slider shows in the sound options again…
BoroBongo efd3a8f
fix: the sound effects slider plays Gothic's own volume at 75% - the …
BoroBongo e4516e4
Merge pull request #581 from Gothic-Unity-Project/fix/sfx-volume
JaXt0r b1a444a
fix: a mob works exactly as long as an NPC or the hero uses it - who …
JaXt0r dded03a
fix: a smith knocked out at the anvil lets go of it like a killed one…
JaXt0r 4bc4df4
fix: a mob whose model has no root node logs why it won't move instea…
JaXt0r 8041949
fix: the sound effects slider works through the SoundEffects mixer gr…
JaXt0r 1b3391b
fix: the sound effects slider defaults to 1 like Gothic - its top end…
JaXt0r 9c736ad
fix: an NPC's animation sound effects play on an animation sound sour…
JaXt0r 470c844
fix: ground sound events are recognised by their prefix independent o…
JaXt0r 2044ff9
fix: an animation sound that doesn't exist no longer cuts the sound s…
JaXt0r 239568f
fix: which sound an animation event plays (footstep by ground, Gothic…
JaXt0r 3518fe9
fix: the hammering range override is a tuning value in Constants.Audi…
JaXt0r f6af5d0
fix: one place decides "visual is a model script" (ZenKitExtension.Ha…
JaXt0r 80e7ada
fix: mob animations are baked during world loading behind the loading…
JaXt0r 6cfd2e6
docs: ADR for the sound effects slider (Gothic's own volume, default …
JaXt0r 745812c
fix: animation sounds carry exactly their Gothic range like in OpenGo…
JaXt0r File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
298 changes: 298 additions & 0 deletions
298
Assets/Gothic-Core/Scripts/Adapters/Animations/AnimationEffectPlayer.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,298 @@ | ||
| using System.Collections.Generic; | ||
| using Gothic.Core.Extensions; | ||
| using Gothic.Core.Logging; | ||
| using Gothic.Core.Manager; | ||
| using Gothic.Core.Models.Animations; | ||
| using Gothic.Core.Services.Meshes; | ||
| using Reflex.Attributes; | ||
| using UnityEngine; | ||
| using ZenKit; | ||
| using Logger = Gothic.Core.Logging.Logger; | ||
| using Object = UnityEngine.Object; | ||
|
|
||
| namespace Gothic.Core.Adapters.Animations | ||
| { | ||
| /// <summary> | ||
| /// Plays the sound (*eventSFX) and particle (*eventPFX, *eventPFXStop) events of an animation track instance | ||
| /// whose frames were crossed this frame (AnimationTrackInstance.GetPending*). Owned by the object that plays the | ||
| /// animation: AnimationSystem (NPCs) and MobAnimator (used mobs). | ||
| /// | ||
| /// Two ways to handle particles: | ||
| /// - one-off (NPCs): every event builds its effect, which destroys itself after playing (_pfxMaxSeconds at most). | ||
| /// - reusing (mobs): an effect is built on its first event and replayed on the next ones - an anvil hit twice per | ||
| /// loop rebuilt its sparks (meshes, materials) every time otherwise. DestroyAllPfx() removes them. | ||
| /// </summary> | ||
| public class AnimationEffectPlayer | ||
| { | ||
| [Inject] private readonly AudioService _audioService; | ||
| [Inject] private readonly MeshService _meshService; | ||
|
|
||
| // *eventPFX effects that run until their *eventPFXStop (same index). Safety: gone after this time anyway. | ||
| private const float _pfxMaxSeconds = 12f; | ||
| private const float _pfxFadeSeconds = 8f; | ||
|
|
||
| /// <summary> | ||
| /// An effect of the reusing mode, built once: the event it belongs to (name + bone) and its particle systems. | ||
| /// </summary> | ||
| private struct ReusedPfx | ||
| { | ||
| public string Name; | ||
| public string Bone; | ||
| public int Index; | ||
| public GameObject Root; | ||
| public ParticleSystem[] Systems; | ||
| } | ||
|
|
||
| private readonly Transform _boneRoot; | ||
| private readonly Transform _stepOrigin; | ||
| private readonly AudioSource _stepSound; | ||
| private readonly AudioSource _clipSound; | ||
| // The volume the clip source had: Gothic's volume of each sound is applied on top of it. | ||
| private readonly float _soundBaseVolume; | ||
| private readonly bool _isReusingPfx; | ||
|
|
||
| // One-off mode: effects with an index, until their *eventPFXStop. | ||
| private readonly Dictionary<int, GameObject> _activePfx = new(); | ||
| private readonly List<int> _activePfxIndices = new(); | ||
|
|
||
| // Reusing mode: every effect built so far. | ||
| private readonly List<ReusedPfx> _reusedPfx = new(); | ||
|
|
||
|
|
||
| /// <param name="boneRoot">The bones of *eventPFX are searched below it; effects without a bone start at it. | ||
| /// </param> | ||
| /// <param name="stepOrigin">Where a step sound (*eventSFXGrnd) asks for the ground below.</param> | ||
| /// <param name="stepSound">Plays the step sounds as one-shots on top of everything else. An NPC's NpcSound | ||
| /// (shared with hits, weapon draws, ...).</param> | ||
| /// <param name="clipSound">Plays the *eventSFX clips, one at a time like the engine's model sound. Needs an own | ||
| /// source: a clip's volume (Gothic's of each sound) and range would turn down the one-shots of a shared source. | ||
| /// Mobs pass their single source for both.</param> | ||
| /// <param name="isReusingPfx">Build each effect once and replay it (mobs), see class summary.</param> | ||
| public AnimationEffectPlayer(Transform boneRoot, Transform stepOrigin, AudioSource stepSound, | ||
| AudioSource clipSound, bool isReusingPfx) | ||
| { | ||
| _boneRoot = boneRoot; | ||
| _stepOrigin = stepOrigin; | ||
| _stepSound = stepSound; | ||
| _clipSound = clipSound; | ||
| _soundBaseVolume = clipSound != null ? clipSound.volume : 1f; | ||
| _isReusingPfx = isReusingPfx; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Plays the sounds whose frame was crossed. Returns true if there were any (also skipped footsteps). | ||
| /// </summary> | ||
| public bool ApplySfxEvents(AnimationTrackInstance trackInstance) | ||
| { | ||
| var sfxEvents = trackInstance.GetPendingSoundEffects(); | ||
| if (sfxEvents == null) | ||
| return false; | ||
|
|
||
| foreach (var sfx in sfxEvents) | ||
| { | ||
| // What to play (footstep by the ground below, Gothic's volume and range) is AudioService's decision. | ||
| // No clip (footsteps off, missing or nosound): nothing changes, a clip still playing on the source | ||
| // (a fire loop) keeps playing. | ||
| var toPlay = _audioService.GetAnimationSfx(sfx.Name, sfx.Range.ToMeter(), _stepOrigin.position); | ||
| if (toPlay.Clip == null) | ||
| continue; | ||
|
|
||
| // A step plays on top of the other sounds (it mustn't cut a swing or a scream). | ||
| if (toPlay.IsStep) | ||
| { | ||
| _stepSound.PlayOneShot(toPlay.Clip, toPlay.Volume); | ||
| continue; | ||
| } | ||
|
|
||
| // Gothic's volume of the sound (Forge_Sharpen 40 of 127). The player's sound effects slider works | ||
| // through the source's mixer group (AudioService.SoundEffectsGroup). One clip at a time. | ||
| _clipSound.clip = toPlay.Clip; | ||
| _clipSound.volume = _soundBaseVolume * toPlay.Volume; | ||
| _clipSound.maxDistance = toPlay.RangeMeters; | ||
| _clipSound.Play(); | ||
| } | ||
|
|
||
| return sfxEvents.Count > 0; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// *eventPFX (frame, index, name, bone, ATTACH) creates the effect at the | ||
| /// bone (following it if attached), *eventPFXStop (frame, index) lets it fade out. Joint smoke, bubbles, ... | ||
| /// </summary> | ||
| public void ApplyPfxEvents(AnimationTrackInstance trackInstance) | ||
| { | ||
| var stops = trackInstance.GetPendingParticleEffectStops(); | ||
| if (stops != null) | ||
| { | ||
| foreach (var stop in stops) | ||
| StopPfx(stop.Index); | ||
| } | ||
|
|
||
| var pfxEvents = trackInstance.GetPendingParticleEffects(); | ||
| if (pfxEvents == null) | ||
| return; | ||
|
|
||
| foreach (var pfx in pfxEvents) | ||
| { | ||
| if (_isReusingPfx && TryReplayPfx(pfx)) | ||
| continue; | ||
|
|
||
| var root = CreatePfx(pfx, trackInstance.AnimationName); | ||
| if (root == null) | ||
| continue; | ||
|
|
||
| if (_isReusingPfx) | ||
| { | ||
| _reusedPfx.Add(new ReusedPfx | ||
| { | ||
| Name = pfx.Name, | ||
| Bone = pfx.Position, | ||
| Index = pfx.Index, | ||
| Root = root, | ||
| Systems = root.GetComponentsInChildren<ParticleSystem>() | ||
| }); | ||
| continue; | ||
| } | ||
|
|
||
| if (pfx.Index != 0) | ||
| { | ||
| StopPfx(pfx.Index); | ||
| _activePfx[pfx.Index] = root; | ||
| _activePfxIndices.Add(pfx.Index); | ||
| } | ||
| Object.Destroy(root, _pfxMaxSeconds); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Builds the effect of a *eventPFX. Returns its root (the effect and its sub emitters), null if it failed. | ||
| /// </summary> | ||
| private GameObject CreatePfx(IEventParticleEffect pfx, string animationName) | ||
| { | ||
| var bone = FindBone(pfx.Position) ?? _boneRoot; | ||
| // A reused effect lives as long as the animator: it must not destroy itself after its first play. | ||
| var isDestroyedAfterPlay = !_isReusingPfx; | ||
| var pfxGo = pfx.Attached | ||
| ? _meshService.CreateVobPfx(pfx.Name, parent: bone.gameObject, destroyAfterPlay: isDestroyedAfterPlay) | ||
| : _meshService.CreateVobPfx(pfx.Name, bone.position, bone.rotation, | ||
| destroyAfterPlay: isDestroyedAfterPlay); | ||
| if (pfxGo == null) | ||
| { | ||
| Logger.LogWarning($"[AnimationPfx] {pfx.Name} couldn't be created ({animationName}).", | ||
| LogCat.Animation); | ||
| return null; | ||
| } | ||
|
|
||
| var parent = pfxGo.transform.parent; | ||
| var root = parent != null && parent != bone ? parent.gameObject : pfxGo; | ||
| foreach (var particleSystem in root.GetComponentsInChildren<ParticleSystem>()) | ||
| { | ||
| var main = particleSystem.main; | ||
| main.simulationSpace = ParticleSystemSimulationSpace.World; | ||
| } | ||
|
|
||
| // A mob's effect stays with the mob (culled with it, destroyed with the world). | ||
| if (_isReusingPfx && !pfx.Attached) | ||
| root.transform.SetParent(_boneRoot, true); | ||
|
|
||
| return root; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Reusing mode: the effect of this event was built before - it starts again. Stopping its emission first | ||
| /// restarts a burst that is still running (the second anvil hit), its particles in the air stay. | ||
| /// </summary> | ||
| private bool TryReplayPfx(IEventParticleEffect pfx) | ||
| { | ||
| for (var i = 0; i < _reusedPfx.Count; i++) | ||
| { | ||
| var reused = _reusedPfx[i]; | ||
| if (reused.Root == null || !reused.Name.EqualsIgnoreCase(pfx.Name) || | ||
| !reused.Bone.EqualsIgnoreCase(pfx.Position)) | ||
| continue; | ||
|
|
||
| foreach (var particleSystem in reused.Systems) | ||
| { | ||
| if (particleSystem == null) | ||
| continue; | ||
| particleSystem.Stop(false, ParticleSystemStopBehavior.StopEmitting); | ||
| particleSystem.Play(false); | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| private void StopPfx(int index) | ||
| { | ||
| if (_isReusingPfx) | ||
| { | ||
| // Reusing mode: the effect only stops emitting, the next event of its index starts it again. | ||
| for (var i = 0; i < _reusedPfx.Count; i++) | ||
| { | ||
| if (_reusedPfx[i].Index == index) | ||
| StopEmitting(_reusedPfx[i].Systems); | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| if (!_activePfx.Remove(index, out var pfxGo)) | ||
| return; | ||
| _activePfxIndices.Remove(index); | ||
| if (pfxGo == null) | ||
| return; | ||
|
|
||
| foreach (var particleSystem in pfxGo.GetComponentsInChildren<ParticleSystem>()) | ||
| particleSystem.Stop(true, ParticleSystemStopBehavior.StopEmitting); | ||
| Object.Destroy(pfxGo, _pfxFadeSeconds); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Every effect running until its *eventPFXStop fades out now (one-off mode). In reusing mode all effects stop | ||
| /// emitting and are destroyed once their particles faded. | ||
| /// </summary> | ||
| public void StopAllPfx() | ||
| { | ||
| if (_isReusingPfx) | ||
| { | ||
| foreach (var reused in _reusedPfx) | ||
| { | ||
| StopEmitting(reused.Systems); | ||
| if (reused.Root != null) | ||
| Object.Destroy(reused.Root, _pfxFadeSeconds); | ||
| } | ||
| _reusedPfx.Clear(); | ||
| return; | ||
| } | ||
|
|
||
| // Backwards: StopPfx removes the index from the list. | ||
| for (var i = _activePfxIndices.Count - 1; i >= 0; i--) | ||
| StopPfx(_activePfxIndices[i]); | ||
| } | ||
|
|
||
| private static void StopEmitting(ParticleSystem[] systems) | ||
| { | ||
| foreach (var particleSystem in systems) | ||
| { | ||
| if (particleSystem != null) | ||
| particleSystem.Stop(false, ParticleSystemStopBehavior.StopEmitting); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// MDS bone names differ in case from the bone GameObjects ("Bip01 Head" vs "BIP01 HEAD"). | ||
| /// </summary> | ||
| private Transform FindBone(string boneName) | ||
| { | ||
| if (string.IsNullOrEmpty(boneName)) | ||
| return null; | ||
| foreach (var child in _boneRoot.GetComponentsInChildren<Transform>()) | ||
| { | ||
| if (child.name.EqualsIgnoreCase(boneName)) | ||
| return child; | ||
| } | ||
| return null; | ||
| } | ||
| } | ||
| } |
2 changes: 2 additions & 0 deletions
2
Assets/Gothic-Core/Scripts/Adapters/Animations/AnimationEffectPlayer.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Claude: A lot of source code (maybe logic?) is now in an Adapter. Correct this way?