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;
	}
        /// <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);
		}
        /// <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);
        }
        /// <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);
        }
        /// <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"]);
        }
        /// <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"]);
            }
        }
Beispiel #7
0
        /// <summary>
        /// Create a PsObject from Dictionary of string, object pairs.
        /// </summary>
        /// <param name="dict">Key-value pairs of data.</param>
        /// <returns>PsObject containing values from Dictionary.</returns>
        public static PsObject Create(Dictionary <string, object> dict)
        {
            PsObject psobj = new PsObject();

            foreach (KeyValuePair <string, object> item in dict)
            {
                string key = item.Key;
                Dictionary <string, object> value = (Dictionary <string, object>)item.Value;

                int type = (int)value["t"];

                switch (type)
                {
                case (int)Constants.PsType.Boolean:
                    psobj.SetBoolean(key, Convert.ToBoolean(value["v"]));
                    break;

                case (int)Constants.PsType.String:
                    psobj.SetString(key, Convert.ToString(value["v"]));
                    break;

                case (int)Constants.PsType.Integer:
                    psobj.SetInt(key, Convert.ToInt32(value["v"]));
                    break;

                case (int)Constants.PsType.Long:
                    psobj.SetLong(key, Convert.ToInt64(value["v"]));
                    break;

                case (int)Constants.PsType.Float:
                    psobj.SetFloat(key, Convert.ToSingle(value["v"]));
                    break;

                case (int)Constants.PsType.PSObject:
                    psobj.SetPsObject(key, PsObject.Create((Dictionary <string, object>)value["v"]));
                    break;

                case (int)Constants.PsType.PSArray:
                    psobj.SetPsArray(key, PsArray.Create((object[])value["v"]));
                    break;

                case (int)Constants.PsType.Number:
                    psobj.SetFloat(key, Convert.ToSingle(value["v"]));
                    break;
                }
            }

            return(psobj);
        }
Beispiel #8
0
        /// <summary>
        /// Create a PsObject from Dictionary of string, object pairs.
        /// </summary>
        /// <param name="dict">Key-value pairs of data.</param>
        /// <returns>PsObject containing values from Dictionary.</returns>
        public static PsObject Create(Dictionary<string, object> dict)
        {
            PsObject psobj = new PsObject();

            foreach (KeyValuePair<string, object> item in dict)
            {
                string key = item.Key;
                Dictionary<string, object> value = (Dictionary<string, object>)item.Value;

                int type = (int)value["t"];

                switch (type)
                {
                    case (int)Constants.PsType.Boolean:
                        psobj.SetBoolean(key, Convert.ToBoolean(value["v"]));
                        break;

                    case (int)Constants.PsType.String:
                        psobj.SetString(key, Convert.ToString(value["v"]));
                        break;

                    case (int)Constants.PsType.Integer:
                        psobj.SetInt(key, Convert.ToInt32(value["v"]));
                        break;

                    case (int)Constants.PsType.Long:
                        psobj.SetLong(key, Convert.ToInt64(value["v"]));
                        break;

                    case (int)Constants.PsType.Float:
                        psobj.SetFloat(key, Convert.ToSingle(value["v"]));
                        break;

                    case (int)Constants.PsType.PSObject:
                        psobj.SetPsObject(key, PsObject.Create((Dictionary<string, object>)value["v"]));
                        break;

                    case (int)Constants.PsType.PSArray:
                        psobj.SetPsArray(key, PsArray.Create((object[])value["v"]));
                        break;

                    case (int)Constants.PsType.Number:
                        psobj.SetFloat(key, Convert.ToSingle(value["v"]));
                        break;
                }
            }

            return psobj;
        }
Beispiel #9
0
        /// <summary>
        /// Create an PsArray from an array of objects.
        /// </summary>
        /// <param name="obj">Array of objects.</param>
        /// <returns></returns>
        public static PsArray Create(object[] obj)
        {
            PsArray psa = new PsArray();

            foreach (object o in obj)
            {
                Dictionary <string, object> dict = (Dictionary <string, object>)o;

                int type = (int)dict["t"];

                switch (type)
                {
                case (int)Constants.PsType.Boolean:
                    psa.AddBoolean(Convert.ToBoolean(dict["v"]));
                    break;

                case (int)Constants.PsType.String:
                    psa.AddString(Convert.ToString(dict["v"]));
                    break;

                case (int)Constants.PsType.Integer:
                    psa.AddInt(Convert.ToInt32(dict["v"]));
                    break;

                case (int)Constants.PsType.Long:
                    psa.AddLong(Convert.ToInt64(dict["v"]));
                    break;

                case (int)Constants.PsType.Float:
                    psa.AddFloat(Convert.ToSingle(dict["v"]));
                    break;

                case (int)Constants.PsType.PSObject:
                    psa.AddPsObject(PsObject.Create((Dictionary <string, object>)dict["v"]));
                    break;

                case (int)Constants.PsType.PSArray:
                    psa.AddPsArray(PsArray.Create((object[])dict["v"]));
                    break;

                case (int)Constants.PsType.Number:
                    psa.AddNumber(Convert.ToSingle(dict["v"]));
                    break;
                }
            }

            return(psa);
        }
Beispiel #10
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"]);
            }
        }
Beispiel #11
0
 /// <summary>
 /// Add a PsObject to the array.
 /// </summary>
 /// <param name="value">PsObject to add.</param>
 public void AddPsObject(PsObject value)
 {
     _content.Add(new PsDataWrapper((int)Constants.PsType.PSObject, value));
 }
	// 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));
	}
	// 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);
	}
	// 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));
	}
Beispiel #15
0
 /// <summary>
 /// Set a PsObject in the PsObject.
 /// </summary>
 /// <param name="key">Key for value.</param>
 /// <param name="value">Value to add.</param>
 public void SetPsObject(string key, PsObject value)
 {
     _content[key] = new PsDataWrapper((int)Constants.PsType.PSObject, value);
 }
	// 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);
	}
        /// <summary>
        /// Initializes a new instance of the LogoutRequest class.
        /// </summary>
        public LogoutRequest() : base(RequestType.Logout)
        {
            PsObject obj = new PsObject();

            Init(obj);
        }
	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));
		}
	}
Beispiel #19
0
 /// <summary>
 /// Add a PsObject to the array.
 /// </summary>
 /// <param name="value">PsObject to add.</param>
 public void AddPsObject(PsObject value)
 {
     _content.Add(new PsDataWrapper((int)Constants.PsType.PSObject, value));
 }
Beispiel #20
0
 /// <summary>
 /// Set a PsObject in the PsObject.
 /// </summary>
 /// <param name="key">Key for value.</param>
 /// <param name="value">Value to add.</param>
 public void SetPsObject(string key, PsObject value)
 {
     _content[key] = new PsDataWrapper((int)Constants.PsType.PSObject, value);
 }
	// 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);
	}
Beispiel #22
0
 /// <summary>
 /// Initializes the internal PsObject.
 /// </summary>
 /// <param name="obj"></param>
 protected void Init(PsObject obj)
 {
     _object = obj;
     _object.SetInt(REQUEST_TYPE, (int)Type);
 }