source: 2015/26/ohjaajat/HillbillyRun/HillbillyRun/HillbillyRun/HillbillyRun/HillbillyRun.cs @ 6202

Revision 6202, 13.8 KB checked in by empaheik, 5 years ago (diff)
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using Jypeli;
5using Jypeli.Assets;
6using Jypeli.Controls;
7using Jypeli.Effects;
8using Jypeli.Widgets;
9
10//Luokka maajussien erikoisuuksille.
11class HillBilly : PlatformCharacter
12{
13    public HillBilly(double leveys, double korkeus)
14        : base(leveys, korkeus)
15    {
16
17    }
18}
19
20/// <summary>
21/// An MMO where you go to war with mages in the lava kingdom.
22/// </summary>
23public class HillbillyRun : PhysicsGame
24{
25    private List<HillBilly> players = new List<HillBilly>();
26    //private List<double> playerPositionsX = new List<double>();
27    //private List<double> playerPositionsY = new List<double>();
28
29    private const double TILE_SIZE = 70;
30
31    private double cameraTargetX; // Sijainti jossa kameran pitäisi olla.
32    private Vector cameraTarget;
33
34    private double cameraOffset = 400; // TODO: Tämän voisi ehkä laskea jostain (esim. ikkunan leveydestä kolmasosa tai jotain).
35    private double cameraSpeed = 2.0; // Kameran liikkumisnopeus.
36
37    # region Images
38    private Image[] groundImages = LoadImages("ground");    //Näitä ei tosin kaikkia kenties jaksa tehdä erilaisiksi levelistä riippuen
39    private Image[] groundTopImages = LoadImages("ground_top");
40    private Image[] supportStructureImages = LoadImages("");
41    private Image[] foreGroundDecorations = LoadImages("viljaa");
42
43    private Image[] houseImages = LoadImages("house", "houseburned");
44
45    private Image cartImage = LoadImage("cart");
46   
47    private Image cartWheelImage = LoadImage("cartwheel");
48    #endregion
49
50    private int levelNumber = 0;
51
52    public override void Begin()
53    {
54        //IntroSequence();
55        //MainMenu();
56        StartGame();
57    }
58
59    void IntroSequence()
60    {
61        Level.Background.Color = Color.Black;
62
63        Label storyLabel = new Label("Once upon a time...");
64        storyLabel.TextColor = Color.Gray;
65        Add(storyLabel);
66
67        Timer textTimer = new Timer();
68        textTimer.Interval = 0.5;
69        textTimer.Timeout += delegate
70        {
71            // TODO vaalenna tekstiä näkyville läpinäkyvyyttä tms. säätämällä
72           
73        };
74
75        // TODO siirrä aliohjelmaan, jolle voi antaa parametrina stringin, joka näytetään siististi feidaamalla esille
76
77
78
79        // TODO replace this SingleShot with something else. It must be the last thing after the intro sequence
80       
81        // Clean up after the intro:
82        Timer.SingleShot(5, delegate
83        {
84            storyLabel.Destroy();
85
86            ClearAll();
87            MainMenu();
88        });
89    }
90
91    void MainMenu()
92    {
93        Level.Background.Color = Color.MediumBlue;
94
95        MultiSelectWindow mainmenu = new MultiSelectWindow("Main menu",
96"Start game", "Credits", "Exit");
97        mainmenu.AddItemHandler(0, StartGame);
98        mainmenu.AddItemHandler(1, ShowCredits);
99        mainmenu.AddItemHandler(2, Exit);
100        mainmenu.DefaultCancel = 2;
101        Add(mainmenu);
102    }
103
104    void ShowCredits()
105    {
106        MessageWindow win = new MessageWindow("This game was made in one week by\nSimo Rinne,\nEmma Heikura,\n and Jouni Potila\n using University of Jyväskylä's\n game programming library Jypeli");
107        win.Closed += delegate
108        {
109            ClearAll();
110            MainMenu();
111        };
112        Add(win);
113    }
114
115    void StartGame()
116    {
117
118        CreateLevel();
119        ScreenSettings();
120
121        // Testailen tässä kärryn luomista.
122        //double size = 0.7;
123        //PhysicsObject cart = new PhysicsObject(400 * size, 80 * size);
124        //cart.X = -1600;
125        //cart.Y = 200;
126        //cart.Image = cartImage;
127        //cart.CollisionIgnoreGroup = 2;
128        //Add(cart);
129        //PhysicsObject cartWheel = new PhysicsObject(160 * size, 160 * size, Shape.Circle);
130        //cartWheel.Image = cartWheelImage;
131        //cartWheel.Position = cart.Position + new Vector(-110, -30) * size;
132        //cartWheel.CollisionIgnoreGroup = 2;
133        //Add(cartWheel);
134        //AxleJoint joint = new AxleJoint(cart, cartWheel);
135        //Add(joint);
136    }
137
138    void ScreenSettings()
139    {
140        Window.Width = 1800;
141        Window.Height = 900;
142
143        Camera.X = cameraTargetX = players[0].X;
144        Camera.Y = cameraTargetY = players[0].Y;
145        //Camera.ZoomToLevel();
146
147        Timer cameraTimer = new Timer();
148        cameraTimer.Interval = 1 / 30.0;
149        cameraTimer.Timeout += UpdateCamera;
150        cameraTimer.Start();
151    }
152
153    #region LevelCreation
154    void CreateLevel()
155    {
156        Gravity = new Vector(0, -1000);
157
158        ColorTileMap level = ColorTileMap.FromLevelAsset("level" + levelNumber);
159        level.SetTileMethod(Color.Black, CreateGroundTop);
160        level.SetTileMethod(Color.Brown, CreateGround);
161
162        level.SetTileMethod(Color.Cyan, CreateHouse);
163        level.SetTileMethod(Color.PaintDotNetBlue, CreateBurnedHouse);
164
165        level.SetTileMethod(Color.Gold, CreatePlayer);
166        level.SetTileMethod(Color.Rose, CreateDummy, Color.Rose);   //TODO: CreateFlame
167        level.SetTileMethod(Color.Harlequin, CreateCart);
168        level.SetTileMethod(Color.White, CreateDummy, Color.White); //TODO: CreateSmoke (optional)
169        //level.SetTileMethod(Color.PaintDotNetBlue, CreateDummy, Color.PaintDotNetBlue);
170        level.SetTileMethod(Color.DarkGray, CreateDummy, Color.DarkGray);   //TODO: CreateTombStone (optional)
171        level.SetTileMethod(Color.Gray, CreateCrawly); 
172        level.SetTileMethod(Color.Red, CreateDummy, Color.Red);    //TODO: CreateWitch
173        level.Execute(TILE_SIZE, TILE_SIZE);
174        level.Optimize(Color.Black, Color.Brown);
175
176        //TileMap level = TileMap.FromLevelAsset("level" + levelNumber);
177        //level.SetTileMethod('P', CreatePlayer);
178        //level.SetTileMethod('#', CreateGroundTop);
179        //level.SetTileMethod('%', CreateGround);
180        //level.Optimize('#', '%');
181        //level.Execute(TILE_SIZE, TILE_SIZE);
182        //Level.CreateBorders(true);
183
184        SetControls();
185    }
186
187    void CreateCrawly(Vector position, double width, double height)
188    {
189        PlatformCharacter crawly = new PlatformCharacter(width * 2, height);
190        crawly.Position = position;
191        crawly.Color = Color.Gray;
192        crawly.CollisionIgnoreGroup = 3;
193        Add(crawly);
194
195        PlatformWandererBrain brain = new PlatformWandererBrain();
196        brain.Speed = 20;
197        brain.FallsOffPlatforms = true;
198
199        crawly.Brain = brain;
200    }
201
202    void CreateHouse(Vector position, double width, double height)
203    {
204        // Näkyvä talo
205        GameObject house = new GameObject(width * 10, height * 8);
206        house.Image = houseImages[0];
207        house.Position = position - new Vector(0, height * 2.5);
208        Add(house, -2);
209
210        PhysicsObject platform = PhysicsObject.CreateStaticObject(house.Width - (TILE_SIZE * 4), 5);
211        platform.IsVisible = false;
212        platform.Position = new Vector(house.X + TILE_SIZE, house.Top);
213        Add(platform);
214    }
215
216    void CreateBurnedHouse(Vector position, double width, double height)
217    {
218        GameObject house = new GameObject(width * 10, height * 8);
219        house.Image = houseImages[1];
220        house.Position = position - new Vector(0, height * 2.5);
221        Add(house, -2);
222
223        PhysicsObject platform = PhysicsObject.CreateStaticObject(house.Width* 0.25, 5);
224        platform.IsVisible = false;
225        platform.Position = new Vector(house.X + platform.Width * 0.5, house.Top);
226        Add(platform);
227    }
228
229    private void CreateCart(Vector position, double width, double height)
230    {
231        double size = 0.7;
232        PhysicsObject cart = new PhysicsObject(400 * size, 80 * size);
233        cart.Position = position;
234        cart.Image = cartImage;
235        cart.CollisionIgnoreGroup = 2;
236        Add(cart);
237        PhysicsObject cartWheel = new PhysicsObject(160 * size, 160 * size, Shape.Circle);
238        cartWheel.Image = cartWheelImage;
239        cartWheel.Position = cart.Position + new Vector(-110, -30) * size;
240        cartWheel.CollisionIgnoreGroup = 2;
241        Add(cartWheel);
242        AxleJoint joint = new AxleJoint(cart, cartWheel);
243        Add(joint);
244    }
245
246
247    private void CreateDummy(Vector position, double width, double height, Color color)
248    {
249        PhysicsObject dummy = PhysicsObject.CreateStaticObject(width, height);
250        dummy.Position = position;
251        dummy.Color = color;
252        //dummy.TextureWrapSize = new Vector(width / TILE_SIZE, height / TILE_SIZE);
253        Add(dummy);
254    }
255
256    void CreatePlayer(Vector position, double width, double height)
257    {
258        HillBilly player = new HillBilly(width, height * 2);
259        player.Shape = Shape.Rectangle;
260        player.Position = position + new Vector(0, height * 0.5);
261        player.Color = Color.White;
262        players.Add(player);
263        Add(player);
264    }
265
266    void CreateGroundTop(Vector position, double width, double height)
267    {
268        // Puolet pienempi näkymätön palikka alla johon törmää.
269        PhysicsObject ground = PhysicsObject.CreateStaticObject(width, height / 2.0);
270        ground.IsVisible = false;
271        ground.Position = position - new Vector(0.0, TILE_SIZE / 4.0);
272        Add(ground);
273
274        // Maanpinnan näkyvä osa.
275        GameObject visibleGround = new GameObject(width, height);
276        visibleGround.Image = groundImages[levelNumber];
277        visibleGround.Position = position;
278        visibleGround.TextureWrapSize = new Vector(width / TILE_SIZE, height / TILE_SIZE);
279        Add(visibleGround, -1);
280
281        int probability = RandomGen.NextInt(100);
282        bool wheat = probability < 20;
283
284        if(wheat)
285        {
286            GameObject wheatBlock = new GameObject(width * 1.5, height * 3);
287            wheatBlock.Image = foreGroundDecorations[0];
288            wheatBlock.Position = visibleGround.Position;
289            Add(wheatBlock, 2);
290        }
291    }
292
293    void CreateGround(Vector position, double width, double height) //TODO: Fiksataan se, että pelihahmot tietysti luiskahtavat tästä läpi, mikäli pääsevät sen sivustaan. Muitakin korjaustapoja tietty on, kuin tässä aliohjelmassa operoiminen.
294    {
295        GameObject ground = new GameObject(width, height);
296        ground.Image = groundTopImages[levelNumber];
297        ground.Position = position;
298        ground.TextureWrapSize = new Vector(width / TILE_SIZE, height / TILE_SIZE);
299        Add(ground);
300    }
301    #endregion
302
303    void SetControls()
304    {
305        Keyboard.Listen(Key.A, ButtonState.Down, delegate { players[0].Walk(-300); }, "Player 1 moves left");
306        Keyboard.Listen(Key.D, ButtonState.Down, delegate { players[0].Walk(300); }, "Player 1 moves right");
307        Keyboard.Listen(Key.W, ButtonState.Down, delegate { players[0].Jump(1000); }, "Player 1 jumps");
308
309        Keyboard.Listen(Key.Left, ButtonState.Down, delegate { players[1].Walk(-300); }, "Player 2 moves left");
310        Keyboard.Listen(Key.Right, ButtonState.Down, delegate { players[1].Walk(300); }, "Player 2 moves right");
311        Keyboard.Listen(Key.Up, ButtonState.Down, delegate { players[1].Jump(1000); }, "Player 2 jumps");
312
313        Keyboard.Listen(Key.F1, ButtonState.Pressed, ShowControlHelp, "Show help");
314        Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Exit game");
315    }
316
317    void UpdateCamera()
318    {
319
320        double minY = players.Min(p => p.Y);
321        double maxY = players.Max(p => p.Y);
322        double minX = players.Min(p => p.X) + cameraOffset;
323
324        Vector minPosition = new Vector(Math.Max(minX, cameraTargetX), minY);
325        Vector maxPosition = new Vector(minX, maxY);
326
327        cameraTarget = (minPosition + maxPosition) * 0.5;
328        cameraTarget.X = Math.Max(cameraTargetX, minX);     //Lellllll.
329
330        cameraTargetX = Math.Max(cameraTargetX, minX);
331
332
333        double windowMax = Camera.ScreenToWorld(new Vector(Window.Width / 2.0, 0)).X;
334        double windowMin = Camera.ScreenToWorld(new Vector(-Window.Width / 2.0, 0)).X;
335        foreach (var player in players)
336        {
337            player.Left = Math.Max(player.Left, windowMin);
338            player.Right = Math.Min(player.Right, windowMax);
339        }
340
341        //double windowMax = Camera.ScreenToWorld(new Vector(Window.Width / 2.0, 0)).X;
342        //double windowMin = Camera.ScreenToWorld(new Vector(-Window.Width / 2.0, 0)).X;
343        //foreach (var player in players)
344        //{
345        //    player.Left = Math.Max(player.Left, windowMin);
346        //    player.Right = Math.Min(player.Right, windowMax);
347        //}
348    }
349
350    protected override void Update(Time time)
351    {
352        Camera.Position += (cameraTarget - Camera.Position) * time.SinceLastUpdate.TotalSeconds * cameraSpeed;
353        //Camera.X += (cameraTargetX - Camera.X) * time.SinceLastUpdate.TotalSeconds * cameraSpeed;
354
355
356        base.Update(time);
357    }
358
359    /// <summary>
360    /// Yritän leikkiä kameralla. Vielä varmaan hetken pidempään.
361    /// </summary>
362    /// <param name="gameTime"></param>
363    /*
364    protected override void Update(Microsoft.Xna.Framework.GameTime gameTime)
365    {
366        foreach(HillBilly player in players)
367        {
368            playerPositionsX.Add(player.Position.X);
369            playerPositionsY.Add(player.Position.Y);
370        }
371
372        double maxX = playerPositionsX.Max();
373        double maxY = playerPositionsY.Max();
374        double minX = playerPositionsX.Min();
375        double minY = playerPositionsY.Min();
376
377        Vector minPosition = new Vector(minX, minY);
378        Vector maxPosition = new Vector(maxX, maxY);
379
380        Camera.Position = (minPosition + maxPosition) * 0.5;
381
382        playerPositionsX.Clear();
383        playerPositionsY.Clear();
384
385        base.Update(gameTime);
386    }
387     */
388}
Note: See TracBrowser for help on using the repository browser.