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

Revision 6151, 6.2 KB checked in by sieerinn, 5 years ago (diff)
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    public override void Begin()
39    {
40        CreateLevel();
41        Extras();
42
43        Camera.X = cameraTargetX = players[0].X;
44
45        Timer cameraTimer = new Timer();
46        cameraTimer.Interval = 1 / 30.0;
47        cameraTimer.Timeout += UpdateCamera;
48        cameraTimer.Start();
49    }
50
51    void Extras()
52    {
53        Gravity = new Vector(0, -1000);
54        Window.Width = 1800;
55        Window.Height = 900;
56    }
57
58    void CreateLevel()
59    {
60        TileMap level = TileMap.FromLevelAsset("level1");
61        level.SetTileMethod('P', CreatePlayer);
62        level.SetTileMethod('#', CreateGroundTop);
63        level.SetTileMethod('%', CreateGround);
64        level.Optimize('#', '%');
65        level.Execute(TILE_SIZE, TILE_SIZE);
66        //Level.CreateBorders(true);
67
68        SetControls();
69    }
70
71    void CreatePlayer(Vector position, double width, double height)
72    {
73        HillBilly player = new HillBilly(width, height * 2);
74        player.Shape = Shape.Rectangle;
75        player.Position = position + new Vector(0, height * 0.5);
76        player.Color = Color.White;
77        players.Add(player);
78        Add(player);
79    }
80
81    void CreateGroundTop(Vector position, double width, double height)
82    {
83        // Puolet pienempi näkymätön palikka alla johon törmää.
84        PhysicsObject ground = PhysicsObject.CreateStaticObject(width, height / 2.0);
85        ground.IsVisible = false;
86        ground.Position = position - new Vector(0.0, TILE_SIZE / 4.0);
87        Add(ground);
88
89        // Maanpinnan näkyvä osa.
90        GameObject visibleGround = new GameObject(width, height);
91        visibleGround.Image = groundImage;
92        visibleGround.Position = position;
93        visibleGround.TextureWrapSize = new Vector(width / TILE_SIZE, height / TILE_SIZE);
94        Add(visibleGround, -1);
95    }
96
97    void CreateGround(Vector position, double width, double height)
98    {
99        GameObject ground = new GameObject(width, height);
100        ground.Image = groundTopImage;
101        ground.Position = position;
102        ground.TextureWrapSize = new Vector(width / TILE_SIZE, height / TILE_SIZE);
103        Add(ground);
104    }
105
106    void SetControls()
107    {
108        Keyboard.Listen(Key.A, ButtonState.Down, delegate { players[0].Walk(-300); }, "Player 1 moves left");
109        Keyboard.Listen(Key.D, ButtonState.Down, delegate { players[0].Walk(300); }, "Player 1 moves right");
110        Keyboard.Listen(Key.W, ButtonState.Down, delegate { players[0].Jump(1000); }, "Player 1 jumps");
111
112        Keyboard.Listen(Key.Left, ButtonState.Down, delegate { players[1].Walk(-300); }, "Player 2 moves left");
113        Keyboard.Listen(Key.Right, ButtonState.Down, delegate { players[1].Walk(300); }, "Player 2 moves right");
114        Keyboard.Listen(Key.Up, ButtonState.Down, delegate { players[1].Jump(1000); }, "Player 2 jumps");
115
116        //Keyboard.Listen(Key.A, ButtonState.Down, delegate { players[0].Walk(Direction.Left); }, "Player 1 moves left");
117        //Keyboard.Listen(Key.D, ButtonState.Down, delegate {players[0].Walk(Direction.Right);}, "Player 1 moves right");
118        //Keyboard.Listen(Key.W, ButtonState.Down, delegate{players[0].Jump(1000);}, "Player 1 jumps");
119
120        //Keyboard.Listen(Key.Left, ButtonState.Down, delegate { players[1].Walk(Direction.Left); }, "Player 2 moves left");
121        //Keyboard.Listen(Key.Right, ButtonState.Down, delegate { players[1].Walk(Direction.Right); }, "Player 2 moves right");
122        //Keyboard.Listen(Key.Up, ButtonState.Down, delegate { players[1].Jump(1000); }, "Player 2 jumps");
123    }
124
125    void UpdateCamera()
126    {
127        double minX = players.Min(p => p.X) + cameraOffset;
128        cameraTargetX = Math.Max(cameraTargetX, minX);
129
130        double windowMax = Camera.ScreenToWorld(new Vector(Window.Width / 2.0, 0)).X;
131        double windowMin = Camera.ScreenToWorld(new Vector(-Window.Width / 2.0, 0)).X;
132        foreach (var player in players)
133        {
134            player.Left = Math.Max(player.Left, windowMin);
135            player.Right = Math.Min(player.Right, windowMax);
136        }
137    }
138
139    protected override void Update(Time time)
140    {
141        Camera.X += (cameraTargetX - Camera.X) * time.SinceLastUpdate.TotalSeconds * cameraSpeed;
142
143        base.Update(time);
144    }
145
146    /// <summary>
147    /// Yritän leikkiä kameralla. Vielä varmaan hetken pidempään.
148    /// </summary>
149    /// <param name="gameTime"></param>
150    /*
151    protected override void Update(Microsoft.Xna.Framework.GameTime gameTime)
152    {
153        foreach(HillBilly player in players)
154        {
155            playerPositionsX.Add(player.Position.X);
156            playerPositionsY.Add(player.Position.Y);
157        }
158
159        double maxX = playerPositionsX.Max();
160        double maxY = playerPositionsY.Max();
161        double minX = playerPositionsX.Min();
162        double minY = playerPositionsY.Min();
163
164        Vector minPosition = new Vector(minX, minY);
165        Vector maxPosition = new Vector(maxX, maxY);
166
167        Camera.Position = (minPosition + maxPosition) * 0.5;
168
169        playerPositionsX.Clear();
170        playerPositionsY.Clear();
171
172        base.Update(gameTime);
173    }
174     */
175}
Note: See TracBrowser for help on using the repository browser.