Behaviors

Behaviors are arbitrary actions that can be attached to any Element2D class (scene layer, scene elements, etc.). For example, you can attach a rotation behavior to make a game element rotate over time or an outline behavior to draw an object's outline.

Using Behaviors

Let's try adding one of the built-in Xen behaviors, MouseScrollZoomBehavior. This behavior adds the ability to zoom in and out of your view with the mouse scroll.
    public class GameMain : GameBase
    {
        protected override void LoadContent()
        {
            // ... code ...
            MouseScrollZoomBehavior.AcquireAndAttach( MainScene );
            base.LoadContent();
        }
    }
And that's it! You can now zoom in and out of the game's MainScene layer with your mouse scroll.

Creating your own behavior

What does it take to create a rotation behavior for elements in my game? A class created from BehaviorBase<> with an UpdateInternal() override method.
    /// <summary>
    /// Behavior that rotates the target extent every update.
    /// </summary>
    class RotationBehavior : BehaviorBase<RotationBehavior>
    {
        private IRenderable2D _target;
        private float _rotationPerUpdate;

        public void Reset( IRenderable2D target, float rotationPerUpdate )
        {
            _target = target;
            _rotationPerUpdate = rotationPerUpdate;
        }

        protected override void ResetDirectState()
        {
            base.ResetDirectState();
            Visible = true;
            _target = null;
        }

        protected override void UpdateInternal( GameTime gameTime )
        {
            base.UpdateInternal( gameTime );
            _target.RenderingExtent.Angle += _rotationPerUpdate;
        }
    }
To add the behavior to our game element, we use the Behaviors collection in the element. This can be done inside your game:
    public class GameMain : GameBase
    {
        protected override void LoadContent()
        {
            // ... code ...
            _myGameElement.Behaviors.Add( RotationBehavior.Acquire() );
            base.LoadContent();
        }
    }
or it can be added as part of your element class:
    class CollidableElement : Element2D<CollidableElement>
    {
        public StaticSprite Sprite;
        private RotationBehavior _rotationBehavior;

        public static CollidableElement Acquire( Vector2 position, float rotation, Texture2D texture )
        {
            var instance = _pool.Acquire();
            instance._rotationBehavior.Reset( instance, rotation );
            instance.RenderingExtent.Anchor = position;
            instance.Sprite = StaticSprite.Acquire( texture );
            return instance;
        }
        public CollidableElement()
        {
            CollisionClass = CollisionClasses.Default;
            _rotationBehavior = RotationBehavior.Acquire();
        }
        public override void Reset()
        {
            VisualComponent = Sprite;
            base.Reset();
            Behaviors.Add( _rotationBehavior );
        }
    }

Last edited Feb 7, 2011 at 12:36 AM by twohalf, version 1

Comments

No comments yet.