]> git.r.bdr.sh - rbdr/super-polarity/blame_incremental - SuperPolarityMac/ScoreScreen.cs
Adds linux project.
[rbdr/super-polarity] / SuperPolarityMac / ScoreScreen.cs
... / ...
CommitLineData
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.IO;
6using Microsoft.Xna.Framework;
7using Microsoft.Xna.Framework.Graphics;
8using Microsoft.Xna.Framework.Input;
9
10namespace SuperPolarity
11{
12 class ScoreScreen : Screen
13 {
14 protected SpriteFont Font;
15 protected NameChooserWidget nameChooser;
16 protected Dictionary<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 Dictionary<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 Scores.Add(parts[0], int.Parse(parts[1]));
63 }
64 }
65 }
66 catch (FileNotFoundException)
67 {
68 }
69 }
70
71 public override void CleanUp()
72 {
73 base.CleanUp();
74 Font = null;
75 }
76
77 public override void Draw(SpriteBatch spriteBatch)
78 {
79 base.Draw(spriteBatch);
80 spriteBatch.DrawString(Font, "YOUR SCORE WAS: " + Game.Player.Score.ToString(), new Vector2(40, Game.GraphicsDevice.Viewport.Height / 2), Color.Black);
81 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));
82 nameChooser.Draw(spriteBatch);
83 DrawScores(spriteBatch);
84 }
85
86 public override void Update(GameTime gameTime)
87 {
88 base.Update(gameTime);
89 InputController.UpdateInput(false);
90 nameChooser.Update(gameTime);
91 }
92
93 protected void DrawScores(SpriteBatch spriteBatch)
94 {
95 var sortedDict = (from entry in Scores orderby entry.Value descending select entry)
96 .Take(5)
97 .ToDictionary(pair => pair.Key, pair => pair.Value);
98
99 var i = 0;
100
101 foreach (KeyValuePair<string, int> entry in sortedDict) {
102 i++;
103 spriteBatch.DrawString(Font, entry.Key + " " + entry.Value, new Vector2(40, Game.GraphicsDevice.Viewport.Height / 2 + 100 + (32 * i)), new Color(0, 0, 0, 64));
104 }
105
106 }
107 }
108}