public CharacterBrain(AnimationsSet animationsSet, GameObject owner) : base(owner)
        {
            _transform = owner.Components.Get <Transform>() ??
                         throw new ComponentNotFoundException <Transform>();

            _animationComponent = owner.Components.Get <AnimatedSpriteRenderComponent>() ??
                                  throw new ComponentNotFoundException <AnimatedSpriteRenderComponent>();
        }
Exemple #2
0
        internal void EventTriggerStateChanged(EventTriggers state, bool active, HashSet <string> muzzles = null)
        {
            var session = Comp.Session;
            var canPlay = !session.DedicatedServer && session.SyncBufferedDistSqr >= Vector3D.DistanceSquared(session.CameraPos, MyPivotPos);

            switch (state)
            {
            case EventTriggers.StopFiring:
            case EventTriggers.PreFire:
            case EventTriggers.Firing:
                if (AnimationsSet.ContainsKey(state))
                {
                    var  addToFiring = AnimationsSet.ContainsKey(EventTriggers.StopFiring) && state == EventTriggers.Firing;
                    uint delay       = 0;
                    if (active)
                    {
                        if (state == EventTriggers.StopFiring)
                        {
                            // Fix this properly
                            var stopLen = 0u;
                            System.WeaponAnimationLengths.TryGetValue(EventTriggers.StopFiring, out stopLen);
                            Timings.ShootDelayTick = stopLen + session.Tick;
                            if (LastEvent == EventTriggers.Firing || LastEvent == EventTriggers.PreFire)
                            {
                                if (CurLgstAnimPlaying != null && CurLgstAnimPlaying.Running)
                                {
                                    delay = CurLgstAnimPlaying.Reverse ? (uint)CurLgstAnimPlaying.CurrentMove : (uint)((CurLgstAnimPlaying.NumberOfMoves - 1) - CurLgstAnimPlaying.CurrentMove);
                                    Timings.ShootDelayTick += delay;
                                }
                            }
                        }
                    }

                    for (int i = 0; i < AnimationsSet[state].Length; i++)
                    {
                        var animation = AnimationsSet[state][i];

                        if (active && !animation.Running && (animation.Muzzle == "Any" || (muzzles != null && muzzles.Contains(animation.Muzzle))))
                        {
                            if (animation.TriggerOnce && animation.Triggered)
                            {
                                continue;
                            }
                            animation.Triggered = true;

                            if (CurLgstAnimPlaying == null || CurLgstAnimPlaying.EventTrigger != state || animation.NumberOfMoves > CurLgstAnimPlaying.NumberOfMoves)
                            {
                                CurLgstAnimPlaying = animation;
                            }

                            if (animation.Muzzle != "Any" && addToFiring)
                            {
                                _muzzlesFiring.Add(animation.Muzzle);
                            }

                            animation.StartTick = session.Tick + animation.MotionDelay + delay;
                            Comp.Session.AnimationsToProcess.Add(animation);
                            animation.Running = true;
                            //animation.Paused = Comp.ResettingSubparts;
                            animation.CanPlay = canPlay;

                            if (animation.DoesLoop)
                            {
                                animation.Looping = true;
                            }
                        }
                        else if (active && animation.DoesLoop)
                        {
                            animation.Looping = true;
                        }
                        else if (!active)
                        {
                            animation.Looping   = false;
                            animation.Triggered = false;
                        }
                    }
                    if (active && state == EventTriggers.StopFiring)
                    {
                        _muzzlesFiring.Clear();
                    }
                }
                break;

            case EventTriggers.StopTracking:
            case EventTriggers.Tracking:
                if (AnimationsSet.ContainsKey(state))
                {
                    var oppositeEvnt = state == EventTriggers.Tracking ? EventTriggers.StopTracking : EventTriggers.Tracking;
                    //if (active) LastEvent = state;
                    for (int i = 0; i < AnimationsSet[state].Length; i++)
                    {
                        var animation = AnimationsSet[state][i];
                        if (active && !animation.Running)
                        {
                            if (animation.TriggerOnce && animation.Triggered)
                            {
                                continue;
                            }
                            animation.Triggered = true;

                            if (CurLgstAnimPlaying == null || CurLgstAnimPlaying.EventTrigger != state || animation.NumberOfMoves > CurLgstAnimPlaying.NumberOfMoves)
                            {
                                CurLgstAnimPlaying = animation;
                            }

                            PartAnimation animCheck;
                            animation.Running = true;
                            //animation.Paused = Comp.ResettingSubparts;
                            animation.CanPlay = canPlay;
                            string opEvent = "";
                            if (animation.EventIdLookup.TryGetValue(oppositeEvnt, out opEvent) && AnimationLookup.TryGetValue(opEvent, out animCheck) && animCheck.Running)
                            {
                                animCheck.Reverse = true;

                                if (!animation.DoesLoop)
                                {
                                    animation.Running = false;
                                }
                                else
                                {
                                    animation.StartTick = Comp.Session.Tick + (uint)animCheck.CurrentMove + animation.MotionDelay;
                                    Comp.Session.AnimationsToProcess.Add(animation);
                                }
                            }
                            else
                            {
                                Comp.Session.AnimationsToProcess.Add(animation);
                                animation.StartTick = session.Tick + animation.MotionDelay;
                            }

                            if (animation.DoesLoop)
                            {
                                animation.Looping = true;
                            }
                        }
                        else if (active && animation.DoesLoop)
                        {
                            animation.Looping = true;
                        }
                        else if (!active)
                        {
                            animation.Looping   = false;
                            animation.Triggered = false;
                        }
                    }
                }
                break;

            case EventTriggers.TurnOn:
            case EventTriggers.TurnOff:
                if (active && AnimationsSet.ContainsKey(state))
                {
                    var oppositeEvnt = state == EventTriggers.TurnOff ? EventTriggers.TurnOn : EventTriggers.TurnOff;

                    if ((state == EventTriggers.TurnOn && !Comp.State.Value.Online) || state == EventTriggers.TurnOff && Comp.State.Value.Online)
                    {
                        return;
                    }

                    //LastEvent = state;
                    for (int i = 0; i < AnimationsSet[state].Length; i++)
                    {
                        var animation = AnimationsSet[state][i];
                        if (!animation.Running)
                        {
                            if (CurLgstAnimPlaying == null || CurLgstAnimPlaying.EventTrigger != state || animation.NumberOfMoves > CurLgstAnimPlaying.NumberOfMoves)
                            {
                                CurLgstAnimPlaying = animation;
                            }

                            PartAnimation animCheck;
                            animation.Running = true;
                            animation.CanPlay = true;
                            //animation.Paused = Comp.ResettingSubparts;
                            string eventName;
                            if (animation.EventIdLookup.TryGetValue(oppositeEvnt, out eventName) && AnimationLookup.TryGetValue(eventName, out animCheck))
                            {
                                if (animCheck.Running)
                                {
                                    animCheck.Reverse = true;
                                    animation.Running = false;
                                }
                                else
                                {
                                    session.ThreadedAnimations.Enqueue(animation);
                                }
                            }
                            else
                            {
                                session.ThreadedAnimations.Enqueue(animation);
                            }

                            animation.StartTick = session.Tick + animation.MotionDelay;
                            if (state == EventTriggers.TurnOff)
                            {
                                animation.StartTick += Timings.OffDelay;
                            }
                        }
                        else
                        {
                            animation.Reverse = false;
                        }
                    }
                }
                break;

            case EventTriggers.EmptyOnGameLoad:
            case EventTriggers.Overheated:
            case EventTriggers.OutOfAmmo:
            case EventTriggers.BurstReload:
            case EventTriggers.Reloading:
                if (AnimationsSet.ContainsKey(state))
                {
                    //if (active) LastEvent = state;
                    for (int i = 0; i < AnimationsSet[state].Length; i++)
                    {
                        var animation = AnimationsSet[state][i];
                        if (active && !animation.Running)
                        {
                            if (animation.TriggerOnce && animation.Triggered)
                            {
                                continue;
                            }
                            animation.Triggered = true;

                            if (CurLgstAnimPlaying == null || CurLgstAnimPlaying.EventTrigger != state || animation.NumberOfMoves > CurLgstAnimPlaying.NumberOfMoves)
                            {
                                CurLgstAnimPlaying = animation;
                            }

                            animation.StartTick = session.Tick + animation.MotionDelay;
                            session.ThreadedAnimations.Enqueue(animation);

                            animation.Running = true;
                            animation.CanPlay = canPlay;
                            //animation.Paused = Comp.ResettingSubparts;

                            if (animation.DoesLoop)
                            {
                                animation.Looping = true;
                            }
                        }
                        else if (active && animation.DoesLoop)
                        {
                            animation.Looping = true;
                        }
                        else if (!active)
                        {
                            animation.Looping   = false;
                            animation.Triggered = false;
                        }
                    }
                }
                break;
            }
            if (active)
            {
                LastEvent = state;
            }
        }
Exemple #3
0
        internal void EventTriggerStateChanged(EventTriggers state, bool active, HashSet <string> muzzles = null)
        {
            if (Comp?.Data.Repo == null || Comp.MyCube == null || Comp.MyCube.MarkedForClose || Comp.Ai == null || Comp.Platform.State != MyWeaponPlatform.PlatformState.Ready)
            {
                return;
            }
            try
            {
                var session  = Comp.Session;
                var distance = Vector3D.DistanceSquared(session.CameraPos, MyPivotPos);
                var canPlay  = !session.DedicatedServer && 64000000 >= distance; //8km max range, will play regardless of range if it moves PivotPos and is loaded

                if (canPlay)
                {
                    PlayParticleEvent(state, active, distance, muzzles);
                }

                if (!AnimationsSet.ContainsKey(state))
                {
                    return;
                }
                if (AnimationDelayTick < Comp.Session.Tick)
                {
                    AnimationDelayTick = Comp.Session.Tick;
                }

                var  set        = false;
                uint startDelay = 0;

                switch (state)
                {
                case EventTriggers.StopFiring:
                case EventTriggers.PreFire:
                case EventTriggers.Firing:
                {
                    var addToFiring = AnimationsSet.ContainsKey(EventTriggers.StopFiring) && state == EventTriggers.Firing;

                    for (int i = 0; i < AnimationsSet[state].Length; i++)
                    {
                        var animation = AnimationsSet[state][i];

                        if (active && !animation.Running && (animation.Muzzle == "Any" || (muzzles != null && muzzles.Contains(animation.Muzzle))))
                        {
                            if (animation.TriggerOnce && animation.Triggered)
                            {
                                continue;
                            }
                            animation.Triggered = true;

                            set = true;

                            if (animation.Muzzle != "Any" && addToFiring)
                            {
                                _muzzlesFiring.Add(animation.Muzzle);
                            }

                            animation.StartTick = session.Tick + animation.MotionDelay;
                            if (state == EventTriggers.StopFiring)
                            {
                                startDelay           = AnimationDelayTick - session.Tick;
                                animation.StartTick += startDelay;
                            }

                            Comp.Session.AnimationsToProcess.Add(animation);
                            animation.Running = true;
                            animation.CanPlay = canPlay;

                            if (animation.DoesLoop)
                            {
                                animation.Looping = true;
                            }
                        }
                        else if (active && animation.DoesLoop)
                        {
                            animation.Looping = true;
                        }
                        else if (!active)
                        {
                            animation.Looping   = false;
                            animation.Triggered = false;
                        }
                    }
                    if (active && state == EventTriggers.StopFiring)
                    {
                        _muzzlesFiring.Clear();
                    }
                    break;
                }

                case EventTriggers.StopTracking:
                case EventTriggers.Tracking:
                {
                    for (int i = 0; i < AnimationsSet[state].Length; i++)
                    {
                        var animation = AnimationsSet[state][i];
                        if (active && !animation.Running)
                        {
                            if (animation.TriggerOnce && animation.Triggered)
                            {
                                continue;
                            }

                            set = true;
                            animation.Triggered = true;
                            animation.Running   = true;
                            animation.CanPlay   = canPlay;

                            Comp.Session.AnimationsToProcess.Add(animation);
                            animation.StartTick = session.Tick + animation.MotionDelay;

                            if (LastEvent == EventTriggers.StopTracking || LastEvent == EventTriggers.Tracking)
                            {
                                startDelay           = (AnimationDelayTick - session.Tick);
                                animation.StartTick += startDelay;
                            }

                            if (animation.DoesLoop)
                            {
                                animation.Looping = true;
                            }
                        }
                        else if (active && animation.DoesLoop)
                        {
                            animation.Looping = true;
                        }
                        else if (!active)
                        {
                            animation.Looping   = false;
                            animation.Triggered = false;
                        }
                    }
                    break;
                }

                case EventTriggers.TurnOn:
                case EventTriggers.TurnOff:
                    if (active)
                    {
                        for (int i = 0; i < AnimationsSet[state].Length; i++)
                        {
                            var animation = AnimationsSet[state][i];
                            if (!animation.Running)
                            {
                                animation.Running = true;
                                animation.CanPlay = true;

                                animation.StartTick = session.Tick + animation.MotionDelay + (AnimationDelayTick - session.Tick);
                                if (state == EventTriggers.TurnOff)
                                {
                                    startDelay           = OffDelay;
                                    animation.StartTick += startDelay;
                                }

                                session.ThreadedAnimations.Enqueue(animation);
                            }
                            else
                            {
                                animation.Reverse = false;
                            }
                        }
                    }
                    break;

                case EventTriggers.EmptyOnGameLoad:
                case EventTriggers.Overheated:
                case EventTriggers.NoMagsToLoad:
                case EventTriggers.BurstReload:
                case EventTriggers.Reloading:
                {
                    for (int i = 0; i < AnimationsSet[state].Length; i++)
                    {
                        var animation = AnimationsSet[state][i];
                        if (animation == null)
                        {
                            continue;
                        }

                        if (active && !animation.Running)
                        {
                            if (animation.TriggerOnce && animation.Triggered)
                            {
                                continue;
                            }
                            animation.Triggered = true;

                            set = true;

                            animation.StartTick = session.Tick + animation.MotionDelay;
                            session.ThreadedAnimations.Enqueue(animation);

                            animation.Running = true;
                            animation.CanPlay = canPlay;

                            if (animation.DoesLoop)
                            {
                                animation.Looping = true;
                            }
                        }
                        else if (active && animation.DoesLoop)
                        {
                            animation.Looping = true;
                        }
                        else if (!active)
                        {
                            animation.Looping   = false;
                            animation.Triggered = false;
                        }
                    }

                    break;
                }
                }
                if (active && set)
                {
                    var animationLength = 0u;

                    LastEvent         = state;
                    LastEventCanDelay = state == EventTriggers.Reloading || state == EventTriggers.StopFiring || state == EventTriggers.TurnOff || state == EventTriggers.TurnOn;

                    if (System.WeaponAnimationLengths.TryGetValue(state, out animationLength))
                    {
                        var delay = session.Tick + animationLength + startDelay;
                        if (delay > AnimationDelayTick)
                        {
                            AnimationDelayTick = delay;
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Log.Line($"Exception in Event Triggered: {e}");
            }
        }
 public CharacterBrain(AnimationsSet animationsSet, GameObject owner) : base(owner)
 {
 }
        public static async ValueTask <CharacterGame> Create(BECanvasComponent canvas, AnimationsSet animationsSet)
        {
            var warrior = new GameObject();

            var animation = animationsSet.GetAnimation("Idle");

            warrior.Components.Add(new Transform(warrior)
            {
                Position  = Vector2.Zero,
                Direction = Vector2.One,
                Size      = animation.FrameSize
            });

            warrior.Components.Add(new AnimatedSpriteRenderComponent(warrior)
            {
                Animation = animation
            });

            warrior.Components.Add(new CharacterBrain(animationsSet, warrior));

            var game = new CharacterGame {
                _context = await canvas.CreateCanvas2DAsync(), _warrior = warrior
            };

            return(game);
        }