Exemplo n.º 1
0
    protected override void OnUpdate()
    {
        Entities.WithNone <SendRpcCommandRequestComponent>().ForEach((Entity reqEnt, ref GoInGameRequest req, ref ReceiveRpcCommandRequestComponent reqSrc) =>
        {
            if (connectedPlayers < GameSession.serverSession.numberOfPlayers)
            {
                PostUpdateCommands.AddComponent <NetworkStreamInGame>(reqSrc.SourceConnection);
                var ghostCollection = GetSingleton <GhostPrefabCollectionComponent>();
                var ghostId         = SerpentineGhostSerializerCollection.FindGhostType <CarStubSnapshotData>();
                var prefab          = EntityManager.GetBuffer <GhostPrefabBuffer>(ghostCollection.serverPrefabs)[ghostId].Value;
                var player          = PostUpdateCommands.Instantiate(prefab);

                int indexOfNextFreeSpawnLocationIndex = Random.Range(0, freeSpawnLocationIndices.Count);
                uint nextSpawnLocationIndex           = freeSpawnLocationIndices[indexOfNextFreeSpawnLocationIndex];
                freeSpawnLocationIndices.RemoveAt(indexOfNextFreeSpawnLocationIndex);

                PostUpdateCommands.SetComponent(player, new Translation {
                    Value = SerializedFields.singleton.spawnLocations[(int)nextSpawnLocationIndex].position
                });
                PostUpdateCommands.SetComponent(player, new Rotation {
                    Value = SerializedFields.singleton.spawnLocations[(int)nextSpawnLocationIndex].rotation
                });
                PostUpdateCommands.SetComponent(player, new SynchronizedCarComponent {
                    PlayerId = EntityManager.GetComponentData <NetworkIdComponent>(reqSrc.SourceConnection).Value
                });
                PostUpdateCommands.SetComponent(player, new MissileScopeComponent {
                    TargetPlayerId = null
                });
                PostUpdateCommands.AddBuffer <PlayerInput>(player);
                PostUpdateCommands.AddBuffer <PowerupSlotElement>(player);
                PostUpdateCommands.AddBuffer <LaserPowerupSlotElement>(player);

                for (int i = 0; i < SerializedFields.singleton.numberOfPowerupSlots; i++)
                {
                    PostUpdateCommands.AppendToBuffer(player, new PowerupSlotElement {
                        Content = PowerupSlotContent.Empty
                    });
                }

                PostUpdateCommands.SetComponent(reqSrc.SourceConnection, new CommandTargetComponent {
                    targetEntity = player
                });
                PostUpdateCommands.DestroyEntity(reqEnt);

                var raceInformationRequest = PostUpdateCommands.CreateEntity();
                PostUpdateCommands.AddComponent(raceInformationRequest, new RaceInformationRequest {
                    laps = GameSession.serverSession.laps
                });
                PostUpdateCommands.AddComponent(raceInformationRequest, new SendRpcCommandRequestComponent {
                    TargetConnection = reqSrc.SourceConnection
                });

                connectedPlayers++;
            }
        });
    }
Exemplo n.º 2
0
    protected override void OnUpdate()
    {
        Entities.ForEach((Entity interactionEntity, ref PowerupCarInteractionComponent interaction) => {
            PostUpdateCommands.DestroyEntity(interactionEntity);

            if (EntityManager.GetComponentData <ActiveComponent>(interaction.Powerup).IsActive)
            {
                DynamicBuffer <PowerupSlotElement> powerupSlots = EntityManager.GetBuffer <PowerupSlotElement>(interaction.Car);

                for (uint slotNumber = 0; slotNumber < powerupSlots.Length; slotNumber++)
                {
                    if (powerupSlots[(int)slotNumber].Content == PowerupSlotContent.Empty)
                    {
                        var powerupId = EntityManager.GetComponentData <PowerupIdComponent>(interaction.Powerup).PowerupId;

                        EntityManager.SetComponentData(interaction.Powerup, new ActiveComponent {
                            IsActive = false
                        });
                        PostUpdateCommands.AddComponent(interaction.Powerup, typeof(Disabled));
                        var ghostRespawnEntity = PostUpdateCommands.CreateEntity();
                        PostUpdateCommands.AddComponent(ghostRespawnEntity, new PowerupRespawnComponent {
                            PowerupId = powerupId, RemainingTime = SerializedFields.singleton.powerupRespawnTime
                        });

                        PowerupSlotContent slotContent = (PowerupSlotContent)((int)EntityManager.GetComponentData <PowerupComponent>(interaction.Powerup).Powerup);

                        unsafe
                        {
                            void *powerupSlotData = null;

                            switch (slotContent)
                            {
                            case PowerupSlotContent.Laser:
                                PostUpdateCommands.AppendToBuffer(interaction.Car, new LaserPowerupSlotElement {
                                    SlotNumber = slotNumber, RemainingShots = SerializedFields.singleton.laserPowerupShots
                                });

                                var laserPowerupSlotData = new PowerupSlotChangedRequest.LaserPowerupSlotData {
                                    RemainingShots = SerializedFields.singleton.laserPowerupShots
                                };
                                powerupSlotData = UnsafeUtility.Malloc(sizeof(PowerupSlotChangedRequest.LaserPowerupSlotData), sizeof(PowerupSlotChangedRequest.LaserPowerupSlotData), Unity.Collections.Allocator.Persistent);
                                UnsafeUtility.CopyStructureToPtr(ref laserPowerupSlotData, powerupSlotData);
                                break;
                            }

                            powerupSlots[(int)slotNumber] = new PowerupSlotElement {
                                Content = slotContent
                            };

                            var car = interaction.Car;

                            Entities.ForEach((Entity connectionEntity, ref NetworkIdComponent id) =>
                            {
                                var ghostEnabledRequest = PostUpdateCommands.CreateEntity();
                                PostUpdateCommands.AddComponent(ghostEnabledRequest, new PowerupEnabledRequest {
                                    PowerupId = powerupId, Enabled = false
                                });
                                PostUpdateCommands.AddComponent(ghostEnabledRequest, new SendRpcCommandRequestComponent {
                                    TargetConnection = connectionEntity
                                });

                                if (id.Value == EntityManager.GetComponentData <SynchronizedCarComponent>(car).PlayerId)
                                {
                                    var powerupSlotChangedRequest = PostUpdateCommands.CreateEntity();
                                    PostUpdateCommands.AddComponent(powerupSlotChangedRequest, new PowerupSlotChangedRequest {
                                        SlotNumber = slotNumber, SlotContent = slotContent, SlotData = powerupSlotData
                                    });
                                    PostUpdateCommands.AddComponent(powerupSlotChangedRequest, new SendRpcCommandRequestComponent {
                                        TargetConnection = connectionEntity
                                    });
                                }
                            });
                        }

                        break;
                    }
                }
            }
        });
    }