protected override void ApplicationStarted()
    {
        defaultPool = PoolManager.GetPool();

        var levelBlueprint  = new LevelBlueprint();
        var levelEntity     = defaultPool.CreateEntity(levelBlueprint);
        var player          = defaultPool.CreateEntity(new PlayerBlueprint(_gameConfiguration.StartingFoodPoints));
        var playerView      = player.GetComponent <ViewComponent>();
        var playerComponent = player.GetComponent <PlayerComponent>();
        var levelComponent  = levelEntity.GetComponent <LevelComponent>();

        levelComponent.Level.DistinctUntilChanged()
        .Subscribe(x => {
            playerView.View.transform.position = Vector3.zero;
            SetupLevel(levelComponent);
        });

        _eventSystem.Receive <PlayerKilledEvent>()
        .Delay(TimeSpan.FromSeconds(_gameConfiguration.IntroLength))
        .Subscribe(x =>
        {
            levelBlueprint.UpdateLevel(levelComponent, 1);
            playerComponent.Food.Value = _gameConfiguration.StartingFoodPoints;
            SetupLevel(levelComponent);
        });
    }
    private void SetupLevel(LevelComponent levelComponent)
    {
        levelComponent.HasLoaded.Value = false;

        defaultPool.RemoveEntitiesContaining(typeof(GameBoardComponent),
                                             typeof(FoodComponent), typeof(WallComponent),
                                             typeof(EnemyComponent), typeof(ExitComponent));

        Observable.Interval(TimeSpan.FromSeconds(_gameConfiguration.IntroLength))
        .First()
        .Subscribe(x => levelComponent.HasLoaded.Value = true);

        defaultPool.CreateEntity(new GameBoardBlueprint());

        for (var i = 0; i < levelComponent.FoodCount; i++)
        {
            defaultPool.CreateEntity(new FoodBlueprint());
        }

        for (var i = 0; i < levelComponent.WallCount; i++)
        {
            defaultPool.CreateEntity(new WallBlueprint());
        }

        for (var i = 0; i < levelComponent.EnemyCount; i++)
        {
            defaultPool.CreateEntity(new EnemyBlueprint());
        }

        defaultPool.CreateEntity(new ExitBlueprint());
    }
Exemple #3
0
        public void Reaction(IEntity entity)
        {
            var viewComponent = entity.GetComponent <ViewComponent>();
            var blueprint     = new SelfDestructBlueprint(viewComponent.View.transform.position);

            _defaultPool.CreateEntity(blueprint); //todo: optimize 68.5%
        }
Exemple #4
0
        public void Execute(IEntity entity)
        {
            var viewComponent = entity.GetComponent <ViewComponent>();
            var blueprint     = new SelfDestructBlueprint(viewComponent.View.transform.position);

            _defaultPool.CreateEntity(blueprint);
        }
        private void _spawnCoins(IEntity entity)
        {
            var viewComponent  = entity.GetComponent <ViewComponent>();
            var go             = viewComponent.View.gameObject;
            var targetPosition = (Vector2)go.transform.position;
            var spawnCounts    = UnityEngine.Random.Range(2, 5);

            for (var i = 0; i < spawnCounts; i++)
            {
                var offsetPosition = Vector2.Scale(UnityEngine.Random.onUnitSphere, Vector2.one);
                var coinEntity     = _pool.CreateEntity();
                var coinComponent  = new CoinComponent()
                {
                    startPosition    = offsetPosition + targetPosition,
                    lifeTime         = 6,
                    elapsedTime      = 0,
                    elapsedBlinkTime = 0,
                    blinkTime        = .5f,
                    score            = 50
                };

                coinEntity.AddComponent(coinComponent);
                coinEntity.AddComponent(new ViewComponent());
            }
        }
Exemple #6
0
        public SceneManager(IPoolManager poolManager)
        {
            this.poolManager = poolManager;
            sceneEntities    = new Dictionary <string, IEntity>();
            scenes           = new List <string>();

            IPool defaultPool = poolManager.GetPool();

            SceneLoadedAsObservable().Subscribe(arg =>
            {
                scenes.Add(arg.Scene.name);
                var entity         = defaultPool.CreateEntity();
                var sceneComponent = new SceneComponent {
                    Scene = arg.Scene
                };
                entity.AddComponent(sceneComponent);
                sceneEntities.Add(new KeyValuePair <string, IEntity>(arg.Scene.name, entity));
            }).AddTo(disposables);

            SceneUnloadedAsObservable().Subscribe(scene =>
            {
                if (sceneEntities.ContainsKey(scene.name))
                {
                    scenes.Remove(scene.name);
                    defaultPool.RemoveEntity(sceneEntities[scene.name]);
                    sceneEntities.Remove(scene.name);
                }
            }).AddTo(disposables);
        }
Exemple #7
0
        public static IEntity CreateEntity(this IPool pool, IEnumerable <IBlueprint> blueprints)
        {
            var entity = pool.CreateEntity();

            entity.ApplyBlueprints(blueprints);
            return(entity);
        }
        private void _createBullet(IEntity entity)
        {
            var viewComponent    = entity.GetComponent <ViewComponent>();
            var go               = viewComponent.View.gameObject;
            var startPosition    = go.transform.position + (go.transform.up * 0.25f);
            var bullet           = _pool.CreateEntity();
            var enemyMask        = 1 << LayerMask.NameToLayer("Enemy");
            var destructibleMask = 1 << LayerMask.NameToLayer("Destructible");
            var finalMask        = enemyMask | destructibleMask;
            var bulletComponent  = new BulletComponent()
            {
                startPosition = startPosition,
                rotation      = _addDispersion(go.transform.rotation),
                elapsedTime   = 0,
                lifeTime      = .8f,
                collisionMask = finalMask
            };
            var actorComponent = new ActorComponent()
            {
                damage = 5
            };

            bullet.AddComponent(bulletComponent);
            bullet.AddComponent(actorComponent);
            bullet.AddComponent(new ViewComponent());
        }
Exemple #9
0
        public static IEntity CreateEntity(this IPool pool, params IBlueprint[] blueprints)
        {
            var entity = pool.CreateEntity();

            entity.ApplyBlueprints(blueprints);
            return(entity);
        }
        private void _createHitScore(IEntity source, IEntity target)
        {
            var sourceActorComponent = source.GetComponent <ActorComponent>();
            var sourcePosition       = Vector3.zero;

            var viewComponent = source.GetComponent <ViewComponent>();
            var go            = viewComponent.View.gameObject;

            sourcePosition = go.transform.position;

            var entity            = _pool.CreateEntity();
            var hitScoreComponent = new HitScoreComponent()
            {
                elapsedTime    = 0,
                lifeTime       = 0.6f,
                targetPosition = sourcePosition,
                targetEntity   = source,
                step           = 0,
                startPosition  = new Vector2(0, 20),
                text           = sourceActorComponent.damage.ToString()
            };

            entity.AddComponent(hitScoreComponent);
            entity.AddComponent(new ViewComponent());
        }
Exemple #11
0
        public IEntity Create(DestructableTypes type, Vector3 pos, Quaternion rotation, Vector3 scale)
        {
            var vfx  = _pool.CreateEntity(_blueprints[(int)type]());
            var view = vfx.GetComponent <ViewComponent>();
            var tr   = view.GameObject.transform;

            tr.position   = pos;
            tr.rotation   = rotation;
            tr.localScale = scale;
            return(vfx);
        }
        private void _createEnemy(Vector2 startPosition)
        {
            var entity         = _pool.CreateEntity();
            var actorComponent = new ActorComponent()
            {
                maxHealth = 20,
                health    = 20,
                damage    = 20
            };
            var enemyComponent = new EnemyComponent()
            {
                startPosition = startPosition,
                collisionMask = 1 << 9, // Player layer
                    speed     = 5,
                    lastDestinationSetterTime = 0,
                    destinationSetterRateTime = 0.5f
            };

            entity.AddComponent(actorComponent);
            entity.AddComponent(enemyComponent);
            entity.AddComponent(new ViewComponent());
        }
        private void _createDestructible(Vector3Int position)
        {
            var entity         = _pool.CreateEntity();
            var actorComponent = new ActorComponent()
            {
                health = 10
            };
            var destructibleComponent = new DestructibleComponent()
            {
                position = position
            };

            entity.AddComponent(actorComponent);
            entity.AddComponent(destructibleComponent);
        }
Exemple #14
0
        private void _createPlayer()
        {
            var entity          = _pool.CreateEntity();
            var playerComponent = new PlayerComponent()
            {
                lastFireTime = 0,
                fireRateTime = 0.1f
            };
            var actorComponent = new ActorComponent()
            {
                health    = 20,
                maxHealth = 100
            };

            entity.AddComponent(playerComponent);
            entity.AddComponent(actorComponent);
            entity.AddComponent(new ViewComponent());
        }
        private void _createHitScore(IEntity entity, Collider2D collider)
        {
            var coinComponent = entity.GetComponent <CoinComponent>();
            var go            = collider.gameObject;

            var hitScoreEntity    = _pool.CreateEntity();
            var hitScoreComponent = new HitScoreComponent()
            {
                elapsedTime    = 0,
                lifeTime       = 0.6f,
                targetPosition = go.transform.position,
                targetEntity   = entity,
                step           = 0,
                startPosition  = new Vector2(0, 20),
                text           = string.Format("+{0}", coinComponent.score)
            };

            hitScoreEntity.AddComponent(hitScoreComponent);
            hitScoreEntity.AddComponent(new ViewComponent());
        }
Exemple #16
0
        private void _createExplosion(IEntity source)
        {
            if (!source.HasComponent <ViewComponent>())
            {
                return;
            }

            var viewComponent = source.GetComponent <ViewComponent>();
            var go            = viewComponent.View.gameObject;
            var startPosition = go.transform.position;

            var explosionComponent = new ExplosionComponent()
            {
                startPosition = startPosition
            };
            var entity = _pool.CreateEntity();

            entity.AddComponent(explosionComponent);
            entity.AddComponent(new ViewComponent());

            MainThreadDispatcher.StartCoroutine(_removeExplosion(entity));
        }
Exemple #17
0
        public IEntity CreateUI <T>(T blueprint) where T : IBlueprint
        {
            IEntity entity = defaultPool.CreateEntity(blueprint);

            return(entity);
        }