Exemplo n.º 1
0
    public UnitComponent SpawnUnit(Unit unit)
    {
        var prefab     = unitMeta.GetUnitPrefab(unit.type);
        var unitObject = Instantiate(
            prefab,
            VectorUtil.Vector2To3(unit.position, unit.height),
            Quaternion.identity
            );

        unitObject.name = SpawnerComponent.GetUnitObjectName(unit);
        var unitComponent = unitObject.GetComponent <UnitComponent>();

        unitComponent.unit          = unit;
        unitComponent.gameComponent = this.gameComponent;
        unitComponent.Initialize();
        unitComponent.healthBar = this.AttachHealthBar(unitObject, unit);
        unit.OnLinkEffect       = (Unit source, Unit target) =>
        {
            Instantiate(
                this.gameComponent.spawnerComponent.linkEffectPrefab,
                VectorUtil.Vector2To3(source.position, source.height),
                Quaternion.identity
                ).GetComponent <LinkEffectComponent>().Initialize(target);
        };
        return(unitComponent);
    }
 private void Awake()
 {
     m_eventDispatcher = FindObjectOfType <EventDispatcher>();
     m_eventDispatcher.ObjectNotificationEvent += ShowNotification;
     m_CurrentStatus               = GameStatus.Loading;
     m_pool                        = GetComponent <SpawnerComponent>();
     m_sliderInstance.minValue     = 0;
     m_sliderInstance.wholeNumbers = true;
     changeSlide(1, 0);
 }
Exemplo n.º 3
0
 private void RegisterComponent(SpawnerComponent comp)
 {
     if (!comp)
     {
         return;
     }
     comp.FixedUpdateAsObservable()
     .Where(_ => comp.IsActive)
     .Where(_ => CheckForSpawn(comp))
     .Subscribe(_ => SpawnEnemies(comp))
     .AddTo(comp);
     comp.EnemiesLeft = comp.MaxEnemiesToSpawn;
 }
    // Lets you convert the editor data representation to the entity optimal runtime representation
    public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
    {
        var spawnerData = new SpawnerComponent
        {
            // The referenced prefab will be converted due to DeclareReferencedPrefabs.
            // So here we simply map the game object to an entity reference to that prefab.
            entity = conversionSystem.GetPrimaryEntity(prefab),
            countX = countX,
            countY = countY,
            countZ = countZ,
            spaceX = spaceX,
            spaceY = spaceY,
            spaceZ = spaceZ
        };

        dstManager.AddComponentData(entity, spawnerData);
    }
        public override void Update(double dt)
        {
            _spawnCountdown -= dt;
            if (_spawnCountdown <= 0)
            {
                ResetCountdown();

                // get the total number of citizens
                int vacanies      = CountVacanies(World.Entities);
                int population    = World.City.Population;
                int maxPopulation = MaxPopulation(World.Entities);

                // TODO: don't just fill vacancies, check to see if the city is "good" enough for immigrants
                if (population < maxPopulation && vacanies > 0)
                {
                    int spawnIndex             = World.Random.Next(0, _spawners.Count);
                    SpawnerComponent  spawner  = _spawners[spawnIndex].Get <SpawnerComponent>();
                    PositionComponent position = _spawners[spawnIndex].Get <PositionComponent>();

                    string spawnID = spawner.Spawns[World.Random.Next(0, spawner.Spawns.Count)];

                    Entity spawned = (Entity)World.Prototypes[spawnID];

                    if (spawned.HasComponent <PositionComponent>())
                    {
                        PositionComponent aPosition = spawned.Get <PositionComponent>();
                        aPosition.X     = position.X;
                        aPosition.Y     = position.Y;
                        aPosition.Index = position.Index;
                    }
                    else
                    {
                        PositionComponent bPosition = new PositionComponent(position.Position);
                        bPosition.Index = position.Index;
                        spawned.AddComponent(bPosition);
                    }

                    World.Entities.Add(spawned);
                }
            }
        }
Exemplo n.º 6
0
        private void SpawnEnemies(SpawnerComponent comp)
        {
            comp.SpawnTimer = 0;
            var coll        = comp.GetComponent <BoxCollider>();
            var areaToSpawn = new Bounds(comp.transform.position, coll.size);

            var enemiesToSpawn = (comp.InFiniteSpawn) ? comp.SpawnCountPerTick : Math.Min(comp.SpawnCountPerTick, comp.EnemiesLeft);

            if (!comp.InFiniteSpawn)
            {
                comp.EnemiesLeft -= enemiesToSpawn;
            }

            Observable.Range(0, enemiesToSpawn)
            .Subscribe(_ => SpawnEnemy(comp.EnemyToSpawn, comp.Parent, areaToSpawn));

            if (comp.EnemiesLeft == 0 && !comp.InFiniteSpawn)
            {
                comp.IsActive    = false;
                comp.EnemiesLeft = comp.MaxEnemiesToSpawn;
            }
        }
Exemplo n.º 7
0
 // Start is called before the first frame update
 void Start()
 {
     _boxCollider2D    = GetComponent <BoxCollider2D>();
     _spawnerComponent = GetComponent <SpawnerComponent>();
     _spawnerComponent.Activate();
 }
Exemplo n.º 8
0
    // Start is called before the first frame update
    void Start()
    {
        mapGen = new MapGeneratorComponent(width, height);

        char[] map = mapGen.GetMap();

        entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;

        MapTranslationComponent mapTranslationComponent = new MapTranslationComponent(map, entityManager, mesh, StoneMaterial, material, width, height);

        NativeArray <Entity> entityArray = mapTranslationComponent.GetEntityArray();

        //Entity e = entityManager.CreateEntity(typeof(MapBuffer));
        Entity f = entityManager.CreateEntity(typeof(MapEntityBuffer));


        //Setup Neighbour components
        for (int i = 0; i < width * height; i++)
        {
            Entity entity = entityArray[i];
            Tile   tile   = entityManager.GetComponentData <Tile>(entity);

            ////Bug, need to add it every iteration or it gets deallocated
            //DynamicBuffer<MapBuffer> bufferFromEntity = entityManager.GetBuffer<MapBuffer>(e);
            //DynamicBuffer<Tile> tileBuffer = bufferFromEntity.Reinterpret<Tile>();
            //tileBuffer.Add(tile);
            DynamicBuffer <MapEntityBuffer> entityBufferFromEntity = entityManager.GetBuffer <MapEntityBuffer>(f);
            DynamicBuffer <Entity>          entityBuffer           = entityBufferFromEntity.Reinterpret <Entity>();
            entityBuffer.Add(entity);

            //NeighbourTiles neighbours = new NeighbourTiles {
            //    eTile = Entity.Null,
            //    neTile = Entity.Null,
            //    seTile = Entity.Null,
            //    nTile = Entity.Null,
            //    nwTile = Entity.Null,
            //    swTile = Entity.Null,
            //    sTile = Entity.Null,
            //    wTile = Entity.Null
            //};

            //if (tile.coordinates.x - 1 >= 0)
            //{
            //    neighbours.wTile = entityArray[(int)math.floor((tile.coordinates.y) * width + (tile.coordinates.x - 1))];
            //    if (tile.coordinates.y - 1 >= 0)
            //        neighbours.swTile = entityArray[(int)math.floor((tile.coordinates.y - 1) * width + tile.coordinates.x - 1)];
            //    if (tile.coordinates.y + 1 < height)
            //        neighbours.nwTile = entityArray[(int)math.floor((tile.coordinates.y + 1) * width + tile.coordinates.x - 1)];
            //}
            //if (tile.coordinates.x + 1 < width)
            //{

            //    neighbours.eTile = entityArray[(int)math.floor((tile.coordinates.y) * width + (tile.coordinates.x + 1))];
            //    if (tile.coordinates.y - 1 >= 0)
            //        neighbours.seTile = entityArray[(int)math.floor((tile.coordinates.y - 1) * width + tile.coordinates.x + 1)];
            //    if (tile.coordinates.y + 1 < height)
            //        neighbours.neTile = entityArray[(int)math.floor((tile.coordinates.y + 1) * width + tile.coordinates.x + 1)];
            //}
            ////Down
            //if (tile.coordinates.y - 1 >= 0)
            //    neighbours.sTile = entityArray[(int)math.floor((tile.coordinates.y - 1) * width + tile.coordinates.x)];

            ////Up
            //if (tile.coordinates.y + 1 < height)
            //    neighbours.nTile = entityArray[(int)math.floor((tile.coordinates.y + 1) * width + tile.coordinates.x)];

            //entityManager.SetComponentData(entity, neighbours);
        }

        SpawnerComponent spawnerComponent = new SpawnerComponent(entityArray);
    }
Exemplo n.º 9
0
 public void Initialize(Unit unit)
 {
     this.target      = GameObject.Find(SpawnerComponent.GetUnitObjectName(unit)).transform;
     this.initialized = true;
 }
Exemplo n.º 10
0
 private bool CheckForSpawn(SpawnerComponent comp)
 {
     comp.SpawnTimer += Time.fixedDeltaTime;
     return(comp.SpawnTimer > comp.SpawnInterval);
 }