Пример #1
0
            public void Execute(Entity entity, int index, ref TerrainSpawner ts)
            {
                if (ts.TerrainBuilt == 0)
                {
                    ts.TerrainBuilt = 1;
                    Entity groundEntity = CommandBuffer.Instantiate(index, prefabs.prefabEntityGround);
                    CommandBuffer.SetComponent(index, groundEntity, new Translation
                    {
                        Value = new float3(0, 0, 0)
                    });
                    CommandBuffer.AddComponent(index, groundEntity, new NonUniformScale
                    {
                        Value = new float3(bounds.Bounds.c1.x - bounds.Bounds.c0.x, 1, bounds.Bounds.c1.y - bounds.Bounds.c0.y)
                    });
                    CommandBuffer.AddComponent(index, groundEntity, new TerrainTag {
                    });

                    for (int count = 0; count < ts.NumOfTrees; count++)
                    {
                        createTree(index, prefabs.prefabEntityTree, prefabs.prefabColliderTree, bounds.Bounds);
                    }


                    for (int count = 0; count < ts.NumOfBubbles; count++)
                    {
                        createBubble(index, prefabs.prefabEntityBubble, bounds.Bounds);
                    }
                }
                else
                {
                    CommandBuffer.RemoveComponent <TerrainSpawner>(index, entity);
                }
            }
        public StateData Copy(int jobIndex, EntityCommandBuffer.Concurrent entityCommandBuffer)
        {
            var stateEntity       = entityCommandBuffer.Instantiate(jobIndex, StateEntity);
            var traitBasedObjects = entityCommandBuffer.SetBuffer <TraitBasedObject>(jobIndex, stateEntity);

            traitBasedObjects.CopyFrom(TraitBasedObjects.AsNativeArray());
            var traitBasedObjectIds = entityCommandBuffer.SetBuffer <TraitBasedObjectId>(jobIndex, stateEntity);

            traitBasedObjectIds.CopyFrom(TraitBasedObjectIds.AsNativeArray());

            var Agents = entityCommandBuffer.SetBuffer <Agent>(jobIndex, stateEntity);

            Agents.CopyFrom(AgentBuffer.AsNativeArray());
            var Locations = entityCommandBuffer.SetBuffer <Location>(jobIndex, stateEntity);

            Locations.CopyFrom(LocationBuffer.AsNativeArray());
            var Targets = entityCommandBuffer.SetBuffer <Target>(jobIndex, stateEntity);

            Targets.CopyFrom(TargetBuffer.AsNativeArray());
            var Collectibles = entityCommandBuffer.SetBuffer <Collectible>(jobIndex, stateEntity);

            Collectibles.CopyFrom(CollectibleBuffer.AsNativeArray());

            return(new StateData
            {
                StateEntity = stateEntity,
                TraitBasedObjects = traitBasedObjects,
                TraitBasedObjectIds = traitBasedObjectIds,

                AgentBuffer = Agents,
                LocationBuffer = Locations,
                TargetBuffer = Targets,
                CollectibleBuffer = Collectibles,
            });
        }
        public void Execute(Entity entity, int index,
                            ref AutoBulletShooter shooter,
                            [ReadOnly] ref LocalToWorld ltw,
                            [ReadOnly] ref Rotation rot)
        {
            shooter.shotTimer -= dt;

            if (shooter.shotTimer <= 0)
            {
                float late = shooter.shotTimer * -1f;
                shooter.shotTimer = shooter.cooldown;

                var e = commandBuffer.Instantiate(index, bulletPrefab);

                var fwd      = ltw.Forward;
                var pos      = ltw.Position;
                var spawnPos = pos + fwd * late;

                commandBuffer.SetComponent(index, e, new Translation {
                    Value = spawnPos
                });
                commandBuffer.SetComponent(index, e, rot);
                commandBuffer.SetComponent(index, e, ltw);
                commandBuffer.SetComponent(index, e, new Bullet {
                    spawnedFrom = entity
                });

                //commandBuffer.SetComponent(index, e, ltw);
            }
        }
Пример #4
0
    protected override void OnUpdate()
    {
        EntityCommandBuffer.Concurrent ecb = endInitializationECBSystem.CreateCommandBuffer().ToConcurrent();
        float deltaTime = UnityEngine.Time.deltaTime;

        Entities.ForEach((Entity entity, int entityInQueryIndex, ref SpawnerEntitiesCubes spawner, ref LocalToWorld localToWorld) =>
        {
            spawner.secondsToNextSpawn -= deltaTime;
            if (spawner.secondsToNextSpawn >= 0)
            {
                return;
            }                                                          //exit, no time to spawn yet

            //queue an instantiate command to the EntityCommandBuffer
            Entity prefabToSpawn      = (spawner.randomness.NextFloat() > 0.5f) ? spawner.redCubePrefab : spawner.blueCubePrefab;
            Entity instantiatedPrefab = ecb.Instantiate(entityInQueryIndex, prefabToSpawn);

            //queue a SetComponent command to position the newly created entity at the back of the scene
            float3 RandomOffset = spawner.randomness.NextFloat3Direction() * spawner.randomness.NextFloat() * spawner.spawnRadius; //random.NextFloat3Direction() gives a random unit-long vector3, and random.NextFloat() returns random float between 0 and 1
            ecb.SetComponent(entityInQueryIndex, instantiatedPrefab, new Translation
            {
                Value = new float3(localToWorld.Position.x + RandomOffset.x, -9, localToWorld.Position.z + RandomOffset.z)
            });

            //update the time to the next entity spawn
            spawner.secondsToNextSpawn += spawner.spawnFrequency;
        }).Schedule();
    }
        public static Entity Instantiate(EntityCommandBuffer.Concurrent ecb,
                                         int jobIndex,
                                         float3 target,
                                         Entity prefab,
                                         Entity prefabTrail,
                                         float3 pos,
                                         float3 vel,
                                         float time)
        {
            var entity = ecb.Instantiate(jobIndex, prefab);

            ecb.SetComponent(jobIndex, entity, new Translation {
                Value = pos,
            });
            var rot = quaternion.LookRotationSafe(vel, new float3(0, 1, 0));

            ecb.SetComponent(jobIndex, entity, new Rotation {
                Value = rot,
            });
            ecb.SetComponent(jobIndex, entity, new MissileComponent {
                Target = target, Velocity = vel,
            });
            ecb.SetComponent(jobIndex, entity, AlivePeriod.Create(time, 2f /* period */));
            TrailSystem.Instantiate(ecb, jobIndex, prefabTrail, entity /* refering_entity */, pos, 0.5f /* width */, new Color(0.8f, 0.8f, 0.8f), time);
            return(entity);
        }
Пример #6
0
 protected override void OnUpdate()
 {   //线程安全的IBufferElementData,World管理的单例锁,Allocator.TempJob 4帧后回收
     EntityCommandBuffer.Concurrent cmdBuffer = m_syncPointCEManager.CreateCommandBuffer().ToConcurrent();
     //sync point job/Task.Run()
     Entities.WithName("SpawnData")                               //找到自己GOToEntity,特殊编译设置
     .WithBurst(FloatMode.Default, FloatPrecision.Standard, true) //sync compile: compile before Update
     .ForEach((Entity entity, int entityInQueryIndex, in SpawnData spawnData, in LocalToWorld location) =>
     {                                                            //entityQueryIndex as CommandEntityIndex
         for (var x = 0; x < spawnData.SpawnSize.x; x++)
         {
             for (var z = 0; z < spawnData.SpawnSize.z; z++)
             {
                 Entity instance = cmdBuffer.Instantiate(entityInQueryIndex, spawnData.PrefabRoot);
                 //center transform(parent.transform), offset
                 float3 pos = math.transform(location.Value, new float3(x, 0, z));
                 //原生Component,new赋值
                 cmdBuffer.SetComponent(entityInQueryIndex, instance, new Translation {
                     Value = pos
                 });
                 //增加Component,也是new赋值
                 cmdBuffer.AddComponent(entityInQueryIndex, instance,
                                        new PlanetRotateBehavior {
                     AngularVelocity = 2
                 });
                 cmdBuffer.AddComponent(entityInQueryIndex, instance, new LifetimeData {
                     TimeSpan = spawnData.TimeSpan
                 });
             }
         }
         //this CommandEntityIndex
         cmdBuffer.DestroyEntity(entityInQueryIndex, entity);
     }).ScheduleParallel();
 public void CreateBurstLine(Entity bullet, int index, float angle, float3 position, float speed,
                             int bulletsInLine)
 {
     for (int i = 0; i < bulletsInLine; ++i)
     {
         float  speedScale = .5f + (.5f / bulletsInLine * i);
         Entity entity     = commandBuffer.Instantiate(index, bullet);
         commandBuffer.SetComponent(index, entity,
                                    new Translation {
             Value = position
         });
         commandBuffer.SetComponent(index, entity,
                                    new Rotation {
             Value =
                 quaternion.AxisAngle(
                     new float3(0, 0, 1),
                     angle)
         });
         commandBuffer.SetComponent(index, entity,
                                    new BulletMovement {
             moveType    = BulletMovementSystem.MoveType.LINEAR,
             moveSpeed   = speed * speedScale,
             rotateSpeed = 0
         });
     }
 }
Пример #8
0
        public void Execute(Entity entity, int index, [ReadOnly] ref Spawner_SpawnAndRemove spawner, [ReadOnly] ref LocalToWorld location)
        {
            var random = new Random(1);

            for (var x = 0; x < spawner.CountX; x++)
            {
                for (var y = 0; y < spawner.CountY; y++)
                {
                    var instance = CommandBuffer.Instantiate(index, spawner.Prefab);

                    // Place the instantiated in a grid with some noise
                    var position = math.transform(location.Value, new float3(x * 1.3F, noise.cnoise(new float2(x, y) * 0.21F) * 2, y * 1.3F));
                    CommandBuffer.SetComponent(index, instance, new Translation {
                        Value = position
                    });
                    CommandBuffer.SetComponent(index, instance, new LifeTime {
                        Value = random.NextFloat(10.0F, 100.0F)
                    });
                    CommandBuffer.SetComponent(index, instance, new RotationSpeed_SpawnAndRemove {
                        RadiansPerSecond = math.radians(random.NextFloat(25.0F, 90.0F))
                    });
                }
            }

            CommandBuffer.DestroyEntity(index, entity);
        }
Пример #9
0
        public void Execute(Entity entity, int index, ref RangeWeapon weapon, [ReadOnly] ref AttackInputs attackInputs)
        {
            if (attackInputs.AttackHeld)
            {
                if (Time >= weapon.LastTimeShot + (1f / weapon.FiringRate))
                {
                    LocalToWorld localToWorld = LocalToWorldFromEntity[weapon.ShootPointEntity];
                    // store gun forward direction
                    quaternion dir = quaternion.LookRotationSafe(localToWorld.Forward, localToWorld.Up);
                    // convert angle to radians
                    float spreadAngle = math.radians(30);
                    // change angle over time
                    spreadAngle = spreadAngle / 2 + math.abs(math.sin(Time * 2)) * spreadAngle / 2;
                    // store angle slice per bullet
                    quaternion diffraction = quaternion.EulerXYZ(0, spreadAngle / (math.ceil(weapon.BulletAmountPerShot) + 1), 0);
                    // prepare offset 50% spreadAngle
                    dir = math.mul(dir, quaternion.EulerXYZ(0, -spreadAngle / 2, 0));

                    for (int i = 0; i < math.ceil(weapon.BulletAmountPerShot); i++)
                    {
                        dir = math.mul(dir, diffraction);

                        Entity spawnedProjectile = entityCommandBuffer.Instantiate(index, weapon.ProjectileEntity);
                        entityCommandBuffer.SetComponent(index, spawnedProjectile, new Translation {
                            Value = localToWorld.Position
                        });
                        entityCommandBuffer.SetComponent(index, spawnedProjectile, new Rotation {
                            Value = dir
                        });
                    }

                    weapon.LastTimeShot = Time;
                }
            }
        }
        public void Execute(Entity entity, int index,
                            [ReadOnly] ref Spawner spawnerFromEntity,
                            [ReadOnly] ref LocalToWorld location)
        {
            var parent             = spawnerFromEntity.Parent;
            var horizontalInterval = spawnerFromEntity.HorizontalInterval;
            var verticalInterval   = spawnerFromEntity.VerticalInterval;
            var depthInterval      = spawnerFromEntity.DepthInterval;
            var origin             = parent != Entity.Null ? LocalToWorldFromEntity[parent].Value : location.Value;

            for (var x = 0; x < spawnerFromEntity.CountX; x++)
            {
                for (var y = 0; y < spawnerFromEntity.CountY; y++)
                {
                    for (var z = 0; z < spawnerFromEntity.CountZ; z++)
                    {
                        // Place the instantiated in a grid
                        var instance = CommandBuffer.Instantiate(index, spawnerFromEntity.Prefab);
                        var position = math.transform(
                            origin,
                            new float3((x - spawnerFromEntity.CountX * 0.5f) * horizontalInterval,
                                       (y - spawnerFromEntity.CountY * 0.5f) * verticalInterval,
                                       (z - spawnerFromEntity.CountZ * 0.5f) * depthInterval));
                        CommandBuffer.SetComponent(index, instance, new Translation {
                            Value = position
                        });
                        if (parent != Entity.Null)
                        {
                            CommandBuffer.SetParent(index, parent, instance);
                        }
                    }
                }
            }
            CommandBuffer.DestroyEntity(index, entity);
        }
        public StateData Copy(int jobIndex, EntityCommandBuffer.Concurrent entityCommandBuffer)
        {
            var stateEntity       = entityCommandBuffer.Instantiate(jobIndex, StateEntity);
            var traitBasedObjects = entityCommandBuffer.SetBuffer <TraitBasedObject>(jobIndex, stateEntity);

            traitBasedObjects.CopyFrom(TraitBasedObjects.AsNativeArray());
            var traitBasedObjectIds = entityCommandBuffer.SetBuffer <TraitBasedObjectId>(jobIndex, stateEntity);

            traitBasedObjectIds.CopyFrom(TraitBasedObjectIds.AsNativeArray());

            var Locations = entityCommandBuffer.SetBuffer <Location>(jobIndex, stateEntity);

            Locations.CopyFrom(LocationBuffer.AsNativeArray());
            var Robots = entityCommandBuffer.SetBuffer <Robot>(jobIndex, stateEntity);

            Robots.CopyFrom(RobotBuffer.AsNativeArray());
            var Dirts = entityCommandBuffer.SetBuffer <Dirt>(jobIndex, stateEntity);

            Dirts.CopyFrom(DirtBuffer.AsNativeArray());
            var Moveables = entityCommandBuffer.SetBuffer <Moveable>(jobIndex, stateEntity);

            Moveables.CopyFrom(MoveableBuffer.AsNativeArray());

            return(new StateData
            {
                StateEntity = stateEntity,
                TraitBasedObjects = traitBasedObjects,
                TraitBasedObjectIds = traitBasedObjectIds,

                LocationBuffer = Locations,
                RobotBuffer = Robots,
                DirtBuffer = Dirts,
                MoveableBuffer = Moveables,
            });
        }
Пример #12
0
 public void Execute(int index)
 {
     Buffer.Instantiate(index, MasterCopy);
     Buffer.AddComponent(index, new EcsTestData {
         value = index
     });
 }
Пример #13
0
            public void Execute(Entity pEntity, int pIndex, ref SpawnData pSpawner, [ReadOnly] ref LocalToWorld pLocalWorld)
            {
                pSpawner.timeCounter -= m_deltaTime;

                if (pSpawner.timeCounter > 0f)
                {
                    return;
                }

                pSpawner.timeCounter = pSpawner.frequency;

                Entity _instance = m_buffer.Instantiate(pIndex, pSpawner.prefab);

                m_buffer.SetComponent(pIndex, _instance, new Translation {
                    Value = pLocalWorld.Position + new float3(m_random.NextFloat() * pSpawner.distance, 0f, 0f)
                });

                m_buffer.SetComponent(pIndex, _instance, new NonUniformScale {
                    Value = new float3(m_random.NextFloat(MIN_SCALE, MAX_SCALE),
                                       m_random.NextFloat(MIN_SCALE, MAX_SCALE),
                                       m_random.NextFloat(MIN_SCALE, MAX_SCALE))
                });

                m_buffer.AddComponent(pIndex, _instance, new Movement {
                    bouncing  = m_random.NextInt(0, 3),
                    speed     = m_random.NextFloat(MIN_SPEED, MAX_SPEED),
                    jumpForce = m_random.NextFloat(MIN_FORCE, MAX_FORCE)
                });
            }
Пример #14
0
            public void Execute(Entity entity, int index, [ReadOnly] ref PlayerSpawner spawner, [ReadOnly] ref LocalToWorld location)
            {
                var position = location.Position;
                var aabb     = new AABB
                {
                    //0.5f will represent halfwidth for now
                    max = position + 0.5f,
                    min = position - 0.5f,
                };
                Entity instance = CommandBuffer.Instantiate(index, spawner.entity);

                CommandBuffer.AddComponent(index, instance, new PlayerData {
                    moveSpeed = 5f, respawnPosition = new float3(0, 0, 0)
                });
                CommandBuffer.AddComponent(index, instance, new PlayerInput {
                });

                CommandBuffer.SetComponent(index, instance, new Translation {
                    Value = new float3(0, 5f, 0)
                });

                CommandBuffer.DestroyEntity(index, entity); //摧毁生成器,可以有多种其他的实现方式,前期开发简单摧毁即可

                CommandBuffer.AddComponent(index, instance, aabb);
            }
        public void Execute(Entity e, int jobIndex, [ReadOnly] ref ZombieSpawnerComponent zombieSpawner)
        {
            int2 minPos = new int2(-100, 175);
            int2 maxPos = new int2(100, 230);

            SpawnCount += 80;
            if (SpawnCount > 1000)
            {
                CommandBuffer.RemoveComponent <ZombieSpawnerComponent>(jobIndex, e);
            }

            var  prefab = zombieSpawner.prefab;
            uint count  = 0;

            while (count++ < 80)
            {
                var instance = CommandBuffer.Instantiate(jobIndex, prefab);
                var space    = Rand.NextInt2(minPos, maxPos);
                space.x += Rand.NextInt(-2, 2);
                space.y += Rand.NextInt(-2, 2);
                var position = new float3(space.x, 1, space.y);
                CommandBuffer.SetComponent(jobIndex, instance, new Translation {
                    Value = position
                });
                CommandBuffer.SetComponent(jobIndex, instance, new MovementComponent {
                    speed = 10 + Rand.NextFloat(-4f, 0f), direction = new float3(0, 0, -1), defaultDirection = new float3(0, 0, -1)
                });
            }
            CommandBuffer.AddComponent(jobIndex, e, new CooldownComponent {
                waitTime = 5f
            });
        }
        public StateData Copy(int jobIndex, EntityCommandBuffer.Concurrent entityCommandBuffer)
        {
            var stateEntity       = entityCommandBuffer.Instantiate(jobIndex, StateEntity);
            var traitBasedObjects = entityCommandBuffer.SetBuffer <TraitBasedObject>(jobIndex, stateEntity);

            traitBasedObjects.CopyFrom(TraitBasedObjects.AsNativeArray());
            var traitBasedObjectIds = entityCommandBuffer.SetBuffer <TraitBasedObjectId>(jobIndex, stateEntity);

            traitBasedObjectIds.CopyFrom(TraitBasedObjectIds.AsNativeArray());

            var Games = entityCommandBuffer.SetBuffer <Game>(jobIndex, stateEntity);

            Games.CopyFrom(GameBuffer.AsNativeArray());
            var Coordinates = entityCommandBuffer.SetBuffer <Coordinate>(jobIndex, stateEntity);

            Coordinates.CopyFrom(CoordinateBuffer.AsNativeArray());
            var Cells = entityCommandBuffer.SetBuffer <Cell>(jobIndex, stateEntity);

            Cells.CopyFrom(CellBuffer.AsNativeArray());
            var Blockers = entityCommandBuffer.SetBuffer <Blocker>(jobIndex, stateEntity);

            Blockers.CopyFrom(BlockerBuffer.AsNativeArray());

            return(new StateData
            {
                StateEntity = stateEntity,
                TraitBasedObjects = traitBasedObjects,
                TraitBasedObjectIds = traitBasedObjectIds,

                GameBuffer = Games,
                CoordinateBuffer = Coordinates,
                CellBuffer = Cells,
                BlockerBuffer = Blockers,
            });
        }
Пример #17
0
    protected override void OnUpdate()
    {
        EntityCommandBuffer.Concurrent ECB = ECBSystem.CreateCommandBuffer().ToConcurrent();
        float deltaTime = Time.DeltaTime;

        Entities
        .ForEach((int entityInQueryIndex, ref FireCooldown fireCooldown, in TurretInput input, in FireInterval fireInterval, in FireSpeed fireSpeed, in LocalToWorld localToWorld, in ProjectilePrefab projectilePrefab, in ProjectileSpawnPoint spawnPointData) =>
        {
            //decrease the cooldown
            if (fireCooldown.Value > 0f)
            {
                fireCooldown.Value -= deltaTime;
            }

            if (input.Fire &&
                fireCooldown.Value <= 0f)
            {
                //fire!
                Entity newProjectile = ECB.Instantiate(entityInQueryIndex, projectilePrefab.Reference);

                //override a few components to position, rotate and push the newly created bullet
                ECB.SetComponent <Translation>(entityInQueryIndex, newProjectile, new Translation {
                    Value = math.transform(localToWorld.Value, spawnPointData.LocalTranslation)
                });
                quaternion worldRotation = math.mul(localToWorld.Rotation, spawnPointData.LocalRotation);
                ECB.SetComponent <Rotation>(entityInQueryIndex, newProjectile, new Rotation {
                    Value = worldRotation
                });
                ECB.SetComponent <PhysicsVelocity>(entityInQueryIndex, newProjectile, new PhysicsVelocity {
                    Linear = fireSpeed.Value * math.forward(worldRotation)
                });

                fireCooldown.Value = fireInterval.Value;
            }
        }).ScheduleParallel();
Пример #18
0
            // ------------------------------------------------------------------------

            public void Execute(
                Entity entity,
                int index,
                [ReadOnly] ref EntitySpawnerData spawnerData,
                [ReadOnly] ref LocalToWorld location)
            {
                int2 min = spawnerData.m_minBounds;
                int2 max = spawnerData.m_maxBounds;

                for (int x = min.x; x <= max.x; ++x)
                {
                    for (int z = min.y; z <= max.y; ++z)
                    {
                        // instantiate
                        var instance = m_commandBuffer.Instantiate(index, spawnerData.m_prefab);

                        // position
                        float3 position = new float3(x, 0.0f, z);
                        // set translation component
                        m_commandBuffer.SetComponent(index, instance,
                                                     new Translation
                        {
                            Value = position
                        }
                                                     );
                    }
                }

                // destroy spawner
                m_commandBuffer.DestroyEntity(index, entity);
            }
        public void Execute(ArchetypeChunk chunk, int chunkIndex, int firstEntityIndex)
        {
            var Velocities   = chunk.GetNativeArray(VelocityType);
            var Translations = chunk.GetNativeArray(TranslationType);
            var Targets      = chunk.GetNativeArray(TargetType);
            var Entities     = chunk.GetNativeArray(EntityType);

            for (int i = 0; i < chunk.Count; ++i)
            {
                var Target = Targets[i];

                if (DyingType.Exists(Target.Value))//This code reads: "If Target is dead, stop following it"
                {
                    ecb.RemoveComponent(chunkIndex, Entities[i], typeof(C_Target));
                    continue;
                }

                var translation = Translations[i];
                var velocity    = Velocities[i];

                float3 targetPos = TranslationData[Target.Value].Value;

                float3 delta           = targetPos - translation.Value;
                float  distanceSquared = delta.x * delta.x + delta.y * delta.y + delta.z * delta.z;

                if (distanceSquared < HitDistance * HitDistance)
                {
                    C_DeathTimer timer = new C_DeathTimer()
                    {
                        TimeRemaining = BeeTimeToDeath
                    };

                    Tag_IsDying tag;
                    ecb.AddComponent(chunkIndex, Target.Value, tag);
                    ecb.SetComponent(chunkIndex, Target.Value, timer);
                    ecb.RemoveComponent(chunkIndex, Entities[i], typeof(C_Target));

                    //Spawn particles;
                    var bloodVel = new C_Velocity()
                    {
                        Value = velocity.Value * .35f
                    };

                    for (int particle = 0; particle < 6; ++particle)
                    {
                        var blood = ecb.Instantiate(chunkIndex, BloodPrefab);
                        ecb.SetComponent(chunkIndex, blood, translation);
                        ecb.SetComponent(chunkIndex, blood, bloodVel);
                    }

                    continue;
                }

                float force = max(1.0, distanceSquared) < AttackDistance * AttackDistance ? AttackForce : ChaseForce;

                velocity.Value += delta * (force * dt / sqrt(distanceSquared));
                Velocities[i]   = velocity;
            }
        }
Пример #20
0
        public void Execute(Entity ent, int index, [ReadOnly] ref Translation translation, ref Enemy enemy)
        {
            enemy.timer -= deltaTime;
            var input = new PointDistanceInput()
            {
                MaxDistance = enemy.rangeDie,
                Filter      = new CollisionFilter()
                {
                    CategoryBits = mask, MaskBits = mask, GroupIndex = 0
                },
                Position = translation.Value
            };
            DistanceHit hit;

            if (world.CalculateDistance(input, out hit))
            {
                cmd.DestroyEntity(index, ent);
                cmd.DestroyEntity(index, world.Bodies[hit.RigidBodyIndex].Entity);
                var e = cmd.Instantiate(index, explosionPrefab);
                cmd.SetComponent(index, e, new Translation()
                {
                    Value = hit.Position
                });
            }
            else if (enemy.timer < 0)
            {
                input.MaxDistance = enemy.rangeShoot;
                if (world.CalculateDistance(input, out hit))
                {
                    var dir = hit.Position - translation.Value;
                    dir *= enemy.rangeShoot / math.length(dir) * 0.5f;
                    cmd.DestroyEntity(index, world.Bodies[hit.RigidBodyIndex].Entity);
                    var e = cmd.Instantiate(index, lazerPrefab);
                    cmd.SetComponent(index, e, new Translation()
                    {
                        Value = translation.Value + dir
                    });
                    cmd.SetComponent(index, e, new Rotation()
                    {
                        Value = quaternion.RotateZ(math.atan2(dir.y, dir.x))
                    });
                    enemy.timer = enemy.cooldown;
                }
            }
        }
Пример #21
0
        private static void SpawnEntity(ref EntityCommandBuffer.Concurrent commandBuffer,
                                        ref AroundCenterCyclicSpawner spawner, ref Random randomGenerator, int entityInQueryIndex)
        {
            var spawnedEntity = commandBuffer.Instantiate(entityInQueryIndex, spawner.EntityToSpawn);

            ApplyRandomPositionOnCircle(ref commandBuffer, ref spawner, ref randomGenerator, entityInQueryIndex,
                                        spawnedEntity);
            ApplyRandomColor(ref commandBuffer, ref randomGenerator, entityInQueryIndex, spawnedEntity);
        }
            // ----------------------------------------------------
            #region // Barrage Methods

            // 自機に狙い撃ち
            void Aiming(ref int jobIndex, ref Position2D enemyPosition, ref EnemyParam param, ref EnemyData data)
            {
                CommandBuffer.Instantiate(jobIndex, this.EnemyBulletPrefab);
                CommandBuffer.SetComponent(jobIndex, enemyPosition);
                CommandBuffer.SetComponent(jobIndex, new BulletData
                {
                    Speed    = param.BulletParam.Speed,
                    Angle    = MathHelper.Aiming(enemyPosition.Value, this.PlayerPosition),
                    Lifespan = param.BulletParam.Lifespan,
                });
            }
        protected override JobHandle OnUpdate(JobHandle inputDeps)
        {
            EntityCommandBuffer.Concurrent ecb         = m_ecbSystem.CreateCommandBuffer().ToConcurrent();
            NativeArray <Entity>           paddles     = m_paddleQuery.ToEntityArray(Allocator.TempJob);
            NativeArray <BallPrefab>       ballPrefabs = m_ballPrefabQuery.ToComponentDataArray <BallPrefab>(Allocator.TempJob);

            float dt = Time.DeltaTime;

            JobHandle jobHandle = Entities
                                  .WithDeallocateOnJobCompletion(paddles)
                                  .WithDeallocateOnJobCompletion(ballPrefabs)
                                  .ForEach((Entity entity, int entityInQueryIndex, ref BallSpawnRequest spawnData) =>
            {
                spawnData.m_delay -= dt;
                if (spawnData.m_delay <= 0.0f)
                {
                    Entity ballEntity = ecb.Instantiate(entityInQueryIndex, ballPrefabs[0].m_prefab);
                    if (spawnData.m_attachToPaddle)
                    {
                        ecb.AddComponent(entityInQueryIndex, ballEntity, new Parent {
                            Value = paddles[0]
                        });
                        ecb.AddComponent(entityInQueryIndex, ballEntity, new LocalToParent {
                        });
                        ecb.AddComponent(entityInQueryIndex, ballEntity, new BlockMovement {
                        });
                        ecb.SetComponent(entityInQueryIndex, ballEntity, new Translation()
                        {
                            Value = new float3(0.0f, 32.0f, 0.0f)
                        });
                    }
                    else
                    {
                        ecb.SetComponent(entityInQueryIndex, ballEntity, new Translation()
                        {
                            Value = new float3(spawnData.m_position.x, spawnData.m_position.y, 0.0f)
                        });
                    }

                    ecb.SetComponent(entityInQueryIndex, ballEntity, new Direction()
                    {
                        m_direction = spawnData.m_direction
                    });
                    ecb.SetComponent(entityInQueryIndex, ballEntity, new Speed()
                    {
                        m_speed = spawnData.m_speed
                    });

                    ecb.DestroyEntity(entityInQueryIndex, entity);
                }
            })
                                  .Schedule(inputDeps);

            m_ecbSystem.AddJobHandleForProducer(jobHandle);
            return(jobHandle);
        }
Пример #24
0
        public void Execute(Entity entity, int index, ref UnitSpawner spawner)
        {
            Entity spawnedEntity = commandBuffer.Instantiate(index, spawner.prefab);

            commandBuffer.SetComponent(index, spawnedEntity, new Translation {
                Value = spawner.spawnPoint
            });
            // what does the line below do?????
            commandBuffer.DestroyEntity(index, entity);
        }
Пример #25
0
        public static void SpawnCell(int entityInQueryIndex, Entity entity, EntityCommandBuffer.Concurrent commandBuffer, float3 positon, float3 scale)
        {
            Entity cellEntity = commandBuffer.Instantiate(entityInQueryIndex, entity);

            commandBuffer.SetComponent(entityInQueryIndex, cellEntity, new Translation()
            {
                Value = positon
            });
            commandBuffer.SetComponent(entityInQueryIndex, cellEntity, new CellComponent(positon, scale));
        }
        public void Execute(int index)
        {
            if (!waves[index].spawned && waves[index].spawnTime < time)
            {
                commandBuffer.Instantiate(index, waves[index].wave);

                waves[index] = new WaveData {
                    spawned = true
                };
            }
        }
Пример #27
0
        private Entity InstansiateEntity(int index, ref SpawnerData spawnerData, ref LocalToWorld localToWorld)
        {
            spawnerData.secondsToNextSpawn = spawnerData.secondsBetweenSpawns;
            Entity instance = entityCommandBuffer.Instantiate(index, spawnerData.prefab);

            entityCommandBuffer.SetComponent(index, instance, new Translation
            {
                Value = localToWorld.Position
            });
            return(instance);
        }
Пример #28
0
        public void Execute(Entity entity, int index, ref Spawner spawner, [ReadOnly] ref LocalToWorld localToWorld)
        {
            Entity instance = entityCommandBuffer.Instantiate(index, spawner.prefab);

            entityCommandBuffer.SetComponent(index, instance, new Translation
            {
                Value = new unityMath.float3(
                    random.NextFloat(-spawner.randomRange, spawner.randomRange),
                    1,
                    random.NextFloat(-spawner.randomRange, spawner.randomRange))
            });
        }
Пример #29
0
 public void Execute(Entity entity, int index, ref Spawner spawner, [ReadOnly] ref LocalToWorld localToWorld)
 {
     spawner.nextSpawnTime -= deltaTime;
     if (spawner.nextSpawnTime <= 0)
     {
         spawner.nextSpawnTime += spawner.spawnDelay;
         Entity instance = entityCommandBuffer.Instantiate(index, spawner.prefab);
         entityCommandBuffer.SetComponent(index, instance, new Translation {
             Value = localToWorld.Position + random.NextFloat3Direction() * random.NextFloat() * spawner.maxDistanceFromSpawner
         });
     }
 }
Пример #30
0
            //Spawn prefabs then move their Transforms to Random position in the world
            public void Execute(Entity entity, int index, ref Spawner spawner, [ReadOnly] ref LocalToWorld local_to_world)
            {
                spawner.seconds_to_next_spawn -= delta_time;

                if (spawner.seconds_to_next_spawn >= 0) { return; }

                spawner.seconds_to_next_spawn += spawner.seconds_betweens_spawns;

                Entity instance = entity_command_buffer.Instantiate(index, spawner.prefab);

                entity_command_buffer.SetComponent(index, instance, new Translation { Value = local_to_world.Position + random.NextFloat3Direction() * random.NextFloat() * spawner.max_distance_from_spawner });
            }