Exemplo n.º 1
0
    public void startGame()
    {
        if (localPlayer.ID != currentPlayer)
        {
            LoadScene(SceneConstants.WAITING_FOR_PLAYERS);
            return;
        }
        PrintLogger.printLog("GameManager: Starting game as player " + localPlayer.ID);

        audioRecorder.startRecording();
        // Since starting and ending 3 recordings in one session sometimes crashes android, this one is commented out

        LoadScene(SceneConstants.SELECT_PERSON);
    }
Exemplo n.º 2
0
    public void LoadScene(int newScene, bool reload = false)
    {
        // If reload is set to true, the method will load the scene even if it's already loaded
        if (newScene < 0 || currentScene == newScene && !reload)
        {
            PrintLogger.printLog("GameManager: Failed loading scene " + newScene + " (already loaded)");
            return;
        }

        if (isStoryView &&
            newScene != SceneConstants.BASE &&
            newScene != SceneConstants.STORY_SCREEN)
        {
            return;
        }

        if (currentScene != SceneConstants.BASE &&
            currentScene != SceneConstants.WAITING_FOR_PLAYERS &&
            newScene != SceneConstants.START &&
            newScene < SceneConstants.SELECT_SOLUTION &&
            currentPlayer != localPlayer.ID)
        {
            // TODO: Instead use game states to decide when a turn is in progress and when to start/end the game
            newScene = SceneConstants.WAITING_FOR_PLAYERS;
            PrintLogger.printLog("GameManager: Switching to waiting screen (currentPlayer: " + currentPlayer + ")");
        }
        else if (newScene == SceneConstants.SELECT_PERSON && turnsPlayed >= 1)
        {
            newScene = SceneConstants.TURN_START;
        }

        int scenesUnloaded = 0;

        for (int i = 0; i < SceneManager.sceneCount; i++)
        {
            Scene scene = SceneManager.GetSceneAt(i);
            if (scene.buildIndex != 0)
            {
                SceneManager.UnloadSceneAsync(scene);
                scenesUnloaded++;
            }
        }

        PrintLogger.printLog("GameManager: Switching to scene " + newScene + " from scene " + currentScene
                             + " (current turn: " + turnsPlayed + ", unloaded " + scenesUnloaded + " scenes)");

        SceneManager.LoadScene(newScene, LoadSceneMode.Additive);
        currentScene = newScene;
    }
    void OnApplicationQuit()
    {
        if (isHost && matchInfo != null)
        {
            // If the match is not destroyed, the room will stay visible for 20-30 seconds, confusing new clients

            // This method is not reliable, as the application quits before DestroyMatch is able to finish
            networkMatcher.DestroyMatch(matchInfo.networkId, 0, OnDestroyMatch);
            PrintLogger.printLog("NetworkManager: Application quit with client as host; destroying room");
        }
        networkMatcher.DropConnection(matchInfo.networkId, matchInfo.nodeId, 0, OnDropConnection);

        GameManagerScript.instance.isActivePlayer = false;
        GameManagerScript.instance.registerPlayers();
    }
Exemplo n.º 4
0
    public override void OnStartClient()
    {
        base.OnStartClient();

        PrintLogger.printLog("GameManager: Client Starting");

        if (isActivePlayer)
        {
            LoadScene(SceneConstants.START);
        }
        else if (isStoryView)
        {
            LoadScene(SceneConstants.STORY_SCREEN);
        }
    }
Exemplo n.º 5
0
    void SelectLinkToTrait(TraitScript trait)
    {
        nextButton.interactable = true; // In case of null pointer

        newLink.storyTagLink = null;
        newLink.traitLink    = trait.TraitObject;

        nextButton.interactable = true;

        PrintLogger.printLog("Attached link to trait: '" + trait.TraitObject.description + "'");
        print(trait.transform.position);

        putLine(trait.transform);

        //storyView.makeCurve(trait.transform.position, selectedStoryTag.transform.position);
    }
    public void nextScene()
    {
        var finishButton = GameObject.Find("Finished Button");

        if (finishButton != null)
        {
            print("Destroying finish button");
            Destroy(finishButton.gameObject);

            Invoke("nextScene", 0.1f);
        }
        else
        {
            PrintLogger.printLog("EventSystem: Going to next scene");
            gameManager.nextScene();
        }
    }
Exemplo n.º 7
0
    public void nextTurn()
    {
        if (!isServer)
        {
            PrintLogger.printLog("GameManager (client): Ended turn as player " + localPlayer.ID);
            localPlayer.CmdNextTurn();
            return;
        }

        // currentTurnIsPositive alternates every turn and again if even playerCount and round : P,N,|N,P
        currentTurnIsPositive = activePlayerCount % 2 == 0 && currentRound % 2 == 0 ?
                                (turnsPlayed + 1) % 2 == 1 : turnsPlayed % 2 == 1;

        turnsPlayed  += 1;
        currentPlayer = (turnsPlayed + 1) % activePlayerCount; // Go through the players round-robin style:

        PrintLogger.printLog("GameManager (server): Next turn");
    }
    public void CmdNextTurn()
    {
        PrintLogger.printLog("NetworkPlayer: Going to next turn/player");

        GameManagerScript.instance.nextTurn();

        // Moved to gameManager - now this command is just to make sure the gameManager runs its method on the server

        /*gameManager.turnsPlayed += 1;
         * gameManager.currentPlayer = gameManager.currentPlayer >= gameManager.playerCount - 1 ? 0 : gameManager.currentPlayer + 1;
         * // Go through the players round-robin style
         *
         * if (gameManager.turnsPlayed >= 5)
         * {
         *  gameManager.RpcEndGame();
         *  return;
         * }*/
    }
    public override void OnClientDisconnect(NetworkConnection conn)
    {
        if (conn != null)
        {
            base.OnClientDisconnect(conn);
        }
        PrintLogger.printLog("Client disconnected, retrying...");

        if (!restartMode)
        {
            isConnected  = false;
            isHost       = false;
            currentMatch = null;
            sessionID    = 0;
            GameManagerScript.instance.LoadScene(SceneConstants.OFFLINE);

            startConnection();
        }
    }
Exemplo n.º 10
0
    public void registerPlayers(bool clientToServer = false)
    {
        if (!isServer && clientToServer)
        {
            if (clientToServer)
            {
                localPlayer.CmdRegisterPlayer();
            }
            return;
        }

        print("GameManager: Starting registering new players");

        var foundPlayers = new List <NetworkPlayerScript>(FindObjectsOfType <NetworkPlayerScript>());

        foreach (var player in playerList.ToArray())
        {
            if (!foundPlayers.Contains(player))
            {
                playerList.Remove(player);
                continue;
            }
        }

        activePlayerCount = 0;
        foreach (var player in foundPlayers)
        {
            int newPlayerID = -1;

            if (player.isActivePlayer)
            {
                activePlayerCount += 1;
                newPlayerID        = getNewPLayerID();
            }

            if (!playerList.Contains(player))
            {
                player.ID = newPlayerID;
                playerList.Add(player);
                PrintLogger.printLog("GameManager (server): Registered player: " + newPlayerID);
            }
        }
    }
    public override void OnClientConnect(NetworkConnection conn)
    {
        base.OnClientConnect(conn);
        PrintLogger.printLog("Client Connection Established");

        isConnected = true;

        if (currentMatch != null)
        {
            var reg = Regex.Match(currentMatch, "^Session ([0-9]+) \\(ch[0-9]+\\)$");
            if (reg.Success)
            {
                sessionID = int.Parse(reg.Groups[1].Value);
                // Group 1 is the first set of parantheses in the regex, which is the session number
                return;
            }
        }

        print("NetworkManager: Invalid Match name '" + currentMatch + "'");
    }
    public void selectCharacter(PersonElementScript personElement)
    {
        Person selectedPerson = null;

        if (personElement == personElement1)
        {
            selectedPerson = personsList[0];
        }
        else if (personElement == personElement2)
        {
            selectedPerson = personsList[1];
        }
        else if (personElement == personElement3)
        {
            selectedPerson = personsList[2];
        }

        gameManager.setPerson(selectedPerson);

        PrintLogger.printLog("CharSelectSystem: New character selected: " + selectedPerson.personName);
        nextScene();
    }
Exemplo n.º 13
0
    public void OnPointerUp(PointerEventData eventData)
    {
        if (!isDragging)
        {
            return;
        }
        PrintLogger.printLog("Dropped StoryTag link");

        isDragging = false;
        var storyV = GameObject.Find("Story Scroll View").GetComponent <RectTransform>();

        //Rect storyViewRect = new Rect(storyV.offsetMax.x, storyV.offsetMax.y,
        //   storyV.rect.width, storyV.rect.height);

        // Check if the linkEnd is inside the storyView
        // (for some reason, .localPosition is top-right and the only one that gives a relatable position for the comparison)
        if (storyV.localPosition.x >= RT.localPosition.x &&
            storyV.localPosition.x - storyV.rect.width <= RT.localPosition.x &&
            storyV.localPosition.y >= RT.localPosition.y &&
            storyV.localPosition.y - storyV.rect.height <= RT.localPosition.y)
        {
            eventSystem.attemptLinkAtPos(transform.position);
        }
    }
Exemplo n.º 14
0
    public static bool Save(string filename, AudioClip clip)
    {
        if (!filename.ToLower().EndsWith(".wav"))
        {
            filename += ".wav";
        }

        var filepath = Path.Combine(Application.persistentDataPath, filename);


        PrintLogger.printLog("Saving audio recording in path: " + filepath);

        // Make sure directory exists if user is saving to sub dir.
        Directory.CreateDirectory(Path.GetDirectoryName(filepath));

        using (var fileStream = CreateEmpty(filepath))
        {
            ConvertAndWrite(fileStream, clip);

            WriteHeader(fileStream, clip);
        }

        return(true); // TODO: return false if there's a failure saving the file
    }
Exemplo n.º 15
0
    public void endRecording(bool trimToSize = true)
    {
        if (Microphone.IsRecording(micDevice))
        {
            PrintLogger.printLog("Ending audio recording");
            if (recording == null)
            {
                PrintLogger.printLog("Failed ending recording: recording became null");
                return;
            }

            if (trimToSize) // Sometimes causes crashes at 3 recordings on Android
            {
                //// Following code copied from
                //// https://answers.unity.com/questions/544264/record-dynamic-length-from-microphone.html

                //Capture the current clip data
                var position = Microphone.GetPosition(micDevice);

                var recording2 = recording;

                float[] soundData = new float[1];

                try
                {
                    soundData = new float[recording2.samples * recording2.channels];
                    PrintLogger.printLog("End 0.5");
                    recording2.GetData(soundData, 0);
                } catch (Exception e)
                {
                    PrintLogger.printLog("Caught exception: " + e.Message);
                }

                //Create shortened array for the data that was used for recording
                var newData = new float[position * recording2.channels];

                //Copy the used samples to a new array
                for (int i = 0; i < newData.Length; i++)
                {
                    newData[i] = soundData[i];
                }

                //One does not simply shorten an AudioClip,
                //    so we make a new one with the appropriate length
                var newClip = AudioClip.Create(recording2.name, position, recording2.channels,
                                               recording2.frequency, false, false);

                newClip.SetData(newData, 0);        //Give it the data from the old clip

                //Replace the old clip
                AudioClip.Destroy(recording);
                recording = newClip;

                //// End copied code
            }

            Microphone.End(micDevice);

            unsavedRecList.Add(recording);
        }
    }
Exemplo n.º 16
0
 public void OnPointerDown(PointerEventData eventData)
 {
     isDragging = true;
     transform.GetChild(0).GetComponent <Image>().enabled = false;
     PrintLogger.printLog("Picked up StoryTag link");
 }
Exemplo n.º 17
0
    public void OnNewTurn(int newTurnsPlayed) // Updated on each client once turnsPlayed changes
    {
        turnsPlayed = newTurnsPlayed;

        if (turnsPlayed % activePlayerCount == 0)
        {
            currentRound += 1;
        }

        currentPlayer         = (turnsPlayed + 1) % activePlayerCount; // Redundantly assign these on each client
        currentTurnIsPositive = activePlayerCount % 2 == 0 && currentRound % 2 == 0 ?
                                (turnsPlayed + 1) % 2 == 1 : turnsPlayed % 2 == 1;

        if (currentTurnIsPositive)
        {
            storyTagPositiveIter += 1;
        }
        else
        {
            storyTagNegativeIter += 1;
        }

        if (!isActivePlayer)
        {
            if (isStoryView)
            {
                LoadScene(SceneConstants.STORY_SCREEN, true); // Reload the view every new turn
            }
            return;
        }

        PrintLogger.printLog("GameManager (client): Next turn; player: " + currentPlayer);

        if (audioRecorder.recordingsSaved >= 2)
        {
            // Trying to end and trim the third recording may crash, so try saving non-trimmed instead
            audioRecorder.endRecording(false);
            audioRecorder.saveAllRecordings();
        }
        else
        {
            audioRecorder.endRecording();      // Ends any ongoing audio recording
            audioRecorder.saveAllRecordings(); // Takes a second or two
        }


        // Everyone gets to play twice:
        if (currentRound > maxRounds)
        {
            PrintLogger.printLog("GameManager: Ending game (turnsplayed: " + turnsPlayed + ")");
            if (isServer)
            {
                RpcEndGame();
            }
            else
            {
                localPlayer.CmdEndGame();
            }
            return;
        }

        PrintLogger.printLog("GameManager: Starting new turn (scene: " + currentScene + ", current player: " + currentPlayer
                             + ", local player: " + localPlayer.ID + ")");
        if (currentPlayer == localPlayer.ID)
        {
            audioRecorder.startRecording();
            LoadScene(SceneConstants.TURN_START);
        }
        else
        {
            LoadScene(SceneConstants.WAITING_FOR_PLAYERS);
        }
    }
 public void previousScene()
 {
     PrintLogger.printLog("EventSystem: Going to previous scene");
     gameManager.previousScene();
 }
 public void OnNewID(int newID)
 {
     PrintLogger.printLog("NetworkPlayer: Registered Player ID: " + newID);
     ID = newID;
 }
Exemplo n.º 20
0
 public void findNewNetworkMatch()
 {
     PrintLogger.printLog("GameManager: Finding new session / match");
     networkManager.findNewMatch();
 }