1 | using System; |
---|
2 | using System.Collections.Generic; |
---|
3 | using System.Linq; |
---|
4 | using System.Text; |
---|
5 | using Jypeli; |
---|
6 | using Jypeli.Assets; |
---|
7 | using Jypeli.Widgets; |
---|
8 | |
---|
9 | /* |
---|
10 | *Klassisia muistutuksia |
---|
11 | * 3 |
---|
12 | * 2 Pelaajan päällä näkyvät elementit |
---|
13 | * 1 Pelaaja, kojootit |
---|
14 | * 0 Lisäkoriste |
---|
15 | * -1 Perustiili |
---|
16 | * -2 |
---|
17 | */ |
---|
18 | |
---|
19 | |
---|
20 | public partial class TheLegendOfGabriel : PhysicsGame |
---|
21 | { |
---|
22 | /// <summary> |
---|
23 | /// Luo kentän .tmx tiedostosta. |
---|
24 | /// </summary> |
---|
25 | void CreateLevel(string levelName) |
---|
26 | { |
---|
27 | bossSpawnPositions.Clear(); |
---|
28 | Level.CreateVerticalBorders(1.0, false, Color.Yellow); |
---|
29 | PhysicsObject leftBorder = Level.CreateLeftBorder(0.0, false); |
---|
30 | PhysicsObject rightBorder = Level.CreateRightBorder(0.0, false); |
---|
31 | leftBorder.X += Level.Width * 0.25; |
---|
32 | rightBorder.X -= Level.Width * 0.25; |
---|
33 | |
---|
34 | var level = TiledTileMap.FromLevelAsset(levelName); |
---|
35 | level.SetTileMethod("base", CreateBaseTile); |
---|
36 | level.SetTileMethod("foreground", CreateForegroundTile); |
---|
37 | level.SetObjectMethod("exit", CreateExit); |
---|
38 | level.SetTileMethod("decoration", CreateDecoration); |
---|
39 | level.SetTileMethod("cover", CreateCover); |
---|
40 | level.SetObjectMethod("story", CreateEncounter); |
---|
41 | //level.SetObjectMethod("player", CreatePlayer); |
---|
42 | level.SetObjectMethod("blockade", CreateBlock); |
---|
43 | level.SetObjectMethod("coyote", CreateCoyote); |
---|
44 | level.SetObjectMethod("boss", CreateBoss); |
---|
45 | level.SetObjectMethod("bossspawn", CreateBossSpawn); |
---|
46 | level.Execute(); |
---|
47 | |
---|
48 | Level.Background.Color = Color.Black; |
---|
49 | } |
---|
50 | |
---|
51 | void CreateEncounter(Vector position, double width, double height, Angle angle, Shape shape, string name, Dictionary<string, string> properties) |
---|
52 | { |
---|
53 | PhysicsObject invisible = new PhysicsObject(width, height) {IsVisible = false, Tag = "storyTime"}; |
---|
54 | invisible.MakeStatic(); |
---|
55 | Add(invisible); |
---|
56 | } |
---|
57 | |
---|
58 | void CreateBossSpawn(Vector position, double width, double height, Angle angle, Shape shape, string name, Dictionary<string, string> properties) |
---|
59 | { |
---|
60 | bossSpawnPositions.Add(position); |
---|
61 | } |
---|
62 | |
---|
63 | void CreateBoss(Vector position, double width, double height, Angle angle, Shape shape, string name, Dictionary<string, string> properties) |
---|
64 | { |
---|
65 | BossHealth = new IntMeter(10, 0, 10); |
---|
66 | |
---|
67 | var bar = new ProgressBar(Level.Width * 0.7, TILE_SIZE * 0.5); |
---|
68 | bar.Y = Screen.Top - TILE_SIZE * 0.5; |
---|
69 | bar.BindTo(BossHealth); |
---|
70 | Add(bar); |
---|
71 | |
---|
72 | // Juna ilmestyy välillä satunnaiseen spawniin. |
---|
73 | var bossTimer = new Timer(); |
---|
74 | bossTimer.Interval = 8; |
---|
75 | bossTimer.Timeout += delegate |
---|
76 | { |
---|
77 | // Luodaan päävaunu. |
---|
78 | var pos = RandomGen.SelectOne<Vector>(bossSpawnPositions); |
---|
79 | var train = CreateBossTrainPart(pos.X, pos.Y, trainAnimation); |
---|
80 | double newPositionX = pos.X + train.Width; |
---|
81 | |
---|
82 | // Luodaan muut vaunut. |
---|
83 | int carriages = (int)(BossHealth.Value / (double)BossHealth.MaxValue * 6); |
---|
84 | for (int i = 0; i < carriages; i++) |
---|
85 | { |
---|
86 | var part = CreateBossTrainPart(newPositionX, pos.Y, carriageAnimation); |
---|
87 | newPositionX += part.Width; |
---|
88 | } |
---|
89 | }; |
---|
90 | bossTimer.Start(); |
---|
91 | |
---|
92 | BossHealth.LowerLimit += delegate |
---|
93 | { |
---|
94 | EnemyDieSound.Play(); |
---|
95 | bossTimer.Stop(); |
---|
96 | Timer.SingleShot(4.0, Victory); |
---|
97 | |
---|
98 | foreach (var part in GetObjectsWithTag("boss")) |
---|
99 | { |
---|
100 | part.Destroy(); |
---|
101 | var smoke = new GameObject(80, 160); |
---|
102 | smoke.X = part.X; |
---|
103 | smoke.Bottom = part.Bottom; |
---|
104 | smoke.Animation = new Animation(smokeAnimation); |
---|
105 | smoke.Animation.Start(1); |
---|
106 | smoke.Animation.Played += () => smoke.Destroy(); |
---|
107 | Add(smoke); |
---|
108 | } |
---|
109 | }; |
---|
110 | } |
---|
111 | |
---|
112 | PhysicsObject CreateBossTrainPart(double left, double y, Animation anim) |
---|
113 | { |
---|
114 | var part = PhysicsObject.CreateStaticObject(70, 40); |
---|
115 | part.Left = left; |
---|
116 | part.Y = y; |
---|
117 | part.Animation = anim; |
---|
118 | part.Animation.Start(); |
---|
119 | part.Velocity = new Vector(-80, 0); |
---|
120 | part.Tag = "boss"; |
---|
121 | part.CollisionIgnoreGroup = 3; |
---|
122 | Add(part); |
---|
123 | |
---|
124 | // Pala on näkyvillä vain kentän sisäpuolella. |
---|
125 | var appearTimer = new Timer(); |
---|
126 | appearTimer.Interval = 0.05; |
---|
127 | appearTimer.Timeout += delegate |
---|
128 | { |
---|
129 | part.IsVisible = part.Left < Level.Right; |
---|
130 | |
---|
131 | if (part.Right < Level.Left) |
---|
132 | { |
---|
133 | part.Destroy(); |
---|
134 | appearTimer.Stop(); |
---|
135 | } |
---|
136 | }; |
---|
137 | appearTimer.Start(); |
---|
138 | |
---|
139 | // Palat luo vihollisia jatkuvasti. |
---|
140 | var enemySpawnTimer = new Timer(); |
---|
141 | enemySpawnTimer.Interval = 2.0; |
---|
142 | enemySpawnTimer.Timeout += delegate |
---|
143 | { |
---|
144 | if (part.Right < Level.Right) |
---|
145 | { |
---|
146 | CreateCoyote(part.Position, TILE_SIZE, TILE_SIZE, Angle.Zero, Shape.Rectangle, "", null); |
---|
147 | } |
---|
148 | }; |
---|
149 | enemySpawnTimer.Start(); |
---|
150 | |
---|
151 | part.Destroyed += delegate |
---|
152 | { |
---|
153 | appearTimer.Stop(); |
---|
154 | enemySpawnTimer.Stop(); |
---|
155 | }; |
---|
156 | |
---|
157 | return part; |
---|
158 | } |
---|
159 | |
---|
160 | /// <summary> |
---|
161 | /// Näkymättömiä esteitä. |
---|
162 | /// </summary> |
---|
163 | void CreateBlock(Vector position, double width, double height, Angle angle, Shape shape, string name, Dictionary<string, string> properties) |
---|
164 | { |
---|
165 | PhysicsObject block = PhysicsObject.CreateStaticObject(width, height); |
---|
166 | block.Position = position; |
---|
167 | block.IsVisible = false; |
---|
168 | Add(block); |
---|
169 | |
---|
170 | } |
---|
171 | |
---|
172 | void CreateCoyote(Vector position, double width, double height, Angle angle, Shape shape, string name, Dictionary<string, string> properties) |
---|
173 | { |
---|
174 | var enemy = new Creature(width, height, 1); |
---|
175 | enemy.MoveAnimations = DirectionalAnimations(coyoteLeft, coyoteRight, coyoteUp, coyoteDown); |
---|
176 | enemy.Image = enemy.MoveAnimations[Direction.Right].CurrentFrame; |
---|
177 | enemy.Position = position; |
---|
178 | enemy.Tag = "enemy"; |
---|
179 | enemy.Brain = new FollowerBrain(player) { Speed = 50 }; |
---|
180 | enemy.CollisionIgnoreGroup = 3; |
---|
181 | Add(enemy, 1); |
---|
182 | enemies.Add(enemy); |
---|
183 | |
---|
184 | AddCollisionHandler(enemy, "playerattack", delegate(PhysicsObject e, PhysicsObject a) |
---|
185 | { |
---|
186 | enemy.Health.Value--; |
---|
187 | }); |
---|
188 | |
---|
189 | enemy.Health.LowerLimit += delegate |
---|
190 | { |
---|
191 | enemy.Destroy(); |
---|
192 | RandomItemDrop(enemy.Position); |
---|
193 | |
---|
194 | EnemyDieSound.Play(); |
---|
195 | |
---|
196 | var smoke = new GameObject(20, 40); |
---|
197 | smoke.X = enemy.X; |
---|
198 | smoke.Bottom = enemy.Bottom; |
---|
199 | smoke.Animation = new Animation(smokeAnimation); |
---|
200 | smoke.Animation.Start(1); |
---|
201 | smoke.Animation.Played += () => smoke.Destroy(); |
---|
202 | Add(smoke); |
---|
203 | }; |
---|
204 | } |
---|
205 | |
---|
206 | void RandomItemDrop(Vector position) |
---|
207 | { |
---|
208 | var chance = RandomGen.NextDouble(0, 100); |
---|
209 | if (chance < 20) |
---|
210 | { |
---|
211 | int itemType = RandomGen.NextInt(1, 3); |
---|
212 | var item = PhysicsObject.CreateStaticObject(10, 10); |
---|
213 | item.Position = position; |
---|
214 | item.Image = itemType == 1 ? GrenadeBoxImage : AmmoBoxImage; |
---|
215 | item.Tag = itemType == 1 ? "grenadebox" : "ammobox"; |
---|
216 | item.IgnoresCollisionResponse = true; |
---|
217 | Add(item); |
---|
218 | } |
---|
219 | } |
---|
220 | |
---|
221 | /// <summary> |
---|
222 | /// Maassa oleva tiili. |
---|
223 | /// </summary> |
---|
224 | void CreateBaseTile(Vector position, double width, double height, Image image, string layerName, Dictionary<string, string> properties) |
---|
225 | { |
---|
226 | CreateBasicTile(position, width, height, image, -1, properties); |
---|
227 | } |
---|
228 | |
---|
229 | /// <summary> |
---|
230 | /// Pohjan päällä, pelaajan alla näkyvä tiili. |
---|
231 | /// </summary> |
---|
232 | void CreateDecoration(Vector position, double width, double height, Image image, string layerName, Dictionary<string, string> properties) |
---|
233 | { |
---|
234 | CreateBasicTile(position, width, height, image, 0, properties); |
---|
235 | } |
---|
236 | |
---|
237 | /// <summary> |
---|
238 | /// Pelaajan päällä näkyvä tiili. |
---|
239 | /// </summary> |
---|
240 | void CreateForegroundTile(Vector position, double width, double height, Image image, string layerName, Dictionary<string, string> properties) |
---|
241 | { |
---|
242 | CreateBasicTile(position, width, height, image, 2, properties); |
---|
243 | } |
---|
244 | |
---|
245 | void CreateCover(Vector position, double width, double height, Image image, string layername, Dictionary<string, string> properties) |
---|
246 | { |
---|
247 | var tile = new GameObject(width, height); |
---|
248 | tile.Tag = "cover"; |
---|
249 | tile.Image = image; |
---|
250 | tile.Position = position; |
---|
251 | Add(tile, 3); |
---|
252 | } |
---|
253 | |
---|
254 | /// <summary> |
---|
255 | /// Luo tavallisen tiilen. |
---|
256 | /// </summary> |
---|
257 | void CreateBasicTile(Vector position, double width, double height, Image image, int layer, Dictionary<string, string> properties) |
---|
258 | { |
---|
259 | var tile = properties.ContainsKey("collide") ? PhysicsObject.CreateStaticObject(width, height) : new GameObject(width, height); //new PhysicsObject(width, height) PhysicsObject.CreateStaticObject(width, height) |
---|
260 | tile.Image = image; |
---|
261 | tile.Position = position; |
---|
262 | Add(tile, layer); |
---|
263 | } |
---|
264 | |
---|
265 | /// <summary> |
---|
266 | /// Luo uloskäynnin, joka vie toiseen kenttään. |
---|
267 | /// </summary> |
---|
268 | void CreateExit(Vector position, double width, double height, Angle angle, Shape shape, string name, Dictionary<string, string> properties) |
---|
269 | { |
---|
270 | var target = properties["goto"] == "disabled" ? null : properties["goto"].Split('@'); // Jos peli kaatuu tälle riville niin joltain exitiltä puuttuu goto-property. |
---|
271 | var exit = new Exit(width, height); |
---|
272 | exit.Position = position; |
---|
273 | if (target != null) |
---|
274 | { |
---|
275 | exit.TargetLevel = target[0]; |
---|
276 | exit.TargetExitName = target[1]; |
---|
277 | } |
---|
278 | exit.Name = name; |
---|
279 | Add(exit); |
---|
280 | exits.Add(exit); |
---|
281 | } |
---|
282 | } |
---|