NodeNetworks are a collection of PositionedNodes which are linked to each other using Links. NodeNetworks are used for pathfinding.
Creating a NodeNetwork in Code
The following code creates a simple NodeNetwork and a Sprite. Pressing the 1, 2, 3, or 4 keys causes the Sprite to move toward a given node on the NodeNetwork.
// Instantiate the NodeNetwork. nodeNetwork =newNodeNetwork(); // Create the 4 nodes.PositionedNode node =nodeNetwork.AddNode();node.Position=newVector3(-150,150,0); // top left node =nodeNetwork.AddNode();node.Position=newVector3(150,150,0); // top right node =nodeNetwork.AddNode();node.Position=newVector3(150,-150,0); // bottom right node =nodeNetwork.AddNode();node.Position=newVector3(-150,-150,0); // bottom left // Link the nodes together. // LinkTo creates two links - one to and one from.float linkCost =1;nodeNetwork.Nodes[0].LinkTo(nodeNetwork.Nodes[1], linkCost);nodeNetwork.Nodes[1].LinkTo(nodeNetwork.Nodes[2], linkCost);nodeNetwork.Nodes[2].LinkTo(nodeNetwork.Nodes[3], linkCost);nodeNetwork.Nodes[3].LinkTo(nodeNetwork.Nodes[0], linkCost); // Make the NodeNetwork visible. Usually only used for debugging // and development.nodeNetwork.Visible=true; // Create the Sprite used to move around the NodeNetwork sprite =SpriteManager.AddSprite(RedBallTexture);sprite.TextureScale=1;
Add the following in CustomActivity:
FlatRedBall.Input.Keyboard keyboard =InputManager.Keyboard; // Press 1, 2, 3, or 4 to set the Sprite's path for the target node.if (keyboard.KeyPushed(Keys.D1)) { nodePath =nodeNetwork.GetPath(refsprite.Position,refnodeNetwork.Nodes[0].Position); }if (keyboard.KeyPushed(Keys.D2)) { nodePath =nodeNetwork.GetPath(refsprite.Position,refnodeNetwork.Nodes[1].Position); }if (keyboard.KeyPushed(Keys.D3)) { nodePath =nodeNetwork.GetPath(refsprite.Position,refnodeNetwork.Nodes[2].Position); }if (keyboard.KeyPushed(Keys.D4)) { nodePath =nodeNetwork.GetPath(refsprite.Position,refnodeNetwork.Nodes[3].Position); } // Progress towards the goal.if (nodePath !=null&&nodePath.Count!=0) {PositionedNode node =nodePath[0];if ((sprite.Position-node.Position).Length() <.1f) {nodePath.RemoveAt(0);sprite.Velocity=newVector3(); }else {float magnitude =50;var directionToMove = (node.Position-sprite.Position).AtLength(50);sprite.Velocity= directionToMove; } }
Loading a NodeNetwork From .nntx
The .nntx file format is a standard way to define node networks; however, there is currently no built-in .nntx creating tool supported. Therefore, apps which are interested in working with nntx will need to create their own .nntx files by using the NodeNetworkSave class.
If you have an existing .nntx you can load it from-file by adding it to the FlatRedBall Editor or to Visual Studio. If you add it directly to Visual Studio, you must manually load the file:
Add the file to your project in Visual Studio
Select the .nntx file once it's in the Solution Explorer.
Press F4 or right click and select Properties
Select "None" for the Build Action.
Select "Copy if newer" for the "Copy to Output Directory".