Ignore:
Timestamp:
2016-07-02 23:10:35 (3 years ago)
Author:
sieerinn
Message:

Dokumentaatiota ja CollisionIgnoreGroup?-ominaisuus lisätty

File:
1 edited

Legend:

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

    r7639 r7643  
    1111    public bool IsGrounded { get; private set; } 
    1212 
     13    public Direction FacingDirection { get; private set; } 
     14 
    1315    public SimplePlatformCharacter(double width, double height) : base(width, height) 
    1416    { 
     17    } 
     18 
     19    public void Walk(double speed) 
     20    { 
     21        if (speed > 0) FacingDirection = Direction.Right; 
     22        if (speed < 0) FacingDirection = Direction.Left; 
     23        Velocity = new Vector(speed, Velocity.Y); 
    1524    } 
    1625 
     
    1928        base.Update(time); 
    2029        IsGrounded = Game.Collisions(this, new Vector(0, -2), true).Count > 0; 
     30    } 
     31} 
     32 
     33class Enemy : SimplePlatformCharacter 
     34{ 
     35    public double Speed { get; set; } 
     36 
     37    public bool FallOffPlatforms { get; set; } 
     38 
     39    private bool walkRight; 
     40 
     41    //private double movement; 
     42 
     43    public Enemy(double width, double height) : base(width, height) 
     44    { 
     45    } 
     46 
     47    public override void Update(Time time) 
     48    { 
     49        //if (movement == 0) 
     50        //    movement = -Speed; 
     51 
     52        if (Game.Collisions(this, new Vector(2, 1), true).Count > 0) 
     53            walkRight = false; 
     54            //movement = -Speed; 
     55 
     56        if (Game.Collisions(this, new Vector(-2, 1), true).Count > 0) 
     57            walkRight = true; 
     58            //movement = Speed; 
     59 
     60        if (!FallOffPlatforms) 
     61        { 
     62            var bottomLeft = new Vector(Left, Bottom + 2); 
     63            var bottomRight = new Vector(Right, Bottom + 2); 
     64 
     65            if (Game.GetTileAt(Game.WorldToTilePosition(bottomRight)) == null) 
     66            { 
     67                walkRight = false; 
     68                //movement = -Speed; 
     69            } 
     70            if (Game.GetTileAt(Game.WorldToTilePosition(bottomLeft)) == null) 
     71            { 
     72                walkRight = true; 
     73                //movement = Speed; 
     74            } 
     75        } 
     76 
     77        //Velocity = new Vector(movement, Velocity.Y); 
     78        Walk(walkRight? Speed : -Speed); 
     79        base.Update(time); 
    2180    } 
    2281} 
     
    49108public class SimplePhysicsTest : SimplePhysics.Game 
    50109{ 
    51     private SimplePlatformCharacter player; 
     110    private SimplePlatformCharacter player1; 
    52111 
    53112    public SimplePhysicsTest() : base(tileSize: 30) 
     
    75134        Camera.ZoomToLevel(); 
    76135 
    77         Keyboard.Listen(Key.Right, ButtonState.Down, Walk, null, player, 200.0); 
    78         Keyboard.Listen(Key.Left, ButtonState.Down, Walk, null, player, -200.0); 
    79         Keyboard.Listen(Key.Up, ButtonState.Down, Jump, null, player); 
     136        Keyboard.Listen(Key.Right, ButtonState.Down, Walk, null, player1, 200.0); 
     137        Keyboard.Listen(Key.Left, ButtonState.Down, Walk, null, player1, -200.0); 
     138        Keyboard.Listen(Key.Up, ButtonState.Down, Jump, null, player1); 
     139        Keyboard.Listen(Key.Space, ButtonState.Pressed, Shoot, null, player1); 
     140        Keyboard.Listen(Key.B, ButtonState.Down, MegaShoot, null, player1); 
    80141        Keyboard.Listen(Key.R, ButtonState.Pressed, () => { ClearAll(); Begin(); }, "Lopeta peli"); 
    81142        Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli"); 
     
    121182    private void CreatePlayer(int x, int y) 
    122183    { 
    123         player = new SimplePlatformCharacter(TileSize * 0.98, TileSize * 1.98); 
    124         player.Position = TileToWorldPosition(x, y); 
    125         player.Tag = "player"; 
    126         player.Crushed += player.Destroy; 
    127         Add(player); 
    128  
    129         player.AddCollisionHandler("enemy", HitsEnemy); 
    130         player.AddCollisionHandler("death", (a, b) => player.Destroy()); 
     184        player1 = new SimplePlatformCharacter(TileSize * 0.98, TileSize * 1.98); 
     185        player1.Position = TileToWorldPosition(x, y); 
     186        player1.Tag = "player"; 
     187        player1.Crushed += player1.Destroy; 
     188        Add(player1); 
     189 
     190        player1.AddCollisionHandler("enemy", HitsEnemy); 
     191        player1.AddCollisionHandler("death", (a, b) => player1.Destroy()); 
    131192    } 
    132193 
     
    140201    private void CreateEnemy(int x, int y) 
    141202    { 
    142         var enemy = new SimplePhysics.Object(TileSize * 0.99, TileSize * 0.99); 
     203        var enemy = new Enemy(TileSize * 0.99, TileSize * 0.99); 
    143204        enemy.Position = TileToWorldPosition(x, y); 
    144205        enemy.Tag = "enemy"; 
     206        enemy.Speed = 50; 
     207        enemy.FallOffPlatforms = true; 
    145208        Add(enemy); 
    146209    } 
     
    148211    #endregion 
    149212 
    150     private void Walk(SimplePhysics.Object obj, double speed) 
    151     { 
    152         player.Velocity = new Vector(speed, player.Velocity.Y); 
     213    private void Walk(SimplePlatformCharacter player, double speed) 
     214    { 
     215        //player1.Velocity = new Vector(speed, player1.Velocity.Y); 
     216        player.Walk(speed); 
    153217    } 
    154218 
     
    156220    { 
    157221        if (obj.IsGrounded) 
    158             player.Velocity = new Vector(player.Velocity.X, 500); 
     222            player1.Velocity = new Vector(player1.Velocity.X, 500); 
     223    } 
     224     
     225    private SimplePhysics.Object CreateProjectile(SimplePlatformCharacter player) 
     226    { 
     227        var direction = player.FacingDirection.GetVector().X; 
     228        var proj = new SimplePhysics.Object(10, 10); 
     229        proj.Position = new Vector(player.X + direction * TileSize, player.Y); 
     230        proj.RestitutionX = 1.0; 
     231        proj.RestitutionY = 0.7; 
     232        proj.Friction = 0.2; 
     233        proj.Tag = "proj"; 
     234        proj.CollisionIgnoreGroup = 1; 
     235        proj.MaximumLifetime = TimeSpan.FromSeconds(3); 
     236        Add(proj); 
     237 
     238        if (Collisions(proj, Vector.Zero, true).Count > 0) 
     239        { 
     240            proj.Destroy(); 
     241        } 
     242 
     243        proj.AddCollisionHandler((a, b) => 
     244        { 
     245            if (b is SimplePlatformCharacter) 
     246            { 
     247                proj.Destroy(); 
     248            } 
     249        }); 
     250 
     251        return proj; 
     252    } 
     253 
     254    private void Shoot(SimplePlatformCharacter player) 
     255    { 
     256        var proj = CreateProjectile(player); 
     257        var direction = player.FacingDirection.GetVector().X; 
     258        proj.Velocity = player.Velocity + new Vector(direction * 200, 100); 
     259    } 
     260 
     261    private void MegaShoot(SimplePlatformCharacter player) 
     262    { 
     263        for (int i = 0; i < 3; i++) 
     264        { 
     265            var proj = CreateProjectile(player); 
     266            var direction = player.FacingDirection.GetVector().X; 
     267            proj.Velocity = player.Velocity + new Vector(direction * 200, 100) + RandomGen.NextVector(0, 100); 
     268        } 
    159269    } 
    160270 
    161271    private void HitsEnemy(SimplePhysics.Object player, SimplePhysics.Object enemy) 
    162272    { 
    163         //player.Destroy(); 
    164         enemy.Destroy(); 
     273        //player1.Destroy(); 
     274        //enemy.Destroy(); 
    165275    } 
    166276 
Note: See TracChangeset for help on using the changeset viewer.