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