]> git.r.bdr.sh - rbdr/super-polarity/blobdiff - SuperPolarityMac/ScoreScreen.cs
Removes spaces.
[rbdr/super-polarity] / SuperPolarityMac / ScoreScreen.cs
diff --git a/SuperPolarityMac/ScoreScreen.cs b/SuperPolarityMac/ScoreScreen.cs
new file mode 100644 (file)
index 0000000..bf43275
--- /dev/null
@@ -0,0 +1,108 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.IO;
+using Microsoft.Xna.Framework;
+using Microsoft.Xna.Framework.Graphics;
+using Microsoft.Xna.Framework.Input;
+
+namespace SuperPolarity
+{
+    class ScoreScreen : Screen
+    {
+        protected SpriteFont Font;
+        protected NameChooserWidget nameChooser;
+        protected Dictionary<string, int> Scores;
+
+        public ScoreScreen(SuperPolarity newGame) : base(newGame) { }
+
+        public override void Initialize()
+        {
+            base.Initialize();
+            nameChooser = new NameChooserWidget(Game, new Vector2(40, Game.GraphicsDevice.Viewport.Height / 4));
+            InputController.RegisterEventForButton("OK", Buttons.A);
+            InputController.RegisterEventForKey("OK", Keys.Z);
+            Scores = new Dictionary<string, int>();
+            ReadScore();
+        }
+
+        public override void LoadContent()
+        {
+            base.LoadContent();
+            Font = Game.Content.Load<SpriteFont>("Fonts\\bigfont");
+            InputController.Bind("OK", HandleNext);
+        }
+
+        public void HandleNext(float value)
+        {
+            if (!Active) { return; }
+            WriteScore();
+            ScreenManager.Pop();
+        }
+
+        public void WriteScore()
+        {
+            using (StreamWriter swriter = new StreamWriter("scores.txt", true))
+            {
+                swriter.WriteLine(nameChooser.Value() + "," + Game.Player.Score.ToString());
+            }
+        }
+
+        public void ReadScore()
+        {
+            try
+            {
+                using (StreamReader sreader = new StreamReader("scores.txt"))
+                {
+                    string line = null;
+                    while ((line = sreader.ReadLine()) != null)
+                    {
+                        string[] parts = line.Split(',');
+                        Scores.Add(parts[0], int.Parse(parts[1]));
+                    }
+                }
+            }
+            catch (FileNotFoundException)
+            {
+            }
+        }
+
+        public override void CleanUp()
+        {
+            base.CleanUp();
+            Font = null;
+        }
+
+        public override void Draw(SpriteBatch spriteBatch)
+        {
+            base.Draw(spriteBatch);
+            spriteBatch.DrawString(Font, "YOUR SCORE WAS: " + Game.Player.Score.ToString(), new Vector2(40, Game.GraphicsDevice.Viewport.Height / 2), Color.Black);
+            spriteBatch.DrawString(Font, "Use Left Stick or Arrows to Choose Name Press A when done", new Vector2(40, Game.GraphicsDevice.Viewport.Height / 2 + 40), new Color(0, 0, 0, 128));
+            nameChooser.Draw(spriteBatch);
+            DrawScores(spriteBatch);
+        }
+
+        public override void Update(GameTime gameTime)
+        {
+            base.Update(gameTime);
+            InputController.UpdateInput(false);
+            nameChooser.Update(gameTime);
+        }
+
+        protected void DrawScores(SpriteBatch spriteBatch)
+        {
+            var sortedDict = (from entry in Scores orderby entry.Value descending select entry)
+                .Take(5)
+                .ToDictionary(pair => pair.Key, pair => pair.Value);
+
+            var i = 0;
+
+            foreach (KeyValuePair<string, int> entry in sortedDict) {
+                i++;
+                spriteBatch.DrawString(Font, entry.Key + " " + entry.Value, new Vector2(40, Game.GraphicsDevice.Viewport.Height / 2 + 100 + (32 * i)), new Color(0, 0, 0, 64));
+            }
+
+        }
+    }
+}