Exemplo n.º 1
0
 private bool IsAnotherPokemonAlreadyExist(Vector2Int index)
 {
     return(pokemonsInBattleIndexes.Contains(index));
 }
Exemplo n.º 2
0
 public void Execute()
 {
     Result.Value = Source.Contains(TestValue);
 }
Exemplo n.º 3
0
        protected override void OnUpdate()
        {
            // Check if we have initialized
            EntityQuery queryGroup = GetEntityQuery(typeof(Initialized));

            if (queryGroup.CalculateEntityCount() > 0)
            {
                return;
            }

            var spawners = new NativeArray <SpawnerInfo>(UninitializedSpawners.CalculateEntityCount(), Allocator.TempJob);

            Entities.WithStoreEntityQueryInField(ref UninitializedSpawners).WithNone <Initialized>()
            .ForEach((int entityInQueryIndex, ref FireGridSpawner spawner) =>
            {
                spawners[entityInQueryIndex] = new SpawnerInfo
                {
                    FirePrefab            = spawner.FirePrefab,
                    RandomSeed            = spawner.RandomSeed,
                    CountX                = spawner.FireCountX,
                    CountZ                = spawner.FireCountZ,
                    InitialHeightVariance = spawner.InitialHeightVariance,
                    TotalFireCount        = spawner.FireCountX * spawner.FireCountZ,
                    Center                = spawner.Origin,
                    StartFireAmount       = spawner.StartFireAmount,
                    StartFireVelocity     = spawner.StartFireVelocity,
                    MinInitialFires       = spawner.MinInitialFires,
                    MaxInitialFires       = spawner.MaxInitialFires,
                };
            }).Run();

            SpawnerInfo spanwerInfoInstance = spawners[0];
            Random      random = new Random((uint)spanwerInfoInstance.RandomSeed);

            foreach (var spawner in spawners)
            {
                EntityManager.Instantiate(spawner.FirePrefab, spawner.TotalFireCount, Allocator.Temp);
            }

            EntityManager.AddComponent <Initialized>(UninitializedSpawners);

            var fireBufferEntity = GetSingletonEntity <FireBuffer>();

            EntityManager.AddComponentData(fireBufferEntity, new FireBufferMetaData
            {
                CountX    = spanwerInfoInstance.CountX,
                CountZ    = spanwerInfoInstance.CountZ,
                TotalSize = spanwerInfoInstance.TotalFireCount
            });

            UnityEngine.Debug.Log($"FireGridSpawner initializing grid [{spanwerInfoInstance.CountX}, {spanwerInfoInstance.CountZ}] Total: {spanwerInfoInstance.TotalFireCount}");

            // Grab Fire buffer
            var gridBufferLookup = GetBufferFromEntity <FireBufferElement>();
            var gridBuffer       = gridBufferLookup[fireBufferEntity];

            gridBuffer.ResizeUninitialized(spanwerInfoInstance.TotalFireCount);
            var gridArray = gridBuffer.AsNativeArray();


            // Start initial fires
            int numberOfFires = random.NextInt(spanwerInfoInstance.MinInitialFires, spanwerInfoInstance.MaxInitialFires);

            NativeArray <int> fireIndiciesArr = new NativeArray <int>(numberOfFires, Allocator.TempJob);

            // Pick indicies of fires that should be started, at random
            for (int i = 0; i < numberOfFires; i++)
            {
                int fireIndex = random.NextInt(0, spanwerInfoInstance.TotalFireCount - 1);
                fireIndiciesArr[i] = fireIndex;
            }

            Entities
            .WithDeallocateOnJobCompletion(fireIndiciesArr)
            .WithDeallocateOnJobCompletion(spawners)
            .ForEach((Entity fireEntity, int entityInQueryIndex, ref Translation translation,
                      ref TemperatureComponent temperature, ref StartHeight height, in BoundsComponent bounds) =>
            {
                for (int i = 0; i < spawners.Length; ++i)
                {
                    var spawner = spawners[i];
                    if (entityInQueryIndex < spawner.TotalFireCount)
                    {
                        int x = entityInQueryIndex % spawner.CountX;
                        int z = entityInQueryIndex / spawner.CountZ;

                        // Start pos
                        var posX = spawner.Center.x + bounds.SizeXZ * (x - (spawner.CountX - 1) / 2);
                        var posZ = spawner.Center.z + bounds.SizeXZ * (z - (spawner.CountZ - 1) / 2);

                        // Add random offset on y to debug that the grid spacing is correct
                        var posY = random.NextFloat(-spawner.InitialHeightVariance, spawner.InitialHeightVariance) + spawner.Center.y;

                        height.Value    = posY;
                        height.Variance = random.NextFloat(0.055f, 0.065f);

                        translation.Value = new float3(posX, posY, posZ);

                        // If we should start a fire, start it
                        if (fireIndiciesArr.Contains(entityInQueryIndex))
                        {
                            temperature.Value    = spawner.StartFireAmount;
                            temperature.Velocity = spawner.StartFireVelocity;
                        }

                        // Set initial fire params and variance
                        temperature.StartVelocity    = spawner.StartFireVelocity;
                        temperature.IgnitionVariance = random.NextFloat(0f, 0.5f);
                        temperature.GridIndex        = entityInQueryIndex;

                        gridArray[entityInQueryIndex] = new FireBufferElement {
                            FireEntity = fireEntity
                        };
                        break;
                    }
                    entityInQueryIndex -= spawner.TotalFireCount;
                }
            }).ScheduleParallel();
        }
Exemplo n.º 4
0
        public void Execute(Entity entity, int index, [ReadOnly] ref Data createrData, [ReadOnly] ref NewDataTag tag)
        {
            //0.代码生成预设,这样可以优化性能
            Entity hexCellPrefab = CommandBuffer.CreateEntity(index);

            CommandBuffer.AddComponent <Cell>(index, hexCellPrefab);
            CommandBuffer.AddComponent <Neighbors>(index, hexCellPrefab);
            CommandBuffer.AddComponent <NeighborsIndex>(index, hexCellPrefab);
            CommandBuffer.AddComponent <ChunkData>(index, hexCellPrefab);
            CommandBuffer.AddComponent <River>(index, hexCellPrefab);
            //1.添加颜色数组,这个数组以后从服务器获取,然后传到这里来处理
            Random random         = new Random(1208905299U);
            int    cellCountX     = createrData.CellCountX;
            int    cellCountZ     = createrData.CellCountZ;
            int    totalCellCount = cellCountZ * cellCountX;
            //保存单元颜色的原生数组
            NativeArray <Color> Colors = new NativeArray <Color>(totalCellCount, Allocator.Temp);
            //保存单元海拔的原生数组
            NativeArray <int> Elevations = new NativeArray <int>(totalCellCount, Allocator.Temp);
            //河流的源头
            NativeList <int> riverSources = new NativeList <int>(totalCellCount / 12, Allocator.Temp);
            //流入的河流索引
            NativeArray <int> riverIn = new NativeArray <int>(totalCellCount, Allocator.Temp);

            Colors[0]     = Color.green;//使第一个单元成为河流的源头
            Elevations[0] = 5;
            riverSources.Add(0);
            //后面将从服务器获取这些数据,现在暂时随机生成
            for (int i = 1; i < cellCountZ * cellCountX; i++)
            {
                Colors[i] = new Color(random.NextFloat(), random.NextFloat(), random.NextFloat());
                int elevtion = random.NextInt(6);
                Elevations[i] = elevtion;
                if (elevtion >= 5)
                {
                    riverSources.Add(i);
                }
            }

            for (int z = 0, i = 0; z < cellCountZ; z++)
            {
                for (int x = 0; x < cellCountX; x++)
                {
                    //2.实例化
                    var instance = CommandBuffer.Instantiate(index, hexCellPrefab);

                    //3.计算阵列对应的六边形单元坐标
                    float _x = (x + z * 0.5f - z / 2) * (HexMetrics.InnerRadius * 2f);
                    float _y = Elevations[i] * HexMetrics.elevationStep;
                    float _z = z * (HexMetrics.OuterRadius * 1.5f);

                    //4.计算当前单元所在六个方向的邻居单元颜色
                    Color[] blendColors = new Color[6];
                    int[]   directions  = new int[6];
                    //当前单元的颜色
                    Color color = Colors[i];
                    //邻居单元的颜色
                    Color neighbor  = color;
                    int   direction = int.MinValue;
                    //判断当前单元所在行数是否为偶数
                    bool ifEven = (z & 1) == 0;
                    //当前单元是否处于行尾
                    bool ifEnd = (i + 1) == (z + 1) * cellCountX;
                    //是否处于行首
                    bool ifStart = i == z * cellCountX;
                    //是否最后一行
                    bool isLastRow = (z == (cellCountZ - 1));

                    //0=东北:NE
                    if (!isLastRow) //非最末行
                    {
                        if (ifEven) //偶数行
                        {
                            neighbor  = Colors[i + cellCountX];
                            direction = i + cellCountX;
                        }
                        else
                        {
                            if (!ifEnd)//最末尾没有相邻的单元
                            {
                                neighbor  = (Colors[i + cellCountX + 1]);
                                direction = i + cellCountX + 1;
                            }
                        }
                    }

                    directions[0]  = direction;
                    blendColors[0] = neighbor;
                    direction      = int.MinValue;
                    //颜色混合1 东:E
                    if (ifEnd)
                    {
                        //如果在地图行尾,没有东邻居
                        neighbor = color;
                    }
                    else
                    {
                        neighbor  = (Colors[i + 1]);
                        direction = i + 1;
                    }

                    directions[1]  = direction;
                    blendColors[1] = neighbor;
                    direction      = int.MinValue;
                    //东南2:SE
                    neighbor = color;
                    if (i >= cellCountX)
                    {
                        if (ifEven)
                        {
                            neighbor  = (Colors[i - cellCountX]);
                            direction = i - cellCountX;
                        }
                        else
                        {
                            if (!ifEnd)
                            {
                                neighbor  = (Colors[i - cellCountX + 1]);
                                direction = i - cellCountX + 1;
                            }
                        }
                    }
                    blendColors[2] = neighbor;
                    directions[2]  = direction;
                    direction      = int.MinValue;
                    //西南3:SW
                    if (i < cellCountX)
                    {
                        neighbor = color;
                    }
                    else
                    {
                        if (ifEven)
                        {
                            if (ifStart)
                            {
                                neighbor = color;
                            }
                            else
                            {
                                neighbor  = (Colors[i - cellCountX - 1]);
                                direction = i - cellCountX - 1;
                            }
                        }
                        else
                        {
                            neighbor  = (Colors[i - cellCountX]);
                            direction = i - cellCountX;
                        }
                    }

                    directions[3]  = direction;
                    blendColors[3] = neighbor;
                    direction      = int.MinValue;
                    //西4:W
                    if (ifStart)
                    {
                        //如果在地图起始位置,没有西邻居
                        neighbor = color;
                    }
                    else
                    {
                        neighbor  = (Colors[i - 1]);
                        direction = i - 1;
                    }
                    blendColors[4] = neighbor;
                    directions[4]  = direction;
                    direction      = int.MinValue;
                    //5西北:NW
                    if (isLastRow)
                    {
                        neighbor = color;
                    }
                    else
                    {
                        if (ifEven)
                        {
                            if (ifStart)
                            {
                                neighbor = color;
                            }
                            else
                            {
                                neighbor  = (Colors[i + cellCountX - 1]);
                                direction = i + cellCountX - 1;
                            }
                        }
                        else
                        {
                            neighbor  = (Colors[i + cellCountX]);
                            direction = i + cellCountX;
                        }
                    }

                    directions[5]  = direction;
                    blendColors[5] = neighbor;
                    //初始化河流数据
                    bool hasRiver         = false;
                    bool hasOutgoingRiver = false;
                    bool hasIncomingRiver = false;
                    int  incomingRiver    = int.MinValue;
                    int  outgoingRiver    = int.MinValue;
                    if (riverSources.Contains(i))
                    {
                        hasRiver = true;
                        int lastE = int.MinValue;
                        for (int j = 0; j < 6; j++)
                        {
                            if (directions[j] != int.MinValue)
                            {
                                int elevationR = Elevations[directions[j]];
                                if (i < directions[j] && !riverSources.Contains(directions[j]) && elevationR <= Elevations[i] && elevationR > lastE)
                                {
                                    hasOutgoingRiver = true;
                                    outgoingRiver    = directions[j];
                                    lastE            = elevationR;
                                }
                                if (riverIn.Contains(directions[j]))
                                {
                                    if (directions[j] == riverIn[i])
                                    {
                                        incomingRiver    = riverIn[i];
                                        hasIncomingRiver = true;
                                    }
                                }
                            }
                        }

                        if (hasOutgoingRiver)
                        {
                            riverIn[outgoingRiver] = i;
                            riverSources.Add(outgoingRiver);
                        }
                        else
                        {
                            hasRiver = hasIncomingRiver;
                        }
                    }

                    //5.设置每个六边形单元的数据
                    CommandBuffer.SetComponent(index, instance, new Cell
                    {
                        Index     = i,
                        Color     = color,
                        Position  = new Vector3(_x, _y, _z),
                        Elevation = Elevations[i],
                        HasRiver  = hasRiver
                    });
                    CommandBuffer.SetComponent(index, instance, new River
                    {
                        HasIncomingRiver = hasIncomingRiver,
                        HasOutgoingRiver = hasOutgoingRiver,
                        IncomingRiver    = incomingRiver,
                        OutgoingRiver    = outgoingRiver
                    });
                    CommandBuffer.SetComponent(index, instance, new Neighbors
                    {
                        NE          = blendColors[0],
                        E           = blendColors[1],
                        SE          = blendColors[2],
                        SW          = blendColors[3],
                        W           = blendColors[4],
                        NW          = blendColors[5],
                        NEElevation = Elevations[directions[0] == int.MinValue ? 0 : directions[0]],
                        EElevation  = Elevations[directions[1] == int.MinValue ? 0 : directions[1]],
                        SEElevation = Elevations[directions[2] == int.MinValue ? 0 : directions[2]],
                        SWElevation = Elevations[directions[3] == int.MinValue ? 0 : directions[3]],
                        WElevation  = Elevations[directions[4] == int.MinValue ? 0 : directions[4]],
                        NWElevation = Elevations[directions[5] == int.MinValue ? 0 : directions[5]]
                    });
                    CommandBuffer.SetComponent(index, instance, new NeighborsIndex
                    {
                        NEIndex = directions[0],
                        EIndex  = directions[1],
                        SEIndex = directions[2],
                        SWIndex = directions[3],
                        WIndex  = directions[4],
                        NWIndex = directions[5]
                    });
                    int chunkX = x / HexMetrics.chunkSizeX;
                    int chunkZ = z / HexMetrics.chunkSizeZ;
                    int localX = x - chunkX * HexMetrics.chunkSizeX;
                    int localZ = z - chunkZ * HexMetrics.chunkSizeZ;
                    CommandBuffer.SetComponent(index, instance, new ChunkData
                    {
                        ChunkId    = chunkX + chunkZ * createrData.ChunkCountX,
                        ChunkIndex = localX + localZ * HexMetrics.chunkSizeX,
                        CellIndex  = i
                    });
                    //6.添加新数据标签NewDataTag组件,激活CellSystem来处理新的数据
                    CommandBuffer.AddComponent <NewDataTag>(index, instance);
                    i++;
                }
            }

            //7.摧毁使用完的预设,节约内存资源
            CommandBuffer.DestroyEntity(index, hexCellPrefab);
            CommandBuffer.RemoveComponent <NewDataTag>(index, entity);
            Colors.Dispose();
            Elevations.Dispose();
            riverSources.Dispose();
            riverIn.Dispose();
        }
    protected override void OnUpdate()
    {
        Entities.WithAll <CurrentTurn>().ForEach((Entity entity, ref CurrentTurn currentTurn) => {
            currentTurn.turnOrder = this.currentTurn;
        });

        Entities.ForEach((DynamicBuffer <EntityBuffer> buffer) =>
        {
            entitiesBuffer = buffer.Reinterpret <Entity>();
        });

        Entities.ForEach((DynamicBuffer <PlayerEntityBuffer> buffer) =>
        {
            playerBuffer = buffer.Reinterpret <Entity>();
        });

        Entities.ForEach((DynamicBuffer <AiBuffer> buffer) =>
        {
            aiBuffer = buffer.Reinterpret <Entity>();
        });

        Entities.WithAll <SSoldier>().ForEach((Entity entity) =>
        {
            sortedArray = entitiesBuffer.AsNativeArray();
            if (!sortedArray.Contains(entity))
            {
                entitiesBuffer.Add(entity);
            }
        });

        Entities.WithAll <AIComponent, SSoldier>().ForEach((Entity entity) =>
        {
            sortedArray = aiBuffer.AsNativeArray();
            if (!sortedArray.Contains(entity))
            {
                aiBuffer.Add(entity);
            }
        });

        Entities.WithNone <AIComponent>().WithAll <SSoldier>().ForEach((Entity entity) =>
        {
            sortedArray = playerBuffer.AsNativeArray();
            if (!sortedArray.Contains(entity))
            {
                playerBuffer.Add(entity);
            }
        });

        if (currentTurn == TurnOrder.Player1)
        {
            var selectedUnitCount = Entities.WithNone <AIComponent>().WithAll <AwaitActionFlag>().ToEntityQuery().CalculateEntityCount();
            if (selectedUnitCount == 0)
            {
                currentTurn = TurnOrder.AITurn;

                //DEBUG stuff
                Entities.WithNone <AIComponent>().WithAll <SSoldier>().ForEach((Entity entity, ref SSoldier soldier) =>
                {
                    PostUpdateCommands.AddComponent(entity, new AwaitActionFlag {
                    });
                    soldier.Movement = 4;
                });
            }
        }

        if (currentTurn == TurnOrder.AITurn)
        {
            var selectedUnitCount = Entities.WithAll <AIComponent, AwaitActionFlag>().ToEntityQuery().CalculateEntityCount();
            if (selectedUnitCount == 0)
            {
                currentTurn = TurnOrder.Player1;

                //DEBUG Stuff
                Entities.WithAll <AIComponent, SSoldier>().ForEach((Entity entity, ref SSoldier soldier) =>
                {
                    PostUpdateCommands.AddComponent(entity, new AwaitActionFlag {
                    });
                    soldier.Movement = 4;
                });
            }
        }

        //sortedArray = turnOrderBuffer.AsNativeArray();
        //sortedArray.Sort(new TurnOrdercomparer { entityManager = EntityManager });


        ////Entities.WithAll<AIComponent, SSoldier>().ForEach((Entity entity) => {
        ////    PostUpdateCommands.RemoveComponent<ReadyToHandle>(entity);
        ////});

        //var selectedUnitCount = Entities.WithAll<UnitSelected>().ToEntityQuery().CalculateEntityCount();
        //if (selectedUnitCount == 0 && turnOrderBuffer.Length >= 0)
        //{
        //    PostUpdateCommands.AddComponent<UnitSelected>(turnOrderBuffer[0]);
        //}

        ////Query for waiting
        //var waiting = Entities.WithAll<AwaitActionFlag>().ToEntityQuery().CalculateEntityCount();
        //if (waiting == 0)
        //{
        //    //TODO - Query for all soldiers, should generalize this later with a more specialized component to designate actual game actors
        //    Entities.WithAll<AIComponent, SSoldier>().ForEach((Entity entity, ref SSoldier soldier) => {
        //        PostUpdateCommands.AddComponent<AwaitActionFlag>(entity);
        //        //Reset all the atributes to the desired values
        //        soldier.Movement = 4;
        //    });

        //    Entities.WithNone<AIComponent>().WithAll<SSoldier>().ForEach((Entity entity, ref SSoldier soldier) => {
        //        PostUpdateCommands.AddComponent<AwaitActionFlag>(entity);
        //        //Reset all the atributes to the desired values
        //        soldier.Movement = 4;
        //    });

        //}
    }
Exemplo n.º 6
0
    // Update is called once per frame
    void Update()
    {
        //trackTarget = CreatureBrowserMono.SelectedCreature;
        if (!initialized)
        {
            initialize(trackTarget);
        }
        else
        {
            if (!em.Exists(trackTarget))
            {
                initialize(trackTarget);
            }
            if (Input.GetKeyUp(KeyCode.A))
            {
                NextTarget(true);
            }
            else if (Input.GetKeyUp(KeyCode.D))
            {
                NextTarget(false);
            }
            NativeArray <Entity> allEntities = em.GetAllEntities(Allocator.TempJob);
            if (!allEntities.Contains(trackTarget))
            {
                Debug.Log("Entity not in array");
                NextTarget(false);
                allEntities.Dispose();
                return;
            }
            allEntities.Dispose();

            Translation targetTrans = new Translation
            {
                Value = float3.zero
            };

            try
            {
                targetTrans = em.GetComponentData <Translation>(trackTarget);
            }
            catch (ArgumentException ex)
            {
                NextTarget(false);
                return;
            }

            if (double.IsNaN(targetTrans.Value.x))
            {
                return;
            }
            Rotation     targetRot  = em.GetComponentData <Rotation>(trackTarget);
            LocalToWorld targetLTW  = em.GetComponentData <LocalToWorld>(trackTarget);
            Target       targetData = em.GetComponentData <Target>(trackTarget);
            transform.position = targetTrans.Value - targetLTW.Forward * FOLLOWDISTANCEZ + new float3(0, 1, 0) * FOLLOWDISTANCEY;

            if (targetData.Entity == Entity.Null || IgnoreTargets)
            {
                transform.LookAt(targetTrans.Value);
            }
            else
            {
                transform.LookAt(targetData.Position);
            }
        }
    }