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 | sealed class Exit : PhysicsObject |
---|
11 | { |
---|
12 | /// <summary> |
---|
13 | /// Kentän nimi johon siirrytään. |
---|
14 | /// </summary> |
---|
15 | public string TargetLevel { get; set; } |
---|
16 | |
---|
17 | /// <summary> |
---|
18 | /// Kohdekentässä olevan exitin nimi jonka päältä aloitetaan. |
---|
19 | /// </summary> |
---|
20 | public string TargetExitName { get; set; } |
---|
21 | |
---|
22 | /// <summary> |
---|
23 | /// Tämän uloskäynnin nimi. |
---|
24 | /// </summary> |
---|
25 | public string Name { get; set; } |
---|
26 | |
---|
27 | public Exit(double width, double height) |
---|
28 | : base(width, height) |
---|
29 | { |
---|
30 | Color = new Color(255, 0, 0, 60); // Debuggausta varten jotta näkee uloskäynnit. |
---|
31 | IgnoresCollisionResponse = true; |
---|
32 | Tag = "exit"; |
---|
33 | } |
---|
34 | } |
---|
35 | |
---|
36 | class Creature : PhysicsObject |
---|
37 | { |
---|
38 | public double MovementSpeed { get; set; } |
---|
39 | |
---|
40 | public Creature(double width, double height) |
---|
41 | : base(width, height) |
---|
42 | { |
---|
43 | LinearDamping = 0.5; |
---|
44 | } |
---|
45 | |
---|
46 | public void Move(Direction direction) |
---|
47 | { |
---|
48 | if (!Game.IsPaused) |
---|
49 | Push(direction.GetVector() * MovementSpeed); |
---|
50 | } |
---|
51 | } |
---|
52 | |
---|
53 | public partial class TheLegendOfGabriel : PhysicsGame |
---|
54 | { |
---|
55 | public const int TILE_SIZE = 20; |
---|
56 | |
---|
57 | private Creature player; |
---|
58 | |
---|
59 | private bool transition = false; |
---|
60 | List<GameObject> oldObjects = new List<GameObject>(); |
---|
61 | List<Exit> exits = new List<Exit>(); |
---|
62 | |
---|
63 | public override void Begin() |
---|
64 | { |
---|
65 | SmoothTextures = false; |
---|
66 | StartGame(); |
---|
67 | } |
---|
68 | |
---|
69 | private void StartGame() |
---|
70 | { |
---|
71 | ClearAll(); |
---|
72 | CreateLevel("testlevel"); |
---|
73 | CreatePlayer(new Vector(30, -30)); |
---|
74 | SetControls(); |
---|
75 | Camera.ZoomToLevel(); |
---|
76 | } |
---|
77 | |
---|
78 | void SetControls() |
---|
79 | { |
---|
80 | Keyboard.Listen(Key.Left, ButtonState.Down, player.Move, null, Direction.Left); |
---|
81 | Keyboard.Listen(Key.Right, ButtonState.Down, player.Move, null, Direction.Right); |
---|
82 | Keyboard.Listen(Key.Up, ButtonState.Down, player.Move, null, Direction.Up); |
---|
83 | Keyboard.Listen(Key.Down, ButtonState.Down, player.Move, null, Direction.Down); |
---|
84 | |
---|
85 | Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, null); |
---|
86 | } |
---|
87 | |
---|
88 | /// <summary> |
---|
89 | /// Luo pelaajan. |
---|
90 | /// </summary> |
---|
91 | void CreatePlayer(Vector position) |
---|
92 | { |
---|
93 | player = new Creature(TILE_SIZE, TILE_SIZE); |
---|
94 | player.MovementSpeed = 2300; |
---|
95 | player.Position = position; |
---|
96 | Add(player); |
---|
97 | |
---|
98 | AddCollisionHandler(player, "exit", CollidesWithExit); |
---|
99 | } |
---|
100 | |
---|
101 | void CollidesWithExit(PhysicsObject pPlayer, PhysicsObject pExit) |
---|
102 | { |
---|
103 | var oldExit = pExit as Exit; |
---|
104 | if (oldExit == null || transition) |
---|
105 | return; |
---|
106 | |
---|
107 | transition = true; |
---|
108 | |
---|
109 | // Otetaan vanhat objektit talteen. |
---|
110 | oldObjects.Clear(); |
---|
111 | foreach (var obj in GetObjects(o => o != player)) |
---|
112 | { |
---|
113 | if (obj != player) |
---|
114 | { |
---|
115 | oldObjects.Add(obj); |
---|
116 | } |
---|
117 | } |
---|
118 | |
---|
119 | // Luodaan seuraava kenttä. |
---|
120 | exits.Clear(); |
---|
121 | CreateLevel(oldExit.TargetLevel); |
---|
122 | |
---|
123 | // Pysäytetään peli siirtymän ajaksi. |
---|
124 | Pause(); |
---|
125 | PhysicsEnabled = false; |
---|
126 | |
---|
127 | // Etsitään seuraavan kentän kohde exitti johon siirrytään. |
---|
128 | var targetExit = exits.FirstOrDefault(e => e.Name == oldExit.TargetExitName); |
---|
129 | |
---|
130 | // Lasketaan siirtymävektorit. |
---|
131 | Direction dir = GetExitDirection(targetExit); |
---|
132 | Vector exitDelta = targetExit.Position - oldExit.Position; |
---|
133 | Vector transitionVector = Level.Size; |
---|
134 | if (dir == Direction.Left || dir == Direction.Right) |
---|
135 | { |
---|
136 | transitionVector.X *= dir.GetVector().X; |
---|
137 | transitionVector.Y = exitDelta.Y; |
---|
138 | } |
---|
139 | else |
---|
140 | { |
---|
141 | transitionVector.Y *= dir.GetVector().Y; |
---|
142 | transitionVector.X = exitDelta.X; |
---|
143 | } |
---|
144 | |
---|
145 | // Siirretään vanhoja objekteja ja pelaajaa. |
---|
146 | foreach (var obj in oldObjects) |
---|
147 | { |
---|
148 | obj.Position += transitionVector; |
---|
149 | } |
---|
150 | player.Position += transitionVector + Direction.Inverse(dir).GetVector() * TILE_SIZE * 4; |
---|
151 | |
---|
152 | // Zoomataan kameraa sopivasti ja liikutetaan se vanhan kentän päälle, josta se sitten siirtyy uuden kentän päälle. |
---|
153 | Camera.Position += transitionVector; |
---|
154 | Vector pos = Camera.Position; |
---|
155 | Camera.ZoomToLevel(); |
---|
156 | Camera.Position = pos; |
---|
157 | } |
---|
158 | |
---|
159 | /// <summary> |
---|
160 | /// Palauttaa suunnan millä puolella kenttää uloskäynti on. |
---|
161 | /// </summary> |
---|
162 | Direction GetExitDirection(Exit exit) |
---|
163 | { |
---|
164 | const double epsilon = 1e-3; |
---|
165 | Func<double, double, bool> isSame = (x, y) => Math.Abs(y - x) < epsilon; |
---|
166 | |
---|
167 | if (isSame(exit.Top, Level.Top)) |
---|
168 | { |
---|
169 | return Direction.Up; |
---|
170 | } |
---|
171 | if (isSame(exit.Bottom, Level.Bottom)) |
---|
172 | { |
---|
173 | return Direction.Down; |
---|
174 | } |
---|
175 | if (isSame(exit.Left, Level.Left)) |
---|
176 | { |
---|
177 | return Direction.Left; |
---|
178 | } |
---|
179 | if (isSame(exit.Right, Level.Right)) |
---|
180 | { |
---|
181 | return Direction.Right; |
---|
182 | } |
---|
183 | return Direction.None; |
---|
184 | } |
---|
185 | |
---|
186 | protected override void PausedUpdate(Time gameTime) |
---|
187 | { |
---|
188 | base.PausedUpdate(gameTime); |
---|
189 | double dt = gameTime.SinceLastUpdate.TotalSeconds; |
---|
190 | |
---|
191 | const double transitionSpeed = 5.0; |
---|
192 | |
---|
193 | if (transition) |
---|
194 | { |
---|
195 | Camera.Position += Camera.Position * -transitionSpeed * dt; |
---|
196 | |
---|
197 | // Siirtymä on ohi, jatketaan peliä ja poistetaan edellinen kenttä. |
---|
198 | if (Camera.Position.Magnitude < 1.0) |
---|
199 | { |
---|
200 | transition = false; |
---|
201 | Pause(); |
---|
202 | PhysicsEnabled = true; |
---|
203 | Camera.Position = Vector.Zero; |
---|
204 | |
---|
205 | foreach (var obj in oldObjects) |
---|
206 | { |
---|
207 | obj.Destroy(); |
---|
208 | } |
---|
209 | oldObjects.Clear(); |
---|
210 | } |
---|
211 | } |
---|
212 | } |
---|
213 | } |
---|