public GameProgram()
 {
     graphics = new GraphicsDeviceManager(this);
     Content.RootDirectory = "Content";
     _scene_MainMenu       = new Scene_MainMenu(ChangeState);
     _scene_Game           = new Scene_Game(ChangeState);
 }
        private bool?serverResult_forfeited = null;                            //TODO: Check for this to not be null, then react.


        protected override void Awake()
        {
            base.Awake();

            Scene_Game.SetActive(false);
            Scene_ClientUI.SetActive(false);
        }
        /// <summary>
        /// Starts talking to the server on another thread.
        /// Returns that thread.
        /// </summary>
        private Thread ContactServer()
        {
            //If nothing is going on, try connecting to a server to ask for a match.
            if (!File.Exists(matchmakingDataFilePath))
            {
                Scene_ClientUI.SetActive(true);
                clientUI.SetIsFindingMatch(false);
                return(TalkToServer(new IPEndPoint(IPAddress.Parse(clientUI.ServerIP),
                                                   clientUI.ServerPort),
                                    TalkToServer_FirstContact));
            }
            else
            {
                //We must have a presence on the server already.
                var matchmakingData = GetMatchmakingData();

                //If we're still waiting for a match, check up on that.
                if (matchmakingData.OpponentName == null)
                {
                    Scene_ClientUI.SetActive(true);
                    clientUI.SetIsFindingMatch(true);
                    return(TalkToServer(matchmakingData.Server, TalkToServer_CheckMatch));
                }
                //Otherwise, pull the game state and load the board.
                else
                {
                    Scene_Game.SetActive(true);
                    return(TalkToServer(matchmakingData.Server, TalkToServer_GetMatchState));
                }
            }
        }
Esempio n. 4
0
    protected override void _Resolver(Hashtable args)
    {
        base._Resolver(args);

        if (args.Contains("Scene_Game"))
        {
            scene_game_ = (Scene_Game)args["Scene_Game"];
        }
    }
Esempio n. 5
0
        // Called when turn switches: To reset and begin playing the animation
        public void Initialise()
        {
            Scene_Game scene = (Scene_Game)Scene;

            if (scene.CurrentPlayers.Count == 0)
            {
                return;
            }

            int           player = scene.GetPlayerTurn();
            CommanderType com    = scene.CurrentPlayers.ElementAt(player).Commander;

            Text_Label.Color  = new Color(com.Colour);
            Text_Label.String = String.Format(Label, com.PlayerName.ToUpper());
            Text_Label.CenterOrigin();

            EnabledTime = Game.Instance.Timer;
            State       = TurnSwitchAnimationState.Entry;
        }
Esempio n. 6
0
    protected override void _Resolver( Hashtable args)
    {
        base._Resolver (args);

        if (args.Contains("Scene_Game")) {
            scene_game_ =(Scene_Game)args["Scene_Game"];
        }
    }
Esempio n. 7
0
    protected override void _Thinker()
    {
        base._Thinker();

        bool processed = false;
        // check if he see
//		if ( false ) {
//			processed = true;
//		}

        int row    = -1;
        int column = -1;

        if (!processed && NavigationMap.GetInstance().GetNearestHumanForMonster(this, out row, out column))
        {
            int new_map_x = 0;
            int new_map_y = 0;
            int distance  = NavigationMap.GetInstance().CalculateDistance(this, row, column);
            if (distance <= GameSettings.GetInstance().MONSTER_SIGHT&&
                NavigationMap.GetInstance().MoveForward(this, row, column, out new_map_x, out new_map_y))
            {
                NavigationMap.GetInstance().UnRegisterActor(this);
                this.pos = new Vector3(new_map_x * GameSettings.GetInstance().TILE_SIZE,
                                       new_map_y * GameSettings.GetInstance().TILE_SIZE,
                                       0.0f);
                NavigationMap.GetInstance().RegisterActor(this);

                int cur_map_x = (int)this.map_pos.x;
                int cur_map_y = (int)this.map_pos.y;

                if (cur_map_x == column && cur_map_y == row)
                {
                    int kill_counter = NavigationMap.GetInstance().EatHuman(this, cur_map_y, cur_map_x);

                    // spawn monsters
                    if (kill_counter > 0)
                    {
                        Scene_Game scene_game = (Scene_Game)SceneManager.GetInstance().cur_scene;
                        for (int i = 0; i < kill_counter; ++i)
                        {
                            scene_game.AddActorSpawner(ActorType.monster, cur_map_y, cur_map_x);
                        }
//						Debug.Log ( "<Monster::_Thinker>, human eaten!" );
                    }
                }
                else
                {
//					Debug.Log ( "<Monster::_Thinker>, running to human, heading to " + column + ", " + row );
                }

                processed = true;
            }
        }

        if (moving_to_target_)
        {
            moving_to_target_ = !processed;
        }

        if (!processed && !moving_to_target_)
        {
            if (NavigationMap.GetInstance().GetRandomPos(this, out target_row_, out target_column_))
            {
                moving_to_target_ = true;
//				Debug.Log ( "<Monster::_Thinker>, random walking, heading to " + target_column_ + ", " + target_row_ );
                processed = true;
            }
        }

        if (moving_to_target_)
        {
            int cur_map_x = (int)this.map_pos.x;
            int cur_map_y = (int)this.map_pos.y;

            if (cur_map_x == target_column_ && cur_map_y == target_row_)
            {
                moving_to_target_ = false;
            }
            else
            {
                int new_map_x = 0;
                int new_map_y = 0;
                if (NavigationMap.GetInstance().MoveForward(this, target_row_, target_column_, out new_map_x, out new_map_y))
                {
//					Debug.Log ( this.gameObject.name + " is moving to " + new_map_x + ", " + new_map_y );
                    NavigationMap.GetInstance().UnRegisterActor(this);
                    this.pos = new Vector3(new_map_x * GameSettings.GetInstance().TILE_SIZE,
                                           new_map_y * GameSettings.GetInstance().TILE_SIZE,
                                           0.0f);
                    NavigationMap.GetInstance().RegisterActor(this);
                }

//				Debug.Log ( "<Monster::_Thinker>, continue random walk " + target_column_ + ", " + target_row_ );
                processed = true;
            }
        }
    }
Esempio n. 8
0
    protected override void _Thinker()
    {
        base._Thinker();

        int row    = 0;
        int column = 0;

        bool processed = false;

        // see if you can move
//		if ( false ) {
//			processed = true;
//		}

        if (!processed && is_spawner_)
        {
            int        cur_map_x  = (int)this.map_pos.x;
            int        cur_map_y  = (int)this.map_pos.y;
            Scene_Game scene_game = (Scene_Game)SceneManager.GetInstance().cur_scene;
            scene_game.AddActorSpawner(ActorType.human, cur_map_y, cur_map_x);
            processed = true;
        }

        // see if he see a monster
        if (!processed && NavigationMap.GetInstance().GetNearestMonster(this, out row, out column))
        {
            int cur_map_x  = (int)this.map_pos.x;
            int cur_map_y  = (int)this.map_pos.y;
            int diff_map_x = Mathf.Abs(cur_map_x - column);
            int diff_map_y = Mathf.Abs(cur_map_y - row);
            int distance   = diff_map_x + diff_map_y;
            if (distance <= GameSettings.GetInstance().HUMAN_AVOID_DISTANCE)
            {
                int new_map_x = 0;
                int new_map_y = 0;
                if (NavigationMap.GetInstance().MoveAway(this, row, column, out new_map_x, out new_map_y))
                {
                    NavigationMap.GetInstance().UnRegisterActor(this);
                    // get away for that area!
                    this.pos = new Vector3(new_map_x * GameSettings.GetInstance().TILE_SIZE,
                                           new_map_y * GameSettings.GetInstance().TILE_SIZE,
                                           0.0f);
                    NavigationMap.GetInstance().RegisterActor(this);
//					Debug.Log ( "<Human::_Thinker>, running away from monster, heading to " + new_map_x + ", " + new_map_y );
                    processed = true;
                }
//				else {
//					if ( NavigationMap.GetInstance().GetRandomNearBy ( this, out new_map_y, out new_map_x ) ) {
//						NavigationMap.GetInstance().UnRegisterActor ( this );
//						this.pos = new Vector3 (new_map_x * GameSettings.GetInstance().TILE_SIZE,
//												new_map_y * GameSettings.GetInstance().TILE_SIZE,
//												0.0f );
//						NavigationMap.GetInstance().RegisterActor ( this );
//					}
//				}
            }
        }

        // check if he see food
        if (!processed && NavigationMap.GetInstance().GetNearestFood(this, out row, out column))
        {
            int new_map_x = 0;
            int new_map_y = 0;

            if (NavigationMap.GetInstance().MoveForward(this, row, column, out new_map_x, out new_map_y))
            {
                NavigationMap.GetInstance().UnRegisterActor(this);
                this.pos = new Vector3(new_map_x * GameSettings.GetInstance().TILE_SIZE,
                                       new_map_y * GameSettings.GetInstance().TILE_SIZE,
                                       0.0f);
                NavigationMap.GetInstance().RegisterActor(this);

                int cur_map_x = (int)this.map_pos.x;
                int cur_map_y = (int)this.map_pos.y;
                if (cur_map_x == column && cur_map_y == row)
                {
                    NavigationMap.GetInstance().EatFood(this, new_map_y, new_map_x);
                    Scene_Game scene_game = (Scene_Game)SceneManager.GetInstance().cur_scene;
//					scene_game.AddActorSpawner ( ActorType.human, cur_map_y, cur_map_x );
                    is_spawner_       = true;
                    moving_to_target_ = false;
                }
            }
//			else {
//				if ( NavigationMap.GetInstance().GetRandomNearBy ( this, out new_map_y, out new_map_x ) ) {
//					NavigationMap.GetInstance().UnRegisterActor ( this );
//					this.pos = new Vector3 (new_map_x * GameSettings.GetInstance().TILE_SIZE,
//											new_map_y * GameSettings.GetInstance().TILE_SIZE,
//											0.0f );
//					NavigationMap.GetInstance().RegisterActor ( this );
//				}
//			}

            processed = true;
//			Debug.Log ( "<Human::_Thinker>, running to food, heading to " + column + ", " + row );
        }

        if (moving_to_target_)
        {
            moving_to_target_ = !processed;
        }

        if (!processed && !moving_to_target_)
        {
            if (NavigationMap.GetInstance().GetRandomPos(this, out target_row_, out target_column_))
            {
                moving_to_target_ = true;
//				Debug.Log ( "<Human::_Thinker>, walking to random pos, heading to " + target_column_ + ", " + target_row_ );
                processed = true;
            }
        }

        if (moving_to_target_)
        {
            int new_map_x = 0;
            int new_map_y = 0;

            if (NavigationMap.GetInstance().MoveForward(this, target_row_, target_column_, out new_map_x, out new_map_y))
            {
                NavigationMap.GetInstance().UnRegisterActor(this);
                this.pos = new Vector3(new_map_x * GameSettings.GetInstance().TILE_SIZE,
                                       new_map_y * GameSettings.GetInstance().TILE_SIZE,
                                       0.0f);
                NavigationMap.GetInstance().RegisterActor(this);

                int cur_map_x = (int)this.map_pos.x;
                int cur_map_y = (int)this.map_pos.y;

                if (cur_map_x == target_column_ && cur_map_y == target_row_)
                {
                    moving_to_target_ = false;
                }
                processed = true;
//				Debug.Log ( "<Human::_Thinker>, continue walking to random pos, heading to " + target_column_ + ", " + target_row_ );
            }
//			else {
//				if ( NavigationMap.GetInstance().GetRandomNearBy ( this, out new_map_y, out new_map_x ) ) {
//					NavigationMap.GetInstance().UnRegisterActor ( this );
//					this.pos = new Vector3 (new_map_x * GameSettings.GetInstance().TILE_SIZE,
//											new_map_y * GameSettings.GetInstance().TILE_SIZE,
//											0.0f );
//					NavigationMap.GetInstance().RegisterActor ( this );
//				}
//			}
        }
    }
        private void TalkToServer_CheckMatch(TcpClient client, NetworkStream serverStream,
                                             BinaryWriter serverWriter, BinaryReader serverReader)
        {
            var matchMakingData = GetMatchmakingData();

            //Ask the server whether a match was found.
            var initialMsg = new Networking.Messages.CheckOpponentFound(matchMakingData.PlayerID);

            if (!Try(() => GameMessageBase.Write(initialMsg, serverWriter)))
            {
                clientUI.SetErrorMsg("Couldn't send 'CheckMatch' message");
                return;
            }

            //Get the response.
            var opponentInfo = TryReadMsg <Networking.Messages.FoundOpponent>(
                serverReader,
                "getting 'CheckMatch' response");

            if (opponentInfo == null)
            {
                return;
            }

            //If an opponent hasn't been found yet, check in again later.
            if (opponentInfo.OpponentName == null)
            {
                return;
            }

            //If this player is going first, we need to generate the initial game board.
            //Note that a game board has already been generated; we can just use that.
            if (opponentInfo.AmIGoingFirst)
            {
                byte[] boardBytes = null;
                using (var boardStream = new MemoryStream())
                {
                    using (var boardWriter = new BinaryWriter(boardStream))
                        TheBoard.Serialize(boardWriter);
                    boardBytes = boardStream.GetBuffer();
                }

                var boardStateMsg = new Networking.Messages.NewBoard(boardBytes,
                                                                     MatchStates.Player1Turn);
                if (!Try(() => GameMessageBase.Write(boardStateMsg, serverWriter)))
                {
                    clientUI.SetErrorMsg("Couldn't send 'NewBoard' response");
                    return;
                }
            }
            //Otherwise, we need to get the current game state.
            else
            {
                //Get and load the state.
                var gameState = TryReadMsg <Networking.Messages.GameState>(
                    serverReader,
                    "getting first 'GameState'");
                if (gameState == null)
                {
                    return;
                }
                serverResult_stateToLoad = gameState;

                //Acknowledge.
                var acknowledge = new Networking.Messages.Acknowledge();
                if (!Try(() => GameMessageBase.Write(acknowledge, serverWriter)))
                {
                    clientUI.SetErrorMsg("Couldn't send 'Ack' after first game state");
                    return;
                }

                //Start the game UI.
                Scene_ClientUI.SetActive(false);
                Scene_Game.SetActive(true);
            }
        }