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();
    }
Пример #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;
            }
        }
    }
Пример #3
0
    protected override void OnUpdate()
    {
        var query = GetEntityQuery(
            typeof(ArcherTagComponentData),
            typeof(SquadProjectileLaunchDataSharedComponentData),
            typeof(Translation),
            typeof(Scale),
            typeof(ActionOnAnimationFrameComponentData),
            typeof(ArcherTargetPositionComponentData)
            );
        var sharedIndices   = new NativeHashMap <int, int>(query.CalculateChunkCount(), Allocator.TempJob);
        var detectedActions = new NativeMultiHashMap <int, ShootData>(query.CalculateEntityCount(), Allocator.TempJob);

        var sharedIndicesJH = new SharedIndicesJob()
        {
            launchType    = GetArchetypeChunkSharedComponentType <SquadProjectileLaunchDataSharedComponentData>(),
            sharedIndices = sharedIndices.AsParallelWriter()
        }.Schedule(query);

        var detectActionsJH = new DetectActionSquadJob()
        {
            detectedActions = detectedActions.AsParallelWriter(),
            launchType      = GetArchetypeChunkSharedComponentType <SquadProjectileLaunchDataSharedComponentData>(),
            actionType      = GetArchetypeChunkComponentType <ActionOnAnimationFrameComponentData>(true),
            scaleType       = GetArchetypeChunkComponentType <Scale>(true),
            targetType      = GetArchetypeChunkComponentType <ArcherTargetPositionComponentData>(true),
            translationType = GetArchetypeChunkComponentType <Translation>(true)
        }.Schedule(query);

        sharedIndicesJH.Complete();
        var indices = sharedIndices.GetKeyArray(Allocator.TempJob);

        detectActionsJH.Complete();

        for (int i = 0; i < indices.Length; i++)
        {
            var sharedData = EntityManager.GetSharedComponentData <SquadProjectileLaunchDataSharedComponentData>(indices[i]).data;

            if (sharedData == null)
            {
                throw new ArgumentNullException($"null data of SquadProjectileLaunchDataSharedComponentData");
            }

            var sprite    = sharedData.spriteData;
            var animation = sharedData.animaionData;
            var collision = sharedData.collisionData;
            var render    = sharedData.renderData;
            var scale     = sharedData.renderScaleData;

            detectedActions.IterateForKey(indices[i], (detect) =>
            {
                var launch            = sharedData.launchData;
                launch.targetPosition = detect.targetPosition;
                launch.targetWidth   *= detect.ownerScale;

                LaunchProjectileSystem.Instance.LaunchArrow(
                    detect.ownerPosition,
                    detect.ownerScale,
                    launch,
                    animation,
                    sprite,
                    render,
                    collision,
                    scale,
                    sharedData.castShadows,
                    sharedData.shadowSettings,
                    sharedData.calcShadowsShifts
                    );
            });
        }

        indices.Dispose();
        sharedIndices.Dispose();
        detectedActions.Dispose();
    }