Ejemplo n.º 1
0
        public Vector2 PlayerJoystickAxis(PlayerColorId playerId)
        {
            // Sanity checks.
            if (this.playerInputs == null)
            {
                return(Vector2.zero);
            }
            if (playerId == PlayerColorId.UNKNOWN)
            {
                return(Vector2.zero);
            }
            if (this.configuring == true)
            {
                return(Vector2.zero);
            }

            // Get the player input via id.
            PlayerInput playerInput = this.GetPlayerInputById(playerId);

            if (playerInput == null)
            {
                return(Vector2.zero);
            }
            if (playerInput.joystickId <= 0)
            {
                return(Vector2.zero);
            }

            string axisName = "Joystick" + playerInput.joystickId; // Eg Joystick1_xAxis

            return(new Vector2(Input.GetAxis(axisName + "_xAxis"), Input.GetAxis(axisName + "_yAxis")));
        }
Ejemplo n.º 2
0
 // Methods
 public void ResetPlayer()
 {
     this.playerId             = PlayerColorId.UNKNOWN;
     this.joystickId           = -1;
     this.firstButtonKeyCodeId = 0;
     this.buttonsPressed.Clear();
     this.lastJoystickPosition = Vector2.zero;
 }
Ejemplo n.º 3
0
 public static Vector2 PlayerJoystickAxisStatic(PlayerColorId playerId)
 {
     // Make sure a SAE.ArcadeMachine.input singleton exists.
     if (SAE.ArcadeMachine.input == null)
     {
         return(Vector2.zero);
     }
     return(SAE.ArcadeMachine.input.PlayerJoystickAxis(playerId));
 }
Ejemplo n.º 4
0
 // Convenience static methods.
 public static bool PlayerPressingButtonStatic(PlayerColorId playerId, int buttonId, bool singlePress = false)
 {
     // Make sure a SAE.ArcadeMachine.input singleton exists.
     if (SAE.ArcadeMachine.input == null)
     {
         return(false);
     }
     return(SAE.ArcadeMachine.input.PlayerPressingButton(playerId, buttonId, singlePress));
 }
Ejemplo n.º 5
0
 private PlayerInput GetPlayerInputById(PlayerColorId playerId)
 {
     // Sanity check.
     if (this.playerInputs == null)
     {
         return(null);
     }
     return(this.playerInputs.FirstOrDefault(item => item.playerId == playerId));
 }
Ejemplo n.º 6
0
        public bool PlayerPressingButton(PlayerColorId playerId, int buttonId, bool singlePress = false)
        {
            // Sanity checks.
            if (this.playerInputs == null)
            {
                return(false);
            }
            if (playerId == PlayerColorId.UNKNOWN)
            {
                return(false);
            }
            if (buttonId < 0)
            {
                return(false);
            }
            if (this.configuring == true)
            {
                return(false);
            }

            // Get the player input via id.
            PlayerInput playerInput = this.GetPlayerInputById(playerId);

            if (playerInput == null)
            {
                return(false);
            }

            // Calculate the button keyCode id and check the key.
            if (singlePress == false)
            {
                return(Input.GetKey(( KeyCode )playerInput.firstButtonKeyCodeId + buttonId));
            }
            else
            {
                return(Input.GetKeyDown(( KeyCode )playerInput.firstButtonKeyCodeId + buttonId));
            }
        }
Ejemplo n.º 7
0
            public Vector2 lastJoystickPosition;                                    // Last checked joystick x,y axis position.

            // Constructor
            public PlayerInput()
            {
                this.playerId       = PlayerColorId.UNKNOWN;
                this.joystickId     = -1;
                this.buttonsPressed = new List <KeyCode>();
            }
Ejemplo n.º 8
0
        public void ConfigurePlayers()
        {
            // Set configuring state.
            this.configuring = true;

            // Check if configuring in the editor.
            if (Application.isEditor == true && this.dontConfigureInEditor == true)
            {
                string[] allJoystickNames = Input.GetJoystickNames();
                Debug.Log("[SAE.ArcadeMachine] " + allJoystickNames.Length + " joysticks autoconfiguring in editor.");

                // Automatically set the detected joysticks using the order they are detected in (Eg first joystick will be yellow player etc)
                for (int i = 0; i < allJoystickNames.Length; i++)
                {
                    if (i >= this.playerInputs.Length)
                    {
                        this.FinishConfiguration(); return;
                    }
                    if (i >= Enum.GetNames(typeof(PlayerColorId)).Length)
                    {
                        this.FinishConfiguration(); return;
                    }

                    this.playerInputs[i].playerId   = ( PlayerColorId )i + 1;
                    this.playerInputs[i].joystickId = i + 1;
                    this.SetPlayerInputFirstButtonKeyCodeId(this.playerInputs[i]);
                }
                this.FinishConfiguration();
                return;
            }

            // Check if skipping configuring any other players.
            if (Input.GetKeyDown(KeyCode.Escape) == true)
            {
                this.FinishConfiguration();
                return;
            }

            // Find the next player to configure.
            int maxPlayers = Enum.GetNames(typeof(PlayerColorId)).Length - 1;

//maxPlayers = 2; // TESTING
            for (int p = 1; p < maxPlayers + 1; p++)
            {
                PlayerColorId playerId = ( PlayerColorId )p;
                if (this.GetPlayerInputById(playerId) == null)
                {
//Debug.Log( "[SAE.ArcadeMachine] Configure " + playerId.ToString() );

                    // Check there is a configuration canvas. If not, create one.
                    if (this.configureCanvas == null)
                    {
                        // Create a canvas.
                        GameObject configCanvas = new GameObject("configCanvas");
                        this.configureCanvas            = configCanvas.AddComponent <Canvas>();
                        this.configureCanvas.renderMode = RenderMode.ScreenSpaceOverlay;

                        // Create a black background to hide the scene.
                        GameObject bgPanelGO = new GameObject("bgPanel");
                        bgPanelGO.transform.SetParent(configCanvas.transform);
                        Image bgPanel = bgPanelGO.AddComponent <Image>();
                        bgPanel.color = Color.black;
                        RectTransform rtPanel = bgPanel.GetComponent <RectTransform>();
                        rtPanel.anchoredPosition = Vector2.zero;
                        rtPanel.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, Screen.width);
                        rtPanel.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, Screen.height);

                        // Create configuration text.
                        GameObject textGO = new GameObject("configText");
                        textGO.transform.SetParent(bgPanel.transform);
                        this.configurationText       = textGO.AddComponent <Text>();
                        this.configurationText.color = Color.white;
                        RectTransform rtText = this.configurationText.GetComponent <RectTransform>();
                        rtText.anchoredPosition = Vector2.up * (Screen.height * 0.15f);
                        rtText.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, Screen.width * 0.9f);
                        this.configurationText.resizeTextForBestFit = true;
                        this.configurationText.font             = Font.CreateDynamicFontFromOSFont("Arial", 36);// Mathf.RoundToInt( Screen.width * 0.02f ) );
                        this.configurationText.supportRichText  = true;
                        this.configurationText.verticalOverflow = VerticalWrapMode.Overflow;
                        this.configurationText.alignment        = TextAnchor.UpperCenter;

                        this.configurationStartTime = Time.unscaledTime;
                    }

                    // Set the title text for configuring.
                    this.configurationText.text = "<b>Input Configuration</b>\n\n";

                    // Give a few seconds before checking for buttons.
                    if (Time.unscaledTime < this.configurationStartTime + 2f)
                    {
                        return;
                    }

                    // Set the text for the configuring player.
                    string playerColorStr = playerId.ToString().Replace("_PLAYER", "");
                    this.configurationText.text += "<color=" + playerColorStr.ToLower() + ">" + playerColorStr + " player press a button or move the joystick.</color>";

                    PlayerInput playerInput = null;

                    // Check for any axis input.
                    int numSetupAxis = 8; // This is the number of xAxis and yAxis joystick inputs set up in the legacy InputManager.
                    for (int i = 0; i < numSetupAxis; i++)
                    {
                        // Check if the joystick id has already been assigned.
                        if (this.GetPlayerInputByJoystickId(i + 1) != null)
                        {
                            continue;
                        }

                        // Check the axis for movement.
                        string axisName = "Joystick" + (i + 1);   // Eg Joystick1_xAxis
                        if (Mathf.Abs(Input.GetAxis(axisName + "_xAxis")) < 0.5f && Mathf.Abs(Input.GetAxis(axisName + "_yAxis")) < 0.5f)
                        {
                            continue;
                        }

                        // Assign the joystick id to the first UNKNOWN player.
                        playerInput = this.GetPlayerInputById(PlayerColorId.UNKNOWN);
                        if (playerInput == null)
                        {
                            Debug.LogWarning("[SAE.ArcadeMachine] Could not get an UNKNOWN player in configuration?!");
                            this.configuring = false;
                            return;
                        }
                        playerInput.playerId   = playerId;
                        playerInput.joystickId = i + 1;

                        break;
                    }

                    // Check for any button presses.
                    if (playerInput == null)
                    {
                        int startKeyCodeId = ( int )KeyCode.Joystick1Button0;   // 350
                        int endKeyCodeId   = ( int )KeyCode.Joystick8Button19;  // 509
                        for (int i = startKeyCodeId; i < endKeyCodeId; i++)
                        {
                            KeyCode keyCode    = ( KeyCode )i;
                            bool    keyPressed = Input.GetKey(keyCode);
                            if (keyPressed == false)
                            {
                                continue;
                            }

                            // Get the joystick and button ids for the pressed keyCode.
                            int joystickId = -1, buttonId = -1;
                            if (this.GetKeyCodeJoystickAndButtonId(keyCode, ref joystickId, ref buttonId) == false)
                            {
                                continue;
                            }
                            if (joystickId == -1 || buttonId == -1)
                            {
                                continue;
                            }                                                      //?

                            // Check if the pressed buttons' joystick id already belongs to another player.
                            if (this.GetPlayerInputByJoystickId(joystickId) != null)
                            {
                                continue;
                            }

                            // Assign the joystick id to the first UNKNOWN player.
                            playerInput = this.GetPlayerInputById(PlayerColorId.UNKNOWN);
                            if (playerInput == null)
                            {
                                Debug.LogWarning("[SAE.ArcadeMachine] Could not get an UNKNOWN player in configuration?!");
                                this.configuring = false;
                                return;
                            }
                            playerInput.playerId   = playerId;
                            playerInput.joystickId = joystickId;

                            break;
                        }
                    }

                    if (playerInput != null)
                    {
                        // Set the first keyCode button based on playerId.
                        this.SetPlayerInputFirstButtonKeyCodeId(playerInput);

                        Debug.Log("[SAE.ArcadeMachine] " + playerInput.playerId.ToString() + " was assigned to joystick " + playerInput.joystickId);

                        // Check if finished configuring (Eg no more UNKOWN players)
                        if (p == maxPlayers)
                        {
                            this.FinishConfiguration();
                        }
                    }

                    return;
                }
            }
        }