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

Revision 6197, 10.6 KB checked in by empaheik, 5 years ago (diff)

ColorTileMap?-versio

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