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

Revision 6161, 8.9 KB checked in by jotapoti, 6 years ago (diff)

Add IntroSequence? & MainMenu?

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    private Image groundImage = LoadImage("ground");
36    private Image groundTopImage = LoadImage("ground_top");
37
38    private Image cartImage = LoadImage("cart");
39    private Image cartWheelImage = LoadImage("cartwheel");
40
41    public override void Begin()
42    {
43        //IntroSequence();
44        MainMenu();
45    }
46
47    void IntroSequence()
48    {
49        Level.Background.Color = Color.Black;
50
51        Label storyLabel = new Label("Once upon a time...");
52        storyLabel.TextColor = Color.Gray;
53        Add(storyLabel);
54
55        Timer textTimer = new Timer();
56        textTimer.Interval = 0.5;
57        textTimer.Timeout += delegate
58        {
59            // TODO vaalenna tekstiä näkyville läpinäkyvyyttä tms. säätämällä
60           
61        };
62
63        // TODO siirrä aliohjelmaan, jolle voi antaa parametrina stringin, joka näytetään siististi feidaamalla esille
64
65
66
67        // TODO replace this SingleShot with something else. It must be the last thing after the intro sequence
68       
69        // Clean up after the intro:
70        Timer.SingleShot(5, delegate
71        {
72            storyLabel.Destroy();
73
74            ClearAll();
75            MainMenu();
76        });
77    }
78
79    void MainMenu()
80    {
81        Level.Background.Color = Color.MediumBlue;
82
83        MultiSelectWindow mainmenu = new MultiSelectWindow("Main menu",
84"Start game", "Credits", "Exit");
85        mainmenu.AddItemHandler(0, StartGame);
86        mainmenu.AddItemHandler(1, ShowCredits);
87        mainmenu.AddItemHandler(2, Exit);
88        mainmenu.DefaultCancel = 2;
89        Add(mainmenu);
90    }
91
92    void ShowCredits()
93    {
94        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");
95        win.Closed += delegate
96        {
97            ClearAll();
98            MainMenu();
99        };
100        Add(win);
101    }
102
103    void StartGame()
104    {
105
106        CreateLevel();
107        Extras();
108
109        Camera.X = cameraTargetX = players[0].X;
110
111        Timer cameraTimer = new Timer();
112        cameraTimer.Interval = 1 / 30.0;
113        cameraTimer.Timeout += UpdateCamera;
114        cameraTimer.Start();
115
116        // Testailen tässä kärryn luomista.
117        double size = 0.7;
118        PhysicsObject cart = new PhysicsObject(400 * size, 80 * size);
119        cart.X = -1600;
120        cart.Y = 200;
121        cart.Image = cartImage;
122        cart.CollisionIgnoreGroup = 2;
123        Add(cart);
124        PhysicsObject cartWheel = new PhysicsObject(160 * size, 160 * size, Shape.Circle);
125        cartWheel.Image = cartWheelImage;
126        cartWheel.Position = cart.Position + new Vector(-110, -30) * size;
127        cartWheel.CollisionIgnoreGroup = 2;
128        Add(cartWheel);
129        AxleJoint joint = new AxleJoint(cart, cartWheel);
130        Add(joint);
131    }
132
133    void Extras()
134    {
135        Gravity = new Vector(0, -1000);
136        Window.Width = 1800;
137        Window.Height = 900;
138    }
139
140    void CreateLevel()
141    {
142        TileMap level = TileMap.FromLevelAsset("level1");
143        level.SetTileMethod('P', CreatePlayer);
144        level.SetTileMethod('#', CreateGroundTop);
145        level.SetTileMethod('%', CreateGround);
146        level.Optimize('#', '%');
147        level.Execute(TILE_SIZE, TILE_SIZE);
148        //Level.CreateBorders(true);
149
150        SetControls();
151    }
152
153    void CreatePlayer(Vector position, double width, double height)
154    {
155        HillBilly player = new HillBilly(width, height * 2);
156        player.Shape = Shape.Rectangle;
157        player.Position = position + new Vector(0, height * 0.5);
158        player.Color = Color.White;
159        players.Add(player);
160        Add(player);
161    }
162
163    void CreateGroundTop(Vector position, double width, double height)
164    {
165        // Puolet pienempi näkymätön palikka alla johon törmää.
166        PhysicsObject ground = PhysicsObject.CreateStaticObject(width, height / 2.0);
167        ground.IsVisible = false;
168        ground.Position = position - new Vector(0.0, TILE_SIZE / 4.0);
169        Add(ground);
170
171        // Maanpinnan näkyvä osa.
172        GameObject visibleGround = new GameObject(width, height);
173        visibleGround.Image = groundImage;
174        visibleGround.Position = position;
175        visibleGround.TextureWrapSize = new Vector(width / TILE_SIZE, height / TILE_SIZE);
176        Add(visibleGround, -1);
177    }
178
179    void CreateGround(Vector position, double width, double height)
180    {
181        GameObject ground = new GameObject(width, height);
182        ground.Image = groundTopImage;
183        ground.Position = position;
184        ground.TextureWrapSize = new Vector(width / TILE_SIZE, height / TILE_SIZE);
185        Add(ground);
186    }
187
188    void SetControls()
189    {
190        Keyboard.Listen(Key.A, ButtonState.Down, delegate { players[0].Walk(-300); }, "Player 1 moves left");
191        Keyboard.Listen(Key.D, ButtonState.Down, delegate { players[0].Walk(300); }, "Player 1 moves right");
192        Keyboard.Listen(Key.W, ButtonState.Down, delegate { players[0].Jump(1000); }, "Player 1 jumps");
193
194        Keyboard.Listen(Key.Left, ButtonState.Down, delegate { players[1].Walk(-300); }, "Player 2 moves left");
195        Keyboard.Listen(Key.Right, ButtonState.Down, delegate { players[1].Walk(300); }, "Player 2 moves right");
196        Keyboard.Listen(Key.Up, ButtonState.Down, delegate { players[1].Jump(1000); }, "Player 2 jumps");
197
198        //Keyboard.Listen(Key.A, ButtonState.Down, delegate { players[0].Walk(Direction.Left); }, "Player 1 moves left");
199        //Keyboard.Listen(Key.D, ButtonState.Down, delegate {players[0].Walk(Direction.Right);}, "Player 1 moves right");
200        //Keyboard.Listen(Key.W, ButtonState.Down, delegate{players[0].Jump(1000);}, "Player 1 jumps");
201
202        //Keyboard.Listen(Key.Left, ButtonState.Down, delegate { players[1].Walk(Direction.Left); }, "Player 2 moves left");
203        //Keyboard.Listen(Key.Right, ButtonState.Down, delegate { players[1].Walk(Direction.Right); }, "Player 2 moves right");
204        //Keyboard.Listen(Key.Up, ButtonState.Down, delegate { players[1].Jump(1000); }, "Player 2 jumps");
205
206        Keyboard.Listen(Key.F1, ButtonState.Pressed, ShowControlHelp, "Show help");
207        Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Exit game");
208    }
209
210    void UpdateCamera()
211    {
212        double minX = players.Min(p => p.X) + cameraOffset;
213        cameraTargetX = Math.Max(cameraTargetX, minX);
214
215        double windowMax = Camera.ScreenToWorld(new Vector(Window.Width / 2.0, 0)).X;
216        double windowMin = Camera.ScreenToWorld(new Vector(-Window.Width / 2.0, 0)).X;
217        foreach (var player in players)
218        {
219            player.Left = Math.Max(player.Left, windowMin);
220            player.Right = Math.Min(player.Right, windowMax);
221        }
222    }
223
224    protected override void Update(Time time)
225    {
226        Camera.X += (cameraTargetX - Camera.X) * time.SinceLastUpdate.TotalSeconds * cameraSpeed;
227
228        base.Update(time);
229    }
230
231    /// <summary>
232    /// Yritän leikkiä kameralla. Vielä varmaan hetken pidempään.
233    /// </summary>
234    /// <param name="gameTime"></param>
235    /*
236    protected override void Update(Microsoft.Xna.Framework.GameTime gameTime)
237    {
238        foreach(HillBilly player in players)
239        {
240            playerPositionsX.Add(player.Position.X);
241            playerPositionsY.Add(player.Position.Y);
242        }
243
244        double maxX = playerPositionsX.Max();
245        double maxY = playerPositionsY.Max();
246        double minX = playerPositionsX.Min();
247        double minY = playerPositionsY.Min();
248
249        Vector minPosition = new Vector(minX, minY);
250        Vector maxPosition = new Vector(maxX, maxY);
251
252        Camera.Position = (minPosition + maxPosition) * 0.5;
253
254        playerPositionsX.Clear();
255        playerPositionsY.Clear();
256
257        base.Update(gameTime);
258    }
259     */
260}
Note: See TracBrowser for help on using the repository browser.