]> git.r.bdr.sh - rbdr/super-polarity/blob - SuperPolarity/ScoreScreen.cs
d879db9e2fd6918db308d0096fb94bafda2a1996
[rbdr/super-polarity] / SuperPolarity / ScoreScreen.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.IO;
6 using Microsoft.Xna.Framework;
7 using Microsoft.Xna.Framework.Graphics;
8 using Microsoft.Xna.Framework.Input;
9
10 namespace SuperPolarity
11 {
12 class ScoreScreen : Screen
13 {
14 protected SpriteFont Font;
15 protected NameChooserWidget nameChooser;
16 protected List<Tuple<String, int>> Scores;
17
18 public ScoreScreen(SuperPolarity newGame) : base(newGame) { }
19
20 public override void Initialize()
21 {
22 base.Initialize();
23 nameChooser = new NameChooserWidget(Game, new Vector2(40, Game.GraphicsDevice.Viewport.Height / 4));
24 InputController.RegisterEventForButton("OK", Buttons.A);
25 InputController.RegisterEventForKey("OK", Keys.Z);
26 Scores = new List<Tuple<String, int>>();
27 ReadScore();
28 }
29
30 public override void LoadContent()
31 {
32 base.LoadContent();
33 Font = Game.Content.Load<SpriteFont>("Fonts\\bigfont");
34 InputController.Bind("OK", HandleNext);
35 }
36
37 public void HandleNext(float value)
38 {
39 if (!Active) { return; }
40 WriteScore();
41 ScreenManager.Pop();
42 }
43
44 public void WriteScore()
45 {
46 using (StreamWriter swriter = new StreamWriter("scores.txt", true))
47 {
48 swriter.WriteLine(nameChooser.Value() + "," + Game.Player.Score.ToString());
49 }
50 }
51
52 public void ReadScore()
53 {
54 try
55 {
56 using (StreamReader sreader = new StreamReader("scores.txt"))
57 {
58 string line = null;
59 while ((line = sreader.ReadLine()) != null)
60 {
61 string[] parts = line.Split(',');
62 var scoreTuple = new Tuple<String, int>(parts[0], int.Parse(parts[1]));
63 Scores.Add (scoreTuple);
64 }
65 }
66 }
67 catch (FileNotFoundException)
68 {
69 }
70 }
71
72 public override void CleanUp()
73 {
74 base.CleanUp();
75 Font = null;
76 }
77
78 public override void Draw(SpriteBatch spriteBatch)
79 {
80 base.Draw(spriteBatch);
81 spriteBatch.DrawString(Font, "YOUR SCORE WAS: " + Game.Player.Score.ToString(), new Vector2(40, Game.GraphicsDevice.Viewport.Height / 2), Color.Black);
82 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));
83 nameChooser.Draw(spriteBatch);
84 DrawScores(spriteBatch);
85 }
86
87 public override void Update(GameTime gameTime)
88 {
89 base.Update(gameTime);
90 InputController.UpdateInput(false);
91 nameChooser.Update(gameTime);
92 }
93
94 protected void DrawScores(SpriteBatch spriteBatch)
95 {
96 var sortedList = (from entry in Scores orderby entry.Item2 descending select entry)
97 .Take (5)
98 .ToList ();
99
100 var i = 0;
101
102 foreach (Tuple<string, int> entry in sortedList) {
103 i++;
104 spriteBatch.DrawString(Font, entry.Item1 + " " + entry.Item2, new Vector2(40, Game.GraphicsDevice.Viewport.Height / 2 + 100 + (32 * i)), new Color(0, 0, 0, 64));
105 }
106
107 }
108 }
109 }