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