Setting a tile on a tilelayer programmatically
by Thijs Sloesen · in Torque X 2D · 02/25/2009 (10:53 am) · 3 replies
Hello, I was wondering if someone could tell me what the "official" way is to set a tile programmatically on a tile layer. I can not find any example in the documentation. Below you will find my current code (which works), but I can not imagine setting a single tile image in a tile layer would require this much code. What am I missing?
My code to set a tile:
Following is the code behind this method:
And following some extension methods:
My code to set a tile:
SetTile(tileLayer, worldPos, 4);
Following is the code behind this method:
private void SetTile(T2DTileLayer tileLayer, Vector2 worldPos, int tileIndex)
{
T2DTileObject tileObject = tileLayer.PickTile(worldPos);
if (tileObject != null)
{
Vector2 gridPos = tileObject.GridPosition;
T2DTileObject newTileObject = (T2DTileObject)tileObject.Clone();
newTileObject.GridPosition = gridPos;
newTileObject.TileType = GetTileType(tileLayer, tileIndex, tileObject.TileType);
tileLayer.SetTileByGridCoords((int)gridPos.X, (int)gridPos.Y, newTileObject);
}
}
private T2DTileType GetTileType((T2DTileLayer tileLayer, int newTileIndex, T2DTileType tileTypeTemplate)
{
T2DTileType foundTileType = tileLayer.TileTypes.Find((Predicate<T2DTileType>)delegate(T2DTileType tileType) { return tileType.MaterialRegionIndex == newTileIndex; });
if (foundTileType == null)
{
// Easy way - clone from an existing object
foundTileType = tileTypeTemplate.Clone();
foundTileType.MaterialRegionIndex = newTileIndex;
foundTileType.Index = tileLayer.TileTypes.MaxIndex() + 1;
foundTileType.Name = string.Format("{0}{1}", foundTileType.Name, foundTileType.Index); // Make sure name is unique, documentation does not specify if this is required
tileLayer.TileTypes.Add(foundTileType);
}
return foundTileType;
}And following some extension methods:
static class LocalExtensions
{
public static T2DTileType Clone(this T2DTileType original)
{
T2DTileType obj = new T2DTileType();
obj._material = original._material;
obj.CollisionPolyBasis = original.CollisionPolyBasis;
obj.CollisionsEnabled = original.CollisionsEnabled;
obj.Index = original.Index;
obj.Material = original.Material;
obj.MaterialRegionIndex = original.MaterialRegionIndex;
obj.ObjectType = original.ObjectType;
return obj;
}
public static int MaxIndex(this List<T2DTileType> list)
{
int max = 0;
foreach (T2DTileType tileType in list)
{
max = (tileType.Index > max ? tileType.Index : max);
}
return max;
}
}
#2
02/28/2009 (1:54 pm)
T2DTileObject _grabbedTile; T2DTileLayer _aTileLayer, _anotherTileLayer; int x1, y1, x2, y2; _grabbedTile = _aTileLayer.GetTileByGridCoords(x1, y1); _anotherTileLayer.SetTileByGridCoords(x2, y2, _grabbedTile);
#3
02/28/2009 (2:09 pm)
If you want to put an image into a tilemap:T2DTileType _tyleType = new T2DTileType();
_tyleType.Material = TorqueObjectDatabase.Instance.FindObject
<GarageGames.Torque.Materials.RenderMaterial>
("YourMaterial");
T2DTileObject _yourTile = new T2DTileObject();
_yourTile.TileType = _tyleType;
_yourPreviouslyCreatedTilemap.SetTileByGridCoords
(x, y, _yourTile);
TorqueObjectDatabase.Instance.Register
(_yourPreviouslyCreatedTilemap);
Torque Owner Thijs Sloesen
Scattered Studio