Ejemplo n.º 1
0
        private void _runDelayedCheck()
        {
            _checkTimerCancel?.Cancel();
            _checkTimerCancel = new CancellationTokenSource();

            Timer.Spawn(DeadCheckDelay, _checkForWinner, _checkTimerCancel.Token);
        }
Ejemplo n.º 2
0
        private void Fire()
        {
            var projectile = Owner.EntityManager.SpawnEntity(_boltType, Owner.Transform.Coordinates);

            if (!projectile.TryGetComponent <PhysicsComponent>(out var physicsComponent))
            {
                Logger.Error("Emitter tried firing a bolt, but it was spawned without a CollidableComponent");
                return;
            }

            physicsComponent.Status = BodyStatus.InAir;

            if (!projectile.TryGetComponent <ProjectileComponent>(out var projectileComponent))
            {
                Logger.Error("Emitter tried firing a bolt, but it was spawned without a ProjectileComponent");
                return;
            }

            projectileComponent.IgnoreEntity(Owner);

            physicsComponent
            .EnsureController <BulletController>()
            .LinearVelocity = Owner.Transform.WorldRotation.ToVec() * 20f;

            projectile.Transform.LocalRotation = Owner.Transform.WorldRotation;

            // TODO: Move to projectile's code.
            Timer.Spawn(3000, () => projectile.Delete());

            EntitySystem.Get <AudioSystem>().PlayFromEntity(_fireSound, Owner);
        }
Ejemplo n.º 3
0
        private void Fire(EmitterComponent component)
        {
            var projectile = EntityManager.SpawnEntity(component.BoltType, EntityManager.GetComponent <TransformComponent>(component.Owner).Coordinates);

            if (!EntityManager.TryGetComponent <PhysicsComponent?>(projectile, out var physicsComponent))
            {
                Logger.Error("Emitter tried firing a bolt, but it was spawned without a PhysicsComponent");
                return;
            }

            physicsComponent.BodyStatus = BodyStatus.InAir;

            if (!EntityManager.TryGetComponent <ProjectileComponent?>(projectile, out var projectileComponent))
            {
                Logger.Error("Emitter tried firing a bolt, but it was spawned without a ProjectileComponent");
                return;
            }

            projectileComponent.IgnoreEntity(component.Owner);

            physicsComponent
            .LinearVelocity = EntityManager.GetComponent <TransformComponent>(component.Owner).WorldRotation.ToWorldVec() * 20f;
            EntityManager.GetComponent <TransformComponent>(projectile).WorldRotation = EntityManager.GetComponent <TransformComponent>(component.Owner).WorldRotation;

            // TODO: Move to projectile's code.
            Timer.Spawn(3000, () => EntityManager.DeleteEntity(projectile));

            SoundSystem.Play(Filter.Pvs(component.Owner), component.FireSound.GetSound(), component.Owner,
                             AudioHelpers.WithVariation(EmitterComponent.Variation).WithVolume(EmitterComponent.Volume).WithMaxDistance(EmitterComponent.Distance));
        }
Ejemplo n.º 4
0
        private void ShotTimerCallback(EmitterComponent component)
        {
            if (component.Deleted)
            {
                return;
            }

            // Any power-off condition should result in the timer for this method being cancelled
            // and thus not firing
            DebugTools.Assert(component.IsPowered);
            DebugTools.Assert(component.IsOn);
            DebugTools.Assert(component.PowerConsumer != null && (component.PowerConsumer.DrawRate <= component.PowerConsumer.ReceivedPower));

            Fire(component);

            TimeSpan delay;

            if (component.FireShotCounter < component.FireBurstSize)
            {
                component.FireShotCounter += 1;
                delay = component.FireInterval;
            }
            else
            {
                component.FireShotCounter = 0;
                var diff = component.FireBurstDelayMax - component.FireBurstDelayMin;
                // TIL you can do TimeSpan * double.
                delay = component.FireBurstDelayMin + _random.NextFloat() * diff;
            }

            // Must be set while emitter powered.
            DebugTools.AssertNotNull(component.TimerCancel);
            Timer.Spawn(delay, () => ShotTimerCallback(component), component.TimerCancel !.Token);
        }
Ejemplo n.º 5
0
        private void StartFiring()
        {
            EverythingIsWellToFire();

            _fireCancelTokenSrc?.Cancel();
            _fireCancelTokenSrc = new CancellationTokenSource();
            var cancelToken = _fireCancelTokenSrc.Token;

            Timer.SpawnRepeating(_firingDelay, Fire, cancelToken);
        }
        public static void MakeSentient(IEntity entity)
        {
            if (entity.HasComponent <AiControllerComponent>())
            {
                entity.RemoveComponent <AiControllerComponent>();
            }

            // Delay spawning these components to avoid race conditions with the deferred removal of AiController.
            Timer.Spawn(100, () =>
            {
                entity.EnsureComponent <MindComponent>();
                entity.EnsureComponent <PlayerInputMoverComponent>();
                entity.EnsureComponent <SharedSpeechComponent>();
                entity.EnsureComponent <SharedEmotingComponent>();
            });
        }
Ejemplo n.º 7
0
        private void PowerOn()
        {
            if (_isPowered)
            {
                return;
            }

            _isPowered = true;

            _fireShotCounter = 0;
            _timerCancel     = new CancellationTokenSource();

            Timer.Spawn(_fireBurstDelayMax, ShotTimerCallback, _timerCancel.Token);

            UpdateAppearance();
        }
Ejemplo n.º 8
0
        public void PowerOn(EmitterComponent component)
        {
            if (component.IsPowered)
            {
                return;
            }

            component.IsPowered = true;

            component.FireShotCounter = 0;
            component.TimerCancel     = new CancellationTokenSource();

            Timer.Spawn(component.FireBurstDelayMax, () => ShotTimerCallback(component), component.TimerCancel.Token);

            UpdateAppearance(component);
        }
        private void _checkForWinner()
        {
            _checkTimerCancel = null;

            if (!_cfg.GetCVar(CCVars.GameLobbyEnableWin))
            {
                return;
            }

            IPlayerSession?winner = null;

            foreach (var playerSession in _playerManager.ServerSessions)
            {
                if (playerSession.AttachedEntity is not {
                    Valid: true
                } playerEntity ||
                    !_entityManager.TryGetComponent(playerEntity, out MobStateComponent? state))
                {
                    continue;
                }

                if (!state.IsAlive())
                {
                    continue;
                }

                if (winner != null)
                {
                    // Found a second person alive, nothing decided yet!
                    return;
                }

                winner = playerSession;
            }

            _chatManager.DispatchServerAnnouncement(winner == null
                ? Loc.GetString("rule-death-match-check-winner-stalemate")
                : Loc.GetString("rule-death-match-check-winner", ("winner", winner)));

            var restartDelay = 10;

            _chatManager.DispatchServerAnnouncement(Loc.GetString("rule-restarting-in-seconds", ("seconds", restartDelay)));

            Timer.Spawn(TimeSpan.FromSeconds(restartDelay), () => EntitySystem.Get <GameTicker>().RestartRound());
        }
Ejemplo n.º 10
0
        private void _checkForWinner()
        {
            _checkTimerCancel = null;

            if (!_cfg.GetCVar(CCVars.GameLobbyEnableWin))
            {
                return;
            }

            IPlayerSession?winner = null;

            foreach (var playerSession in _playerManager.GetAllPlayers())
            {
                var playerEntity = playerSession.AttachedEntity;
                if (playerEntity == null ||
                    !playerEntity.TryGetComponent(out IMobStateComponent? state))
                {
                    continue;
                }

                if (!state.IsAlive())
                {
                    continue;
                }

                if (winner != null)
                {
                    // Found a second person alive, nothing decided yet!
                    return;
                }

                winner = playerSession;
            }

            _chatManager.DispatchServerAnnouncement(winner == null
                ? Loc.GetString("Everybody is dead, it's a stalemate!")
                : Loc.GetString("{0} wins the death match!", winner));

            var restartDelay = 10;

            _chatManager.DispatchServerAnnouncement(Loc.GetString("Restarting in {0} seconds.", restartDelay));

            Timer.Spawn(TimeSpan.FromSeconds(restartDelay), () => _gameTicker.RestartRound());
        }
Ejemplo n.º 11
0
 public static void AddTimer(this EntityUid entity, Timer timer, CancellationToken cancellationToken = default)
 {
     entity
     .EnsureTimerComponent()
     .AddTimer(timer, cancellationToken);
 }
Ejemplo n.º 12
0
 public static void AddTimer(this IEntity entity, Timer timer, CancellationToken cancellationToken = default)
 {
     entity
     .EnsureComponent <TimerComponent>()
     .AddTimer(timer, cancellationToken);
 }