1 | using System; |
---|
2 | using System.Linq; |
---|
3 | using System.Collections.Generic; |
---|
4 | using Jypeli; |
---|
5 | using Jypeli.Assets; |
---|
6 | using Jypeli.Controls; |
---|
7 | using Jypeli.Effects; |
---|
8 | using Jypeli.Widgets; |
---|
9 | using Point = Microsoft.Xna.Framework.Point; |
---|
10 | |
---|
11 | class Room : GameObject |
---|
12 | { |
---|
13 | // Sijainti pelin ruudukossa. |
---|
14 | public Point Location { get; set; } |
---|
15 | |
---|
16 | // Päällä näkyvä "katto" kun huone ei ole vielä kaivettu. |
---|
17 | public GameObject Roof { get; set; } |
---|
18 | |
---|
19 | // Neljä seinää, jotka on GameObjekteja. |
---|
20 | public Dictionary<Direction, GameObject> Walls { get; set; } |
---|
21 | |
---|
22 | public String Type { get; set; } |
---|
23 | |
---|
24 | public int Damage { get; set; } |
---|
25 | |
---|
26 | public int Culture { get; set; } |
---|
27 | |
---|
28 | private bool dug; // Onko huone kaivettu? |
---|
29 | public bool Dug |
---|
30 | { |
---|
31 | get { return dug; } |
---|
32 | set |
---|
33 | { |
---|
34 | dug = value; |
---|
35 | Roof.IsVisible = !Dug; |
---|
36 | } |
---|
37 | } |
---|
38 | |
---|
39 | public Room(Point paikka) |
---|
40 | : base(Dungeon.RUUDUN_KOKO, Dungeon.RUUDUN_KOKO) |
---|
41 | { |
---|
42 | Walls = new Dictionary<Direction, GameObject>(); |
---|
43 | Location = paikka; |
---|
44 | } |
---|
45 | |
---|
46 | public int Hinta { get; set; } |
---|
47 | |
---|
48 | } |
---|
49 | |
---|
50 | public class Dungeon : PhysicsGame |
---|
51 | { |
---|
52 | public const int RUUDUN_KOKO = 64; |
---|
53 | |
---|
54 | #region Kuvat |
---|
55 | Image lattiaKuva = LoadImage("floor"); |
---|
56 | Image seinaKuva = LoadImage("wall"); |
---|
57 | Image reikaSeinaKuva = LoadImage("wallhole"); |
---|
58 | Image kiviKuva = LoadImage("rock"); |
---|
59 | static Image kulttuuriKuva1 = LoadImage("es"); |
---|
60 | static Image kulttuuriKuva2 = LoadImage("nyan"); |
---|
61 | static Image kulttuuriKuva3 = LoadImage("spurdo"); |
---|
62 | Image[] huoneKuvat = new Image[] { kulttuuriKuva1, kulttuuriKuva2, kulttuuriKuva3 }; |
---|
63 | #endregion |
---|
64 | |
---|
65 | int[] hinnat = new int[] { 100, 200, 300 }; |
---|
66 | private int barbaariMaara = 3; |
---|
67 | |
---|
68 | Room[,] huoneet; |
---|
69 | Room spawn; |
---|
70 | Timer barbaariAjastin = new Timer(); |
---|
71 | Room ostettu; |
---|
72 | Point digStart; // Huoneen sijainti, josta kaivuu aloitetaan. |
---|
73 | bool digging = false; |
---|
74 | |
---|
75 | IntMeter kulttuuri = new IntMeter(300, 0, 2000); |
---|
76 | |
---|
77 | int vaakaHuoneet = 12; |
---|
78 | int pystyHuoneet = 8; |
---|
79 | |
---|
80 | public override void Begin() |
---|
81 | { |
---|
82 | ClearAll(); |
---|
83 | Kontrollit(); |
---|
84 | UlkoAsuRoskaa(); |
---|
85 | Kauppa(); |
---|
86 | |
---|
87 | // Luodaan huoneet ruutuihin. |
---|
88 | huoneet = new Room[vaakaHuoneet, pystyHuoneet]; |
---|
89 | foreach (var paikka in RuutujenPaikat()) |
---|
90 | { |
---|
91 | var huone = CreateRoom(paikka); |
---|
92 | Mouse.ListenOn(huone, MouseButton.Left, ButtonState.Pressed, () => RoomPressed(huone), null); |
---|
93 | Mouse.ListenOn(huone, MouseButton.Left, ButtonState.Released, () => RoomReleased(huone), null); |
---|
94 | huoneet[paikka.X, paikka.Y] = huone; |
---|
95 | } |
---|
96 | |
---|
97 | LuoSpawn(); |
---|
98 | |
---|
99 | barbaariAjastin.Timeout += delegate { LuoBarbaareja(); }; |
---|
100 | barbaariAjastin.Interval = 3; |
---|
101 | } |
---|
102 | |
---|
103 | void UlkoAsuRoskaa() |
---|
104 | { |
---|
105 | //Luodaan taustaolio |
---|
106 | //GameObject tausta = new GameObject(vaakaHuoneet * RUUDUN_KOKO, pystyHuoneet * RUUDUN_KOKO); |
---|
107 | //tausta.Color = Color.DarkBrown; |
---|
108 | //tausta.Position -= new Vector(RUUDUN_KOKO * 0.5, RUUDUN_KOKO * 0.5); |
---|
109 | //Add(tausta); |
---|
110 | |
---|
111 | Level.Background.Color = Color.Black; |
---|
112 | |
---|
113 | Label rahat = new Label(); |
---|
114 | rahat.BindTo(kulttuuri); |
---|
115 | rahat.Position = new Vector(Level.Right + Level.Width * 0.2, Level.Bottom + Level.Height * 0.1); |
---|
116 | rahat.TextColor = Color.White; |
---|
117 | rahat.IntFormatString = "Käytettävää kulttuuria: {0:D3}"; |
---|
118 | Add(rahat); |
---|
119 | } |
---|
120 | |
---|
121 | void Kauppa() |
---|
122 | { |
---|
123 | for (int i = 0; i < huoneKuvat.Length; i++) |
---|
124 | { |
---|
125 | Point lokaatio = new Point(0, 0); |
---|
126 | |
---|
127 | Room kuva = new Room(lokaatio); |
---|
128 | kuva.Position = new Vector((Level.Right + Level.Width * 0.05), (Level.Top - Level.Height * 0.25 - (i * RUUDUN_KOKO))); |
---|
129 | kuva.Image = huoneKuvat[i]; |
---|
130 | kuva.Hinta = hinnat[i]; |
---|
131 | Add(kuva); |
---|
132 | |
---|
133 | Mouse.ListenOn(kuva, MouseButton.Left, ButtonState.Pressed, delegate(Room a) { ostettu = a; }, "Asetetaan ostettu huone paikoilleen", kuva); |
---|
134 | |
---|
135 | Label teksti = new Label(); |
---|
136 | teksti.Position = kuva.Position + new Vector((RUUDUN_KOKO * 1.2), 0); |
---|
137 | teksti.TextColor = Color.White; |
---|
138 | teksti.Text = hinnat[i].ToString(); |
---|
139 | Add(teksti); |
---|
140 | } |
---|
141 | } |
---|
142 | |
---|
143 | void LuoBarbaareja() |
---|
144 | { |
---|
145 | PhysicsObject barbaari = new PhysicsObject(RUUDUN_KOKO * 0.3, RUUDUN_KOKO * 0.3); |
---|
146 | barbaari.Color = Color.Red; |
---|
147 | //barbaari.Position = RandomGen.NextVector(Level.Right, Level.Bottom, Level.Left, Level.Top); |
---|
148 | barbaari.Position = spawn.Position; |
---|
149 | Add(barbaari); |
---|
150 | } |
---|
151 | |
---|
152 | void Kontrollit() |
---|
153 | { |
---|
154 | IsMouseVisible = true; |
---|
155 | Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli"); |
---|
156 | Keyboard.Listen(Key.F1, ButtonState.Pressed, Begin, null); |
---|
157 | |
---|
158 | Keyboard.Listen(Key.Space, ButtonState.Pressed, SeuraavaAalto, "Anna kivan barbaariaallon tulla"); |
---|
159 | } |
---|
160 | |
---|
161 | void SeuraavaAalto() |
---|
162 | { |
---|
163 | barbaariMaara += 2; |
---|
164 | barbaariAjastin.Start(barbaariMaara); |
---|
165 | } |
---|
166 | |
---|
167 | void LuoSpawn() |
---|
168 | { |
---|
169 | spawn = huoneet[(int)(huoneet.GetLength(0) * 0.5), 0]; |
---|
170 | spawn.Dug = true; |
---|
171 | |
---|
172 | Direction oviSuunta = ((spawn.Position - new Vector(0, RUUDUN_KOKO * 0.5)).Angle.MainDirection); |
---|
173 | spawn.Walls[oviSuunta].Image = reikaSeinaKuva; |
---|
174 | |
---|
175 | Light valo = new Light(); |
---|
176 | valo.Position = spawn.Position; |
---|
177 | valo.Intensity = 1; |
---|
178 | valo.Distance = 40; |
---|
179 | Add(valo); |
---|
180 | |
---|
181 | } |
---|
182 | |
---|
183 | void RoomPressed(Room huone) |
---|
184 | { |
---|
185 | digStart = huone.Location; |
---|
186 | digging = true; |
---|
187 | } |
---|
188 | |
---|
189 | void RoomReleased(Room kohdeHuone) |
---|
190 | { |
---|
191 | digging = false; |
---|
192 | if (CanDig()) |
---|
193 | { |
---|
194 | // Merkataan huoneet kaivetuksi. |
---|
195 | var alkuHuone = huoneet[digStart.X, digStart.Y]; |
---|
196 | kohdeHuone.Dug = alkuHuone.Dug = true; |
---|
197 | |
---|
198 | // Reiät seinään. |
---|
199 | Direction oviSuunta = (kohdeHuone.Position - alkuHuone.Position).Angle.MainDirection; |
---|
200 | Direction toinenOviSuunta = Direction.Inverse(oviSuunta); |
---|
201 | alkuHuone.Walls[oviSuunta].Image = reikaSeinaKuva; |
---|
202 | kohdeHuone.Walls[toinenOviSuunta].Image = reikaSeinaKuva; |
---|
203 | } |
---|
204 | } |
---|
205 | |
---|
206 | Room CreateRoom(Point paikka) |
---|
207 | { |
---|
208 | Room huone = new Room(paikka); |
---|
209 | huone.Image = lattiaKuva; |
---|
210 | huone.Position = new Vector(paikka.X - huoneet.GetLength(0) / 2, paikka.Y - huoneet.GetLength(1) / 2) * RUUDUN_KOKO; |
---|
211 | foreach (var suunta in Suunnat()) |
---|
212 | huone.Walls[suunta] = CreateWall(huone.Position, suunta); |
---|
213 | Add(huone, -1); |
---|
214 | huone.Roof = new GameObject(RUUDUN_KOKO, RUUDUN_KOKO); |
---|
215 | huone.Roof.Image = kiviKuva; |
---|
216 | huone.Roof.Position = huone.Position; |
---|
217 | Add(huone.Roof, 2); |
---|
218 | |
---|
219 | Mouse.ListenOn(huone, MouseButton.Left, ButtonState.Pressed, AsetaHuone, "Asetetaan ostettu huone paikoilleen", huone); |
---|
220 | |
---|
221 | return huone; |
---|
222 | } |
---|
223 | |
---|
224 | void AsetaHuone(Room huone) |
---|
225 | { |
---|
226 | if(ostettu != null && huone.Dug && (kulttuuri.Value > huone.Hinta)) |
---|
227 | { |
---|
228 | huone.Damage = ostettu.Damage; |
---|
229 | huone.Culture = ostettu.Culture; |
---|
230 | huone.Image = ostettu.Image; |
---|
231 | kulttuuri.Value -= ostettu.Hinta; |
---|
232 | ostettu = null; |
---|
233 | } |
---|
234 | } |
---|
235 | |
---|
236 | GameObject CreateWall(Vector paikka, Direction suunta) |
---|
237 | { |
---|
238 | GameObject wall = new GameObject(RUUDUN_KOKO + 2, RUUDUN_KOKO + 2); |
---|
239 | wall.Position = paikka; |
---|
240 | wall.Image = seinaKuva; |
---|
241 | wall.Angle = suunta.Angle; |
---|
242 | Add(wall, 1); |
---|
243 | return wall; |
---|
244 | } |
---|
245 | |
---|
246 | bool CanDig() |
---|
247 | { |
---|
248 | Room kohdeHuone = GetRoom(MuunnaJypelista(Mouse.PositionOnWorld)); |
---|
249 | int dx = Math.Abs(kohdeHuone.Location.X - digStart.X); |
---|
250 | int dy = Math.Abs(kohdeHuone.Location.Y - digStart.Y); |
---|
251 | return dx + dy == 1; |
---|
252 | } |
---|
253 | |
---|
254 | protected override void Paint(Canvas canvas) |
---|
255 | { |
---|
256 | if (digging) |
---|
257 | { |
---|
258 | canvas.BrushColor = CanDig() ? Color.Green : Color.Red; |
---|
259 | canvas.DrawLine(huoneet[digStart.X, digStart.Y].Position, Mouse.PositionOnWorld); |
---|
260 | } |
---|
261 | base.Paint(canvas); |
---|
262 | } |
---|
263 | |
---|
264 | #region Extra Juttuja |
---|
265 | |
---|
266 | IEnumerable<Direction> Suunnat() |
---|
267 | { |
---|
268 | yield return Direction.Left; |
---|
269 | yield return Direction.Right; |
---|
270 | yield return Direction.Up; |
---|
271 | yield return Direction.Down; |
---|
272 | } |
---|
273 | |
---|
274 | IEnumerable<Point> RuutujenPaikat() |
---|
275 | { |
---|
276 | return from y in Enumerable.Range(0, huoneet.GetLength(1)) |
---|
277 | from x in Enumerable.Range(0, huoneet.GetLength(0)) |
---|
278 | select new Point(x, y); |
---|
279 | } |
---|
280 | |
---|
281 | Room GetRoom(Point paikka) |
---|
282 | { |
---|
283 | if (paikka.X >= 0 && paikka.X < huoneet.GetLength(0) && paikka.Y >= 0 && paikka.Y < huoneet.GetLength(1)) |
---|
284 | { |
---|
285 | return huoneet[paikka.X, paikka.Y]; |
---|
286 | } |
---|
287 | return null; |
---|
288 | } |
---|
289 | |
---|
290 | /// <summary> |
---|
291 | /// Muuntaa Jypeli-koordinaatin peliruudukon koordinaatiksi. |
---|
292 | /// </summary> |
---|
293 | Point MuunnaJypelista(Vector paikka) |
---|
294 | { |
---|
295 | Vector siirrettyOrigo = paikka + RUUDUN_KOKO * (0.5 * new Vector(huoneet.GetLength(0), huoneet.GetLength(1)) + new Vector(0.5, 0.5)); |
---|
296 | Vector wtf = new Vector(siirrettyOrigo.X < 0 ? -1 : 0, siirrettyOrigo.Y < 0 ? -1 : 0); |
---|
297 | Vector valmis = wtf + siirrettyOrigo / RUUDUN_KOKO; |
---|
298 | return new Point((int)valmis.X, (int)valmis.Y); |
---|
299 | } |
---|
300 | |
---|
301 | #endregion |
---|
302 | } |
---|