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

Revision 6715, 10.4 KB checked in by empaheik, 8 years ago (diff)

Kakkoskentässä kojootteja.

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                CreateCoyote(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        var enemy = new Creature(width, height, 1);
178        enemy.MoveAnimations = DirectionalAnimations(coyoteLeft, coyoteRight, coyoteUp, coyoteDown);
179        enemy.Image = enemy.MoveAnimations[Direction.Right].CurrentFrame;
180        enemy.Position = position;
181        enemy.Tag = "enemy";
182        enemy.Brain = new FollowerBrain(player) { Speed = 50 };
183        enemy.CollisionIgnoreGroup = 3;
184        Add(enemy, 1);
185        enemies.Add(enemy);
186
187        AddCollisionHandler(enemy, "playerattack", delegate(PhysicsObject e, PhysicsObject a)
188        {
189            enemy.Health.Value--;
190        });
191       
192        enemy.Health.LowerLimit += delegate
193        {
194            enemy.Destroy();
195            RandomItemDrop(enemy.Position);
196
197            EnemyDieSound.Play();
198
199            var smoke = new GameObject(20, 40);
200            smoke.X = enemy.X;
201            smoke.Bottom = enemy.Bottom;
202            smoke.Animation = new Animation(smokeAnimation);
203            smoke.Animation.Start(1);
204            smoke.Animation.Played += () => smoke.Destroy();
205            Add(smoke);
206        };
207    }
208
209    void RandomItemDrop(Vector position)
210    {
211        var chance = RandomGen.NextDouble(0, 100);
212        if (chance < 20)
213        {
214            int itemType = RandomGen.NextInt(1, 3);
215            var item = PhysicsObject.CreateStaticObject(10, 10);
216            item.Position = position;
217            item.Image = itemType == 1 ? GrenadeBoxImage : AmmoBoxImage;
218            item.Tag = itemType == 1 ? "grenadebox" : "ammobox";
219            item.IgnoresCollisionResponse = true;
220            Add(item);
221        }
222    }
223
224    /// <summary>
225    /// Maassa oleva tiili.
226    /// </summary>
227    void CreateBaseTile(Vector position, double width, double height, Image image, string layerName, Dictionary<string, string> properties)
228    {
229        CreateBasicTile(position, width, height, image, -1, properties);
230    }
231
232    /// <summary>
233    /// Pohjan päällä, pelaajan alla näkyvä tiili.
234    /// </summary>
235    void CreateDecoration(Vector position, double width, double height, Image image, string layerName, Dictionary<string, string> properties)
236    {
237        CreateBasicTile(position, width, height, image, 0, properties);
238    }
239
240    /// <summary>
241    /// Pelaajan päällä näkyvä tiili.
242    /// </summary>
243    void CreateForegroundTile(Vector position, double width, double height, Image image, string layerName, Dictionary<string, string> properties)
244    {
245        CreateBasicTile(position, width, height, image, 2, properties);
246    }
247
248    void CreateCover(Vector position, double width, double height, Image image, string layername, Dictionary<string, string> properties)
249    {
250        var tile = new GameObject(width, height);
251        tile.Tag = "cover";
252        tile.Image = image;
253        tile.Position = position;
254        Add(tile, 3);
255    }
256
257    /// <summary>
258    /// Luo tavallisen tiilen.
259    /// </summary>
260    void CreateBasicTile(Vector position, double width, double height, Image image, int layer, Dictionary<string, string> properties)
261    {
262        //var tile = new GameObject(width, height);
263        var tile = properties.ContainsKey("collide") ? PhysicsObject.CreateStaticObject(width, height) : new GameObject(width, height); //new PhysicsObject(width, height)  PhysicsObject.CreateStaticObject(width, height)
264        tile.Image = image;
265        tile.Position = position;
266        Add(tile, layer);
267    }
268
269    /// <summary>
270    /// Luo uloskäynnin, joka vie toiseen kenttään.
271    /// </summary>
272    void CreateExit(Vector position, double width, double height, Angle angle, Shape shape, string name, Dictionary<string, string> properties)
273    {
274        var target = properties["goto"] == "disabled" ? null : properties["goto"].Split('@'); // Jos peli kaatuu tälle riville niin joltain exitiltä puuttuu goto-property.
275        var exit = new Exit(width, height);
276        exit.Position = position;
277        if (target != null)
278        {
279            exit.TargetLevel = target[0];
280            exit.TargetExitName = target[1];
281        }
282        exit.Name = name;
283        Add(exit);
284        exits.Add(exit);
285
286        bool create = properties.ContainsKey("block");
287        if(create && !woodDestroyed)
288        {
289            PhysicsObject fence = PhysicsObject.CreateStaticObject(TILE_SIZE, TILE_SIZE);
290            fence.Image = FenceImage;
291            fence.Position = exit.Position - new Vector(0, TILE_SIZE);
292            Add(fence, 3);
293
294            AddCollisionHandler(fence, "playerattack", delegate(PhysicsObject destroyable, PhysicsObject a)
295            {
296               destroyable.Destroy();
297               woodDestroyed = true;
298            });
299       
300        }
301    }
302}
Note: See TracBrowser for help on using the repository browser.