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 | Player player; |
---|
12 | private ColorTileMap map; |
---|
13 | public ColorTileMap Map { get { return map; } set { map = value; } } |
---|
14 | private Dictionary<string, Image> images = new Dictionary<string, Image>(); |
---|
15 | public Dictionary<string, Image> Images { get { return images; } set { images = value; } } |
---|
16 | private Dictionary<string, Image[]> imageLists = new Dictionary<string, Image[]>(); |
---|
17 | public Dictionary<string, Image[]> ImageLists { get { return imageLists; } set { imageLists = value; } } |
---|
18 | private ProtoLevel currentLevel; |
---|
19 | public ProtoLevel CurrentLevel { get { return currentLevel; } set { currentLevel = value; } } |
---|
20 | |
---|
21 | void AssignKeys() |
---|
22 | { |
---|
23 | Keyboard.Listen(Key.Escape, ButtonState.Down, Exit, "Lopeta Peli"); |
---|
24 | Keyboard.Listen(Key.Up, ButtonState.Down, player.thrusterStart, "Lento", 1.0); |
---|
25 | Keyboard.Listen(Key.Up, ButtonState.Released, player.thrusterEnd, "Lento"); |
---|
26 | Keyboard.Listen(Key.Down, ButtonState.Down, player.thrusterStart, "Lento", -0.2); |
---|
27 | Keyboard.Listen(Key.Down, ButtonState.Released, player.thrusterEnd, "Lento"); |
---|
28 | Keyboard.Listen(Key.Left, ButtonState.Down, player.rotate, "Lento", 4.0); |
---|
29 | Keyboard.Listen(Key.Right, ButtonState.Down, player.rotate, "Lento", -4.0); |
---|
30 | } |
---|
31 | void LoadAllImages() |
---|
32 | { |
---|
33 | images["background0"] = LoadImage("graphics/backgrounds/space_background"); |
---|
34 | images["backgroundmars"] = LoadImage("graphics/backgrounds/Mars"); |
---|
35 | images["player"] = LoadImage("graphics/ships/player"); |
---|
36 | images["tile0"] = LoadImage("graphics/tiles/tile0"); |
---|
37 | imageLists["player_thruster"] = LoadImages("graphics/effects/thrusters/thruster0.1", "graphics/effects/thrusters/thruster0.2"); |
---|
38 | imageLists["enemy1"] = LoadImages("graphics/Enemy/Enemy1.0","graphics/Enemy/Enemy1.1","graphics/Enemy/Enemy1.2"); |
---|
39 | } |
---|
40 | void LoadLevel(ProtoLevel level) |
---|
41 | { |
---|
42 | ClearAll(); |
---|
43 | currentLevel = level; |
---|
44 | if (level.IsPlanet) |
---|
45 | { |
---|
46 | Gravity = new Vector(0, -400); |
---|
47 | } |
---|
48 | Add(this.player); |
---|
49 | Camera.Follow(this.player); |
---|
50 | AssignKeys(); |
---|
51 | LevelFromImage("graphics/levels/" + level.TileMapSrc); |
---|
52 | } |
---|
53 | void LevelFromImage(string levelName) |
---|
54 | { |
---|
55 | Dictionary<String, String> convert = new Dictionary<String, String>(); |
---|
56 | convert.Add("000000", "tile0"); |
---|
57 | ColorTileMap tileMap = ColorTileMap.FromLevelAsset(levelName); |
---|
58 | tileMap.SetTileMethod(Color.FromHexCode("000000"), createTile, convert["000000"]); |
---|
59 | tileMap.SetTileMethod(Color.FromHexCode("ff0000"), spawnPlayer); |
---|
60 | tileMap.SetTileMethod(Color.FromHexCode("00ff00"), spawnEnemy); |
---|
61 | |
---|
62 | double w = 40, h = 40; |
---|
63 | tileMap.Execute(w, h); |
---|
64 | map = tileMap; |
---|
65 | //int laskValk = 0; |
---|
66 | //int laskPun = 0; |
---|
67 | //int lask = 0; |
---|
68 | for (int y = 0; y < tileMap.RowCount; y++) |
---|
69 | { |
---|
70 | for (int x = 0; x < tileMap.ColumnCount; x++) |
---|
71 | { |
---|
72 | String hexColor = tileMap.GetTile(y, x).ToString().Substring(2, 6); |
---|
73 | |
---|
74 | //if (hexColor == "FFFFFF") { laskValk++; continue; } |
---|
75 | //if (hexColor == "FF00FF") { laskPun++; continue; } |
---|
76 | //if (hexColor == "FF0000") { lask++; continue; } |
---|
77 | //if (hexColor == "FF0000") { lask++; continue; } |
---|
78 | if (convert.ContainsKey(hexColor)) |
---|
79 | { |
---|
80 | //tätä ei ole optimoitu; nyt kopioi koko kentän vaikka tarvitsisi vain murto-osan kentästä |
---|
81 | 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]); |
---|
82 | 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]); |
---|
83 | } |
---|
84 | } |
---|
85 | } |
---|
86 | |
---|
87 | //luo backgroundit (3) |
---|
88 | GameObject bg = new GameObject(1,1); |
---|
89 | //bg.Image = images["ba"]; |
---|
90 | } |
---|
91 | void createTile(Vector position, double w, double h, string id) |
---|
92 | { |
---|
93 | if (id == "") { return; } |
---|
94 | MikonPhysicsObject tile = new MikonPhysicsObject(this, w, h); |
---|
95 | tile.MakeStatic(); |
---|
96 | tile.AbsolutePosition = position; |
---|
97 | tile.Image = images[id]; |
---|
98 | Add(tile); |
---|
99 | } |
---|
100 | void spawnPlayer(Vector position, double w, double h) |
---|
101 | { |
---|
102 | player.Position = position; |
---|
103 | } |
---|
104 | void spawnEnemy(Vector position, double w, double h) |
---|
105 | { |
---|
106 | new Enemy1(this, w, h, position); |
---|
107 | } |
---|
108 | public override void Begin() |
---|
109 | { |
---|
110 | // TODO: Kirjoita peli tähän |
---|
111 | LoadAllImages(); |
---|
112 | this.player = new Player(this); |
---|
113 | this.player.IsUpdated = true; |
---|
114 | |
---|
115 | LoadLevel(new ProtoLevel(this, "0", true));//tää on huono metodi, TODO: luo level-classit ennen lataamista |
---|
116 | } |
---|
117 | } |
---|