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 | |
---|
9 | public class Proto236b : PhysicsGame //true survivor |
---|
10 | { |
---|
11 | private Player player; |
---|
12 | public Player Player { get { return player; } set { player = value; } } |
---|
13 | private Hud hud; |
---|
14 | public Hud Hud { get { return hud; } set { hud = value; } } |
---|
15 | private ColorTileMap map; |
---|
16 | public ColorTileMap Map { get { return map; } set { map = value; } } |
---|
17 | private Dictionary<string, Image> images = new Dictionary<string, Image>(); |
---|
18 | public Dictionary<string, Image> Images { get { return images; } set { images = value; } } |
---|
19 | private Dictionary<string, Image[]> imageLists = new Dictionary<string, Image[]>(); |
---|
20 | public Dictionary<string, Image[]> ImageLists { get { return imageLists; } set { imageLists = value; } } |
---|
21 | private Music music; |
---|
22 | public Music Music { get { return music; } set { music = value; } } |
---|
23 | private ProtoLevel currentLevel; |
---|
24 | public ProtoLevel CurrentLevel { get { return currentLevel; } set { currentLevel = value; } } |
---|
25 | private ScreenView gameScreen; |
---|
26 | public ScreenView GameScreen { get { return gameScreen; } set { gameScreen = value; } } |
---|
27 | private Dictionary<string, ProtoLevel> levels = new Dictionary<string, ProtoLevel>(); |
---|
28 | private Galaxy galaxy; |
---|
29 | public Galaxy Galaxy { get { return galaxy; } set { galaxy = value; } } |
---|
30 | private Dictionary<int, Dictionary<int, List<GameObject>>> positionGrid = new Dictionary<int, Dictionary<int, List<GameObject>>>(); |
---|
31 | private List<GameObject> addedList = new List<GameObject>(); |
---|
32 | |
---|
33 | void AssignKeys() |
---|
34 | { |
---|
35 | Keyboard.Listen(Key.Escape, ButtonState.Down, Exit, "Lento"); |
---|
36 | Keyboard.Listen(Key.Up, ButtonState.Down, player.thrusterStart, "Lento", 1.0); |
---|
37 | Keyboard.Listen(Key.Up, ButtonState.Released, player.thrusterEnd, "Lento"); |
---|
38 | Keyboard.Listen(Key.Down, ButtonState.Down, player.thrusterStart, "Lento", -0.2); |
---|
39 | Keyboard.Listen(Key.Down, ButtonState.Released, player.thrusterEnd, "Lento"); |
---|
40 | Keyboard.Listen(Key.Left, ButtonState.Down, player.rotate, "Lento", 4.0); |
---|
41 | Keyboard.Listen(Key.Right, ButtonState.Down, player.rotate, "Lento", -4.0); |
---|
42 | Keyboard.Listen(Key.LeftControl, ButtonState.Down, player.shoot, "Lento"); |
---|
43 | } |
---|
44 | void LoadAllImages() |
---|
45 | { |
---|
46 | images["space_background"] = LoadImage("graphics/backgrounds/Space"); |
---|
47 | images["mars_background"] = LoadImage("graphics/backgrounds/Mars"); |
---|
48 | images["planet2"] = LoadImage("graphics/Planets/MapPlanet4"); |
---|
49 | images["planet3"] = LoadImage("graphics/Planets/MapPlanet3"); |
---|
50 | images["planet1_background"] = LoadImage("graphics/backgrounds/planet1"); |
---|
51 | images["planet2_background"] = LoadImage("graphics/backgrounds/planet2"); |
---|
52 | images["planet3_background"] = LoadImage("graphics/backgrounds/planet1"); |
---|
53 | images["player"] = LoadImage("graphics/ships/player1.0"); |
---|
54 | images["galaxy_map"] = LoadImage("graphics/HUD/galaxy_map"); |
---|
55 | imageLists["planet_map"] = LoadImages("graphics/HUD/RadarPlaneetta1.0", "graphics/HUD/RadarPlaneetta1.1"); |
---|
56 | |
---|
57 | //kaikki tilet |
---|
58 | images["grass1"] = LoadImage("graphics/tiles/tilenurmi"); |
---|
59 | images["soil1"] = LoadImage("graphics/tiles/tilemulta"); |
---|
60 | images["wood1"] = LoadImage("graphics/tiles/tilepuu"); |
---|
61 | |
---|
62 | imageLists["player_thruster"] = LoadImages("graphics/effects/thrusters/thruster0.1", "graphics/effects/thrusters/thruster0.2"); |
---|
63 | imageLists["enemy1"] = LoadImages("graphics/Enemy/Enemy1.0", "graphics/Enemy/Enemy1.1", "graphics/Enemy/Enemy1.2"); |
---|
64 | imageLists["HUD_radar"] = LoadImages("graphics/HUD/HUD_radar1.0", "graphics/HUD/HUD_radar1.1"); |
---|
65 | images["HUD_ship"] = LoadImage("graphics/HUD/cross1.0"); |
---|
66 | } |
---|
67 | void LoadAllMusic() |
---|
68 | { |
---|
69 | this.music = new Music(); |
---|
70 | } |
---|
71 | public SoundEffect loadSound(string ef) |
---|
72 | { |
---|
73 | return LoadSoundEffect(ef); |
---|
74 | } |
---|
75 | public void LoadLevel(string levelId, Angle planetAngle/*PlanetAngle määrittää missä kulmassa planeettaan tullaan tai sieltä poistutaan*/) |
---|
76 | { |
---|
77 | ClearAll(); |
---|
78 | |
---|
79 | //MessageDisplay.Add("loading level '" + level.TileMapSrc + "'..."); |
---|
80 | currentLevel = this.levels[levelId]; |
---|
81 | |
---|
82 | Level.Background.Color = Color.Black; |
---|
83 | this.player.Velocity = Vector.Zero;//resetoi pelaajan velocity |
---|
84 | Add(this.player, 1); |
---|
85 | Camera.Follow(this.player); |
---|
86 | |
---|
87 | LevelFromImage(currentLevel); |
---|
88 | |
---|
89 | MediaPlayer.Stop(); |
---|
90 | string music = currentLevel.PickMusic(); |
---|
91 | if (music != "") |
---|
92 | { |
---|
93 | MediaPlayer.Play(music); |
---|
94 | } |
---|
95 | |
---|
96 | if (currentLevel.IsPlanet) |
---|
97 | { |
---|
98 | Gravity = new Vector(0, -200); |
---|
99 | player.LinearDamping = 0.99;//ns ilmanvastus |
---|
100 | galaxy.CurrentPlanet = galaxy.Planets[currentLevel.Id];//aseta galaksiin tiedot nykyisestä planeetasta |
---|
101 | player.Y = Level.Top - Screen.Height; |
---|
102 | //player.X = (planetAngle.Degrees / Angle.FullAngle.Degrees) * Level.Width - Level.Width / 2; |
---|
103 | Layers[-3].RelativeTransition = new Vector(1, 1); |
---|
104 | } |
---|
105 | else |
---|
106 | { |
---|
107 | Gravity = new Vector(0, 0); |
---|
108 | player.LinearDamping = 1;//avaruudessa ei ilmanvastusta |
---|
109 | Camera.Zoom(0.4); |
---|
110 | if (galaxy.CurrentPlanet != null)//current planet voi olla null jos pelaaja ei vielä ole käynyt millään planeetalla. |
---|
111 | { |
---|
112 | //aseta pelaaja galaksiin |
---|
113 | player.Position = galaxy.CurrentPlanet.Position; |
---|
114 | //laske vielä vektori, mistä suunnasta pelaaja poistui |
---|
115 | player.Position += Vector.FromLengthAndAngle(galaxy.CurrentPlanet.Radius + 50, planetAngle); |
---|
116 | player.AbsoluteAngle = planetAngle; |
---|
117 | } |
---|
118 | Layers[-1].RelativeTransition = new Vector(0.1, 0.1); |
---|
119 | } |
---|
120 | |
---|
121 | AssignKeys(); |
---|
122 | initializeHUD(); |
---|
123 | } |
---|
124 | void CreateBg(string id, int offset, double scale = 1, int layer = -3) |
---|
125 | { |
---|
126 | double h = 1, w = 1; |
---|
127 | if (currentLevel.IsPlanet) |
---|
128 | { |
---|
129 | h = 150 * 40; |
---|
130 | w = Level.Width; |
---|
131 | } |
---|
132 | else |
---|
133 | { |
---|
134 | h = Level.Height * scale; |
---|
135 | w = Level.Width * scale; |
---|
136 | } |
---|
137 | GameObject bg = new GameObject(w, h); |
---|
138 | bg.Image = images[id]; |
---|
139 | bg.X += Level.Width * offset; |
---|
140 | if (!currentLevel.IsPlanet) |
---|
141 | { |
---|
142 | bg.Y += Level.Height * offset; |
---|
143 | } |
---|
144 | Add(bg, layer); |
---|
145 | } |
---|
146 | void LevelFromImage(ProtoLevel level) |
---|
147 | { |
---|
148 | Dictionary<String, String> convert = new Dictionary<String, String>(); |
---|
149 | convert.Add("000000", "grass1"); |
---|
150 | convert.Add("00ffff", "wood1"); |
---|
151 | ColorTileMap tileMap = ColorTileMap.FromLevelAsset(level.TileMapSrc); |
---|
152 | tileMap.SetTileMethod(Color.FromHexCode("000000"), createTile, convert["000000"], false); |
---|
153 | tileMap.SetTileMethod(Color.FromHexCode("00ffff"), createTile, convert["00ffff"], true); |
---|
154 | tileMap.SetTileMethod(Color.FromHexCode("ff0000"), spawnPlayer); |
---|
155 | tileMap.SetTileMethod(Color.FromHexCode("00ff00"), spawnEnemy); |
---|
156 | |
---|
157 | //shopit |
---|
158 | //tileMap.SetTileMethod(Color.FromHexCode("ff7700"), createShop); |
---|
159 | |
---|
160 | //kaikki spacessa olevat jutut voisi alkaa CC |
---|
161 | tileMap.SetTileMethod(Color.FromHexCode("ccffff"), createPlanet, "planet2");//planeetta 2 |
---|
162 | tileMap.SetTileMethod(Color.FromHexCode("ccffcc"), createPlanet, "planet3");//planeetta 1 |
---|
163 | |
---|
164 | double w = 0, h = 0; |
---|
165 | //tiilien välinen skaala valitaan |
---|
166 | if (CurrentLevel.IsPlanet) |
---|
167 | { |
---|
168 | w = 40; h = 40; |
---|
169 | } |
---|
170 | else |
---|
171 | { |
---|
172 | w = 2000; h = 2000; |
---|
173 | } |
---|
174 | tileMap.Execute(w, h); |
---|
175 | map = tileMap; |
---|
176 | |
---|
177 | if (currentLevel.IsPlanet) |
---|
178 | { |
---|
179 | //luo backgroundit (2 valetta ja 1 oikea) |
---|
180 | CreateBg(level.BackgroundId, -1); |
---|
181 | CreateBg(level.BackgroundId, 0); |
---|
182 | CreateBg(level.BackgroundId, 1); |
---|
183 | //lasketaan montako tiiltä tarvitaan |
---|
184 | double zoom = 0.7; |
---|
185 | var tilesNeeded = Math.Floor((Screen.Width / 2 / 40) / zoom); |
---|
186 | |
---|
187 | //vasen puoli |
---|
188 | for (int x = 0; x < tilesNeeded; x++) |
---|
189 | { |
---|
190 | for (int y = 0; y < tileMap.RowCount; y++) |
---|
191 | { |
---|
192 | String hexColor = tileMap.GetTile(y, x).ToString().Substring(2, 6).ToLower(); |
---|
193 | if (convert.ContainsKey(hexColor)) |
---|
194 | createTile(new Vector(x * w + w / 2 - Level.Width / 2 + Level.Width, (tileMap.RowCount - y - 1) * h + h / 2 - Level.Height / 2), w, h, convert[hexColor], false); |
---|
195 | } |
---|
196 | } |
---|
197 | //oikea puoli |
---|
198 | for (int x = tileMap.ColumnCount - 1; x > tileMap.ColumnCount - 1 - tilesNeeded; x--) |
---|
199 | { |
---|
200 | for (int y = 0; y < tileMap.RowCount; y++) |
---|
201 | { |
---|
202 | String hexColor = tileMap.GetTile(y, x).ToString().Substring(2, 6).ToLower(); |
---|
203 | if (convert.ContainsKey(hexColor)) |
---|
204 | createTile(new Vector(x * w + w / 2 - Level.Width / 2 - Level.Width, (tileMap.RowCount - y - 1) * h + h / 2 - Level.Height / 2), w, h, convert[hexColor], false); |
---|
205 | } |
---|
206 | } |
---|
207 | } |
---|
208 | else |
---|
209 | { |
---|
210 | CreateBg("space_background", 0, 1, -1); |
---|
211 | } |
---|
212 | } |
---|
213 | public void initializeHUD() |
---|
214 | { |
---|
215 | if (hud != null) |
---|
216 | { |
---|
217 | //poista vanha hud |
---|
218 | hud.Destroy(); |
---|
219 | } |
---|
220 | hud = new Hud(this); |
---|
221 | } |
---|
222 | void createTile(Vector position, double w, double h, string id, bool breakable = false) |
---|
223 | { |
---|
224 | if (id == "") { return; } |
---|
225 | if (breakable) |
---|
226 | { |
---|
227 | MikonPhysicsObject tile = new MikonPhysicsObject(this, w, h); |
---|
228 | tile.MakeStatic(); |
---|
229 | tile.AbsolutePosition = position; |
---|
230 | tile.Image = images[id]; |
---|
231 | Add(tile); |
---|
232 | } |
---|
233 | else |
---|
234 | { |
---|
235 | PhysicsObject tile = new PhysicsObject(w, h); |
---|
236 | tile.MakeStatic(); |
---|
237 | tile.AbsolutePosition = position; |
---|
238 | tile.Image = images[id]; |
---|
239 | Add(tile); |
---|
240 | } |
---|
241 | } |
---|
242 | void createPlanet(Vector position, double w, double h, string id) |
---|
243 | { |
---|
244 | Planet planet = new Planet(this, position, 5000, id); |
---|
245 | AddCheap(planet); |
---|
246 | } |
---|
247 | void createShop(Vector position, double w, double h) |
---|
248 | { |
---|
249 | Shop shop = new Shop(); |
---|
250 | Add(shop); |
---|
251 | } |
---|
252 | void spawnPlayer(Vector position, double w, double h) |
---|
253 | { |
---|
254 | player.Position = position; |
---|
255 | } |
---|
256 | void spawnEnemy(Vector position, double w, double h) |
---|
257 | { |
---|
258 | new Enemy1(this, w, h, position); |
---|
259 | } |
---|
260 | void LoadAllLevels() |
---|
261 | { |
---|
262 | this.levels["space"] = new ProtoLevel(this, "!space", false, "space_background"); |
---|
263 | this.levels["planet2"] = new ProtoLevel(this, "planet2", true, "planet1_background"); |
---|
264 | this.levels["planet3"] = new ProtoLevel(this, "planet3", true, "planet2_background"); |
---|
265 | } |
---|
266 | public override void Begin() |
---|
267 | { |
---|
268 | // TODO: Kirjoita peli tähän |
---|
269 | SmoothTextures = false; |
---|
270 | LoadAllMusic(); |
---|
271 | LoadAllImages(); |
---|
272 | LoadAllLevels(); |
---|
273 | |
---|
274 | this.player = new Player(this); |
---|
275 | this.player.attachWeapon(); |
---|
276 | this.player.IsUpdated = true; |
---|
277 | |
---|
278 | this.gameScreen = Screen; |
---|
279 | this.galaxy = new Galaxy(this); |
---|
280 | |
---|
281 | LoadLevel("space", Angle.Zero);//avaruus ladataan jotta galaksi saa planeettansa |
---|
282 | //LoadLevel("planet2", Angle.Zero); |
---|
283 | } |
---|
284 | protected override void Update(Microsoft.Xna.Framework.GameTime gameTime) |
---|
285 | { |
---|
286 | Predicate<GameObject> predicate = isGameObject; |
---|
287 | List<GameObject> list = this.GetObjects(predicate); |
---|
288 | int x = Convert.ToInt32(Math.Round(player.X / 5000) * 5000); |
---|
289 | int y = Convert.ToInt32(Math.Round(player.Y / 5000) * 5000); |
---|
290 | foreach (GameObject obj in addedList)//tuhoa vanhat objektit |
---|
291 | { |
---|
292 | Remove(obj); |
---|
293 | } |
---|
294 | addedList.Clear(); |
---|
295 | foreach (GameObject obj in list)//lisää uudet objektit |
---|
296 | { |
---|
297 | if (!positionGrid.ContainsKey(x)) { continue; } |
---|
298 | if (!positionGrid[x].ContainsKey(y)) { continue; } |
---|
299 | for (var i = 0; i < positionGrid[x][y].Count; i++) |
---|
300 | { |
---|
301 | positionGrid[x][y][i].Layer = new Layer(); |
---|
302 | addedList.Add(positionGrid[x][y][i]); |
---|
303 | } |
---|
304 | } |
---|
305 | base.Update(gameTime); |
---|
306 | } |
---|
307 | private bool isGameObject(GameObject obj) |
---|
308 | { |
---|
309 | return (obj is GameObject); |
---|
310 | } |
---|
311 | private void AddCheap(GameObject o) |
---|
312 | { |
---|
313 | int x = Convert.ToInt32(Math.Round(o.X / 5000) * 5000), |
---|
314 | y = Convert.ToInt32(Math.Round(o.Y / 5000) * 5000); |
---|
315 | if (!positionGrid.ContainsKey(x)) |
---|
316 | { |
---|
317 | Dictionary<int, List<GameObject>> posGridX = new Dictionary<int, List<GameObject>>(); |
---|
318 | positionGrid.Add(x, posGridX); |
---|
319 | } |
---|
320 | if (!positionGrid[x].ContainsKey(y)) |
---|
321 | { |
---|
322 | List<GameObject> posGridY = new List<GameObject>(); |
---|
323 | positionGrid[x].Add(y, posGridY); |
---|
324 | } |
---|
325 | GameObject uusi = ((GameObject)o); |
---|
326 | positionGrid[x][y].Add(uusi); |
---|
327 | Add(uusi); |
---|
328 | Remove(uusi); |
---|
329 | } |
---|
330 | } |
---|