source: 2015/27/ohjaajat/TheLegendOfGabriel/TheLegendOfGabriel/TheLegendOfGabriel/LevelCreation.cs @ 6716

Revision 6716, 10.9 KB checked in by sieerinn, 8 years ago (diff)

Norjalaiset moukarinheittäjät tehty, kranaatti toimii taas junaa vastaan.

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