void CheckBulletsCollision()
 {
     for (var i = 0; i < _asteroids.Length; i++)
     {
         var asteroid1 = _asteroids[i];
         if (!asteroid1.Frozen)
         {
             for (var j = 0; j < _bullets.Count; j++)
             {
                 var bullet = _bullets[j];
                 if (ISceneObjectInfo.CheckCollision(asteroid1, bullet, out var deltaPositionNormalized))
                 {
                     asteroid1.Durability -= _shipInfo.WeaponDamage;
                     if (asteroid1.Durability <= 0f)
                     {
                         MessageBroker.Default.Publish(SharedMessage.Create(this, SharedMessage.MessageType.CREATE_EXPLOSION, asteroid1.Position.Value));
                         asteroid1.Position.Value = new Vector3(0f, 0f, -2f);
                     }
                     bullet.Position.Value += new Vector3(0f, 0f, 20f);
                     _bullets.Remove(bullet);
                     j--;
                 }
             }
         }
     }
 }
 void CheckAsteroidsCollision()
 {
     for (var i = 0; i < _asteroids.Length; i++)
     {
         var asteroid1 = _asteroids[i];
         if (!asteroid1.Frozen)
         {
             for (var j = i + 1; j < _asteroids.Length; j++)
             {
                 var asteroid2 = _asteroids[j];
                 if (!asteroid2.Frozen)
                 {
                     if (ISceneObjectInfo.CheckCollision(asteroid1, asteroid2, out var deltaPositionNormalized))
                     {
                         asteroid1.CollidedWith.Value = asteroid2;
                         asteroid2.CollidedWith.Value = asteroid1;
                         asteroid2.LinearVelocity    -= deltaPositionNormalized * asteroid1.Scale.Value * 0.01f;
                         asteroid1.LinearVelocity    += deltaPositionNormalized * asteroid2.Scale.Value * 0.01f;
                     }
                 }
             }
             if (ISceneObjectInfo.CheckCollision(asteroid1, _shipInfo, out var deltaPositionNormalized2))
             {
                 MessageBroker.Default.Publish(SharedMessage.Create(this, SharedMessage.MessageType.SHIP_COLLIDED, asteroid1));
             }
         }
     }
 }
 void CheckEndGame()
 {
     if (_asteroids[_asteroids.Length - 1].Position.Value.z < 0f)
     {
         MessageBroker.Default.Publish(SharedMessage.Create(this, SharedMessage.MessageType.GAME_OVER, true));
     }
 }
Exemplo n.º 4
0
        async void Start()
        {
            _playerInfo = new PlayerInfo(MaxHitPoints);

            _shipInfo              = new ShipInfo();
            _shipInfo.Speed        = Speed;
            _shipInfo.WeaponDamage = WeaponDamage;
            _shipInfo.WeaponPlaces = new Vector3[4] {
                new Vector3(-0.6f, 0f, 0.5f),
                new Vector3(0.6f, 0f, 0.5f),
                new Vector3(-0.35f, 0f, 0.3f),
                new Vector3(0.35f, 0f, 0.3f)
            };
            _shipInfo.WeaponShootingFrequency = WeaponShootingFrequency;
            _shipInfo.Position.Value          = new Vector3(0f, 0f, 0f);
            _shipInfo.Active = true;

            _levelInfo = PersistentState.Get().CurrentLevelInfo;

            LevelGenerator.GenerateAsteroids(_levelInfo, out _asteroids);

            await System.Threading.Tasks.Task.Yield();

            MessageBroker.Default.Publish(SharedMessage.Create(this, SharedMessage.MessageType.INIT_PLAYER, _playerInfo));
            MessageBroker.Default.Publish(SharedMessage.Create(this, SharedMessage.MessageType.INIT_LEVEL, _levelInfo));
            MessageBroker.Default.Publish(SharedMessage.Create(this, SharedMessage.MessageType.INIT_ASTEROIDS, _asteroids));
            MessageBroker.Default.Publish(SharedMessage.Create(this, SharedMessage.MessageType.INIT_SHIP, _shipInfo));
        }
 void CreateExplosion(Vector3 position)
 {
     var explosion = new EffectInfo();
     explosion.EndTime = Time.time + 0.5f;
     explosion.Position.Value = position;
     _explosions.Add(explosion);
     
     MessageBroker.Default.Publish(SharedMessage.Create(this, SharedMessage.MessageType.SHOW_EXPLOSION, explosion));
 }
        void ShipCollided(ISceneObjectInfo collider)
        {
            MessageBroker.Default.Publish(SharedMessage.Create(this, SharedMessage.MessageType.CREATE_EXPLOSION, collider.Position.Value));
            collider.Position.Value = new Vector3(0f, 0f, -2f);

            _playerInfo.HitPoints.Value--;
            if (_playerInfo.HitPoints.Value == 0)
            {
                MessageBroker.Default.Publish(SharedMessage.Create(this, SharedMessage.MessageType.GAME_OVER, false));
            }
        }
        void CreateBullet(Vector3 position)
        {
            var bullet = new BulletInfo();

            bullet.EndTime        = Time.time + 3f;
            bullet.LinearVelocity = new Vector3(0f, 0f, 10f);
            bullet.Position.Value = position;
            _bullets.Add(bullet);

            MessageBroker.Default.Publish(SharedMessage.Create(this, SharedMessage.MessageType.SHOW_BULLET, bullet));
        }
        void Shoot()
        {
            if (_shipInfo.Active && (_shipInfo.WeaponLastShotTime.Value + 1f / _shipInfo.WeaponShootingFrequency < Time.time))
            {
                _shipInfo.WeaponLastShotPlace++;
                if (_shipInfo.WeaponLastShotPlace >= _shipInfo.WeaponPlaces.Length)
                {
                    _shipInfo.WeaponLastShotPlace = 0;
                }
                _shipInfo.WeaponLastShotTime.Value = Time.time;

                MessageBroker.Default.Publish(SharedMessage.Create(this, SharedMessage.MessageType.SHOOT, null));
            }
        }
 void ApplyAsteroidData()
 {
     MessageBroker.Default.Publish(SharedMessage.Create(this, SharedMessage.MessageType.APPLY_ASTEROID_DATA, _asteroids));
 }
Exemplo n.º 10
0
        void Shoot()
        {
            var position = _shipInfo.Position.Value + _shipInfo.WeaponPlaces[_shipInfo.WeaponLastShotPlace];

            MessageBroker.Default.Publish(SharedMessage.Create(this, SharedMessage.MessageType.CREATE_BULLET, position));
        }
        void SmoothPosition()
        {
            var position = Vector3.Lerp(_shipInfo.Position.Value, _shipTargetPosition, Time.deltaTime * 10f);

            MessageBroker.Default.Publish(SharedMessage.Create(this, SharedMessage.MessageType.MOVE_SHIP, position));
        }