source: 2010/23/hniemi/BlockPusher/Blockpusher/Blockpusher/Blockpusher.cs @ 2638

Revision 2638, 8.3 KB checked in by hniemi, 11 years ago (diff)

voittosplashi lisätty ja back-nappula kentänvalintavalikkoon lisätty

Line 
1using System;
2using System.Collections.Generic;
3using Jypeli;
4using Jypeli.Assets;
5using Jypeli.Controls;
6using Jypeli.Effects;
7using Jypeli.Widgets;
8using System.IO;
9
10/// <summary>
11/// Blockpusher-game
12///
13/// TODO: clones needed from levels
14/// </summary>
15public class Blockpusher : Game
16{
17    public static readonly int BlockSize = 20;
18
19    private readonly Color playerColor = Color.Blue;
20    private readonly Color satisfyColor = Color.Green;
21    private readonly Color unsatisfyColor = Color.Red;
22
23    private int currentLevelIndex = 0;
24    private BlockLevel currentLevel;
25    private List<BlockLevel> levels = new List<BlockLevel>();
26
27    private bool gameSaved = false;
28    private IList<string> savedLevel;
29
30    private List<String> testLevel = new List<string>(new String[]
31                                 {"xxxxxxxxx",
32                                  "x  x  xmx",
33                                  "xb p  xbx",
34                                  "xm x    x",
35                                  "x  xxxxxx",
36                                  "x      xx",
37                                  "xxxxxxxxx"});
38
39    /// <summary>
40    /// Starts the game
41    /// </summary>
42    public override void Begin()
43    {
44        Menu();
45        LoadLevels();
46    }
47
48    /// <summary>
49    /// Starts new game
50    /// </summary>
51    public void NewGame(int levelIndex) 
52    {
53        ClearAll();
54        currentLevelIndex = levelIndex;
55        currentLevel = levels[levelIndex].Clone();
56        currentLevel.Victory += VictorySplash;
57        currentLevel.AddToGame();
58        SetControls();
59    }
60
61    /// <summary>
62    /// Loads levels from directory
63    /// </summary>
64    /// <param name="directory">Not in use</param>
65    public void LoadLevels(string directory=@"Data\Levels")
66    {
67        //TODO: Tee lukeminen valmiiksi
68
69        IEnumerable<string> folders = Directory.EnumerateDirectories(directory,"*");
70
71        foreach (string s in folders)
72        {
73            //Exludes hidden folder (with . before name)
74            if (s.Length != 0 && s[s.LastIndexOf('\\')+1] != '.')
75            {
76                //MessageDisplay.Add(s);
77                LoadLevels(s);
78            }
79        }
80
81        IEnumerable<string> files = Directory.EnumerateFiles(directory, "*");
82        foreach (string file in files)
83        {
84            BlockLevel level = new BlockLevel(this);
85            level.CreateField(ReadFile(file));
86
87            level.Name = file.Substring(file.LastIndexOf('\\')+1, file.LastIndexOf('.') - file.LastIndexOf('\\')-1);
88            levels.Add(level);
89        }
90    }
91
92    #region Menu
93    /// <summary>
94    /// Creates startmenu for game
95    /// </summary>
96    public void Menu()
97    {
98        int labelHeighth = 50;
99
100        ClearAll();
101        IsMouseVisible = true;
102        PhoneBackButton.Listen(ConfirmExit, "Lopeta peli");
103        Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli");
104
105        List<Tuple<string, Action>> labels = new List<Tuple<string, Action>>();
106        if (gameSaved) labels.Add(new Tuple<string, Action>("Continue", ContinueGame));
107        labels.Add(new Tuple<string, Action>("New game", delegate { LevelSelectionMenu(); }));
108        labels.Add(new Tuple<string, Action>("Quit?", ConfirmExit));
109
110        for (int i = 0; i < labels.Count; i++)
111        {
112            PushButton button = new PushButton(200, labelHeighth);
113            button.Text = labels[i].Item1;
114            button.Y = -(i * labelHeighth);
115            button.Clicked += labels[i].Item2;
116            Add(button);
117        }
118    }
119
120    /// <summary>
121    /// Asks if you want to go to main menu or continue playing
122    /// </summary>
123    public void ConfirmMenu()
124    {
125        ClearControls();
126        MultiSelectWindow w = new MultiSelectWindow("Back to menu?", new String[] { "Yes", "No!" });
127        w.AddItemHandler(0, delegate { MakeContinuable(); Menu(); });
128        w.AddItemHandler(1, delegate { SetControls(); w.Close(); });
129        Add(w);
130    }
131
132    /// <summary>
133    /// Shows "Victory"-label
134    /// </summary>
135    public void VictorySplash() 
136    {
137        Label victory = new Label("Victory!");
138        victory.Color = Color.DarkCyan;
139        victory.TextColor = Color.Fuchsia;
140
141        Add(victory);
142
143        Timer.SingleShot(1, Victory);
144    }
145
146    /// <summary>
147    /// Eventhandler for victory
148    /// </summary>
149    public void Victory()
150    {
151        IsMouseVisible = true;
152        MultiSelectWindow w = new MultiSelectWindow("Conratulations! Do you want to:", new String[] { "Retry", "Next Level", "Main menu" });
153        w.AddItemHandler(0, delegate { NewGame(currentLevelIndex); });
154        w.AddItemHandler(1, delegate { NewGame((currentLevelIndex+1) % levels.Count); });
155        w.AddItemHandler(2, Menu);
156       
157        Add(w);
158    }
159
160    /// <summary>
161    /// Shows levelselection menu
162    /// </summary>
163    public void LevelSelectionMenu(int current = 0)
164    {
165        ClearAll();
166        int buttonheight = 50;
167
168        Label topic = new Label("Levels");
169
170        Widget buttons = new Widget(new HorizontalLayout());
171        Widget bottomButtons = new Widget(new HorizontalLayout());
172
173        PushButton previous = new PushButton(150, buttonheight, "Previous");
174        buttons.Add(previous);
175
176        PushButton next = new PushButton(150, buttonheight, "Next");
177        buttons.Add(next);
178
179        Label name = new Label(levels[current].Name);
180
181        PushButton back = new PushButton(150, buttonheight, "Back");
182        bottomButtons.Add(back);
183
184        PushButton play = new PushButton(150, buttonheight, "Select");
185        bottomButtons.Add(play);
186
187        topic.Top = Screen.Top;
188        buttons.Top = topic.Bottom;
189        name.Y = buttons.Bottom - (name.Height / 2);
190
191        bottomButtons.Bottom = Screen.Bottom;
192       
193        Add(topic);
194        Add(buttons);
195        Add(name);
196        Add(bottomButtons);
197
198        levels[current].AddToGame();
199
200        previous.Clicked += delegate { LevelSelectionMenu((current-1 + levels.Count) % levels.Count); };
201        next.Clicked += delegate { LevelSelectionMenu((current + 1) % levels.Count); };
202        play.Clicked += delegate { NewGame(current); };
203        back.Clicked += Menu;
204
205    }
206    #endregion
207
208    /// <summary>
209    /// Continues game from current save
210    /// </summary>
211    public void ContinueGame() 
212    {
213        ClearAll();
214        currentLevel = new BlockLevel(this);
215        currentLevel.Victory += VictorySplash;
216
217        currentLevel.CreateField(ReadFile("Data/CurrentSave.txt"));
218        currentLevel.AddToGame();
219        SetControls();
220    }
221
222    /// <summary>
223    /// Saves game
224    /// </summary>
225    public void MakeContinuable()
226    {
227        gameSaved = true;
228        savedLevel = currentLevel.CurrentState();
229
230        File.WriteAllLines("Data/CurrentSave.txt", savedLevel);
231    }
232
233    /// <summary>
234    /// Reads lines from file
235    /// </summary>
236    /// <param name="file">Sourcefile</param>
237    public List<String> ReadFile(string file) 
238    {
239        List<String> lines = new List<string>();
240        try
241        {
242            using (StreamReader sr = new StreamReader(file))
243            {
244                String line;
245                while ((line = sr.ReadLine()) != null)
246                {
247                    lines.Add(line);
248                }
249            }
250        }
251        catch (Exception e)
252        {
253            return null;
254        }
255        return lines;
256    }
257
258    /// <summary>
259    /// Moves player in current level
260    /// </summary>
261    /// <param name="xMov">X-movement</param>
262    /// <param name="yMov">Y-movement</param>
263    public void MovePlayer(int xMov, int yMov)
264    {
265        currentLevel.MovePlayer(xMov, yMov);
266    }
267
268    /// <summary>
269    /// Sets controllisteners for keyboard
270    /// </summary>
271    public void SetControls() 
272    {
273        Keyboard.Listen(Key.Up, ButtonState.Pressed, MovePlayer, null, 0, -1);
274        Keyboard.Listen(Key.Down, ButtonState.Pressed, MovePlayer, null, 0, 1);
275        Keyboard.Listen(Key.Left, ButtonState.Pressed, MovePlayer, null, -1, 0);
276        Keyboard.Listen(Key.Right, ButtonState.Pressed, MovePlayer, null, 1, 0);
277
278        Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmMenu, "Lopeta peli");
279    }
280}
Note: See TracBrowser for help on using the repository browser.