Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using Microsoft.Xna.Framework;
- using SMLimitless;
- namespace SmlSprites.ComponentsSample
- {
- public sealed class WalkerComponent : SpriteComponent
- {
- private Vector2 walkingVelocity; // defined in pixels per second
- private bool turnOnCliffs;
- private Direction currentDirection;
- public WalkerComponent(Sprite owner, Vector2 walkingVelocity, bool turnOnCliffs, Direction startingDirection)
- : base(owner)
- {
- this.walkingVelocity = walkingVelocity;
- this.turnOnCliffs = turnOnCliffs;
- if (startingDirection == Direction.Left || startingDirection == Direction.Right)
- {
- this.currentDirection = startingDirection;
- }
- else
- {
- throw new ArgumentException(string.Format("SmlSprites.Components.WalkerComponent.ctor(Sprite, Vector2, bool, Direction): Invalid direction {0}. Please use Left or Right.", startingDirection), "startingDirection");
- }
- }
- public override void Update()
- {
- float delta = GameServices.GameTime.GetElapsedMilliseconds();
- Vector2 moveDistance = this.walkingVelocity * delta;
- Vector2 newPosition = this.owner.Position + (this.currentDirection == Direction.Right ? moveDistance : -moveDistance);
- // Check if we're about to fall off a cliff by seeing if there's a block under our new position
- if (this.turnOnCliffs)
- {
- Vector2 newBottomCenter = new Vector2(newPosition.X + (owner.Size.X / 2f), newPosition.Y + (new.Size.Y));
- newBottomCenter.Y++;
- if (this.ownerLevel.GetBlockAt(newBottomCenter) == null)
- {
- // there's no block beneath us
- if (this.currentDirection == Direction.Left)
- {
- this.currentDirection = Direction.Right;
- }
- else if (this.currentDirection == Direction.Right)
- {
- this.currentDirection = Direction.Left;
- }
- }
- }
- }
- public override void HandleTileCollision(Tile tile, Intersection intersect)
- {
- if (tile.CollisionType != TileCollisionType.Passive && intersect.IsIntersecting)
- {
- if (intersect.Direction == Direction.Left && (tile.CollisionType == TileCollisionType.Solid || tile.CollisionType == TileCollisionType.LeftSolid))
- {
- this.currentDirection = Direction.Left;
- }
- else if (intersect.Direction == Direction.Right && (tile.CollisionType == TileCollisionType.Solid || tile.CollisionType == TileCollisionType.RightSolid))
- {
- this.currentDirection = Direction.Right;
- }
- }
- }
- }
- public sealed class GreenKoopaComponent : SpriteComponent
- {
- private WalkerComponent walker;
- private string lastOwnerState;
- // The shell wait is the amount of time it takes for a shell to emerge after it's been stomped by a player.
- private const float maxShellWait = 10f;
- private float currentShellWait = 0f;
- public GreenKoopaComponent(Sprite owner, Vector2 walkingVelocity, Direction startingDirection) : base(owner)
- {
- this.walker = new WalkerComponent(owner, walkingVelocity, false, startingDirection);
- }
- public override void Update()
- {
- float delta = GameServices.GameTime.GetElapsedSeconds();
- if (this.ownerState == "default")
- {
- // We're in the default state, so we just have to update the walker component.
- this.walker.Update();
- }
- else if (this.ownerState == "shell" && this.lastOwnerState == "default")
- {
- // We've been stomped on this frame, so we need to start our shell wait.
- this.currentShellWait = this.maxShellWait;
- }
- else if (this.ownerState == "shell")
- {
- // We're still in the shell, so we just need to update the shell wait.
- this.currentShellWait -= delta;
- }
- this.lastOwnerState = owner.State;
- }
- public override void HandleTileCollision()
- {
- // We don't need any more of a special response than what walker components do.
- this.walker.Update();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement