1 | using System; |
---|
2 | using System.Collections.Generic; |
---|
3 | using System.Linq; |
---|
4 | using Jypeli; |
---|
5 | using Jypeli.Assets; |
---|
6 | using Jypeli.Controls; |
---|
7 | using Jypeli.Effects; |
---|
8 | using Jypeli.Widgets; |
---|
9 | |
---|
10 | //Luokka maajussien erikoisuuksille. |
---|
11 | class 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> |
---|
23 | public 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 | |
---|
37 | public override void Begin() |
---|
38 | { |
---|
39 | CreateLevel(); |
---|
40 | Extras(); |
---|
41 | |
---|
42 | Camera.X = cameraTargetX = players[0].X; |
---|
43 | } |
---|
44 | |
---|
45 | void Extras() |
---|
46 | { |
---|
47 | Gravity = new Vector(0, -1000); |
---|
48 | Window.Width = 1800; |
---|
49 | Window.Height = 900; |
---|
50 | } |
---|
51 | |
---|
52 | void CreateLevel() |
---|
53 | { |
---|
54 | TileMap level = TileMap.FromLevelAsset("level1"); |
---|
55 | level.SetTileMethod('P', CreatePlayer); |
---|
56 | level.SetTileMethod('#', CreateGround); |
---|
57 | level.Optimize('#'); |
---|
58 | level.Execute(TILE_SIZE, TILE_SIZE); |
---|
59 | Level.CreateBorders(true); |
---|
60 | |
---|
61 | SetControls(); |
---|
62 | } |
---|
63 | |
---|
64 | void CreatePlayer(Vector position, double width, double height) |
---|
65 | { |
---|
66 | HillBilly player = new HillBilly(width, height * 2); |
---|
67 | player.Shape = Shape.Rectangle; |
---|
68 | player.Position = position + new Vector(0, height * 0.5); |
---|
69 | player.Color = Color.White; |
---|
70 | players.Add(player); |
---|
71 | Add(player); |
---|
72 | } |
---|
73 | |
---|
74 | void CreateGround(Vector position, double width, double height) |
---|
75 | { |
---|
76 | PhysicsObject ground = PhysicsObject.CreateStaticObject(width, height); |
---|
77 | ground.Image = groundImage; |
---|
78 | ground.Position = position; |
---|
79 | ground.TextureWrapSize = new Vector(width / TILE_SIZE, height / TILE_SIZE); |
---|
80 | Add(ground); |
---|
81 | } |
---|
82 | |
---|
83 | void SetControls() |
---|
84 | { |
---|
85 | Keyboard.Listen(Key.A, ButtonState.Down, delegate { players[0].Walk(-300); }, "Player 1 moves left"); |
---|
86 | Keyboard.Listen(Key.D, ButtonState.Down, delegate { players[0].Walk(300); }, "Player 1 moves right"); |
---|
87 | Keyboard.Listen(Key.W, ButtonState.Down, delegate { players[0].Jump(1000); }, "Player 1 jumps"); |
---|
88 | |
---|
89 | Keyboard.Listen(Key.Left, ButtonState.Down, delegate { players[1].Walk(-300); }, "Player 2 moves left"); |
---|
90 | Keyboard.Listen(Key.Right, ButtonState.Down, delegate { players[1].Walk(300); }, "Player 2 moves right"); |
---|
91 | Keyboard.Listen(Key.Up, ButtonState.Down, delegate { players[1].Jump(1000); }, "Player 2 jumps"); |
---|
92 | |
---|
93 | //Keyboard.Listen(Key.A, ButtonState.Down, delegate { players[0].Walk(Direction.Left); }, "Player 1 moves left"); |
---|
94 | //Keyboard.Listen(Key.D, ButtonState.Down, delegate {players[0].Walk(Direction.Right);}, "Player 1 moves right"); |
---|
95 | //Keyboard.Listen(Key.W, ButtonState.Down, delegate{players[0].Jump(1000);}, "Player 1 jumps"); |
---|
96 | |
---|
97 | //Keyboard.Listen(Key.Left, ButtonState.Down, delegate { players[1].Walk(Direction.Left); }, "Player 2 moves left"); |
---|
98 | //Keyboard.Listen(Key.Right, ButtonState.Down, delegate { players[1].Walk(Direction.Right); }, "Player 2 moves right"); |
---|
99 | //Keyboard.Listen(Key.Up, ButtonState.Down, delegate { players[1].Jump(1000); }, "Player 2 jumps"); |
---|
100 | } |
---|
101 | |
---|
102 | protected override void Update(Time time) |
---|
103 | { |
---|
104 | double minX = players.Min(p => p.X) + cameraOffset; |
---|
105 | cameraTargetX = Math.Max(cameraTargetX, minX); |
---|
106 | Camera.X += (cameraTargetX - Camera.X) * time.SinceLastUpdate.TotalSeconds * cameraSpeed; |
---|
107 | |
---|
108 | double windowMax = Camera.ScreenToWorld(new Vector(Window.Width / 2.0, 0)).X; |
---|
109 | double windowMin = Camera.ScreenToWorld(new Vector(-Window.Width / 2.0, 0)).X; |
---|
110 | foreach (var player in players) |
---|
111 | { |
---|
112 | player.Left = Math.Max(player.Left, windowMin); |
---|
113 | player.Right = Math.Min(player.Right, windowMax); |
---|
114 | } |
---|
115 | |
---|
116 | base.Update(time); |
---|
117 | } |
---|
118 | |
---|
119 | /// <summary> |
---|
120 | /// Yritän leikkiä kameralla. Vielä varmaan hetken pidempään. |
---|
121 | /// </summary> |
---|
122 | /// <param name="gameTime"></param> |
---|
123 | /* |
---|
124 | protected override void Update(Microsoft.Xna.Framework.GameTime gameTime) |
---|
125 | { |
---|
126 | foreach(HillBilly player in players) |
---|
127 | { |
---|
128 | playerPositionsX.Add(player.Position.X); |
---|
129 | playerPositionsY.Add(player.Position.Y); |
---|
130 | } |
---|
131 | |
---|
132 | double maxX = playerPositionsX.Max(); |
---|
133 | double maxY = playerPositionsY.Max(); |
---|
134 | double minX = playerPositionsX.Min(); |
---|
135 | double minY = playerPositionsY.Min(); |
---|
136 | |
---|
137 | Vector minPosition = new Vector(minX, minY); |
---|
138 | Vector maxPosition = new Vector(maxX, maxY); |
---|
139 | |
---|
140 | Camera.Position = (minPosition + maxPosition) * 0.5; |
---|
141 | |
---|
142 | playerPositionsX.Clear(); |
---|
143 | playerPositionsY.Clear(); |
---|
144 | |
---|
145 | base.Update(gameTime); |
---|
146 | } |
---|
147 | */ |
---|
148 | } |
---|