/// <summary>
 ///		Initializes a new instance of this class with the given tile data.
 /// </summary>
 /// <param name="data">Data that this segment should contain.</param>
 /// <param name="width">Width in tiles of this segment.</param>
 /// <param name="height">Height in tiles of this segment.</param>
 /// <param name="tileWidth">Width of each tile in this segment.</param>
 /// <param name="tileHeight">Height of each tile in this segment.</param>
 public TilemapSegmentNode(TileNode[,] data, int width, int height, int tileWidth, int tileHeight)
 {
     _tileData = data;
     _width = width;
     _height = height;
     _tileWidth = tileWidth;
     _tileHeight = tileHeight;
     _boundingRectangle.Width = width * tileWidth;
     _boundingRectangle.Height = height * tileHeight;
     _name = "Tile Map Segment";
     DeinitializeCollision(); // Tiles look after collision - For the moment.
 }
        /// <summary>
        ///		Loads the next node in the given binary reader and returns it.
        /// </summary>
        /// <param name="reader">Binary reader to read node from.</param>
        /// <param name="getProgress">If set to true this node is used to judge the current progress.</param>
        /// <returns>Scene node that was loaded from the binary reader.</returns>
        private SceneNode LoadNode(BinaryReader reader, bool getProgress)
        {
            // Read in the name of this node's class.
            string name = reader.ReadString();

            //HighPreformanceTimer timer = new HighPreformanceTimer();

            // Create a new instance of this entity and tell it to load itself.
            // (See if its a known object first as its quicker to get it without reflection)
            SceneNode node = null;
            if (name.ToLower().EndsWith("binaryphoenix.fusion.engine.entitys.entitynode"))
                node = new EntityNode();
            else if (name.ToLower().EndsWith("binaryphoenix.fusion.engine.entitys.scriptedentitynode"))
                node = new ScriptedEntityNode();
            else if (name.ToLower().EndsWith("binaryphoenix.fusion.engine.scenenode"))
                node = new SceneNode();
            else if (name.ToLower().EndsWith("binaryphoenix.fusion.engine.entitys.groupnode"))
                node = new GroupNode();
            else if (name.ToLower().EndsWith("binaryphoenix.fusion.engine.entitys.emitternode"))
                node = new EmitterNode();
            else if (name.ToLower().EndsWith("binaryphoenix.fusion.engine.entitys.tilemapsegmentnode"))
                node = new TilemapSegmentNode();
            else if (name.ToLower().EndsWith("binaryphoenix.fusion.engine.entitys.pathmarkernode"))
                node = new PathMarkerNode();
            else if (name.ToLower().EndsWith("binaryphoenix.fusion.engine.entitys.tilenode"))
                node = new TileNode();
            else
                node = (SceneNode)ReflectionMethods.CreateObject(name);

            //DebugLogger.WriteLog("Created scene graph node " + name + " in " + timer.DurationMillisecond + ".\n");
            //timer.Restart();

            // Load in this nodes details.
            node.UniqueID = (_uniqueIDTracker++);
            node.Load(reader);

            //DebugLogger.WriteLog("Loaded scene graph node " + name + " in " + timer.DurationMillisecond + ".\n");

            // Read in all the children of this node, and attach
            // them to this node.
            DebugLogger.IncreaseIndent();
            int childCount = reader.ReadInt32();
            for (int i = 0; i < childCount; i++)
            {
                SceneNode childNode = LoadNode(reader);
                node.AddChild(childNode);

                if (getProgress == true)
                    _loadingProgress = (int)(((float)(i + 1) / (float)childCount) * 100.0f);
            }
            DebugLogger.DecreaseIndent();

            return node;
        }
 public TilemapSegmentNode(int width, int height, int tileWidth, int tileHeight)
 {
     _tileData = new TileNode[width, height];
     _width = width;
     _height = height;
     _tileWidth = tileWidth;
     _tileHeight = tileHeight;
     _boundingRectangle.Width = width * tileWidth;
     _boundingRectangle.Height = height * tileHeight;
     _name = "Tile Map Segment";
     for (int x = 0; x < width; x++)
         for (int y = 0; y < height; y++)
         {
             _tileData[x, y] = new TileNode();
             _tileData[x, y].Position(x * _tileWidth, y * _tileHeight, _tileData[x, y].Transformation.Z);
             _tileData[x, y].Parent = this;
         }
     DeinitializeCollision(); // Tilemaps do not produce collision.
 }
        /// <summary>
        ///		Resizes this entity to the given dimensions (in pixels)
        /// </summary>
        /// <param name="width">Width to resize entity to.</param>
        /// <param name="height">Height to resize entity to.</param>
        public override void Resize(int width, int height)
        {
            width /= _tileWidth;
            height /= _tileHeight;
            if (width <= 0) width = 1;
            if (height <= 0) height = 1;

            TileNode[,] newData = new TileNode[width, height];
            if (_tileData != null)
                for (int x = 0; x < width; x++)
                    for (int y = 0; y < height; y++)
                    {
                        newData[x, y] = (x >= _width || y >= _height) ? new TileNode() : _tileData[x, y];
                        newData[x, y].Position(x * _tileWidth, y * _tileHeight, newData[x, y].Transformation.Z);
                        newData[x, y].Parent = this;
                    }

            _tileData = newData;
            _width = width;
            _height = height;
            _boundingRectangle.Width = _width * _tileWidth;
            _boundingRectangle.Height = _height * _tileHeight;
        }
        /// <summary>
        ///		Loads this tilemap segment node from a given binary reader.
        /// </summary>
        /// <param name="reader">Binary reader to load this tilemap segment node from.</param>
        public override void Load(BinaryReader reader)
        {
            // Load all the basic entity details.
            base.Load(reader);

            // Load all the tilemap specific details.
            _width = reader.ReadInt32();
            _height = reader.ReadInt32();
            _tileWidth = reader.ReadInt16();
            _tileHeight = reader.ReadInt16();

            // Load in all the tileset in this map.
            _tileData = new TileNode[_width, _height];
            for (int x = 0; x < _width; x++)
                for (int y = 0; y < _height; y++)
                {
                    _tileData[x, y] = new TileNode();
                    if (_tileData[x, y].Parent != null)
                        _tileData[x, y].Parent.RemoveChild(_tileData[x, y]);
                    _tileData[x, y].Parent = this;
                    SceneGraph.AttachNode(_tileData[x, y]);
                    _tileData[x, y].Load(reader);
                }
        }