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