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

Revision 6196, 8.5 KB checked in by empaheik, 6 years ago (diff)

Jäsentelyä + voisi kyllä laittaa menun alussa kommentteihin koska debug.

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