Changeset 6539 for 2015


Ignore:
Timestamp:
2015-06-29 18:18:31 (8 years ago)
Author:
sieerinn
Message:

Kenttäsiirtymä on melko valmis.

Location:
2015/27/ohjaajat/TheLegendOfGabriel/TheLegendOfGabriel
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • 2015/27/ohjaajat/TheLegendOfGabriel/TheLegendOfGabriel/TheLegendOfGabriel/LevelCreation.cs

    r6538 r6539  
    88{ 
    99    /// <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> 
    2110    /// Luo kentän .tmx tiedostosta. 
    2211    /// </summary> 
     
    2615        level.SetTileMethod("base", CreateBaseTile); 
    2716        level.SetTileMethod("foreground", CreateForegroundTile); 
     17        level.SetObjectMethod("exit", CreateExit); 
    2818        level.Execute(); 
    2919 
     
    5747        Add(tile, layer); 
    5848    } 
     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    } 
    5964} 
  • 2015/27/ohjaajat/TheLegendOfGabriel/TheLegendOfGabriel/TheLegendOfGabriel/TheLegendOfGabriel.cs

    r6538 r6539  
    11using System; 
    22using System.Collections.Generic; 
     3using System.Linq; 
    34using Jypeli; 
    45using Jypeli.Assets; 
     
    67using Jypeli.Effects; 
    78using Jypeli.Widgets; 
     9using Microsoft.Xna.Framework; 
     10using Physics2DDotNet; 
     11using Color = Jypeli.Color; 
     12 
     13sealed 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} 
    838 
    939class Creature : PhysicsObject 
     
    1949    public void Move(Direction direction) 
    2050    { 
    21         Push(direction.GetVector() * MovementSpeed); 
     51        if (!Game.IsPaused) 
     52            Push(direction.GetVector() * MovementSpeed); 
    2253    } 
    2354} 
     
    2960    private Creature player; 
    3061 
     62    private bool transition = false; 
     63    List<GameObject> oldObjects = new List<GameObject>(); 
     64    List<Exit> exits = new List<Exit>(); 
     65 
    3166    public override void Begin() 
    3267    { 
     
    3873    { 
    3974        ClearAll(); 
    40         CreateLevel("testlevel2"); 
     75        CreateLevel("testlevel"); 
    4176        CreatePlayer(new Vector(30, -30)); 
    4277        SetControls(); 
     
    5388        Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, null); 
    5489    } 
     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    } 
    55216} 
  • 2015/27/ohjaajat/TheLegendOfGabriel/TheLegendOfGabriel/TheLegendOfGabriel/TiledTileMap.cs

    r6538 r6539  
    1111{ 
    1212    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); 
    1414 
    1515    public delegate void TileMethod( 
     
    102102            var len = Math.Sqrt(Math.Pow(obj.Width * 0.5, 2) + Math.Pow(obj.Height * 0.5, 2)); 
    103103            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); 
    105105        } 
    106106    } 
  • 2015/27/ohjaajat/TheLegendOfGabriel/TheLegendOfGabriel/TheLegendOfGabrielContent/testlevel.tmx

    r6538 r6539  
    88 </layer> 
    99 <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"> 
    1111   <properties> 
    1212    <property name="goto" value="testlevel2@bottom"/> 
  • 2015/27/ohjaajat/TheLegendOfGabriel/TheLegendOfGabriel/TheLegendOfGabrielContent/testlevel2.tmx

    r6538 r6539  
    88 </layer> 
    99 <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> 
    1115 </objectgroup> 
    1216</map> 
Note: See TracChangeset for help on using the changeset viewer.