[SerializeField] private AudioPlayable _damageSound; // global for now but can be per character/ennemy protected override void OnGamePresentationUpdate() { if (_audioSource == null) { return; } // Item sounds foreach (var gameActionEvent in PresentationEvents.GameActionEvents.SinceLastPresUpdate) { SimWorld.TryGetComponent(gameActionEvent.GameActionContext.Action, out SimAssetId entitySimAssetID); GameObject entityPrefab = PresentationHelpers.FindSimAssetPrefab(entitySimAssetID); if (entityPrefab != null) { var sfx = entityPrefab.GetComponent <GameActionAuth>()?.SfxOnUse; if (sfx != null) { sfx.PlayOn(_audioSource); } } } // Damage sounds foreach (var hpDeltaEvent in PresentationEvents.HealthDeltaEvents.SinceLastPresUpdate) { if (_damageSound != null) { _damageSound.PlayOn(_audioSource); } } }
protected override void OnGamePresentationUpdate() { foreach (var gameActionEvent in PresentationEvents.GameActionEvents.SinceLastPresUpdate) { SimWorld.TryGetComponent(gameActionEvent.GameActionContext.LastPhysicalInstigator, out FixTranslation lastPhysicalInstigatorTranslation); // ITEM USED VFX // only do the vfx if not an auto attack if (!SimWorld.HasComponent <AutoAttackAction>(gameActionEvent.GameActionContext.ActionInstigatorActor) || (SimWorld.TryGetComponent(gameActionEvent.GameActionContext.ActionInstigatorActor, out AutoAttackAction autoAttack) && autoAttack.Value != gameActionEvent.GameActionContext.Action)) { SimWorld.TryGetComponent(gameActionEvent.GameActionContext.ActionInstigatorActor, out SimAssetId instigatorAssetId); GameObject itemPrefab = PresentationHelpers.FindSimAssetPrefab(instigatorAssetId); if (itemPrefab != null && itemPrefab.TryGetComponent(out ItemAuth itemAuth)) { if (itemAuth.Icon != null && ItemUsedVFX != null) { ItemUsedVFX.TriggerVFX(new KeyValuePair <string, object>("Location", lastPhysicalInstigatorTranslation.Value.ToUnityVec()) , new KeyValuePair <string, object>("Sprite", itemAuth.Icon)); } } } // GAME ACTION USED VFX SimWorld.TryGetComponent(gameActionEvent.GameActionContext.Action, out SimAssetId gameActionAssetId); GameObject gameActionPrefab = PresentationHelpers.FindSimAssetPrefab(gameActionAssetId); if (gameActionPrefab != null && gameActionPrefab.TryGetComponent(out GameActionAuth gameActionAuth)) { VFXDefinition instigatorVFX = gameActionAuth.InstigatorVFX; if (instigatorVFX != null) { instigatorVFX.TriggerVFX(new KeyValuePair <string, object>("Location", lastPhysicalInstigatorTranslation.Value.ToUnityVec())); } VFXDefinition targetsVFX = gameActionAuth.TargetsVFX; if (targetsVFX != null && gameActionEvent.GameActionContext.Targets.IsCreated) { for (int i = 0; i < gameActionEvent.GameActionContext.Targets.Length; i++) { Entity target = gameActionEvent.GameActionContext.Targets[i]; if (SimWorld.TryGetComponent(target, out FixTranslation targetLocation)) { targetsVFX.TriggerVFX(new KeyValuePair <string, object>("Location", targetLocation.Value.ToUnityVec())); } } } } } }
private bool HandleGameActionAnimation() { foreach (GameActionUsedEventData gameActionEvent in PresentationEvents.GameActionEvents.SinceLastPresUpdate) { if (gameActionEvent.GameActionContext.LastPhysicalInstigator == SimEntity && gameActionEvent.GameActionContext.Action != Entity.Null && !_hasTriggeredAnAnimation) { TriggerAnimationInteruptionOnStateChange(); _hasTriggeredAnAnimation = true; _lastTransitionTime = SimWorld.Time.ElapsedTime; // GAME ACTION AUTH & ANIMATION TRIGGER SimWorld.TryGetComponent(gameActionEvent.GameActionContext.Action, out SimAssetId instigatorAssetId); GameObject instigatorPrefab = PresentationHelpers.FindSimAssetPrefab(instigatorAssetId); if (instigatorPrefab.TryGetComponent(out GameActionAuth gameActionAuth)) { _currentAnimation = gameActionAuth.Animation; // add additionnal animation to play in queue (skip the first we'll play) if (gameActionAuth.Animation != null) { for (int i = 1; i < gameActionEvent.GameActionResult.Count; i++) { // ANIMATION DATA List <KeyValuePair <string, object> > currentAnimationData = new List <KeyValuePair <string, object> >(); currentAnimationData.Add(new KeyValuePair <string, object>("GameActionContext", gameActionEvent.GameActionContext)); switch (i) { case 1: currentAnimationData.Add(new KeyValuePair <string, object>("GameActionContextResult", gameActionEvent.GameActionResult.DataElement_1)); break; case 2: currentAnimationData.Add(new KeyValuePair <string, object>("GameActionContextResult", gameActionEvent.GameActionResult.DataElement_2)); break; case 3: currentAnimationData.Add(new KeyValuePair <string, object>("GameActionContextResult", gameActionEvent.GameActionResult.DataElement_3)); break; default: break; } _queuedAnimations.Add(new QueuedAnimation() { Data = currentAnimationData, Definition = _currentAnimation, HasPlayed = false }); } // ANIMATION DATA List <KeyValuePair <string, object> > animationData = new List <KeyValuePair <string, object> >(); animationData.Add(new KeyValuePair <string, object>("GameActionContext", gameActionEvent.GameActionContext)); animationData.Add(new KeyValuePair <string, object>("GameActionContextResult", gameActionEvent.GameActionResult.DataElement_0)); _currentAnimation.TriggerAnimation(SimEntity, _spriteStartPos, _bone, animationData); } } _previousState = AnimationType.GameAction; } } if (!_hasTriggeredAnAnimation) { // find queued animation to play QueuedAnimation animationToPlay = null; for (int i = 0; i < _queuedAnimations.Count; i++) { if (!_queuedAnimations[i].HasPlayed) { animationToPlay = _queuedAnimations[i]; _queuedAnimations[i].HasPlayed = true; break; } } if (animationToPlay != null) { TriggerAnimationInteruptionOnStateChange(); _hasTriggeredAnAnimation = true; _lastTransitionTime = SimWorld.Time.ElapsedTime; _currentAnimation = animationToPlay.Definition ?? FindAnimation(AnimationType.GameAction); _currentAnimation.TriggerAnimation(SimEntity, _spriteStartPos, _bone, animationToPlay.Data); _previousState = AnimationType.GameAction; } else { _queuedAnimations.Clear(); } } return(_hasTriggeredAnAnimation); }