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 | using Gui; |
---|
12 | |
---|
13 | public class TheDungeonGame : PhysicsGame |
---|
14 | { |
---|
15 | public const double ROOMTHICKNESS = 60; |
---|
16 | public const double ROOMWIDTH = 800 - ROOMTHICKNESS; |
---|
17 | public const double ROOMHEIGHT = 450; |
---|
18 | |
---|
19 | public static Vector roomSize = new Vector(ROOMWIDTH, ROOMHEIGHT); |
---|
20 | private Player player; |
---|
21 | public Player Player { get { return player; } } |
---|
22 | private LevelGenerator generator; |
---|
23 | public LevelGenerator LevelGen { get { return generator; } } |
---|
24 | |
---|
25 | private Vector cameraPosMission, cameraVelocity, newPos, oldPos; |
---|
26 | private Room oldRoom; |
---|
27 | private bool moveCamera = false; |
---|
28 | |
---|
29 | public Image playerPic = LoadImage("player"); |
---|
30 | public List<Image> levelTopBottoms = new List<Image>(); |
---|
31 | public List<Image> levelLeftRights = new List<Image>(); |
---|
32 | public List<Image> levelBackGrounds = new List<Image>(); |
---|
33 | public List<Image> objectTextures = new List<Image>(); |
---|
34 | public static List<SoundEffect> soundEffects = new List<SoundEffect>(); |
---|
35 | public InGameGui gui; |
---|
36 | public static MediaPlayer soundPlayer; |
---|
37 | |
---|
38 | public override void Begin() |
---|
39 | { |
---|
40 | IsMouseVisible = true; |
---|
41 | SetWindowSize(800, 600); |
---|
42 | showMainMenu(); |
---|
43 | Keyboard.Listen(Key.Escape, ButtonState.Pressed, Exit, "Lopeta peli"); |
---|
44 | } |
---|
45 | |
---|
46 | private void showMainMenu() |
---|
47 | { |
---|
48 | MultiSelectWindow menu = new MultiSelectWindow("Welcome to: \nThe Dungeon Game!",new string[]{"Play!", "Exit"}); |
---|
49 | menu.AddItemHandler(0, initGame); |
---|
50 | menu.AddItemHandler(1, Exit); |
---|
51 | Add(menu); |
---|
52 | } |
---|
53 | |
---|
54 | private void initGame() |
---|
55 | { |
---|
56 | Level.Width = ROOMWIDTH * 20; |
---|
57 | Level.Height = ROOMHEIGHT * 20; |
---|
58 | setupLevelTextures(1); |
---|
59 | setupObjectTextures(); |
---|
60 | setupSoundEffects(); |
---|
61 | soundPlayer = MediaPlayer; |
---|
62 | |
---|
63 | player = new Player(this, Vector.Zero, playerPic); |
---|
64 | player.setupWeapon(); |
---|
65 | |
---|
66 | generator = new LevelGenerator(this, Level.Size, new Vector(20, 20)); |
---|
67 | generator.generateRandomLevel(10, 20); |
---|
68 | generator.initDungeon(); |
---|
69 | setupCamera(); |
---|
70 | setupPlayer(); |
---|
71 | |
---|
72 | gui = new InGameGui(this); |
---|
73 | Add(gui); |
---|
74 | |
---|
75 | AddCollisionHandler(player, player.performCollision); |
---|
76 | AddCollisionHandler(player, "Item", player.handleItemPickup); |
---|
77 | |
---|
78 | |
---|
79 | Keyboard.Listen(Key.R, ButtonState.Pressed, restartGame, null); |
---|
80 | } |
---|
81 | |
---|
82 | private void setupSoundEffects() |
---|
83 | { |
---|
84 | soundEffects.Add(LoadSoundEffect("sound/door_close")); |
---|
85 | soundEffects.Add(LoadSoundEffect("sound/door_open")); |
---|
86 | } |
---|
87 | |
---|
88 | private void setupLevelTextures(int levels) |
---|
89 | { |
---|
90 | for (int i = 1; i <= levels; i++) |
---|
91 | { |
---|
92 | levelTopBottoms.Add(LoadImage("level" + i + "/topBottom")); |
---|
93 | levelLeftRights.Add(LoadImage("level" + i + "/leftRight")); |
---|
94 | levelBackGrounds.Add(LoadImage("level" + i + "/levelBackground")); |
---|
95 | } |
---|
96 | } |
---|
97 | |
---|
98 | private void setupObjectTextures() |
---|
99 | { |
---|
100 | objectTextures.Add(LoadImage("objects/door_closed")); |
---|
101 | objectTextures.Add(LoadImage("objects/door_opened")); |
---|
102 | objectTextures.Add(LoadImage("objects/bossdoor")); |
---|
103 | } |
---|
104 | |
---|
105 | public Image getLeftRightWall(int level) |
---|
106 | { |
---|
107 | return levelLeftRights[level - 1]; |
---|
108 | } |
---|
109 | |
---|
110 | public Image getTopBottomWall(int level) |
---|
111 | { |
---|
112 | return levelTopBottoms[level - 1]; |
---|
113 | } |
---|
114 | |
---|
115 | public Image getLevelBackground(int level) |
---|
116 | { |
---|
117 | return levelBackGrounds[level - 1]; |
---|
118 | } |
---|
119 | |
---|
120 | public void moveToRoom(Room room, int dir) |
---|
121 | { |
---|
122 | oldPos = player.currentRoom.Position + new Vector(ROOMWIDTH / 2 + ROOMTHICKNESS / 2, -ROOMHEIGHT / 2 + ROOMTHICKNESS / 4); |
---|
123 | newPos = room.Position + new Vector(ROOMWIDTH / 2 + ROOMTHICKNESS / 2, -ROOMHEIGHT / 2 + ROOMTHICKNESS / 4); |
---|
124 | oldRoom = player.currentRoom; |
---|
125 | |
---|
126 | if (room.isBuilt) room.restoreLevel(); |
---|
127 | else room.buildLevel(); |
---|
128 | |
---|
129 | oldRoom.killBrains(); |
---|
130 | moveCameraTo(newPos - oldPos); |
---|
131 | player.Position = player.currentRoom.getDoor(dir).Position + RoomDirection.getOffsetFromWorldDir(dir) * 130; |
---|
132 | } |
---|
133 | |
---|
134 | public void moveCameraTo(Vector pos) |
---|
135 | { |
---|
136 | cameraPosMission = pos; |
---|
137 | cameraVelocity = pos * 1.5; |
---|
138 | moveCamera = true; |
---|
139 | } |
---|
140 | |
---|
141 | private void moveCameraPart() |
---|
142 | { |
---|
143 | Camera.Velocity = cameraVelocity; |
---|
144 | Vector remainingLenght = Camera.Position - newPos; |
---|
145 | if (Math.Abs((int)remainingLenght.X) < 20 && Math.Abs((int)remainingLenght.Y) < 20) |
---|
146 | { |
---|
147 | cameraPosMission = Vector.Zero; |
---|
148 | cameraVelocity = Vector.Zero; |
---|
149 | Camera.Velocity = cameraVelocity; |
---|
150 | Camera.Position = newPos; |
---|
151 | moveCamera = false; |
---|
152 | oldRoom.hideLevel(); |
---|
153 | player.currentRoom.restoreBrains(); |
---|
154 | |
---|
155 | if (player.currentRoom.roomName == "Boss Room") |
---|
156 | { |
---|
157 | EntityBossMario mario = (EntityBossMario)player.currentRoom.getEntityAt(3, 3); |
---|
158 | gui.setupBossGauge(mario); |
---|
159 | } |
---|
160 | } |
---|
161 | } |
---|
162 | |
---|
163 | private void setupPlayer() |
---|
164 | { |
---|
165 | player.Position = generator.getRoomAt(generator.CenterRoom).Position + new Vector(ROOMWIDTH / 2, -ROOMHEIGHT / 2); |
---|
166 | player.currentRoom = generator.getRoomAt(generator.CenterRoom); |
---|
167 | player.currentRoom.buildLevel(); |
---|
168 | Add(player, 2); |
---|
169 | } |
---|
170 | |
---|
171 | private void setupCamera() |
---|
172 | { |
---|
173 | Room centerRoom = generator.getRoomAt((int)(generator.CenterRoom.X), (int)(generator.CenterRoom.Y)); |
---|
174 | Camera.Position = centerRoom.Position + new Vector(ROOMWIDTH / 2 + ROOMTHICKNESS / 2, -ROOMHEIGHT / 2 + ROOMTHICKNESS / 4); |
---|
175 | } |
---|
176 | |
---|
177 | private void regenerateLevel() |
---|
178 | { |
---|
179 | generator.destroyDungeon(); |
---|
180 | generator = new LevelGenerator(this, Level.Size, new Vector(20, 20)); |
---|
181 | generator.generateRandomLevel(10, 20); |
---|
182 | generator.initDungeon(); |
---|
183 | player.Position = generator.getRoomAt(generator.CenterRoom).Position + new Vector(ROOMWIDTH / 2, -ROOMHEIGHT / 2); |
---|
184 | player.currentRoom = generator.getRoomAt(generator.CenterRoom); |
---|
185 | player.currentRoom.buildLevel(); |
---|
186 | setupCamera(); |
---|
187 | } |
---|
188 | |
---|
189 | public void restartGame() |
---|
190 | { |
---|
191 | Keyboard.EnableAll(); |
---|
192 | player.resetValues(); |
---|
193 | gui.Destroy(); |
---|
194 | gui = new InGameGui(this); |
---|
195 | Add(gui); |
---|
196 | regenerateLevel(); |
---|
197 | } |
---|
198 | |
---|
199 | int errorAmount = 0; |
---|
200 | protected override void Update(Time time) |
---|
201 | { |
---|
202 | try // Tapa pakottaa Updatea toimimaan LevelGeneratorin kanssa |
---|
203 | { |
---|
204 | base.Update(time); |
---|
205 | } |
---|
206 | catch (FormatException) // Kaatuu tähän melkein kokoajan kun yrittää luoda tasoa. Mun koodi liian "likainen", tai hidas? |
---|
207 | { |
---|
208 | if (errorAmount > 50) // Virheitä voi tulla aika monta samaan aikaan, joten tehdään raja, jolloin regeneroidaan leveli |
---|
209 | { |
---|
210 | regenerateLevel(); |
---|
211 | errorAmount = 0; |
---|
212 | } |
---|
213 | errorAmount++; |
---|
214 | return; // Skipataan "virheelisiä" tickea ja huijataan peliä niin kuin pahat merimiehet (dirty pirates, Arr) |
---|
215 | } |
---|
216 | if (moveCamera) |
---|
217 | { |
---|
218 | moveCameraPart(); |
---|
219 | } |
---|
220 | if (player != null) |
---|
221 | player.Update(time); |
---|
222 | } |
---|
223 | } |
---|