Esempio n. 1
0
        // Create and initialize the player object
        private MDPlayerInfo GetOrCreatePlayerObject(int PeerId)
        {
            if (Players.ContainsKey(PeerId))
            {
                return(Players[PeerId]);
            }

            Type PlayerType = GameInstance.GetPlayerInfoType();

            if (!MDStatics.IsSameOrSubclass(PlayerType, typeof(MDPlayerInfo)))
            {
                MDLog.Error(LOG_CAT, $"Provided player type [{PlayerType.Name}] is not a subclass of MDPlayerInfo");
                return(null);
            }

            MDPlayerInfo Player = Activator.CreateInstance(PlayerType) as MDPlayerInfo;

            Player.SetPeerId(PeerId);
            Player.PauseMode = PauseModeEnum.Process;
            AddChild(Player);
            Players.Add(PeerId, Player);

            OnPlayerInfoCreated(Player);

            return(Player);
        }
Esempio n. 2
0
        public void Disconnect()
        {
            MDLog.Info(LOG_CAT, "Disconnected from server");
            IsSessionStarted = false;
            foreach (int PeerId in Players.Keys)
            {
                MDPlayerInfo Player = Players[PeerId];
                OnPlayerLeftEvent(PeerId);
                Player.RemoveAndFree();
            }

            NetworkedMultiplayerENet peer = GetPeer();

            if (peer != null)
            {
                peer.CloseConnection();
                SetNetworkPeer(null);
            }

            StopUPNP();
            Players.Clear();
            ClearNetworkedNodes();
            OnSessionEndedEvent();
            SceneBuffer.Clear();
        }
Esempio n. 3
0
        /// <summary>
        /// Get the  player info for the peer
        /// </summary>
        /// <param name="Instance">The node this is called from</param>
        /// <param name="PeerId">The peer id</param>
        /// <typeparam name="T">The type of player info you want</typeparam>
        /// <returns>The player info as T</returns>
        public static T GetPlayerInfo <T>(this Node Instance, int PeerId) where T : MDPlayerInfo
        {
            MDGameSession Session    = Instance.GetGameSession();
            MDPlayerInfo  PlayerInfo = Session.GetPlayerInfo(PeerId);

            return(PlayerInfo as T);
        }
Esempio n. 4
0
 // Removes the MDPlayerInfo belonging to the PeerId
 private void RemovePlayerObject(int PeerId)
 {
     if (Players.ContainsKey(PeerId))
     {
         MDPlayerInfo Player = Players[PeerId];
         PreparePlayerInfoForRemoval(Player);
         Player.RemoveAndFree();
         Players.Remove(PeerId);
     }
 }
Esempio n. 5
0
        private void OnPlayerInitialized(int PeerId)
        {
            MDPlayerInfo PlayerInfo = GetPlayerInfo(PeerId);

            if (PlayerInfo != null)
            {
                PlayerInfo.HasInitialized = true;
                NotifyPlayerInitializedEvent(PeerId);
                MDLog.Debug(LOG_CAT, $"Player {PeerId} Initialized");
            }
        }
Esempio n. 6
0
        public bool StartStandalone()
        {
            MDLog.Info(LOG_CAT, "Starting Standalone Game Session");
            OnPlayerJoined_Internal(STANDALONE_ID);
            MDPlayerInfo PlayerInfo = GetPlayerInfo(STANDALONE_ID);

            if (PlayerInfo != null)
            {
                PlayerInfo.BeginInitialization();
            }
            OnSessionStartedEvent();
            IsSessionStarted = true;
            return(true);
        }
Esempio n. 7
0
        private void SynchronizeCurrentPlayers(int Joiner)
        {
            foreach (int PeerId in Players.Keys)
            {
                if (PeerId == Joiner)
                {
                    continue;
                }

                MDPlayerInfo PlayerInfo = GetPlayerInfo(PeerId);
                if (PlayerInfo != null)
                {
                    this.MDRpcId(Joiner, nameof(OnSynchronizePlayer), PeerId, PlayerInfo.HasInitialized);
                }
            }
        }
Esempio n. 8
0
        // Called on the server when a client connects
        private void ServerOnPeerConnected(int PeerId)
        {
            MDLog.Info(LOG_CAT, $"Peer [ID: {PeerId}] connected");
            OnPlayerJoined_Internal(PeerId);

            MDPlayerInfo PlayerInfo = GetPlayerInfo(PeerId);

            if (PlayerInfo != null)
            {
                PlayerInfo.BeginInitialization();
            }

            SynchronizeCurrentPlayers(PeerId);
            BroadcastNewPlayerJoined(PeerId);
            SynchronizeNetworkedNodes(PeerId);
        }
Esempio n. 9
0
        private void ServerOnStarted()
        {
            if (GameInstance.UseUPNP())
            {
                ServerUPNP = InitUPNP(UPNPPort);
            }

            MDLog.Info(LOG_CAT, "Server started");
#if !GODOT_SERVER
            OnPlayerJoined_Internal(SERVER_ID);
            MDPlayerInfo PlayerInfo = GetPlayerInfo(SERVER_ID);
            if (PlayerInfo != null)
            {
                PlayerInfo.BeginInitialization();
            }
#endif
            OnSessionStartedEvent();
            IsSessionStarted = true;
        }
Esempio n. 10
0
 /// <summary>
 /// Called right before a player info is removed
 /// </summary>
 /// <param name="PlayerInfo">The player info that is going to be removed</param>
 protected virtual void PreparePlayerInfoForRemoval(MDPlayerInfo PlayerInfo)
 {
 }
Esempio n. 11
0
 /// <summary>
 /// Called on all clients to perform any initialization on a new player info when a player joins, include self
 /// </summary>
 /// <param name="PlayerInfo">The player info for the joining client</param>
 protected virtual void OnPlayerInfoCreated(MDPlayerInfo PlayerInfo)
 {
 }