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

Revision 6198, 11.8 KB checked in by empaheik, 5 years ago (diff)

Lell talosia.

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 ElementCreation
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, CreateDummy, Color.Gray);  //TODO: 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
171        //TileMap level = TileMap.FromLevelAsset("level" + levelNumber);
172        //level.SetTileMethod('P', CreatePlayer);
173        //level.SetTileMethod('#', CreateGroundTop);
174        //level.SetTileMethod('%', CreateGround);
175        //level.Optimize('#', '%');
176        //level.Execute(TILE_SIZE, TILE_SIZE);
177        //Level.CreateBorders(true);
178
179        SetControls();
180    }
181
182    void CreateHouse(Vector position, double width, double height)
183    {
184        // Näkyvä talo
185        GameObject house = new GameObject(width * 10, height * 8);
186        house.Image = houseImages[0];
187        house.Position = position - new Vector(0, height * 2.5);
188        Add(house, -2);
189
190        PhysicsObject platform = PhysicsObject.CreateStaticObject(house.Width - (TILE_SIZE * 4), 5);
191        platform.IsVisible = false;
192        platform.Position = new Vector(house.X + TILE_SIZE, house.Top);
193        Add(platform);
194    }
195
196    void CreateBurnedHouse(Vector position, double width, double height)
197    {
198        GameObject house = new GameObject(width * 10, height * 8);
199        house.Image = houseImages[1];
200        house.Position = position - new Vector(0, height * 2.5);
201        Add(house, -2);
202
203        PhysicsObject platform = PhysicsObject.CreateStaticObject(house.Width* 0.25, 5);
204        platform.IsVisible = false;
205        platform.Position = new Vector(house.X + platform.Width * 0.5, house.Top);
206        Add(platform);
207    }
208
209    private void CreateCart(Vector position, double width, double height)
210    {
211        double size = 0.7;
212        PhysicsObject cart = new PhysicsObject(400 * size, 80 * size);
213        cart.Position = position;
214        cart.Image = cartImage;
215        cart.CollisionIgnoreGroup = 2;
216        Add(cart);
217        PhysicsObject cartWheel = new PhysicsObject(160 * size, 160 * size, Shape.Circle);
218        cartWheel.Image = cartWheelImage;
219        cartWheel.Position = cart.Position + new Vector(-110, -30) * size;
220        cartWheel.CollisionIgnoreGroup = 2;
221        Add(cartWheel);
222        AxleJoint joint = new AxleJoint(cart, cartWheel);
223        Add(joint);
224    }
225
226
227    private void CreateDummy(Vector position, double width, double height, Color color)
228    {
229        PhysicsObject dummy = PhysicsObject.CreateStaticObject(width, height);
230        dummy.Position = position;
231        dummy.Color = color;
232        //dummy.TextureWrapSize = new Vector(width / TILE_SIZE, height / TILE_SIZE);
233        Add(dummy);
234    }
235
236    void CreatePlayer(Vector position, double width, double height)
237    {
238        HillBilly player = new HillBilly(width, height * 2);
239        player.Shape = Shape.Rectangle;
240        player.Position = position + new Vector(0, height * 0.5);
241        player.Color = Color.White;
242        players.Add(player);
243        Add(player);
244    }
245
246    void CreateGroundTop(Vector position, double width, double height)
247    {
248        // Puolet pienempi näkymätön palikka alla johon törmää.
249        PhysicsObject ground = PhysicsObject.CreateStaticObject(width, height / 2.0);
250        ground.IsVisible = false;
251        ground.Position = position - new Vector(0.0, TILE_SIZE / 4.0);
252        Add(ground);
253
254        // Maanpinnan näkyvä osa.
255        GameObject visibleGround = new GameObject(width, height);
256        visibleGround.Image = groundImages[levelNumber];
257        visibleGround.Position = position;
258        visibleGround.TextureWrapSize = new Vector(width / TILE_SIZE, height / TILE_SIZE);
259        Add(visibleGround, -1);
260    }
261
262    void CreateGround(Vector position, double width, double height)
263    {
264        GameObject ground = new GameObject(width, height);
265        ground.Image = groundTopImages[levelNumber];
266        ground.Position = position;
267        ground.TextureWrapSize = new Vector(width / TILE_SIZE, height / TILE_SIZE);
268        Add(ground);
269    }
270    #endregion
271
272    void SetControls()
273    {
274        Keyboard.Listen(Key.A, ButtonState.Down, delegate { players[0].Walk(-300); }, "Player 1 moves left");
275        Keyboard.Listen(Key.D, ButtonState.Down, delegate { players[0].Walk(300); }, "Player 1 moves right");
276        Keyboard.Listen(Key.W, ButtonState.Down, delegate { players[0].Jump(1000); }, "Player 1 jumps");
277
278        Keyboard.Listen(Key.Left, ButtonState.Down, delegate { players[1].Walk(-300); }, "Player 2 moves left");
279        Keyboard.Listen(Key.Right, ButtonState.Down, delegate { players[1].Walk(300); }, "Player 2 moves right");
280        Keyboard.Listen(Key.Up, ButtonState.Down, delegate { players[1].Jump(1000); }, "Player 2 jumps");
281
282        Keyboard.Listen(Key.F1, ButtonState.Pressed, ShowControlHelp, "Show help");
283        Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Exit game");
284    }
285
286    void UpdateCamera()
287    {
288        double minX = players.Min(p => p.X) + cameraOffset;
289        cameraTargetX = Math.Max(cameraTargetX, minX);
290
291        double windowMax = Camera.ScreenToWorld(new Vector(Window.Width / 2.0, 0)).X;
292        double windowMin = Camera.ScreenToWorld(new Vector(-Window.Width / 2.0, 0)).X;
293        foreach (var player in players)
294        {
295            player.Left = Math.Max(player.Left, windowMin);
296            player.Right = Math.Min(player.Right, windowMax);
297        }
298    }
299
300    protected override void Update(Time time)
301    {
302        Camera.X += (cameraTargetX - Camera.X) * time.SinceLastUpdate.TotalSeconds * cameraSpeed;
303
304        base.Update(time);
305    }
306
307    /// <summary>
308    /// Yritän leikkiä kameralla. Vielä varmaan hetken pidempään.
309    /// </summary>
310    /// <param name="gameTime"></param>
311    /*
312    protected override void Update(Microsoft.Xna.Framework.GameTime gameTime)
313    {
314        foreach(HillBilly player in players)
315        {
316            playerPositionsX.Add(player.Position.X);
317            playerPositionsY.Add(player.Position.Y);
318        }
319
320        double maxX = playerPositionsX.Max();
321        double maxY = playerPositionsY.Max();
322        double minX = playerPositionsX.Min();
323        double minY = playerPositionsY.Min();
324
325        Vector minPosition = new Vector(minX, minY);
326        Vector maxPosition = new Vector(maxX, maxY);
327
328        Camera.Position = (minPosition + maxPosition) * 0.5;
329
330        playerPositionsX.Clear();
331        playerPositionsY.Clear();
332
333        base.Update(gameTime);
334    }
335     */
336}
Note: See TracBrowser for help on using the repository browser.