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

Revision 6199, 12.4 KB checked in by empaheik, 5 years ago (diff)

Ryömiviä harmaita palikoita.

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