Esempio n. 1
0
        protected override void OnUpdate()
        {
            if (grendePrefab == Entity.Null)
            {
                var collection = GetSingleton <GhostPrefabCollectionComponent>();
                var buffer     = EntityManager.GetBuffer <GhostPrefabBuffer>(collection.ServerPrefabs);
                for (int i = 0; i < buffer.Length; i++)
                {
                    if (!EntityManager.HasComponent <GrenadeTagComponent>(buffer[i].Value))
                    {
                        continue;
                    }

                    grendePrefab =
                        GhostCollectionSystem.CreatePredictedSpawnPrefab(EntityManager, buffer[i].Value);
                    break;
                }
            }

            Entities.ForEach((
                                 ref GhostComponent ghost,
                                 ref GhostOwnerComponent ghostOwner,
                                 ref AbilityGrenadeComponent ability,
                                 ref PlayerControlledState state) =>
            {
                // 在回滚时,进行跳过,仍手雷操作是不需要进行回滚的。
                if (_ghostPredictionSystemGroup.PredictingTick <= ability.LastFireTick)
                {
                    return;
                }

                ability.LastFireTick = _ghostPredictionSystemGroup.PredictingTick;

                ++ability.CurFireRate;

                // 测试雷
                if (state.Command.R && ability.CurFireRate > fireRate && ability.LastFireTick % 5 == 0)
                {
                    ability.CurFireRate = 0;

                    var ent = EntityManager.Instantiate(grendePrefab);

                    if (isServer)
                    {
                        EntityManager.SetComponentData(ent, ghostOwner);
                    }

                    // 同时创建GameObject
                    var linkSystem = World.GetOrCreateSystem <GameObjectManager>();
                    var gameObject = linkSystem.CreateDefaultGhostGameObject(ent);
                    gameObject.transform.position = state.Command.FirePos;
                    gameObject.transform.rotation = Quaternion.LookRotation(state.Command.FireDir);
                    gameObject.GetComponent <GrenadeScript>().Init(World, this.isServer);
                }
            });
        }
Esempio n. 2
0
        protected override void OnUpdate()
        {
            if (m_BulletPrefab == Entity.Null)
            {
                var prefabEntity = GetSingletonEntity <GhostPrefabCollectionComponent>();
                var prefabs      = EntityManager.GetBuffer <GhostPrefabBuffer>(prefabEntity);
                var foundPrefab  = Entity.Null;
                for (int i = 0; i < prefabs.Length; ++i)
                {
                    if (EntityManager.HasComponent <BulletTagComponent>(prefabs[i].Value))
                    {
                        foundPrefab = prefabs[i].Value;
                    }
                }
                if (foundPrefab != Entity.Null)
                {
                    m_BulletPrefab = GhostCollectionSystem.CreatePredictedSpawnPrefab(EntityManager, foundPrefab);
                }
            }

            var level           = GetSingleton <LevelComponent>();
            var commandBuffer   = m_Barrier.CreateCommandBuffer().AsParallelWriter();
            var deltaTime       = Time.DeltaTime;
            var displacement    = 100.0f;
            var playerForce     = level.playerForce;
            var bulletVelocity  = level.bulletVelocity;
            var bulletPrefab    = m_BulletPrefab;
            var currentTick     = m_PredictionGroup.PredictingTick;
            var inputFromEntity = GetBufferFromEntity <ShipCommandData>(true);

            Entities.WithReadOnly(inputFromEntity).WithAll <ShipTagComponentData, ShipCommandData>().
            ForEach((Entity entity, int nativeThreadIndex, ref Translation position, ref Rotation rotation,
                     ref Velocity velocity, ref ShipStateComponentData state,
                     in GhostOwnerComponent ghostOwner, in PredictedGhostComponent prediction) =>
            {
                if (!GhostPredictionSystemGroup.ShouldPredict(currentTick, prediction))
                {
                    return;
                }
                var input = inputFromEntity[entity];
                ShipCommandData inputData;
                if (!input.GetDataAtTick(currentTick, out inputData))
                {
                    inputData.shoot = 0;
                }

                state.State = inputData.thrust;

                if (inputData.left == 1)
                {
                    rotation.Value = math.mul(rotation.Value,
                                              quaternion.RotateZ(math.radians(-displacement * deltaTime)));
                }

                if (inputData.right == 1)
                {
                    rotation.Value = math.mul(rotation.Value,
                                              quaternion.RotateZ(math.radians(displacement * deltaTime)));
                }

                if (inputData.thrust == 1)
                {
                    float3 fwd      = new float3(0, playerForce * deltaTime, 0);
                    velocity.Value += math.mul(rotation.Value, fwd).xy;
                }

                position.Value.xy += velocity.Value * deltaTime;

                var canShoot = state.WeaponCooldown == 0 || SequenceHelpers.IsNewer(currentTick, state.WeaponCooldown);
                if (inputData.shoot != 0 && canShoot)
                {
                    if (bulletPrefab != Entity.Null)
                    {
                        var e = commandBuffer.Instantiate(nativeThreadIndex, bulletPrefab);

                        commandBuffer.SetComponent(nativeThreadIndex, e, position);
                        commandBuffer.SetComponent(nativeThreadIndex, e, rotation);

                        var vel = new Velocity
                        {
                            Value = math.mul(rotation.Value, new float3(0, bulletVelocity, 0)).xy
                        };

                        commandBuffer.SetComponent(nativeThreadIndex, e,
                                                   new GhostOwnerComponent {
                            NetworkId = ghostOwner.NetworkId
                        });
                        commandBuffer.SetComponent(nativeThreadIndex, e, vel);
                    }

                    state.WeaponCooldown = currentTick + k_CoolDownTicksCount;
                }

                /*else if (canShoot)
                 * {
                 *  state.WeaponCooldown = 0;
                 * }*/
            }).ScheduleParallel();