Example #1
0
    void Start()
    {
        // find the server so that we can interact with it
        _server = Utility.FindComponent <Server>(Server.NAME);
        _server.ConnectionLostEvent += OnConnectionLost;
        _server.ExtensionEvent      += OnResponse;

        _chat = Utility.FindComponent <ChatController>(ChatController.NAME);

        _cam = Camera.main.GetComponent <CamFollow>();

        LoadMap();

        // pull player image from prefs and randomly place them on the top of the map
        int type = PlayerPrefs.GetInt(Constants.PLAYER_TYPE, 0);;
        int x    = Random.Range(5, 20);
        int y    = Random.Range(0, -10);

        _player = CreateCharacter(type, new Vector2(x, y));

        _cam.Target = _player.gameObject;

        _otherPlayers = new Dictionary <string, Player>();

        // let the server (and other players) this player has just joined the game
        PsObject psobj = new PsObject();

        psobj.SetInt(ServerConstants.PLAYER_TYPE, type);
        psobj.SetIntArray(ServerConstants.PLAYER_POSITION, new List <int>()
        {
            x, y
        });

        _server.SendRequest(new ExtensionRequest(PlayerCommand.GetCommand(PlayerCommand.PlayerEnum.Start), psobj));

        _running = true;
    }
Example #2
0
    void Update()
    {
        if (_running && _player.CanMove)
        {
            bool send = false;

            // check if the player can move in the direction requested.  if not make them face that direction
            if (Input.GetKey(KeyCode.DownArrow))
            {
                if (_map.CanMove(_player.Position + Player.DIR_DOWN))
                {
                    _player.MoveTo(PlayerDirection.Down);
                    send = true;
                }
                else
                {
                    _player.Face(PlayerDirection.Down);
                }
            }
            else if (Input.GetKey(KeyCode.LeftArrow))
            {
                if (_map.CanMove(_player.Position + Player.DIR_LEFT))
                {
                    _player.MoveTo(PlayerDirection.Left);
                    send = true;
                }
                else
                {
                    _player.Face(PlayerDirection.Left);
                }
            }
            else if (Input.GetKey(KeyCode.RightArrow))
            {
                if (_map.CanMove(_player.Position + Player.DIR_RIGHT))
                {
                    _player.MoveTo(PlayerDirection.Right);
                    send = true;
                }
                else
                {
                    _player.Face(PlayerDirection.Right);
                }
            }
            else if (Input.GetKey(KeyCode.UpArrow))
            {
                if (_map.CanMove(_player.Position + Player.DIR_UP))
                {
                    _player.MoveTo(PlayerDirection.Up);
                    send = true;
                }
                else
                {
                    _player.Face(PlayerDirection.Up);
                }
            }

            // let the server know this player is moving to a new map cell
            if (send)
            {
                PsObject psobj = new PsObject();
                psobj.SetIntArray(ServerConstants.PLAYER_POSITION, Utility.Vector2ToList(_player.Target));

                _server.SendRequest(new ExtensionRequest(PlayerCommand.GetCommand(PlayerCommand.PlayerEnum.Move), psobj));
            }
        }

        // shoot a fireball
        if (Input.GetKey(KeyCode.Space) && _player.CanShoot)
        {
            _player.Shoot();

            // let the server know this player shot something
            PsObject psobj = new PsObject();
            psobj.SetIntArray(ServerConstants.PLAYER_POSITION, Utility.Vector2ToList(_player.Target));
            psobj.SetIntArray(ServerConstants.PLAYER_HEADING, Utility.Vector2ToList(_player.GetDirectionVector()));

            _server.SendRequest(new ExtensionRequest(PlayerCommand.GetCommand(PlayerCommand.PlayerEnum.Shoot), psobj));
        }
    }