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