using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; using tAC_Engine.Graphics.Entities; using tAC_Engine.Graphics; using tAC_Engine.GUI; using Util; namespace FaceOff { class FaceOffGame : GameScreen { bool raised = false; SpriteBatch mSpriteBatch; EntitySprite mBackground; EntitySprite mMouse; SoundManager sound; SubmitButton submitButton; ProgressBar myBar; Score score; List mComponents; InputHandler mInputHandler; int[] categories; private Logger mLogger; Board board; FaceTray tray; private int cellSize; private bool draggingCell; String copyrightMsg1 = "Face images are from the DVD \"Mind Reading: The Interactive Guide to Emotions\" by Simon Baron-Cohen (ISBN 978-1-84310-559-6)."; String copyrightMsg2 = "Used by permission of Jessica Kingsley Publishers, http://www.jkp.com/catalogue/book/Mind_Reading_sit"; Dialog copyrightDialog1, copyrightDialog2; public override void Initialize() { copyrightDialog1 = new Dialog("Copyright Notice", copyrightMsg1, string.Empty, Corner.BottomLeft, 0, true); copyrightDialog2 = new Dialog("Copyright Notice", copyrightMsg2, string.Empty, Corner.BottomLeft, 0, true); mLogger = GetLogger(); mLogger.Initialize(); mComponents = new List(); mInputHandler = GetInputHandler(); mInputHandler.Initialize(); mInputHandler.BindButton(Keys.Space, Trigger.Activated, (int)EventCode.FO_INPUT_KEY_SPACE); mInputHandler.BindButton(MouseButton.Left, Trigger.Activated, (int)EventCode.FO_INPUT_MOUSE_LEFT_DOWN); mInputHandler.BindButton(MouseButton.Left, Trigger.Deactivated, (int)EventCode.FO_INPUT_MOUSE_LEFT_UP); mMouse = new EntitySprite(Content, @"Texture\Cursor"); mMouse.BlendMode = SpriteBlendMode.AlphaBlend; mMouse.Position = Vector2.Zero; mMouse.Initialize(); mBackground = new EntitySprite(Content, @"Texture\Background"); mBackground.Position = Vector2.Zero; mBackground.Initialize(); sound = new SoundManager(); sound.LoadContent(Content); mLogger.Write((int)EventCode.FO_START_BOARD, typeof(EventCode), "1"); submitButton = new SubmitButton(); submitButton.LoadContent(Content); submitButton.initialize(); int bestHeight = ((GameConfig.DesiredBoardHeight) / GameConfig.Rows); int bestWidth = ((GameConfig.DesiredBoardWidth) / GameConfig.Columns); if (bestHeight < bestWidth) { cellSize = bestHeight; } else { cellSize = bestWidth; } FaceLoader.Initialize(Content); board = new Board(GameConfig.Rows, GameConfig.Columns, cellSize, sound, mLogger); board.LoadContent(Content); tray = new FaceTray(cellSize, mLogger); tray.LoadContent(Content); board.initialize(); board.GenerateBoard(); categories = new int[GameConfig.FaceTraySize]; //fill an array with all of the category types for each group for (int i = 0; i < board.Groups.Count; ++i) { categories[i] = (board.Groups.ElementAt(i)).Category; } tray.initialize(); tray.PopulateTray(categories); submitButton.SetPosition(tray.Position.X + tray.Width + 10, tray.Position.Y + (cellSize / 2) - (submitButton.Texture.Height / 2)); myBar = new ProgressBar(Content, new Vector2(300, 580), 0, 200, sound); score = new Score(); mComponents.Add(mBackground); Dialog[] dialogList = { copyrightDialog1, copyrightDialog2 }; DialogScreen ds = new DialogScreen(dialogList, true); Raise(ds); base.Initialize(); } protected override void LoadContent() { mSpriteBatch = new SpriteBatch(GraphicsDevice); foreach (GameComponent gc in mComponents) { EntitySprite s = gc as EntitySprite; if (s != null) { s.SpriteBatch = mSpriteBatch; } } mMouse.SpriteBatch = mSpriteBatch; base.LoadContent(); } /// /// UnloadContent will be called once per game and is the place to unload /// all content. /// protected override void UnloadContent() { // TODO: Unload any non ContentManager content here base.UnloadContent(); } /// /// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input, and playing audio. /// /// Provides a snapshot of timing values. public override void Update(GameTime gameTime) { base.Update(gameTime); mMouse.Position = new Vector2(mInputHandler.MouseLocation.X, mInputHandler.MouseLocation.Y); // TODO: Add your update logic here while (mInputHandler.HasEvent()) { InputEvent inputEvent = mInputHandler.GetNextEvent(); switch ((EventCode)inputEvent.EventCode) { case EventCode.FO_INPUT_KEY_SPACE: inputEvent.WriteToLog(mLogger, Enum.GetName(typeof(EventCode), EventCode.FO_INPUT_KEY_SPACE)); if (myBar.Progress >= 100) { board.initialize(); board.GenerateBoard(); categories = new int[GameConfig.FaceTraySize]; //fill an array with all of the category types for each group for (int i = 0; i < board.Groups.Count; ++i) { categories[i] = (board.Groups.ElementAt(i)).Category; } tray.initialize(); tray.PopulateTray(categories); } break; case EventCode.FO_INPUT_MOUSE_LEFT_DOWN: mLogger.Write((int)EventCode.FO_INPUT_MOUSE_LEFT_DOWN, typeof(EventCode), "X=" + mMouse.Position.X + "_Y=" + mMouse.Position.Y); //dont check mouse clicks if the mouse is not visible if (mMouse.Visible == false) { break; } if (submitButton.CollisionBox.Contains(new Vector3(mMouse.Position.X, mMouse.Position.Y, 0)) == ContainmentType.Contains) { //inputEvent.WriteToLog(mLogger, "FO_INPUT_SubmitPressed"); mLogger.Write((int)EventCode.FO_SUBMIT_PRESSED, typeof(EventCode)); //set the submit button to clicked so that the proper texture is drawn submitButton.Clicked = true; score.Submit++; //initiate the validation of the groups board.validateBoard(); } else { int action = tray.checkClick(mMouse.Position); //if the user is dragging a face or loading a cell, then do not draw the mouse cursor if (action == 0) { draggingCell = true; } //if the user did not click the tray, check to see if they clicked //the board else { action = board.checkClick(mMouse.Position); } //if the user clicked on the board to undo a cell placement //the action will be equal to a cell reference for the cell the user clicked to undo if (action != 0) { tray.undo(action); } } break; case EventCode.FO_INPUT_MOUSE_LEFT_UP: //inputEvent.WriteToLog(mLogger, "FO_INPUT_MouseReleased"); mLogger.Write((int)EventCode.FO_INPUT_MOUSE_LEFT_UP, typeof(EventCode)); if (submitButton.Clicked == true) { submitButton.Clicked = false; } else { TrayCell tempCell = tray.getClickedCell(); if (tempCell != null) { //pass the clicked cell as a param to board during check bool placed = board.checkRelease(mMouse.Position, tempCell); //notify tray that the cell is on the board or not tray.cellRelease(placed); } } draggingCell = false; break; } } if (board.ValidatingBoard || tray.Loading || draggingCell) { mMouse.Visible = false; } else { mMouse.Visible = true; } //if the board is done validating make sure that the facetray faces are returned if(!(board.ValidatingBoard)){ List undo = board.getUndoGroup(); if( undo != null ) { //mark the number undone to indicate how many were wrong for the score score.Wrong += undo.Count; tray.undo(undo); board.clearUndoGroup(); } List removeFromTray = board.getTrayGroup(); if (removeFromTray != null) { tray.removeFromTray(removeFromTray); board.clearTrayGroup(); } } if (tray.Completed && myBar.StartUpdating) { //update the progress bar myBar.calcProgress(score.Wrong); score.calculateScore(); } //if all of the cells in the facetray have been placed correctly //then decide between load the next level and show end screen if (tray.Completed && myBar.DoneUpdating) { mLogger.Write((int)EventCode.FO_END_BOARD, typeof(EventCode), (score.BoardNumber+1).ToString()); if (myBar.Progress >= 100 && !raised) { MiniGameScoreScreen screen = new MiniGameScoreScreen(); //game is finished mInputHandler.Disable(); raised = true; Disable(); Raise(new EndScreen(score)); } if (myBar.Progress < 100) { board.initialize(); board.GenerateBoard(); categories = new int[GameConfig.FaceTraySize]; //fill an array with all of the category types for each group for (int i = 0; i < board.Groups.Count; ++i) { categories[i] = (board.Groups.ElementAt(i)).Category; } tray.initialize(); tray.PopulateTray(categories); score.BoardNumber++; mLogger.Write((int)EventCode.FO_START_BOARD, typeof(EventCode), (score.BoardNumber+1).ToString()); myBar.StartUpdating = true; } } board.Update(gameTime); tray.Update(gameTime, mMouse.Position); submitButton.Update(gameTime); myBar.Update(); } public void End() { Enabled = false; Visible = false; mLogger.Dispose(); Done(); } /// /// This is called when the game should draw itself. /// /// Provides a snapshot of timing values. public override void Draw(GameTime gameTime) { base.Draw(gameTime); foreach (GameComponent gc in mComponents) { EntitySprite s = gc as EntitySprite; if (s != null) { s.Draw(gameTime); } } mSpriteBatch.Begin(); myBar.Draw(mSpriteBatch); submitButton.Draw(mSpriteBatch); board.Draw(mSpriteBatch); tray.Draw(mSpriteBatch); mSpriteBatch.End(); if (mMouse.Visible) { mMouse.Draw(gameTime); } } } }