Exemplo n.º 1
0
    void Awake()
    {
        // SIngltonとしての処理
        if (Instance != null)
        {
            Destroy(gameObject);
            return;
        }
        Instance = this;
        DontDestroyOnLoad(gameObject);

        Score     = new ReactiveProperty <int>();
        HighScore = new ReactiveProperty <int>();

        LoadHighScore();
        // スコアが更新されたら必要に応じてハイスコアを更新
        Score.WithLatestFrom(HighScore, (s, h) => new { s, h })
        .Where(v => v.s > v.h)
        .Subscribe(v => {
            HighScore.Value = v.s;
        })
        .AddTo(this);
        // ハイスコア更新されたら保存
        HighScore.Subscribe(_ => {
            SaveHighScore();
        }).AddTo(this);
    }
    private void SendMessageToOtherManagers(byte[] message, int type)
    {
        GameSystemData   data        = new GameSystemData(SystemID, message);
        MultiBaseRequest baseRequest = new MultiBaseRequest(MultiPossibleRequest.MultiGameData, MessagePackSerializer.Serialize(data));

        client.SendMessageToServer(MessagePackSerializer.Serialize(baseRequest));
    }
    //sends a request to all team managers to tell them to select a team
    public void SendSelectTeamRequest()
    {
        if (client.hostAuthority)
        {
            //store the system id and request type in the gamesystemdata object
            GameSystemData systemData = new GameSystemData(SystemID, new byte[1] {
                (byte)TeamSystemRequest.SelectTeamRequest
            });
            //create the base request with the serialized gamesystemdata object
            MultiBaseRequest baseRequest =
                new MultiBaseRequest(MultiPossibleRequest.MultiGameData, MessagePackSerializer.Serialize <GameSystemData>(systemData));

            //send the serialized request
            client.SendMessageToServer(MessagePackSerializer.Serialize <MultiBaseRequest>(baseRequest));
        }
    }
    protected void SendRequest <T>(T request, int requestType)
    {
        //get the team request as bytes
        byte[] serializedRequest = MessagePackSerializer.Serialize(request);
        //store the request with type
        List <byte> serializedRequestWType = new List <byte>(serializedRequest.Length + 1);

        serializedRequestWType.Add((byte)requestType);
        //store the rest of the request
        for (int i = 0; i < serializedRequest.Length; i++)
        {
            serializedRequestWType.Add(serializedRequest[i]);
        }

        //create the systemData request
        GameSystemData systemData = new GameSystemData(SystemID, serializedRequestWType.ToArray());
        //create the base request with the serialized gamesystemdata object
        MultiBaseRequest baseRequest =
            new MultiBaseRequest(MultiPossibleRequest.MultiGameData, MessagePackSerializer.Serialize(systemData));

        //send the serialized request
        client.SendMessageToServer(MessagePackSerializer.Serialize(baseRequest));
    }
 public abstract void HandleMessage(GameSystemData data);
Exemplo n.º 6
0
 public override void HandleMessage(GameSystemData message)
 {
 }
    public override void HandleMessage(GameSystemData data)
    {
        //get the request type from the first byte in the array
        TeamSystemRequest request = (TeamSystemRequest)data.D[0];

        //then remove that byte from the message to leave it readable
        data.D = data.D.AsMemory().Slice(1).ToArray();

        switch (request)
        {
        //we're receiving the state of the teams
        case TeamSystemRequest.TeamAllocationData:
            TeamAllocationData teamData = MessagePackSerializer.Deserialize <TeamAllocationData>(data.D);
            //store the new team setup
            ClientTeams = teamData.ClientTeams();

            //iterate through all of the clients we have data about
            foreach (int key in ClientTeams.Keys)
            {
                //change the team
                client.gamePlayers[key].team = ClientTeams[key];
                if (playerListUI)
                {
                    playerListUI.AddPlayerToTeamList(client.gamePlayers[key].AsPlayerData(), ClientTeams[key]);
                }
            }
            break;

        //someone wants to change teams
        case TeamSystemRequest.ChangeTeamRequest:
            ChangeTeamRequest teamRequest = MessagePackSerializer.Deserialize <ChangeTeamRequest>(data.D);

            //see if they have a team entry, if not, give them one, if yes, change the entry
            if (ClientTeams.TryGetValue(teamRequest.C, out _))
            {
                ClientTeams[teamRequest.C] = teamRequest.R;
            }
            else
            {
                ClientTeams.Add(teamRequest.C, teamRequest.R);
            }

            //change the player's team too
            client.gamePlayers[teamRequest.C].team = teamRequest.R;
            //and tell the player list ui to do the same
            if (playerListUI)
            {
                playerListUI.AddPlayerToTeamList(client.gamePlayers[teamRequest.C].AsPlayerData(), teamRequest.R);
            }

            //sync the system if we can
            SyncSystem();

            break;

        //we've been asked to select a team, so we need to ask the player
        case TeamSystemRequest.SelectTeamRequest:
            //spawn the correct select team ui depending on the input method
            LevelSelectUI levelUI;
            if (client.inputMethod == InputMethod.VR)
            {
                Instantiate(SelectTeamPhysicalUI);
            }
            else if (levelUI = FindObjectOfType <LevelSelectUI>())
            {
                levelUI.OpenNonVRSelectTeamUI(this);
            }
            break;
        }
    }
Exemplo n.º 8
0
    //listen for requests
    void messageReceivedListener(byte[] _message)
    {
        try
        {
            //get the base request object
            MultiBaseRequest baseRequest = MessagePackSerializer.Deserialize <MultiBaseRequest>(_message);
            Debug.Log(String.Format("<<<Client>>>: Recieved \"{0}\"", MessagePackSerializer.ConvertToJson(_message)));
            switch ((MultiPossibleRequest)baseRequest.RT)
            {
            //initial data for setup
            case MultiPossibleRequest.MultiInitialData:
                MultiInitialData initialData = MessagePackSerializer.Deserialize <MultiInitialData>(baseRequest.R);

                //store the thread key as the client id, they're the same thing
                _ClientID = initialData.T;
                actions.Enqueue(() => StartCoroutine(HandleMultiInitialData(initialData)));
                break;

            //the server would like to spawn an object
            case MultiPossibleRequest.MultiSpawnObject:
                MultiSpawnRequest multiSpawnRequest = MessagePackSerializer.Deserialize <MultiSpawnRequest>(baseRequest.R);

                //queue spawning the object
                actions.Enqueue(() => SpawnObject(multiSpawnRequest.I));
                break;

            //the server would like to sync an object
            case MultiPossibleRequest.MultiSyncObject:
                MultiSyncRequest multiSyncRequest = MessagePackSerializer.Deserialize <MultiSyncRequest>(baseRequest.R);

                //queue the synced object handle
                actions.Enqueue(() => syncedObjects[multiSyncRequest.ID].HandleSyncRequest(multiSyncRequest));
                break;

            //the server would like to change scenes
            case MultiPossibleRequest.MultiChangeScene:
                MultiChangeSceneRequest multiChangeScene = MessagePackSerializer.Deserialize <MultiChangeSceneRequest>(baseRequest.R);

                //queue the scene change
                actions.Enqueue(() => SceneManager.LoadScene(possibleScenes[multiChangeScene.N].name));
                currentScene = possibleScenes[multiChangeScene.N];

                actions.Enqueue(() =>
                {
                    //get all of the systems in this scene
                    GameObject[] systems = GameObject.FindGameObjectsWithTag("GameSystem");
                    //iterate through them, storing a reference to each of them
                    foreach (GameObject system in systems)
                    {
                        gameSystems.Add(system.GetComponent <GameSystem>().SystemID, system.GetComponent <GameSystem>());
                    }
                });

                //reset the dictionary of players
                gamePlayers = new Dictionary <int, GamePlayer>();

                //spawn our prefab on the network
                MultiSpawnPlayer spawnPlayerScene     = new MultiSpawnPlayer((int)inputMethod, _ClientID, TeamSystem.Team.A, PlayerName); //FindObjectOfType<TeamSystem>().localTeam);
                MultiBaseRequest spawnPlayerBaseScene = new MultiBaseRequest(MultiPossibleRequest.MultiSpawnPlayer, MessagePackSerializer.Serialize(spawnPlayerScene));
                SendMessageToServer(MessagePackSerializer.Serialize(spawnPlayerBaseScene));

                //spawn our prefab here
                actions.Enqueue(() =>
                {
                    //spawn in the new synced object instance
                    GameObject instance = Instantiate(possibleScenes[multiChangeScene.N].PlayerPrefabs[(int)inputMethod]);
                    GamePlayer player   = instance.GetComponent <GamePlayer>();
                    //store its index and its local status
                    player.LocalOwned = true;
                    //store it
                    gamePlayers[_ClientID] = player;

                    //do setup
                    player.PlayerSetup(_ClientID, TeamSystem.Team.A, PlayerName);     //FindObjectOfType<TeamSystem>().localTeam);
                });
                break;

            //the server would like to sync a scene object
            case MultiPossibleRequest.MultiSceneSyncObject:
                MultiSyncRequest multiSceneSync = MessagePackSerializer.Deserialize <MultiSyncRequest>(baseRequest.R);

                //queue the synced object handle
                actions.Enqueue(() => sceneSyncedObjects[multiSceneSync.ID].HandleSyncRequest(multiSceneSync));
                break;

            //the server wants us to take host authority
            case MultiPossibleRequest.MultiHostAuthChange:
                hostAuthority = true;
                break;

            //there's new connection to the game, send a dict of id's and indexes
            case MultiPossibleRequest.MultiNewConnection:
                MultiNewConnection newConnection = MessagePackSerializer.Deserialize <MultiNewConnection>(baseRequest.R);
                //dictionary of the sceneobjects with non negative indices(root objects)
                Dictionary <int, int> idIndexes = new Dictionary <int, int>();
                foreach (int key in syncedObjects.Keys)
                {
                    idIndexes.Add(key, syncedObjects[key].index);
                }


                //Send the request
                MultiInitialData sceneObjects = new MultiInitialData(newConnection.tN, idIndexes, gamePlayers, possibleScenes.IndexOf(currentScene), syncedObjectsTotal, newConnection.tN);
                MultiBaseRequest request      = new MultiBaseRequest(MultiPossibleRequest.MultiInitialData, MessagePackSerializer.Serialize(sceneObjects));
                SendMessageToServer(MessagePackSerializer.Serialize(request));

                break;

            //the server would like us to despawn an object
            case MultiPossibleRequest.MultiDespawnObject:
                MultiDespawnObject despawnObject = MessagePackSerializer.Deserialize <MultiDespawnObject>(baseRequest.R);

                //destroy the object
                Debug.Log(String.Format("MultiClient.cs:291 Destroying {0}", syncedObjects[despawnObject.ID].gameObject.name));
                actions.Enqueue(() => Destroy(syncedObjects[despawnObject.ID].gameObject));
                break;

            case MultiPossibleRequest.MultiGameData:
                GameSystemData systemData = MessagePackSerializer.Deserialize <GameSystemData>(baseRequest.R);

                //pass on this data, the game system knows what to do with it
                actions.Enqueue(() => gameSystems[systemData.S].HandleMessage(systemData));
                break;

            case MultiPossibleRequest.MultiSpawnPlayer:
                MultiSpawnPlayer spawnPlayer = MessagePackSerializer.Deserialize <MultiSpawnPlayer>(baseRequest.R);

                //spawn in the player and ensure it knows which client it belongs to
                actions.Enqueue(() =>
                {
                    GameObject instance = Instantiate(currentScene.PlayerPrefabs[spawnPlayer.T]);

                    GamePlayer gamePlayer = instance.GetComponent <GamePlayer>();
                    //store it
                    gamePlayers[spawnPlayer.C] = gamePlayer;

                    //do setup
                    gamePlayer.PlayerSetup(spawnPlayer.C, (TeamSystem.Team)spawnPlayer.t, spawnPlayer.P);
                });
                break;

            case MultiPossibleRequest.MultiSyncPlayer:
                MultiSyncPlayer syncPlayer = MessagePackSerializer.Deserialize <MultiSyncPlayer>(baseRequest.R);

                actions.Enqueue(() => gamePlayers[syncPlayer.C].HandleMessage(syncPlayer.S));

                break;
            }
        }
        catch (Exception ex)
        {
            Debug.LogError(ex);
            Debug.LogError(_message);
        }
    }