using System; using Microsoft.Xna.Framework.Storage; using System.IO; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Input; /* * For any questions or help, contact me: * Zepharis Lubrano, 3861368 * zepharislubrano@hotmail.com */ class HighScore : TextGameObject { public string[] NameGrid; //Grid to store all player names. public int[] ScoreGrid; //Grid to store all scores. public string[] RankingGrid; //Grid to store the six ranks. public int[] LevelGrid; //Grid to store levels public string HighscorePath = "Content/Highscore/Highscore.txt"; //The directory where the text file is being saved. public string WholeHighscore; //The whole .txt file converted to a single string. public string playerName; private GameWorld gameWorld; Duck duck2; SpriteGameObject back; float duck2positionx; SpriteGameObject background, background2, HighScoreContainer, HighScoreContainer2, HighScoreContainer3, HighScoreContainer4, HighScoreContainer5, HighScoreContainer6; int Index; public HighScore(GameWorld gameWorld, int layer = 150, string id = "") : base(gameWorld, "Fonts/Hud", layer, id) { this.gameWorld = gameWorld; NameGrid = new string[6]; ScoreGrid = new int[6]; RankingGrid = new string[6]; LevelGrid = new int[6]; playerName = "---"; background = new SpriteGameObject(gameWorld, "Sprites/Background", 0, "Background"); background2 = new SpriteGameObject(gameWorld, "Sprites/Background2", 2, "Background2"); HighScoreContainer = new SpriteGameObject(gameWorld, "Sprites/HighscoreContainer", 3, "HSContainer"); HighScoreContainer2 = new SpriteGameObject(gameWorld, "Sprites/HighscoreContainer", 3, "HSContainer2"); HighScoreContainer3 = new SpriteGameObject(gameWorld, "Sprites/HighscoreContainer", 3, "HSContainer3"); HighScoreContainer4 = new SpriteGameObject(gameWorld, "Sprites/HighscoreContainer", 3, "HSContainer4"); HighScoreContainer5 = new SpriteGameObject(gameWorld, "Sprites/HighscoreContainer", 3, "HSContainer5"); HighScoreContainer6 = new SpriteGameObject(gameWorld, "Sprites/HighscoreContainer", 3, "HSContainer6"); HighScoreContainer.Position = new Vector2(605.0f, 175.0f); HighScoreContainer2.Position = new Vector2(605.0f, 212.0f); HighScoreContainer3.Position = new Vector2(605.0f, 247.0f); HighScoreContainer4.Position = new Vector2(605.0f, 282.0f); HighScoreContainer5.Position = new Vector2(605.0f, 317.0f); HighScoreContainer6.Position = new Vector2(605.0f, 352.0f); duck2 = new Duck(gameWorld, 3, "Duck2"); back = new SpriteGameObject(gameWorld, "Sprites/Back", 4, "back"); duck2.LoadAnimation(gameWorld, "Sprites/Duck/BlackDuckIdle", "blackidle", true); back.Position = new Vector2(30, 581); duck2positionx = back.Position.X + back.Center.X; duck2.Position = new Vector2(duck2positionx, back.Position.Y); RankingGrid[0] = "1ST"; RankingGrid[1] = "2ND"; RankingGrid[2] = "3RD"; RankingGrid[3] = "4TH"; RankingGrid[4] = "5TH"; RankingGrid[5] = "6TH"; if (!File.Exists(HighscorePath))//If the .txt file doesn't exist, create one with a standard list of names and scores. StandardList(); else LoadHighScores(); //else, read the existing file. } //Writes High Score text to the .txt file. public void HighscoreWriter() { TextWriter textwriter = new StreamWriter(HighscorePath); for (int i = 0; i < RankingGrid.Length; i++) { string FormattedScore = ScoreGrid[i].ToString("000000"); //Formats each score in the ScoreGrid to a standard form. string FormattedLevel = LevelGrid[i].ToString("000"); textwriter.WriteLine(RankingGrid[i] + '\t' + FormattedScore + '\t' + FormattedLevel + '\t' + NameGrid[i] + '\n'); } textwriter.Close(); } //Standard list of players and scores. (The ranking is based on a 'reversed alphabetical order') public void StandardList() { NameGrid[0] = "ZEP"; ScoreGrid[0] = 60; LevelGrid[0] = 6; NameGrid[1] = "ROB"; ScoreGrid[1] = 50; LevelGrid[1] = 5; NameGrid[2] = "LCN"; ScoreGrid[2] = 40; LevelGrid[2] = 4; NameGrid[3] = "LEO"; ScoreGrid[3] = 30; LevelGrid[3] = 3; NameGrid[4] = "KAT"; ScoreGrid[4] = 20; LevelGrid[4] = 2; NameGrid[5] = "BOB"; ScoreGrid[5] = 10; LevelGrid[5] = 1; HighscoreWriter(); } public void EditHighScore(ScoreGameObject score, LevelGameObject level) { Index = -1; //A value under 0 as the 'null-value' //creates a for-loop that goes through all the values in the Score-grid. for (int i = 0; i < ScoreGrid.Length; i++) { if (score.Score > ScoreGrid[i]) //The moment the score is higher than any score in the Score Grid, remember the index where this happened. { Index = i; break; } } if (Index > -1) //Only when the value isn't null, edit the High Score. { //the index's lower than the Index where the swapping will take place, will move one rank lower. for (int i = ScoreGrid.Length - 1; i > Index; i--) { NameGrid[i] = NameGrid[i - 1]; ScoreGrid[i] = ScoreGrid[i - 1]; LevelGrid[i] = LevelGrid[i - 1]; } NameGrid[Index] = playerName; ScoreGrid[Index] = score.Score; LevelGrid[Index] = level.Level; HighscoreWriter(); //After all this, the High Score will be written to the existing .txt file. } } //The High Score Reader public void LoadHighScores() { TextReader textreader = new StreamReader(HighscorePath); WholeHighscore = textreader.ReadToEnd(); //The whole High Score given back as a string. int offset; //The distance between every name/score. //a for-loop to get all the names and put them as individual values in the NameGrid. for (int i = 0; i < NameGrid.Length; i++) { offset = 21 * i; NameGrid[i] = WholeHighscore.Substring(offset + 15, 3); } //a for-loop to get all the scores and put them as individual values in the ScoreGrid. for (int i = 0; i < LevelGrid.Length; i++) { offset = 21 * i; LevelGrid[i] = int.Parse(WholeHighscore.Substring(offset + 11, 3)); } //a for-loop to get all the scores and put them as individual values in the ScoreGrid. for (int i = 0; i < ScoreGrid.Length; i++) { offset = 21 * i; ScoreGrid[i] = int.Parse(WholeHighscore.Substring(offset + 4, 6)); } textreader.Close(); //close the text reader. } public void HandleInput(InputHelper inputHelper) { GameModeHandler gmHandler = gameWorld.Find("gmHandler") as GameModeHandler; int index = playerName.IndexOf('-'); if (playerName.Length == 3 && index != -1) { if (inputHelper.KeyPressed(Keys.A)) { playerName = playerName.Remove(index, 1); playerName = playerName.Insert(index, "A"); } if (inputHelper.KeyPressed(Keys.B)) { playerName = playerName.Remove(index, 1); playerName = playerName.Insert(index, "B"); } if (inputHelper.KeyPressed(Keys.C)) { playerName = playerName.Remove(index, 1); playerName = playerName.Insert(index, "C"); } if (inputHelper.KeyPressed(Keys.D)) { playerName = playerName.Remove(index, 1); playerName = playerName.Insert(index, "D"); } if (inputHelper.KeyPressed(Keys.E)) { playerName = playerName.Remove(index, 1); playerName = playerName.Insert(index, "E"); } if (inputHelper.KeyPressed(Keys.F)) { playerName = playerName.Remove(index, 1); playerName = playerName.Insert(index, "F"); } if (inputHelper.KeyPressed(Keys.G)) { playerName = playerName.Remove(index, 1); playerName = playerName.Insert(index, "G"); } if (inputHelper.KeyPressed(Keys.H)) { playerName = playerName.Remove(index, 1); playerName = playerName.Insert(index, "H"); } if (inputHelper.KeyPressed(Keys.I)) { playerName = playerName.Remove(index, 1); playerName = playerName.Insert(index, "I"); } if (inputHelper.KeyPressed(Keys.J)) { playerName = playerName.Remove(index, 1); playerName = playerName.Insert(index, "J"); } if (inputHelper.KeyPressed(Keys.K)) { playerName = playerName.Remove(index, 1); playerName = playerName.Insert(index, "K"); } if (inputHelper.KeyPressed(Keys.L)) { playerName = playerName.Remove(index, 1); playerName = playerName.Insert(index, "L"); } if (inputHelper.KeyPressed(Keys.M)) { playerName = playerName.Remove(index, 1); playerName = playerName.Insert(index, "M"); } if (inputHelper.KeyPressed(Keys.N)) { playerName = playerName.Remove(index, 1); playerName = playerName.Insert(index, "N"); } if (inputHelper.KeyPressed(Keys.O)) { playerName = playerName.Remove(index, 1); playerName = playerName.Insert(index, "O"); } if (inputHelper.KeyPressed(Keys.P)) { playerName = playerName.Remove(index, 1); playerName = playerName.Insert(index, "P"); } if (inputHelper.KeyPressed(Keys.Q)) { playerName = playerName.Remove(index, 1); playerName = playerName.Insert(index, "Q"); } if (inputHelper.KeyPressed(Keys.R)) { playerName = playerName.Remove(index, 1); playerName = playerName.Insert(index, "R"); } if (inputHelper.KeyPressed(Keys.S)) { playerName = playerName.Remove(index, 1); playerName = playerName.Insert(index, "S"); } if (inputHelper.KeyPressed(Keys.T)) { playerName = playerName.Remove(index, 1); playerName = playerName.Insert(index, "T"); } if (inputHelper.KeyPressed(Keys.U)) { playerName = playerName.Remove(index, 1); playerName = playerName.Insert(index, "U"); } if (inputHelper.KeyPressed(Keys.V)) { playerName = playerName.Remove(index, 1); playerName = playerName.Insert(index, "V"); } if (inputHelper.KeyPressed(Keys.W)) { playerName = playerName.Remove(index, 1); playerName = playerName.Insert(index, "W"); } if (inputHelper.KeyPressed(Keys.X)) { playerName = playerName.Remove(index, 1); playerName = playerName.Insert(index, "X"); } if (inputHelper.KeyPressed(Keys.Y)) { playerName = playerName.Remove(index, 1); playerName = playerName.Insert(index, "Y"); } if (inputHelper.KeyPressed(Keys.Z)) { playerName = playerName.Remove(index, 1); playerName = playerName.Insert(index, "Z"); } if (inputHelper.KeyPressed(Keys.OemPeriod)) { playerName = playerName.Remove(index, 1); playerName = playerName.Insert(index, "."); } } if (inputHelper.KeyPressed(Keys.Back)) { playerName = "---"; } Player player = gameWorld.Find("Cursor") as Player; player.HandleInput(inputHelper, gameWorld); } public void EditPlayerName() { NameGrid[Index] = playerName; HighscoreWriter(); } public override void Update(GameTime gameTime, GameWorld gameWorld) { duck2.PlayAnimation("blackidle"); duck2.Velocity = new Vector2(-5, 0); duck2.AnimPlayer.Mirror = duck2.Velocity.X < 0; duck2.AnimPlayer.Update(gameTime); if (Index != -1) EditPlayerName(); LoadHighScores(); } public override void Draw(GameTime gameTime, SpriteBatch spriteBatch) { background.Draw(gameTime, spriteBatch); background2.Draw(gameTime, spriteBatch); duck2.Draw(gameTime, spriteBatch); back.Draw(gameTime, spriteBatch); HighScoreContainer.Draw(gameTime, spriteBatch); HighScoreContainer2.Draw(gameTime, spriteBatch); HighScoreContainer3.Draw(gameTime, spriteBatch); HighScoreContainer4.Draw(gameTime, spriteBatch); HighScoreContainer5.Draw(gameTime, spriteBatch); HighScoreContainer6.Draw(gameTime, spriteBatch); spriteBatch.DrawString(spriteFont, WholeHighscore, new Vector2(600.0f, 180.0f), Color.White); Player player = gameWorld.Find("Cursor") as Player; player.Draw(gameTime, spriteBatch); } public string PlayerName { get { return playerName; } } public Duck Duck { get { return duck2; } } public SpriteGameObject Back { get { return back; } } }