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