Introduction

Objects in Engine Nine is grouped into a tree structure called scene tree. The root node of the tree should always be a Scene. A node in the scene tree can be an arbitrary .NET object. But in order for a node to contain child nodes, the type of that node has to implement Nine.IContainer, which contains a list of child nodes:

public interface IContainer
{
    IList Children { get; }
}

In order to get the parent node, the object can implement Nine.IComponent interface, which contains a property to access parent container object:

public interface IComponent
{
    IContainer Parent { get; set; }
}

There are several built-in types of containers and components: Group, Scene, Transformable, Component. The functions of these special nodes are described below.

 

Transform Hierarchy

A Group in Engine Nine is used to combine objects into a single object. A group can contain any type of object. In fact, scene is derived from group. When group and transformable are used together, they created a transform hierarchy. The following XAML markup shows an example of 2D sprite hierarchy. The same concept can be extended to 3D space.

image
<Sprite Texture="{ContentReference Assets/Arrow}" />
<Group Transform2D.Rotation="{Degrees -45}" Transform2D.Position="50,120">
    <Sprite Texture="{ContentReference Assets/Arrow}" />
    <Sprite Texture="{ContentReference Assets/Arrow}" Transform2D.Rotation="{Degrees -45}" Transform2D.Position="50,120" />
</Group>

 

Component

Engine Nine uses a component based architecture to avoid the class explosion problems caused by using inheritance. A component can be a plain .NET object. The following example shows how to create a unit normally found in real-time strategy games:

<Group xmlns="http://schemas.microsoft.com/nine/2011/xaml" xmlns:g="clr-namespace:MyGame;assembly=MyGame">
    <Model Source="{ContentReference Model/Charactors/Zergling}" />
    <g:Unit HitPoint="1000" MaxHitPoint="1000" />
</Group>

Where the Unit class can be simply defined as:

public class Unit
{
    public int HitPoint { get; set; }
    public int MaxHitPoint { get; set; }
}

 

To manipulate the container object of a component, derive from Component class and implement appropriate interfaces.  Group also comes with a handful of methods to find the child nodes inside the group. The follow example shows a component that manipulates a physics object based on keyboard input.

public class ExamplePhysicsController : Component
{
    public float Speed { get; set; }

    protected override void Update(TimeSpan elapsedTime)
    {
        var Body = Parent.Find<RigidBody>();
        if (Keyboard.GetState().IsKeyDown(Keys.Left))
            Body.ApplyImpulse(Speed * Vector3.Left);
    }
}

Last edited Dec 11, 2012 at 9:03 AM by yufeih, version 13

Comments

No comments yet.