- Timestamp:
- 2015-06-29 18:18:31 (8 years ago)
- Location:
- 2015/27/ohjaajat/TheLegendOfGabriel/TheLegendOfGabriel
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
2015/27/ohjaajat/TheLegendOfGabriel/TheLegendOfGabriel/TheLegendOfGabriel/LevelCreation.cs
r6538 r6539 8 8 { 9 9 /// <summary> 10 /// Luo pelaajan.11 /// </summary>12 void CreatePlayer(Vector position)13 {14 player = new Creature(TILE_SIZE, TILE_SIZE);15 player.MovementSpeed = 2300;16 player.Position = position;17 Add(player);18 }19 20 /// <summary>21 10 /// Luo kentän .tmx tiedostosta. 22 11 /// </summary> … … 26 15 level.SetTileMethod("base", CreateBaseTile); 27 16 level.SetTileMethod("foreground", CreateForegroundTile); 17 level.SetObjectMethod("exit", CreateExit); 28 18 level.Execute(); 29 19 … … 57 47 Add(tile, layer); 58 48 } 49 50 /// <summary> 51 /// Luo uloskäynnin, joka vie toiseen kenttään. 52 /// </summary> 53 void CreateExit(Vector position, double width, double height, Angle angle, Shape shape, string name, Dictionary<string, string> properties) 54 { 55 var target = properties["goto"].Split('@'); // Jos peli kaatuaa tälle riville niin joltain exitiltä puuttuu goto-property. 56 var exit = new Exit(width, height); 57 exit.Position = position; 58 exit.TargetLevel = target[0]; 59 exit.TargetExitName = target[1]; 60 exit.Name = name; 61 Add(exit); 62 exits.Add(exit); 63 } 59 64 } -
2015/27/ohjaajat/TheLegendOfGabriel/TheLegendOfGabriel/TheLegendOfGabriel/TheLegendOfGabriel.cs
r6538 r6539 1 1 using System; 2 2 using System.Collections.Generic; 3 using System.Linq; 3 4 using Jypeli; 4 5 using Jypeli.Assets; … … 6 7 using Jypeli.Effects; 7 8 using Jypeli.Widgets; 9 using Microsoft.Xna.Framework; 10 using Physics2DDotNet; 11 using Color = Jypeli.Color; 12 13 sealed class Exit : PhysicsObject 14 { 15 /// <summary> 16 /// Kentän nimi johon siirrytään. 17 /// </summary> 18 public string TargetLevel { get; set; } 19 20 /// <summary> 21 /// Kohdekentässä olevan exitin nimi jonka päältä aloitetaan. 22 /// </summary> 23 public string TargetExitName { get; set; } 24 25 /// <summary> 26 /// Tämän uloskäynnin nimi. 27 /// </summary> 28 public string Name { get; set; } 29 30 public Exit(double width, double height) 31 : base(width, height) 32 { 33 Color = new Color(255, 0, 0, 60); // Debuggausta varten jotta näkee uloskäynnit. 34 IgnoresCollisionResponse = true; 35 Tag = "exit"; 36 } 37 } 8 38 9 39 class Creature : PhysicsObject … … 19 49 public void Move(Direction direction) 20 50 { 21 Push(direction.GetVector() * MovementSpeed); 51 if (!Game.IsPaused) 52 Push(direction.GetVector() * MovementSpeed); 22 53 } 23 54 } … … 29 60 private Creature player; 30 61 62 private bool transition = false; 63 List<GameObject> oldObjects = new List<GameObject>(); 64 List<Exit> exits = new List<Exit>(); 65 31 66 public override void Begin() 32 67 { … … 38 73 { 39 74 ClearAll(); 40 CreateLevel("testlevel 2");75 CreateLevel("testlevel"); 41 76 CreatePlayer(new Vector(30, -30)); 42 77 SetControls(); … … 53 88 Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, null); 54 89 } 90 91 /// <summary> 92 /// Luo pelaajan. 93 /// </summary> 94 void CreatePlayer(Vector position) 95 { 96 player = new Creature(TILE_SIZE, TILE_SIZE); 97 player.MovementSpeed = 2300; 98 player.Position = position; 99 Add(player); 100 101 AddCollisionHandler(player, "exit", CollidesWithExit); 102 } 103 104 void CollidesWithExit(PhysicsObject pPlayer, PhysicsObject pExit) 105 { 106 var oldExit = pExit as Exit; 107 if (oldExit == null || transition) 108 return; 109 110 transition = true; 111 112 // Otetaan vanhat objektit talteen. 113 oldObjects.Clear(); 114 foreach (var obj in GetObjects(o => o != player)) 115 { 116 if (obj != player) 117 { 118 oldObjects.Add(obj); 119 } 120 } 121 122 // Luodaan seuraava kenttä. 123 exits.Clear(); 124 CreateLevel(oldExit.TargetLevel); 125 126 // Pysäytetään peli siirtymän ajaksi. 127 Pause(); 128 PhysicsEnabled = false; 129 130 // Etsitään seuraavan kentän kohde exitti johon siirrytään. 131 var targetExit = exits.FirstOrDefault(e => e.Name == oldExit.TargetExitName); 132 133 // Lasketaan siirtymävektorit. 134 Direction dir = GetExitDirection(targetExit); 135 Vector exitDelta = targetExit.Position - oldExit.Position; 136 Vector transitionVector = Level.Size; 137 if (dir == Direction.Left || dir == Direction.Right) 138 { 139 transitionVector.X *= dir.GetVector().X; 140 transitionVector.Y = exitDelta.Y; 141 } 142 else 143 { 144 transitionVector.Y *= dir.GetVector().Y; 145 transitionVector.X = exitDelta.X; 146 } 147 148 // Siirretään vanhoja objekteja ja pelaajaa. 149 foreach (var obj in oldObjects) 150 { 151 obj.Position += transitionVector; 152 } 153 player.Position += transitionVector + Direction.Inverse(dir).GetVector() * TILE_SIZE * 4; 154 155 // Zoomataan kameraa sopivasti ja liikutetaan se vanhan kentän päälle, josta se sitten siirtyy uuden kentän päälle. 156 Camera.Position += transitionVector; 157 Vector pos = Camera.Position; 158 Camera.ZoomToLevel(); 159 Camera.Position = pos; 160 } 161 162 /// <summary> 163 /// Palauttaa suunnan millä puolella kenttää uloskäynti on. 164 /// </summary> 165 Direction GetExitDirection(Exit exit) 166 { 167 const double epsilon = 1e-3; 168 Func<double, double, bool> isSame = (x, y) => Math.Abs(y - x) < epsilon; 169 170 if (isSame(exit.Top, Level.Top)) 171 { 172 return Direction.Up; 173 } 174 if (isSame(exit.Bottom, Level.Bottom)) 175 { 176 return Direction.Down; 177 } 178 if (isSame(exit.Left, Level.Left)) 179 { 180 return Direction.Left; 181 } 182 if (isSame(exit.Right, Level.Right)) 183 { 184 return Direction.Right; 185 } 186 return Direction.None; 187 } 188 189 protected override void PausedUpdate(Time gameTime) 190 { 191 base.PausedUpdate(gameTime); 192 double dt = gameTime.SinceLastUpdate.TotalSeconds; 193 194 const double transitionSpeed = 3.0; 195 196 if (transition) 197 { 198 Camera.Position += Camera.Position * -transitionSpeed * dt; 199 200 // Siirtymä on ohi, jatketaan peliä ja poistetaan edellinen kenttä. 201 if (Camera.Position.Magnitude < 1.0) 202 { 203 transition = false; 204 Pause(); 205 PhysicsEnabled = true; 206 Camera.Position = Vector.Zero; 207 208 foreach (var obj in oldObjects) 209 { 210 obj.Destroy(); 211 } 212 oldObjects.Clear(); 213 } 214 } 215 } 55 216 } -
2015/27/ohjaajat/TheLegendOfGabriel/TheLegendOfGabriel/TheLegendOfGabriel/TiledTileMap.cs
r6538 r6539 11 11 { 12 12 public delegate void ObjectMethod( 13 Vector position, double width, double height, Angle angle, Shape shape, Dictionary<string, string> properties);13 Vector position, double width, double height, Angle angle, Shape shape, string name, Dictionary<string, string> properties); 14 14 15 15 public delegate void TileMethod( … … 102 102 var len = Math.Sqrt(Math.Pow(obj.Width * 0.5, 2) + Math.Pow(obj.Height * 0.5, 2)); 103 103 var positionFix = Vector.FromLengthAndAngle(len, angle + Angle.FromRadians(-Math.Atan2(obj.Height, obj.Width))); 104 method(topLeft + positionFix, obj.Width, obj.Height, angle, shape, obj. Properties);104 method(topLeft + positionFix, obj.Width, obj.Height, angle, shape, obj.Name, obj.Properties); 105 105 } 106 106 } -
2015/27/ohjaajat/TheLegendOfGabriel/TheLegendOfGabriel/TheLegendOfGabrielContent/testlevel.tmx
r6538 r6539 8 8 </layer> 9 9 <objectgroup color="#65e2d8" name="exit"> 10 <object id="1" x="60" y="0" width="60" height="20">10 <object id="1" name="top" x="60" y="0" width="60" height="20"> 11 11 <properties> 12 12 <property name="goto" value="testlevel2@bottom"/> -
2015/27/ohjaajat/TheLegendOfGabriel/TheLegendOfGabriel/TheLegendOfGabrielContent/testlevel2.tmx
r6538 r6539 8 8 </layer> 9 9 <objectgroup color="#25cbc6" name="exit"> 10 <object id="2" x="260" y="280" width="60" height="20"/> 10 <object id="2" name="bottom" x="260" y="280" width="60" height="20"> 11 <properties> 12 <property name="goto" value="testlevel@top"/> 13 </properties> 14 </object> 11 15 </objectgroup> 12 16 </map>
Note: See TracChangeset
for help on using the changeset viewer.