Advertisement
smc_gamer

SML Sprite Component Sample

Jun 24th, 2013
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.80 KB | None | 0 0
  1. using System;
  2. using Microsoft.Xna.Framework;
  3. using SMLimitless;
  4.  
  5. namespace SmlSprites.ComponentsSample
  6. {
  7.     public sealed class WalkerComponent : SpriteComponent
  8.     {
  9.         private Vector2 walkingVelocity; // defined in pixels per second
  10.         private bool turnOnCliffs;
  11.         private Direction currentDirection;
  12.        
  13.         public WalkerComponent(Sprite owner, Vector2 walkingVelocity, bool turnOnCliffs, Direction startingDirection)
  14.         : base(owner)
  15.         {
  16.             this.walkingVelocity = walkingVelocity;
  17.             this.turnOnCliffs = turnOnCliffs;
  18.             if (startingDirection == Direction.Left || startingDirection == Direction.Right)
  19.             {
  20.                 this.currentDirection = startingDirection;
  21.             }
  22.             else
  23.             {
  24.                 throw new ArgumentException(string.Format("SmlSprites.Components.WalkerComponent.ctor(Sprite, Vector2, bool, Direction): Invalid direction {0}. Please use Left or Right.", startingDirection), "startingDirection");
  25.             }
  26.         }
  27.        
  28.         public override void Update()
  29.         {
  30.             float delta = GameServices.GameTime.GetElapsedMilliseconds();
  31.             Vector2 moveDistance = this.walkingVelocity * delta;
  32.             Vector2 newPosition = this.owner.Position + (this.currentDirection == Direction.Right ? moveDistance : -moveDistance);
  33.            
  34.             // Check if we're about to fall off a cliff by seeing if there's a block under our new position
  35.             if (this.turnOnCliffs)
  36.             {
  37.                 Vector2 newBottomCenter = new Vector2(newPosition.X + (owner.Size.X / 2f), newPosition.Y + (new.Size.Y));
  38.                 newBottomCenter.Y++;
  39.                 if (this.ownerLevel.GetBlockAt(newBottomCenter) == null)
  40.                 {
  41.                     // there's no block beneath us
  42.                     if (this.currentDirection == Direction.Left)
  43.                     {
  44.                         this.currentDirection = Direction.Right;
  45.                     }
  46.                     else if (this.currentDirection == Direction.Right)
  47.                     {
  48.                         this.currentDirection = Direction.Left;
  49.                     }
  50.                 }
  51.             }
  52.         }
  53.        
  54.         public override void HandleTileCollision(Tile tile, Intersection intersect)
  55.         {
  56.             if (tile.CollisionType != TileCollisionType.Passive && intersect.IsIntersecting)
  57.             {
  58.                 if (intersect.Direction == Direction.Left && (tile.CollisionType == TileCollisionType.Solid || tile.CollisionType == TileCollisionType.LeftSolid))
  59.                 {
  60.                     this.currentDirection = Direction.Left;
  61.                 }
  62.                 else if (intersect.Direction == Direction.Right && (tile.CollisionType == TileCollisionType.Solid || tile.CollisionType == TileCollisionType.RightSolid))
  63.                 {
  64.                     this.currentDirection = Direction.Right;
  65.                 }
  66.             }
  67.         }
  68.     }
  69.  
  70.     public sealed class GreenKoopaComponent : SpriteComponent
  71.     {
  72.         private WalkerComponent walker;
  73.         private string lastOwnerState;
  74.        
  75.         // The shell wait is the amount of time it takes for a shell to emerge after it's been stomped by a player.
  76.         private const float maxShellWait = 10f;
  77.         private float currentShellWait = 0f;
  78.        
  79.         public GreenKoopaComponent(Sprite owner, Vector2 walkingVelocity, Direction startingDirection) : base(owner)
  80.         {
  81.             this.walker = new WalkerComponent(owner, walkingVelocity, false, startingDirection);
  82.         }
  83.        
  84.         public override void Update()
  85.         {
  86.             float delta = GameServices.GameTime.GetElapsedSeconds();
  87.            
  88.             if (this.ownerState == "default")
  89.             {
  90.                 // We're in the default state, so we just have to update the walker component.
  91.                 this.walker.Update();
  92.             }
  93.             else if (this.ownerState == "shell" && this.lastOwnerState == "default")
  94.             {
  95.                 // We've been stomped on this frame, so we need to start our shell wait.
  96.                 this.currentShellWait = this.maxShellWait;
  97.             }
  98.             else if (this.ownerState == "shell")
  99.             {
  100.                 // We're still in the shell, so we just need to update the shell wait.
  101.                 this.currentShellWait -= delta;
  102.             }
  103.        
  104.             this.lastOwnerState = owner.State;
  105.         }
  106.        
  107.         public override void HandleTileCollision()
  108.         {
  109.             // We don't need any more of a special response than what walker components do.
  110.             this.walker.Update();
  111.         }
  112.     }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement