示例#1
0
        public override void Update(GameObject gameObject, GameTime gameTime)
        {
            _physics = gameObject.GetComponent <APhysics>();

            if (_physics == null)
            {
                return;
            }

            AnimationIndex nextIndex = currentlyPlaying;

            var     physics   = gameObject.GetComponent <TopDownPhysics>();
            Vector2 facingDir = new Vector2(physics.facingDirection.X, physics.facingDirection.Y);

            // Only update the variable if the X facing side changes.
            _previousSide = facingDir.X != 0 ? facingDir.X : _previousSide;

            // Running animation.
            if (_physics.movingDirection.X < 0)
            {
                nextIndex = AnimationIndex.RUN_LEFT;
            }
            else if (_physics.movingDirection.X > 0)
            {
                nextIndex = AnimationIndex.RUN_RIGHT;
            }
            else if (_physics.movingDirection.Y < 0)
            {
                nextIndex = AnimationIndex.RUN_UP;
            }
            else if (_physics.movingDirection.Y > 0)
            {
                nextIndex = AnimationIndex.RUN_DOWN;
            }
            // Idle animation.
            else if (_physics.facingDirection.X < 0 && _physics.movingDirection.X == 0)
            {
                nextIndex = AnimationIndex.IDLE_LEFT;
            }
            else if (_physics.facingDirection.X > 0 && _physics.movingDirection.X == 0)
            {
                nextIndex = AnimationIndex.IDLE_RIGHT;
            }
            else if (_physics.facingDirection.Y < 0 && _physics.movingDirection.Y == 0)
            {
                nextIndex = AnimationIndex.IDLE_UP;
            }
            else if (_physics.facingDirection.Y > 0 && _physics.movingDirection.Y == 0)
            {
                nextIndex = AnimationIndex.IDLE_DOWN;
            }

            if (GetRealValue(nextIndex) != currentlyPlaying)
            {
                currentlyPlaying = GetRealValue(nextIndex);
                _animations[(int)GetRealValue(currentlyPlaying)]?.Reset();
            }

            _animations[(int)GetRealValue(currentlyPlaying)]?.Update(gameObject, gameTime);
        }
 public Particle(APhysics physics, AGraphics graphics, Vector2 position, double lifetime) : base(position, physics, graphics)
 {
     this.lifetime = lifetime;
     // Only set task if lifetime is greater than zero. That way we can set lifetime to be
     // "forever" and set it to done when we see fit.
     if (lifetime >= 0)
     {
         Misc.TaskScheduler.AddTask(() => done = true, lifetime, lifetime, this.id);
     }
 }
        public PlayerStateMachine(Player player) : base(PlayerStates.Walking)
        {
            _player = player;

            //
            // Setup components for walking state.
            //

            _walkingInput    = new PlayerInput();
            _walkingPhysics  = new TopDownPhysics(_player.WalkSpeed, _player.WalkSpeed / 2, 0.9f);
            _walkingGraphics = new P8TopDownAnimator(P8TopDownAnimator.AnimationMode.SIDES_ONLY);
            ((P8TopDownAnimator)_walkingGraphics).RunLeft   = new SpriteAnimation(new P8Sprite(33, 1, 1, true, false), 4, 0.3f);
            ((P8TopDownAnimator)_walkingGraphics).IdleLeft  = new SpriteAnimation(new P8Sprite(32, 1, 1, true, false), 1, 0.3f);
            ((P8TopDownAnimator)_walkingGraphics).RunRight  = new SpriteAnimation(new P8Sprite(33, 1, 1, false, false), 4, 0.3f);
            ((P8TopDownAnimator)_walkingGraphics).IdleRight = new SpriteAnimation(new P8Sprite(32, 1, 1, false, false), 1, 0.3f);
        }