Пример #1
0
        /// <summary>
        /// Initializes a new instance of the ExtensionRequest class.
        /// </summary>
        /// <param name="command">Command for this request.  A subcommand may also be specified using the format "command.subcommand".</param>
        /// <param name="obj">Data for the request.</param>
        public ExtensionRequest(string command, PsObject obj) : base(RequestType.Extension)
        {
            Command = command;

            obj.SetString(REQUEST_COMMAND, Command);

            Init(obj);
        }
Пример #2
0
        /// <summary>
        /// Initializes a new instance of the LoginRequest class.
        /// </summary>
        /// <param name="username">Username to login with.</param>
        /// <param name="password">Password for the username.</param>
        public LoginRequest(string username, string password) : base(RequestType.Login)
        {
            PsObject obj = new PsObject();

            obj.SetString(REQUEST_USERNAME, username);
            obj.SetString(REQUEST_PASSWORD, password);

            Init(obj);
        }
Пример #3
0
    // a group of players to add.  generally when a player first logs in and gets all the currently connected players
    private void AddPlayers(PsObject psobj)
    {
        List <PsObject> players = psobj.GetPsObjectArray(ServerConstants.PLAYER_OBJ);

        for (int i = 0; i < players.Count; ++i)
        {
            AddPlayer(players[i], false);
        }
    }
Пример #4
0
        /// <summary>
        /// Initializes a new instance of the PublicMessageRequest class.
        /// </summary>
        /// <param name="message">Message to be sent to the server.</param>
        public PublicMessageRequest(string message) : base(RequestType.PublicMessage)
        {
            Message = message;

            PsObject psobj = new PsObject();

            psobj.SetString(PM_MSG, Message);

            Init(psobj);
        }
Пример #5
0
        /// <summary>
        /// Override to pack the data for this event.
        /// </summary>
        /// <param name="dict">Key-Value pairs of data for this event.</param>
        override public void Create(Dictionary <string, object> dict)
        {
            Dictionary <string, object> tmp = (Dictionary <string, object>)dict[REQUEST_COMMAND];
            string str = Convert.ToString(tmp["v"]);

            string[] split = str.Split('.');
            Command = split[0];
            if (split.Length == 2)
            {
                SubCommand = split[1];
            }

            tmp  = (Dictionary <string, object>)dict[EXTENSION_DATA];
            Data = PsObject.Create((Dictionary <string, object>)tmp["v"]);
        }
Пример #6
0
        /// <summary>
        /// Override to pack the data for this event.
        /// </summary>
        /// <param name="dict">Key-Value pairs of data for this event.</param>
        override public void Create(Dictionary <string, object> dict)
        {
            Dictionary <string, object> tmp = (Dictionary <string, object>)dict[PM_USER];

            User = Convert.ToString(tmp["v"]);

            tmp     = (Dictionary <string, object>)dict[PM_MSG];
            Message = Convert.ToString(tmp["v"]);

            if (dict.ContainsKey(PM_DATA))
            {
                tmp  = (Dictionary <string, object>)dict[PM_DATA];
                Data = PsObject.Create((Dictionary <string, object>)tmp["v"]);
            }
        }
Пример #7
0
    // another player moved
    private void PlayerMove(PsObject psobj)
    {
        string name = psobj.GetString(ServerConstants.PLAYER_NAME);

        Player player = FindPlayer(name);

        if (player == null)
        {
            return;
        }

        List <int> position = psobj.GetIntArray(ServerConstants.PLAYER_POSITION);

        player.MoveTo(Utility.ListToVector2(position));
    }
Пример #8
0
        /// <summary>
        /// Override to pack the data for this event.
        /// </summary>
        /// <param name="dict">Key-Value pairs of data for this event.</param>
        override public void Create(Dictionary <string, object> dict)
        {
            Dictionary <string, object> tmp = (Dictionary <string, object>)dict[LOGIN_SUCCESS];

            Success = Convert.ToBoolean(tmp["v"]);
            if (dict.ContainsKey(LOGIN_MSG))
            {
                tmp     = (Dictionary <string, object>)dict[LOGIN_MSG];
                Message = Convert.ToString(tmp["v"]);
            }
            if (dict.ContainsKey(LOGIN_DATA))
            {
                tmp  = (Dictionary <string, object>)dict[LOGIN_DATA];
                Data = PsObject.Create((Dictionary <string, object>)tmp["v"]);
            }
        }
Пример #9
0
    // player left the game
    private void PlayerLeave(PsObject psobj)
    {
        string name = psobj.GetString(ServerConstants.PLAYER_NAME);

        Player player = FindPlayer(name);

        if (player == null)
        {
            return;
        }

        // update chat a player left
        _chat.UserAction(name, ChatAction.Leave);

        GameObject.Destroy(player.gameObject);
    }
Пример #10
0
    // another player shot
    private void PlayerShoot(PsObject psobj)
    {
        string name = psobj.GetString(ServerConstants.PLAYER_NAME);

        Player player = FindPlayer(name);

        if (player == null)
        {
            return;
        }

        List <int> position = psobj.GetIntArray(ServerConstants.PLAYER_POSITION);
        List <int> heading  = psobj.GetIntArray(ServerConstants.PLAYER_HEADING);

        Shot.Create(player, Utility.ListToVector2(position), Utility.ListToVector2(heading));
    }
Пример #11
0
    // add a single player.  generally when somebody new joins after the game has started
    private void AddPlayer(PsObject psobj, bool updateStatus)
    {
        string name = psobj.GetString(ServerConstants.PLAYER_NAME);

        if (_otherPlayers.ContainsKey(name))
        {
            return;
        }

        int        type     = psobj.GetInt(ServerConstants.PLAYER_TYPE);
        List <int> position = psobj.GetIntArray(ServerConstants.PLAYER_POSITION);

        Player player = CreateCharacter(type, Utility.ListToVector2(position), OthersLayer);

        player.Face(PlayerDirection.Down);
        _otherPlayers.Add(name, player);

        // update chat a player entered
        if (updateStatus)
        {
            _chat.UserAction(name, ChatAction.Enter);
        }
    }
Пример #12
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;
    }
Пример #13
0
    // got some kind of message from the server
    private void OnResponse(Dictionary <string, object> message)
    {
        string   command    = (string)message["command"];
        string   subCommand = (string)message["subcommand"];
        PsObject data       = (PsObject)message["data"];

        // if the message is a player command
        if (command == PlayerCommand.BASE_COMMAND)
        {
            // get the subcommand
            PlayerCommand.PlayerEnum action = (PlayerCommand.PlayerEnum)System.Enum.Parse(typeof(PlayerCommand.PlayerEnum), subCommand, true);

            switch (action)
            {
            case PlayerCommand.PlayerEnum.InfoPlayer:
                AddPlayers(data);
                break;

            case PlayerCommand.PlayerEnum.InfoGroup:
                AddPlayer(data, true);
                break;

            case PlayerCommand.PlayerEnum.Move:
                PlayerMove(data);
                break;

            case PlayerCommand.PlayerEnum.Shoot:
                PlayerShoot(data);
                break;

            case PlayerCommand.PlayerEnum.Leave:
                PlayerLeave(data);
                break;
            }
        }
    }
Пример #14
0
        /// <summary>
        /// Initializes a new instance of the LogoutRequest class.
        /// </summary>
        public LogoutRequest() : base(RequestType.Logout)
        {
            PsObject obj = new PsObject();

            Init(obj);
        }
Пример #15
0
 /// <summary>
 /// Initializes the internal PsObject.
 /// </summary>
 /// <param name="obj"></param>
 protected void Init(PsObject obj)
 {
     _object = obj;
     _object.SetInt(REQUEST_TYPE, (int)Type);
 }
Пример #16
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));
        }
    }