aboutsummaryrefslogtreecommitdiff
path: root/SuperPolarity/ScoreScreen.cs
blob: 63418d87b6793cb4d5a75547062f1efc4206aa9d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
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 List<Tuple<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 List<Tuple<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(',');
						var scoreTuple = new Tuple<String, int>(parts[0], int.Parse(parts[1]));
						Scores.Add (scoreTuple);
                    }
                }
            }
            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 sortedList = (from entry in Scores orderby entry.Item2 descending select entry)
                .Take (5)
				.ToList ();

            var i = 0;

            foreach (Tuple<string, int> entry in sortedList) {
                i++;
                spriteBatch.DrawString(Font, entry.Item1 + " " + entry.Item2, new Vector2(40, Game.GraphicsDevice.Viewport.Height / 2 + 100 + (32 * i)), new Color(0, 0, 0, 64));
            }

        }
    }
}