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

Revision 6147, 3.1 KB checked in by empaheik, 6 years ago (diff)

Raakapohja + kameraleikkejä

Line 
1using System;
2using System.Collections.Generic;
3using Jypeli;
4using Jypeli.Assets;
5using Jypeli.Controls;
6using Jypeli.Effects;
7using Jypeli.Widgets;
8
9//Luokka maajussien erikoisuuksille.
10class HillBilly : PlatformCharacter2
11{
12    public HillBilly(double leveys, double korkeus)
13        : base(leveys, korkeus)
14    {
15       
16    }
17}
18
19/// <summary>
20/// An MMO where you go to war with mages in the lava kingdom.
21/// </summary>
22public class HillbillyRun : PhysicsGame
23{
24    private List<HillBilly> players = new List<HillBilly>();
25    private List<double> playerPositionsX = new List<double>();
26    private List<double> playerPositionsY = new List<double>();
27
28    public override void Begin()
29    {
30        CreateLevel();
31        Extras();
32    }
33
34    void Extras()
35    {
36        Gravity = new Vector(0, -1000);
37        Window.Width = 1800;
38        Window.Height = 900;
39    }
40
41    void CreateLevel()
42    {
43        TileMap level = TileMap.FromLevelAsset("level1");
44        level.SetTileMethod('P', CreatePlayer);
45        level.Execute(50, 50);
46        level.Optimize();
47        Level.CreateBorders(true);
48
49        SetControls();
50    }
51
52    void CreatePlayer(Vector location, double width, double height)
53    {
54        HillBilly player = new HillBilly(width, height * 2);
55        player.Shape = Shape.Rectangle;
56        player.Position = location + new Vector(0, height * 0.5);
57        player.Color = Color.White;
58        players.Add(player);
59        Add(player);
60    }
61
62    void SetControls()
63    {
64        Keyboard.Listen(Key.A, ButtonState.Down, delegate { players[0].Walk(Direction.Left); }, "Player 1 moves left");
65        Keyboard.Listen(Key.D, ButtonState.Down, delegate {players[0].Walk(Direction.Right);}, "Player 1 moves right");
66        Keyboard.Listen(Key.W, ButtonState.Down, delegate{players[0].Jump(1000);}, "Player 1 jumps");
67
68        Keyboard.Listen(Key.Left, ButtonState.Down, delegate { players[1].Walk(Direction.Left); }, "Player 2 moves left");
69        Keyboard.Listen(Key.Right, ButtonState.Down, delegate { players[1].Walk(Direction.Right); }, "Player 2 moves right");
70        Keyboard.Listen(Key.Up, ButtonState.Down, delegate { players[1].Jump(1000); }, "Player 2 jumps");
71    }
72
73
74    /// <summary>
75    /// Yritän leikkiä kameralla. Vielä varmaan hetken pidempään.
76    /// </summary>
77    /// <param name="gameTime"></param>
78    protected override void Update(Microsoft.Xna.Framework.GameTime gameTime)
79    {
80        foreach(HillBilly player in players)
81        {
82            playerPositionsX.Add(player.Position.X);
83            playerPositionsY.Add(player.Position.Y);
84        }
85
86        double maxX = playerPositionsX.Max();
87        double maxY = playerPositionsY.Max();
88        double minX = playerPositionsX.Min();
89        double minY = playerPositionsY.Min();
90
91        Vector minPosition = new Vector(minX, minY);
92        Vector maxPosition = new Vector(maxX, maxY);
93
94        Camera.Position = (minPosition + maxPosition) * 0.5;
95
96        playerPositionsX.Clear();
97        playerPositionsY.Clear();
98
99        base.Update(gameTime);
100    }
101}
Note: See TracBrowser for help on using the repository browser.