Ejemplo n.º 1
0
        protected override void OnUpdate()
        {
            var spawnTimer = _spawnCapsuleData.SpawnTimer -= Time.DeltaTime;

            if (spawnTimer <= 0)
            {
                var newEntity = EntityManager.Instantiate(_spawnCapsuleData.EntityPrefab);

                var newTranslation = new Translation()
                {
                    Value = _spawnCapsuleData.RandomSpawnPos
                };

                EntityManager.SetComponentData(newEntity, newTranslation);
                _spawnCapsuleData.SpawnTimer = _spawnCapsuleData.SpawnInterval;
            }

            if (Input.GetKeyDown(_spawnCapsuleData.FunKey))
            {
                var newSpawnData = new SpawnCapsuleData()
                {
                    EntityPrefab     = _spawnCapsuleData.EntityPrefab,
                    FunKey           = _spawnCapsuleData.FunKey,
                    MinSpawnPosition = new float3(12.5f, 0.8f, 12.5f),
                    MaxSpawnPosition = new float3(12.5f, 0.8f, 12.5f),
                    Random           = Random.CreateFromIndex(1),
                    SpawnInterval    = 0.01f,
                    SpawnTimer       = 0.01f,
                };

                SetSingleton(newSpawnData);
                _spawnCapsuleData = GetSingleton <SpawnCapsuleData>();
            }
        }
Ejemplo n.º 2
0
        private void Awake()
        {
            #region spawn
            _particles = new List <Clay>(_spawnOnStart);
            var random = Random.CreateFromIndex((uint)DateTime.Now.Millisecond);

            for (int i = 0; i < _spawnOnStart; i++)
            {
                var newParticle = Instantiate(_particlePrefab);

                var randomOutput = (Vector3)random.NextFloat3() - new Vector3(0.5f, 0.5f, 0.5f);
                var startingPos  = randomOutput * _radiusToSpawnIn;
                newParticle.transform.position = startingPos + transform.position;

                newParticle.ParticlePositions = new Vector4[_maxParticlesToSimulate];

                _particles.Add(newParticle);
            }
            #endregion

            _particlePositions   = new NativeArray <Vector3>(_particles.Count, Allocator.Persistent);
            _toMove              = new NativeArray <Vector3>(_particles.Count, Allocator.Persistent);
            _closestSpheres      = new NativeArray <Vector3>(_particles.Count * _maxParticlesToSimulate, Allocator.Persistent);
            _closestSpheresCount = new NativeArray <int>(_particles.Count, Allocator.Persistent);

            _octree = new Octree(_octSettings, _particles.Count);

            for (int i = 0; i < _particles.Count; i++)
            {
                _particlePositions[i] = _particles[i].transform.position;
            }
        }
Ejemplo n.º 3
0
        public Entity CreateWorker(WorkerConfig workerConfig, int2 position)
        {
            float3 worldPos = blockGroupSystem.VisualOrigin + new float3(position.x, 0, position.y);

            Entity block = blockGroupSystem.GetBlock(position);

            Entity entity = CreateBaseEntity(worldPos);

            Color color = workerConfig.Color;

            EntityManager.AddComponentData(entity, new Worker()
            {
                Ability        = workerConfig.ability,
                CurrentBlock   = block,
                Frequency      = workerConfig.frequency,
                SizeLossPerHit = workerConfig.sizeLossPerHit,
                Radius         = workerConfig.radius,
                MaxBounces     = workerConfig.maxBounces,
                MarkDuration   = workerConfig.markDuration,
                Timer          = 0.75f,
                Color          = new float4(color.r, color.g, color.b, color.a)
            });

            EntityManager.AddComponentData(entity, new DestinationPoint()
            {
                Value = position, PreviousPoint = position
            });
            EntityManager.AddComponentData(entity, new VerticalLimit()
            {
                FlightHeight = Random.CreateFromIndex((uint)Time.ElapsedTime).NextInt(5, 8)
            });
            EntityManager.AddComponentData(entity, new NonUniformScale()
            {
                Value = workerConfig.size
            });
            EntityManager.AddComponentData(entity, new DrillPower()
            {
                Amount = workerConfig.power, Frequency = workerConfig.frequency
            });
            EntityManager.AddComponentData(entity, new WorkerAnimations()
            {
                Bounce = bounceCurve, Move = moveCurve
            });
            RenderMeshUtility.AddComponents(entity, EntityManager, cachedMeshDescriptions[workerConfig]);
            EntityManager.SetComponentData(entity, new Translation()
            {
                Value = blockGroupSystem.ToWorldPoint(position, 0)
            });
            return(entity);
        }
        protected override void OnUpdate()
        {
            // remaining frames of the current iteration
            var remaining_frames = GetSingleton <has_current_iteration>().remaining_frames;

            // randomly determine entities to destroy. Gather them in the destroyed_entities list.
            var subjects_count = subjects_query.CalculateEntityCount();

            using var destroyed_entities = new NativeList <Entity>(subjects_count, TempJob);
            var j1 = Entities.WithName("EnqueueDeletes")
                     .WithAll <is_a_test_subject>()
                     .ForEach((int entityInQueryIndex, Entity e) => {
                if (hash(uint2((uint)entityInQueryIndex, remaining_frames)) < (uint.MaxValue / 4 * 3))
                {
                    destroyed_entities.AddNoResize(e);
                }
            })
                     .WithStoreEntityQueryInField(ref subjects_query)
                     .Schedule(Dependency);

            // gather entities to spawn, generate positions for them
            var count = spawns_query.CalculateEntityCount();

            using var spawn_prefabs = new NativeList <Entity>(count, TempJob);
            using var spawn_counts  = new NativeList <int>(count, TempJob);
            using var positions     = new NativeList <float2>(count * 100, TempJob);
            var j2 = Entities.WithName("GeneratePositions")
                     .ForEach((int entityInQueryIndex, in spawns spawns, in Translation translation) => {
                var current_prefab_i = spawn_prefabs.Length - 1;
                var instance_count   = (int)spawns.count_per_fame;
                var prefab           = spawns.prefab;
                if (current_prefab_i != -1 && spawn_prefabs[current_prefab_i] == prefab)
                {
                    spawn_counts.get_ref(current_prefab_i) += instance_count;
                }
                else
                {
                    spawn_prefabs.Add(prefab);
                    spawn_counts.Add(instance_count);
                }

                var spawn_hash = hash(uint2((uint)entityInQueryIndex, remaining_frames));
                var random     = Random.CreateFromIndex(spawn_hash);
                for (var i = 0; i < instance_count; i++)
                {
                    var position = translation.Value.xz + random.NextFloat2(new float2(-100, -100), new float2(100, 100));
                    positions.Add(position);
                }
            })
Ejemplo n.º 5
0
    //returns true if it has successfully initialized
    public static bool Init(Vector2Int mapSize, Vector2Int gridSize, [NotNull] Node[,] map, NativeArray <JobNode> jobMap, uint seed = 666505999)
    {
        if (HasInit)
        {
            return(false);
        }
        if (mapSize.x == 0 || mapSize.y == 0)
        {
            return(false);
        }

        _mapSize   = mapSize;
        _gridSize  = gridSize;
        NodeMap    = map;
        JobNodeMap = jobMap;

        _random = Random.CreateFromIndex(seed);

        HasInit = true;
        return(true);
    }
Ejemplo n.º 6
0
        protected override void OnUpdate()
        {
            if (groupSystem.IsReady == false)
            {
                return;
            }

            var map = groupSystem.BlocksMap;

            EntityCommandBuffer buffer = commandBufferSystem.CreateCommandBuffer();

            ComponentDataFromEntity <Depth> depthLookup = GetComponentDataFromEntity <Depth>();

            float time = (float)Time.ElapsedTime;

            float deltaTime = Time.DeltaTime;
            var   size      = groupSystem.GroupSize;

            NativeHashMap <int2, int> checkMap = new
                                                 NativeHashMap <int2, int>(groupSystem.GroupSize.x * groupSystem.GroupSize.y,
                                                                           Allocator.TempJob);

            NativeList <int2> pointBuffer = new
                                            NativeList <int2>(groupSystem.GroupSize.x * groupSystem.GroupSize.y,
                                                              Allocator.TempJob);

            var    rand            = Random.CreateFromIndex((uint)Time.ElapsedTime);;
            var    isDrilledLookup = GetComponentDataFromEntity <IsBeingDrilled>();
            var    markLookup      = GetComponentDataFromEntity <Mark>();
            float3 origin          = groupSystem.VisualOrigin;

            float scaleMultiplierThreshold = 0.25f;

            Dependency = Entities.WithReadOnly(depthLookup)
                         .ForEach((Entity entity,
                                   ref Worker worker,
                                   ref Translation translation,
                                   ref NonUniformScale scale,
                                   ref DestinationPoint destination,
                                   ref VerticalLimit verticalLimit,
                                   ref DrillPower power,
                                   in WorkerAnimations animations) =>
            {
                float drillTime = worker.Timer;

                bool isBouncing = power.Bounces < worker.MaxBounces;

                if (isBouncing == false && worker.Timer >= 1)
                {
                    power.Bounces = 0;
                }

                float y =
                    verticalLimit.Value + CurveUtil.Evaluate(ref animations.Bounce.Value.Keyframes, drillTime) * verticalLimit.FlightHeight * scale.Value.x;

                if (isBouncing == false)
                {
                    var t         = CurveUtil.Evaluate(ref animations.Move.Value.Keyframes, drillTime);
                    float3 newPos = origin +
                                    math.lerp(
                        new float3(destination.PreviousPoint.x, y, destination.PreviousPoint.y),
                        new float3(destination.Value.x, y, destination.Value.y), t);
                    translation.Value = newPos;
                }
                else
                {
                    translation.Value = new float3(translation.Value.x, y, translation.Value.z);
                }

                if (worker.Timer >= 1 && isBouncing)
                {
                    worker.Timer         = 0;
                    float newScale       = scale.Value.x - worker.SizeLossPerHit;
                    scale.Value          = new float3(newScale, newScale, newScale);
                    Depth depth          = depthLookup[worker.CurrentBlock];
                    var centerMarkAmount = power.Amount * math.max(scale.Value.x, 0.1f);
                    int2 center          = destination.Value;
                    verticalLimit.Value  = -(depth.Value + centerMarkAmount);

                    LeaveMark(size, center, buffer, centerMarkAmount, map,
                              depthLookup, worker);

                    if (worker.Radius > 0)
                    {
                        float factor = 0.8f;

                        LeaveMark(size, new int2(center.x + 1, center.y), buffer, power.Amount * factor, map,
                                  depthLookup, worker);

                        LeaveMark(size, new int2(center.x - 1, center.y), buffer, power.Amount * factor, map,
                                  depthLookup, worker);

                        LeaveMark(size, new int2(center.x, center.y + 1), buffer, power.Amount * factor, map,
                                  depthLookup, worker);

                        LeaveMark(size, new int2(center.x, center.y - 1), buffer, power.Amount * factor, map,
                                  depthLookup, worker);
                    }

                    if (worker.Radius > 1)
                    {
                        float factor = 0.6f;

                        LeaveMark(size, new int2(center.x + 1, center.y - 1), buffer, power.Amount * factor, map,
                                  depthLookup, worker);

                        LeaveMark(size, new int2(center.x - 1, center.y - 1), buffer, power.Amount * factor, map,
                                  depthLookup, worker);

                        LeaveMark(size, new int2(center.x + 1, center.y + 1), buffer, power.Amount * factor, map,
                                  depthLookup, worker);

                        LeaveMark(size, new int2(center.x - 1, center.y + 1), buffer, power.Amount * factor, map,
                                  depthLookup, worker);
                    }

                    if (worker.Radius > 2)
                    {
                        float spread = 0.4f;

                        LeaveMark(size, new int2(center.x + 2, center.y), buffer, power.Amount * spread, map,
                                  depthLookup, worker);

                        LeaveMark(size, new int2(center.x, center.y - 2), buffer, power.Amount * spread, map,
                                  depthLookup, worker);

                        LeaveMark(size, new int2(center.x - 2, center.y), buffer, power.Amount * spread, map,
                                  depthLookup, worker);

                        LeaveMark(size, new int2(center.x, center.y + 2), buffer, power.Amount * spread, map,
                                  depthLookup, worker);
                    }

                    if (worker.Radius > 3)
                    {
                        float spread = 0.2f;

                        LeaveMark(size, new int2(center.x - 1, center.y + 2), buffer, power.Amount * spread, map,
                                  depthLookup, worker);

                        LeaveMark(size, new int2(center.x - 2, center.y + 1), buffer, power.Amount * spread, map,
                                  depthLookup, worker);

                        LeaveMark(size, new int2(center.x - 2, center.y - 1), buffer, power.Amount * spread, map,
                                  depthLookup, worker);

                        LeaveMark(size, new int2(center.x - 1, center.y - 2), buffer, power.Amount * spread, map,
                                  depthLookup, worker);

                        LeaveMark(size, new int2(center.x + 1, center.y - 2), buffer, power.Amount * spread, map,
                                  depthLookup, worker);

                        LeaveMark(size, new int2(center.x + 2, center.y - 1), buffer, power.Amount * spread, map,
                                  depthLookup, worker);

                        LeaveMark(size, new int2(center.x + 2, center.y + 1), buffer, power.Amount * spread, map,
                                  depthLookup, worker);

                        LeaveMark(size, new int2(center.x + 1, center.y + 2), buffer, power.Amount * spread, map,
                                  depthLookup, worker);
                    }


                    if (worker.Radius > 4)
                    {
                        float spread = 0.1f;

                        LeaveMark(size, new int2(center.x - 3, center.y), buffer, power.Amount * spread, map,
                                  depthLookup, worker);

                        LeaveMark(size, new int2(center.x + 3, center.y), buffer, power.Amount * spread, map,
                                  depthLookup, worker);

                        LeaveMark(size, new int2(center.x, center.y - 3), buffer, power.Amount * spread, map,
                                  depthLookup, worker);

                        LeaveMark(size, new int2(center.x, center.y + 3), buffer, power.Amount * spread, map,
                                  depthLookup, worker);
                    }

                    power.Bounces++;

                    if (power.Bounces >= worker.MaxBounces)
                    {
                        if (BlockUtil.GetClosestBlockOnSameLevel(rand, destination.Value, depthLookup, isDrilledLookup, map, checkMap, pointBuffer, size, out int2 next))
                        {
                            buffer.RemoveComponent <IsBeingDrilled>(worker.CurrentBlock);
                            destination.PreviousPoint = destination.Value;
                            destination.Value         = next;
                            worker.CurrentBlock       = map[next];
                        }
                    }

                    if (newScale <= 0.1f)
                    {
                        buffer.DestroyEntity(entity);
                        buffer.RemoveComponent <IsBeingDrilled>(worker.CurrentBlock);
                    }
                }

                float scaleMultiplier = math.max(scale.Value.x, scaleMultiplierThreshold);
                worker.Timer         += deltaTime / worker.Frequency / scaleMultiplier;
                worker.Timer          = math.min(1, worker.Timer);
            }).Schedule(Dependency);

            Dependency.Complete();

            pointBuffer.Dispose(Dependency);
            checkMap.Dispose(Dependency);

            commandBufferSystem.AddJobHandleForProducer(Dependency);

            CurrentJob = Dependency;
        }