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

Revision 6203, 13.7 KB checked in by empaheik, 6 years ago (diff)

Poistettu ylimääräinen rivi. Nyt kameran pitäisi olla a-ok.

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.ZoomToLevel();
145
146        Timer cameraTimer = new Timer();
147        cameraTimer.Interval = 1 / 30.0;
148        cameraTimer.Timeout += UpdateCamera;
149        cameraTimer.Start();
150    }
151
152    #region LevelCreation
153    void CreateLevel()
154    {
155        Gravity = new Vector(0, -1000);
156
157        ColorTileMap level = ColorTileMap.FromLevelAsset("level" + levelNumber);
158        level.SetTileMethod(Color.Black, CreateGroundTop);
159        level.SetTileMethod(Color.Brown, CreateGround);
160
161        level.SetTileMethod(Color.Cyan, CreateHouse);
162        level.SetTileMethod(Color.PaintDotNetBlue, CreateBurnedHouse);
163
164        level.SetTileMethod(Color.Gold, CreatePlayer);
165        level.SetTileMethod(Color.Rose, CreateDummy, Color.Rose);   //TODO: CreateFlame
166        level.SetTileMethod(Color.Harlequin, CreateCart);
167        level.SetTileMethod(Color.White, CreateDummy, Color.White); //TODO: CreateSmoke (optional)
168        //level.SetTileMethod(Color.PaintDotNetBlue, CreateDummy, Color.PaintDotNetBlue);
169        level.SetTileMethod(Color.DarkGray, CreateDummy, Color.DarkGray);   //TODO: CreateTombStone (optional)
170        level.SetTileMethod(Color.Gray, CreateCrawly); 
171        level.SetTileMethod(Color.Red, CreateDummy, Color.Red);    //TODO: CreateWitch
172        level.Execute(TILE_SIZE, TILE_SIZE);
173        level.Optimize(Color.Black, Color.Brown);
174
175        //TileMap level = TileMap.FromLevelAsset("level" + levelNumber);
176        //level.SetTileMethod('P', CreatePlayer);
177        //level.SetTileMethod('#', CreateGroundTop);
178        //level.SetTileMethod('%', CreateGround);
179        //level.Optimize('#', '%');
180        //level.Execute(TILE_SIZE, TILE_SIZE);
181        //Level.CreateBorders(true);
182
183        SetControls();
184    }
185
186    void CreateCrawly(Vector position, double width, double height)
187    {
188        PlatformCharacter crawly = new PlatformCharacter(width * 2, height);
189        crawly.Position = position;
190        crawly.Color = Color.Gray;
191        crawly.CollisionIgnoreGroup = 3;
192        Add(crawly);
193
194        PlatformWandererBrain brain = new PlatformWandererBrain();
195        brain.Speed = 20;
196        brain.FallsOffPlatforms = true;
197
198        crawly.Brain = brain;
199    }
200
201    void CreateHouse(Vector position, double width, double height)
202    {
203        // Näkyvä talo
204        GameObject house = new GameObject(width * 10, height * 8);
205        house.Image = houseImages[0];
206        house.Position = position - new Vector(0, height * 2.5);
207        Add(house, -2);
208
209        PhysicsObject platform = PhysicsObject.CreateStaticObject(house.Width - (TILE_SIZE * 4), 5);
210        platform.IsVisible = false;
211        platform.Position = new Vector(house.X + TILE_SIZE, house.Top);
212        Add(platform);
213    }
214
215    void CreateBurnedHouse(Vector position, double width, double height)
216    {
217        GameObject house = new GameObject(width * 10, height * 8);
218        house.Image = houseImages[1];
219        house.Position = position - new Vector(0, height * 2.5);
220        Add(house, -2);
221
222        PhysicsObject platform = PhysicsObject.CreateStaticObject(house.Width* 0.25, 5);
223        platform.IsVisible = false;
224        platform.Position = new Vector(house.X + platform.Width * 0.5, house.Top);
225        Add(platform);
226    }
227
228    private void CreateCart(Vector position, double width, double height)
229    {
230        double size = 0.7;
231        PhysicsObject cart = new PhysicsObject(400 * size, 80 * size);
232        cart.Position = position;
233        cart.Image = cartImage;
234        cart.CollisionIgnoreGroup = 2;
235        Add(cart);
236        PhysicsObject cartWheel = new PhysicsObject(160 * size, 160 * size, Shape.Circle);
237        cartWheel.Image = cartWheelImage;
238        cartWheel.Position = cart.Position + new Vector(-110, -30) * size;
239        cartWheel.CollisionIgnoreGroup = 2;
240        Add(cartWheel);
241        AxleJoint joint = new AxleJoint(cart, cartWheel);
242        Add(joint);
243    }
244
245
246    private void CreateDummy(Vector position, double width, double height, Color color)
247    {
248        PhysicsObject dummy = PhysicsObject.CreateStaticObject(width, height);
249        dummy.Position = position;
250        dummy.Color = color;
251        //dummy.TextureWrapSize = new Vector(width / TILE_SIZE, height / TILE_SIZE);
252        Add(dummy);
253    }
254
255    void CreatePlayer(Vector position, double width, double height)
256    {
257        HillBilly player = new HillBilly(width, height * 2);
258        player.Shape = Shape.Rectangle;
259        player.Position = position + new Vector(0, height * 0.5);
260        player.Color = Color.White;
261        players.Add(player);
262        Add(player);
263    }
264
265    void CreateGroundTop(Vector position, double width, double height)
266    {
267        // Puolet pienempi näkymätön palikka alla johon törmää.
268        PhysicsObject ground = PhysicsObject.CreateStaticObject(width, height / 2.0);
269        ground.IsVisible = false;
270        ground.Position = position - new Vector(0.0, TILE_SIZE / 4.0);
271        Add(ground);
272
273        // Maanpinnan näkyvä osa.
274        GameObject visibleGround = new GameObject(width, height);
275        visibleGround.Image = groundImages[levelNumber];
276        visibleGround.Position = position;
277        visibleGround.TextureWrapSize = new Vector(width / TILE_SIZE, height / TILE_SIZE);
278        Add(visibleGround, -1);
279
280        int probability = RandomGen.NextInt(100);
281        bool wheat = probability < 20;
282
283        if(wheat)
284        {
285            GameObject wheatBlock = new GameObject(width * 1.5, height * 3);
286            wheatBlock.Image = foreGroundDecorations[0];
287            wheatBlock.Position = visibleGround.Position;
288            Add(wheatBlock, 2);
289        }
290    }
291
292    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.
293    {
294        GameObject ground = new GameObject(width, height);
295        ground.Image = groundTopImages[levelNumber];
296        ground.Position = position;
297        ground.TextureWrapSize = new Vector(width / TILE_SIZE, height / TILE_SIZE);
298        Add(ground);
299    }
300    #endregion
301
302    void SetControls()
303    {
304        Keyboard.Listen(Key.A, ButtonState.Down, delegate { players[0].Walk(-300); }, "Player 1 moves left");
305        Keyboard.Listen(Key.D, ButtonState.Down, delegate { players[0].Walk(300); }, "Player 1 moves right");
306        Keyboard.Listen(Key.W, ButtonState.Down, delegate { players[0].Jump(1000); }, "Player 1 jumps");
307
308        Keyboard.Listen(Key.Left, ButtonState.Down, delegate { players[1].Walk(-300); }, "Player 2 moves left");
309        Keyboard.Listen(Key.Right, ButtonState.Down, delegate { players[1].Walk(300); }, "Player 2 moves right");
310        Keyboard.Listen(Key.Up, ButtonState.Down, delegate { players[1].Jump(1000); }, "Player 2 jumps");
311
312        Keyboard.Listen(Key.F1, ButtonState.Pressed, ShowControlHelp, "Show help");
313        Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Exit game");
314    }
315
316    void UpdateCamera()
317    {
318
319        double minY = players.Min(p => p.Y);
320        double maxY = players.Max(p => p.Y);
321        double minX = players.Min(p => p.X) + cameraOffset;
322
323        Vector minPosition = new Vector(Math.Max(minX, cameraTargetX), minY);
324        Vector maxPosition = new Vector(minX, maxY);
325
326        cameraTarget = (minPosition + maxPosition) * 0.5;
327        cameraTarget.X = Math.Max(cameraTargetX, minX);     //Lellllll.
328
329        cameraTargetX = Math.Max(cameraTargetX, minX);
330
331
332        double windowMax = Camera.ScreenToWorld(new Vector(Window.Width / 2.0, 0)).X;
333        double windowMin = Camera.ScreenToWorld(new Vector(-Window.Width / 2.0, 0)).X;
334        foreach (var player in players)
335        {
336            player.Left = Math.Max(player.Left, windowMin);
337            player.Right = Math.Min(player.Right, windowMax);
338        }
339
340        //double windowMax = Camera.ScreenToWorld(new Vector(Window.Width / 2.0, 0)).X;
341        //double windowMin = Camera.ScreenToWorld(new Vector(-Window.Width / 2.0, 0)).X;
342        //foreach (var player in players)
343        //{
344        //    player.Left = Math.Max(player.Left, windowMin);
345        //    player.Right = Math.Min(player.Right, windowMax);
346        //}
347    }
348
349    protected override void Update(Time time)
350    {
351        Camera.Position += (cameraTarget - Camera.Position) * time.SinceLastUpdate.TotalSeconds * cameraSpeed;
352        //Camera.X += (cameraTargetX - Camera.X) * time.SinceLastUpdate.TotalSeconds * cameraSpeed;
353
354
355        base.Update(time);
356    }
357
358    /// <summary>
359    /// Yritän leikkiä kameralla. Vielä varmaan hetken pidempään.
360    /// </summary>
361    /// <param name="gameTime"></param>
362    /*
363    protected override void Update(Microsoft.Xna.Framework.GameTime gameTime)
364    {
365        foreach(HillBilly player in players)
366        {
367            playerPositionsX.Add(player.Position.X);
368            playerPositionsY.Add(player.Position.Y);
369        }
370
371        double maxX = playerPositionsX.Max();
372        double maxY = playerPositionsY.Max();
373        double minX = playerPositionsX.Min();
374        double minY = playerPositionsY.Min();
375
376        Vector minPosition = new Vector(minX, minY);
377        Vector maxPosition = new Vector(maxX, maxY);
378
379        Camera.Position = (minPosition + maxPosition) * 0.5;
380
381        playerPositionsX.Clear();
382        playerPositionsY.Clear();
383
384        base.Update(gameTime);
385    }
386     */
387}
Note: See TracBrowser for help on using the repository browser.