Changeset 7649


Ignore:
Timestamp:
2016-07-03 10:00:22 (7 years ago)
Author:
sieerinn
Message:

Kama jonka voi poimia ja heittää

Location:
2016/27/SimoR/SimplePhysicsTest/SimplePhysicsTest
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • 2016/27/SimoR/SimplePhysicsTest/SimplePhysicsTest/SimplePhysicsTest/SimplePhysicsTest.cs

    r7648 r7649  
    1515    public SimplePlatformCharacter(double width, double height) : base(width, height) 
    1616    { 
     17        FacingDirection = Direction.Right; 
    1718    } 
    1819 
     
    3637 
    3738    public bool Crouch { get; set; } 
     39 
     40    public SimplePhysics.Object Carrying { get; set; } 
    3841 
    3942    private readonly double fullHeight; 
     
    163166        tilemap.SetTileMethod(Color.FromHexCode("FFB949"), CreateDestroyableBrick); 
    164167        tilemap.SetTileMethod(Color.FromHexCode("FFFF00"), CreateCoin); 
     168        tilemap.SetTileMethod(Color.FromHexCode("BCB2FF"), CreateTestItem); 
    165169        tilemap.SetTileMethod(Color.Blue, CreatePlayer); 
    166170        tilemap.SetTileMethod(Color.Red, CreateEnemy); 
     
    175179        Keyboard.Listen(Key.Down, ButtonState.Pressed, Crouch, null, player1, true); 
    176180        Keyboard.Listen(Key.Down, ButtonState.Released, Crouch, null, player1, false); 
    177         Keyboard.Listen(Key.Space, ButtonState.Pressed, Shoot, null, player1); 
    178         Keyboard.Listen(Key.B, ButtonState.Down, MegaShoot, null, player1); 
     181        Keyboard.Listen(Key.Space, ButtonState.Pressed, PickupThrow, null, player1); 
     182        Keyboard.Listen(Key.Z, ButtonState.Pressed, Shoot, null, player1); 
     183        Keyboard.Listen(Key.X, ButtonState.Down, MegaShoot, null, player1); 
    179184        Keyboard.Listen(Key.R, ButtonState.Pressed, () => { ClearAll(); Begin(); }, "Lopeta peli"); 
    180185        Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli"); 
     
    208213        SetTile(x, y, tile); 
    209214 
     215        // Tiili hajoaa jos pelaaja törmää siihen alapuolelta. 
    210216        tile.AddCollisionHandler("player", (t, player) => 
    211217        { 
     
    242248        player1.Tag = "player"; 
    243249        player1.Crushed += player1.Destroy; 
     250        player1.CollisionIgnoreGroup = 1; 
    244251        Add(player1); 
     252 
     253        // Ajastin, joka pitää kannossa olevan kaman pään päällä. 
     254        var timer = new Timer { Interval = 0.01 }; 
     255        timer.Timeout += () => 
     256        { 
     257            if (player1.Carrying != null) 
     258            { 
     259                player1.Carrying.X = player1.X; 
     260                player1.Carrying.Bottom = player1.Top; 
     261            } 
     262        }; 
     263        timer.Start(); 
     264 
     265        // Kuollessa pudotetaan kannossa ollut kama. 
     266        player1.Destroyed += () => 
     267        { 
     268            timer.Stop(); 
     269            if (player1.Carrying != null) 
     270            { 
     271                player1.Carrying.Velocity = player1.Velocity; 
     272                player1.Carrying.IgnoresGravity = false; 
     273                player1.Carrying = null; 
     274            } 
     275        }; 
    245276 
    246277        player1.AddCollisionHandler("enemy", HitsEnemy); 
     
    260291        var enemy = new Enemy(TileSize * 0.99, TileSize * 0.99); 
    261292        enemy.Position = TileToWorldPosition(x, y); 
     293        enemy.Color = Color.Purple; 
    262294        enemy.Tag = "enemy"; 
    263295        enemy.Speed = 50; 
     
    272304        coin.Tag = "coin"; 
    273305        coin.Color = Color.Yellow; 
     306        coin.Shape = Shape.Star; 
    274307        coin.IgnoresCollisionResponse = true; 
    275308        coin.IgnoresGravity = true; 
    276309        Add(coin); 
     310    } 
     311 
     312    private void CreateTestItem(int x, int y) 
     313    { 
     314        var item = new SimplePhysics.Object(TileSize * 0.99, TileSize * 0.99); 
     315        item.Position = TileToWorldPosition(x, y); 
     316        item.Tag = "item"; 
     317        item.Color = Color.DarkGreen; 
     318        item.Shape = Shape.Circle; 
     319        item.RestitutionX = 0.5; 
     320        item.RestitutionY = 0.5; 
     321        item.CollisionIgnoreGroup = 1; 
     322        Add(item); 
    277323    } 
    278324 
     
    324370    } 
    325371 
     372    private void PickupThrow(Player player) 
     373    { 
     374        // Heitetään kannossa oleva kama. 
     375        if (player.Carrying != null) 
     376        { 
     377            if (Collisions(player.Carrying, Vector.Zero, true).Count == 0) 
     378            { 
     379                player.Carrying.IgnoresGravity = false; 
     380                player.Carrying.Velocity = new Vector(player.Velocity.X * 2, player.Velocity.Y + 100); 
     381                player.Carrying = null; 
     382            } 
     383        } 
     384        else // Tai sitten poimitaan lähellä oleva kama. 
     385        { 
     386            var items = GetObjectsWithTag("item"); 
     387            foreach (var item in items) 
     388            { 
     389                if (player.IntersectsWith(item)) 
     390                { 
     391                    player.Carrying = (SimplePhysics.Object)item; 
     392                    player.Carrying.IgnoresGravity = true; 
     393                    break; 
     394                } 
     395            } 
     396        } 
     397    } 
     398 
    326399    private void Shoot(SimplePlatformCharacter player) 
    327400    { 
Note: See TracChangeset for help on using the changeset viewer.