public void Launch()
    {
        var manager = World.Active.EntityManager;

        var query = manager.CreateEntityQuery(
            typeof(ArcherTagComponentData),
            typeof(AnimationPauseComponentData),
            typeof(SquadTagSharedComponentData)
            );

        var entities = new NativeMultiHashMap <int, Entity>(query.CalculateEntityCount(), Allocator.TempJob);

        new SharedIndicesJob()
        {
            entities     = entities.AsParallelWriter(),
            squadTagType = manager.GetArchetypeChunkSharedComponentType <SquadTagSharedComponentData>(),
            entityType   = manager.GetArchetypeChunkEntityType()
        }.Schedule(query).Complete();

        var keys = entities.GetUniqueKeys(Allocator.TempJob);

        for (int i = 0; i < keys.Length; i++)
        {
            var squad = manager.GetSharedComponentData <SquadTagSharedComponentData>(keys[i]);
            if (squad.id.value != spawner.SquadId)
            {
                continue;
            }

            entities.IterateForKey(keys[i], (val) =>
            {
                var pause = manager.GetComponentData <AnimationPauseComponentData>(val);

                var spread          = pause.pauseData.pauseSpread;
                pause.timerToResume = Random.value * spread;

                manager.SetComponentData(val, pause);
            });

            break;
        }

        keys.Dispose();
        entities.Dispose();
    }
Example #2
0
    protected override void OnUpdate()
    {
        var query = GetEntityQuery(
            typeof(SquadTagSharedComponentData),
            typeof(SquadComponentData),
            typeof(LinearMovementComponentData)
            );
        var chunkCount  = query.CalculateChunkCount();
        var entityCount = query.CalculateEntityCount();

        chunkCountDataBySharedIndex.Dispose();
        chunkCountDataBySharedIndex = new NativeMultiHashMap <int, ChunkSquadCountData>(chunkCount, Allocator.TempJob);
        indicesInSquadBySharedIndices.Dispose();
        indicesInSquadBySharedIndices = new NativeMultiHashMap <int, int>(entityCount, Allocator.TempJob);
        //получим индексы SquadTagSharedComponentData
        //и количество ентити в каждом чанке
        //а также,уже установленные индексы в отрядах
        var sharedIndicesJob = new SharedIndicesJobChunk()
        {
            chunkCountDataBySharedIndex   = chunkCountDataBySharedIndex.AsParallelWriter(),
            indicesInSquadBySharedIndices = indicesInSquadBySharedIndices.AsParallelWriter(),
            squadTagType  = GetArchetypeChunkSharedComponentType <SquadTagSharedComponentData>(),
            squadDataType = GetArchetypeChunkComponentType <SquadComponentData>()
        }.Schedule(query);

        sharedIndicesJob.Complete();

        sharedIndices.Dispose();
        sharedIndices = chunkCountDataBySharedIndex.GetUniqueKeys(Allocator.TempJob);

        //для всех отрядов получаем новые индексы
        entityCountBySharedIndex.Dispose();
        entityCountBySharedIndex = new NativeHashMap <int, int>(sharedIndices.Length, Allocator.TempJob);
        for (int i = 0; i < sharedIndices.Length; i++)
        {
            var key = sharedIndices[i];

            var maxCntForSharedIndex = 0;
            chunkCountDataBySharedIndex.IterateForKey(key, (ival) =>
            {
                maxCntForSharedIndex += ival.entityCount;
            });
            entityCountBySharedIndex.TryAdd(key, maxCntForSharedIndex);
        }
        //устанавливаем кол-во человек в отряде пока идёт формирование новых индексов
        for (int i = 0; i < sharedIndices.Length; i++)
        {
            var sharedData = EntityManager.GetSharedComponentData <SquadTagSharedComponentData>(sharedIndices[i]);
            int cnt        = 0;
            if (!entityCountBySharedIndex.TryGetValue(sharedIndices[i], out cnt))
            {
                continue;
            }
            if (sharedData.unitCount.value != cnt)
            {
                sharedData.unitCount.value = cnt;
                sharedData.unitCount.valueChangedEventFlag = true;
            }
            else
            {
                sharedData.unitCount.valueChangedEventFlag = false;
            }
        }
    }