Exemple #1
0
        public void Fire(Quaternion rotation)
        {
            // Lazer beam
            Destroy(Instantiate(LazerBeamPrefab, transform.position, rotation), 0.2f);

            // Check for hit
            RaycastHit            hit;
            MyCharacterController hitCC = null;

            if (Physics.Raycast(transform.position, rotation * Vector3.forward, out hit, 100, -1, QueryTriggerInteraction.Ignore))
            {
                // Spawn visual feedback
                Destroy(Instantiate(ImpactPrefab, hit.point, Quaternion.identity), 5f);

                // Authoritative logic
                if (!GameStatics.OnlineSession.IsClient())
                {
                    Rigidbody hitR = hit.collider.attachedRigidbody;
                    if (hitR)
                    {
                        hitCC = hitR.GetComponent <MyCharacterController>();
                        if (hitCC)
                        {
                            hitCC.OnHitFX();
                            GameStatics.GameManager.SimulationSystem.AddSimulationEvent(new LazerHitEvent(hitCC), GameStatics.GameManager.SimulationSystem.SimulationTick);
                        }
                    }
                }
            }

            if (GameStatics.OnlineSession.IsServer())
            {
                // Send lazer msg event
                LazerFireMsg lhm = new LazerFireMsg();
                lhm.AtTick         = GameStatics.GameManager.SimulationSystem.SimulationTick;
                lhm.FiringPlayerId = OwningCharacter.OwningPlayer.GetId();
                lhm.FiringRotation = rotation;
                lhm.HitCharacterId = (hitCC == null) ? -1 : hitCC.GetId();
                lhm.Serialize(GameStatics.OnlineSession.NetBuffer);
                GameStatics.OnlineSession.SendBufferToAllClients(OnlineSession.ReliableSequencedChannelId);
            }
        }
        private void OnReadyToInitiateGame()
        {
            int characterCounter = 0;

            // Create and spawn local players & characters
            {
                // Player controller
                MyPlayerController newPlayer = Instantiate(GameStatics.GameData.PlayerPrefab);
                newPlayer.OrbitCamera = Instantiate <OrbitCamera>(GameStatics.GameData.CameraPrefab);
                SimulationSystem.RegisterPlayerController(newPlayer, -1);
                if (GameStatics.OnlineSession.IsOnline())
                {
                    NetworkSpawnPlayer(newPlayer, newPlayer.GetId(), -1);
                }

                // Character
                MyCharacterController newCharacter = Instantiate(GameStatics.GameData.CharacterPrefab);
                newPlayer.OrbitCamera.SetFollowTransform(newCharacter.CameraFollowPoint);
                newPlayer.OrbitCamera.IgnoredColliders = newCharacter.GetComponentsInChildren <Collider>();
                PlaceCharacterAtSpawnPointIndex(newCharacter, characterCounter);
                KinematicCharacterSubsystem.RegisterCharacter(newCharacter, true);
                if (GameStatics.OnlineSession.IsOnline())
                {
                    NetworkSpawnCharacter(newCharacter, newCharacter.GetId(), newPlayer.GetId());
                }

                // Assign character to player
                newPlayer.Character       = newCharacter;
                newCharacter.OwningPlayer = newPlayer;

                characterCounter++;
            }

            // Create and spawn client players & characters
            foreach (ConnectionInfo ci in ConnectionInfos)
            {
                // Player controller
                MyPlayerController newPlayer = Instantiate(GameStatics.GameData.PlayerPrefab);
                SimulationSystem.RegisterPlayerController(newPlayer, ci.ConnectionId);
                if (GameStatics.OnlineSession.IsOnline())
                {
                    NetworkSpawnPlayer(newPlayer, newPlayer.GetId(), ci.ConnectionId);
                }

                // Register as player for that connection
                ci.PlayerControllerIds.Add(newPlayer.GetId());

                // Character
                MyCharacterController newCharacter = Instantiate(GameStatics.GameData.CharacterPrefab);
                PlaceCharacterAtSpawnPointIndex(newCharacter, characterCounter);
                KinematicCharacterSubsystem.RegisterCharacter(newCharacter, false);
                if (GameStatics.OnlineSession.IsOnline())
                {
                    NetworkSpawnCharacter(newCharacter, newCharacter.GetId(), newPlayer.GetId());
                }

                // Assign character to player
                newPlayer.Character       = newCharacter;
                newCharacter.OwningPlayer = newPlayer;

                characterCounter++;
            }

            // Tell clients to initiate game
            if (GameStatics.OnlineSession.IsOnline())
            {
                SimpleEventMsg sem = new SimpleEventMsg();
                sem.Event = SimpleEventMsg.EventType.InitiateGame;
                sem.Serialize(GameStatics.OnlineSession.NetBuffer);
                GameStatics.OnlineSession.SendBufferToAllClients(OnlineSession.ReliableSequencedChannelId);
            }
        }
 public void RegisterCharacterAtId(MyCharacterController character, int id, bool hasOwnership)
 {
     Characters[id] = character;
     character.SetId(id);
     character.SetHasOwnership(hasOwnership);
 }