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 MathHelper; |
---|
9 | using Entity; |
---|
10 | using Rooms; |
---|
11 | |
---|
12 | public class TheDungeonGame : PhysicsGame |
---|
13 | { |
---|
14 | public const double ROOMTHICKNESS = 60; |
---|
15 | public const double ROOMWIDTH = 800 - ROOMTHICKNESS; |
---|
16 | public const double ROOMHEIGHT = 450; |
---|
17 | |
---|
18 | public static Vector roomSize = new Vector(ROOMWIDTH, ROOMHEIGHT); |
---|
19 | private Player player; |
---|
20 | private LevelGenerator generator; |
---|
21 | |
---|
22 | public override void Begin() |
---|
23 | { |
---|
24 | IsMouseVisible = true; |
---|
25 | SetWindowSize(800, 600); |
---|
26 | Level.Width = ROOMWIDTH * 20; |
---|
27 | Level.Height = ROOMHEIGHT * 20; |
---|
28 | |
---|
29 | generator = new LevelGenerator(this, Level.Size, new Vector(20, 20)); |
---|
30 | generator.generateRandomLevel(5, 10); |
---|
31 | generator.buildDungeon(); |
---|
32 | Room centerRoom = generator.getRoomAt((int)(generator.CenterRoom.X), (int)(generator.CenterRoom.Y)); |
---|
33 | Camera.Position = centerRoom.Position + new Vector(ROOMWIDTH/2 + ROOMTHICKNESS/2, -ROOMHEIGHT/2 + ROOMTHICKNESS/4); |
---|
34 | |
---|
35 | player = new Player(this, Vector.Zero); |
---|
36 | setupPlayer(); |
---|
37 | |
---|
38 | Keyboard.Listen(Key.Escape, ButtonState.Pressed, Exit, "Lopeta peli"); |
---|
39 | Keyboard.Listen(Key.R, ButtonState.Pressed, regenerateLevel, null); |
---|
40 | } |
---|
41 | |
---|
42 | private void setupPlayer() |
---|
43 | { |
---|
44 | player.Position = generator.getRoomAt((int)(generator.CenterRoom.X), (int)(generator.CenterRoom.Y)).Position + new Vector(ROOMWIDTH / 2, -ROOMHEIGHT / 2); |
---|
45 | Add(player, 1); |
---|
46 | } |
---|
47 | |
---|
48 | private void regenerateLevel() |
---|
49 | { |
---|
50 | generator.destroyDungeon(); |
---|
51 | generator = new LevelGenerator(this, Level.Size, new Vector(20, 20)); |
---|
52 | generator.generateRandomLevel(10, 15); |
---|
53 | generator.buildDungeon(); |
---|
54 | } |
---|
55 | |
---|
56 | int errorAmount = 0; |
---|
57 | protected override void Update(Time time) |
---|
58 | { |
---|
59 | try // Tapa pakottaa Updatea toimimaan LevelGeneratorin kanssa |
---|
60 | { |
---|
61 | base.Update(time); |
---|
62 | } |
---|
63 | catch (FormatException) // Kaatuu tähän melkein kokoajan kun yrittää luoda tasoa. Mun koodi liian "likainen", tai hidas? |
---|
64 | { |
---|
65 | if (errorAmount > 50) // Virheitä voi tulla aika monta samaan aikaan, joten tehdään raja, jolloin regeneroidaan leveli |
---|
66 | { |
---|
67 | regenerateLevel(); |
---|
68 | errorAmount = 0; |
---|
69 | } |
---|
70 | errorAmount++; |
---|
71 | return; // Skipataan "virheelisiä" tickea ja huijataan peliä niin kuin pahat merimiehet (dirty pirates, Arr) |
---|
72 | } |
---|
73 | |
---|
74 | player.Update(time); |
---|
75 | } |
---|
76 | } |
---|