Adam's Lair Forum

game development and casual madness
It is currently 2017/11/18, 19:30

All times are UTC + 1 hour [ DST ]




Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: 2017/10/15, 22:25 
Junior Member
Junior Member

Joined: 2016/08/03, 22:07
Posts: 45
Location: Florida
Role: Hobbyist
To select a tile from a tile map, do I need to add a rigid body to each tile (if that's even possible) and check for collision on click?

Is there something built in that I'm missing for this?

My current path is to grab the mouse coordinates when it's clicked, convert to world coordinates, then determine what tile is clicked by subtracting the X of the tile map transform and dividing that by the width of the tilemap to get the X index. Repeat for Y index... However, I'm struggling to get meaningful info from the tile map transform regarding its positioning.


Top
 Profile  
 
PostPosted: 2017/10/16, 03:39 
Junior Member
Junior Member

Joined: 2016/08/03, 22:07
Posts: 45
Location: Florida
Role: Hobbyist
I'm pretty sure at this point there's nothing built in but I wanted to share my code in case someone else is searching and would like to use it:
Code:
Tilemap tilesGameBoard;
Camera camera;

public void OnInit(InitContext context)
{
    if (context == InitContext.Activate)
    {
        //Get a reference the to the game board
        tilesGameBoard = Scene.Current.FindComponent<Tilemap>(true);
        camera = Scene.Current.FindComponent<Camera>(true);
    }
}

public void OnUpdate()
{
    if(DualityApp.Mouse.ButtonHit(Duality.Input.MouseButton.Left))
    {             
        Vector2 tileIndex = GetTilePosFromWorldPos(camera.GetSpaceCoord(DualityApp.Mouse.Pos).Xy);
        Tile clickedTile = tilesGameBoard.Tiles[(int)tileIndex.X, (int)tileIndex.Y];
    }
}

private Point2 GetTilePosFromWorldPos(Vector2 worldPos)
{
    float gridMinX = tilesGameBoard.GameObj.Transform.Pos.X - (tilesGameBoard.Size.X * ((Tileset)tilesGameBoard.Tileset).TileSize.X) / 2;
    float gridMinY = tilesGameBoard.GameObj.Transform.Pos.Y - (tilesGameBoard.Size.Y * ((Tileset)tilesGameBoard.Tileset).TileSize.Y) / 2;

   int xTileIndex = (int)((worldPos.X - gridMinX) / ((Tileset)tilesGameBoard.Tileset).TileSize.X);
   int yTileIndex = (int)((worldPos.Y - gridMinY) / ((Tileset)tilesGameBoard.Tileset).TileSize.Y);

   return new Point2(xTileIndex, yTileIndex);
}


Top
 Profile  
 
PostPosted: 2017/10/17, 19:47 
Site Admin
Site Admin
User avatar

Joined: 2013/05/11, 22:30
Posts: 2027
Location: Germany
Role: Professional
There actually is a method to shortcut through the manual tile calculations: TilemapRenderer.GetTileAtLocalPos will give you the 2D tile index from an object space coordinate. To get from world space to object space, use Transform.GetLocalPoint.

Sorry for the late answer ^^

_________________
Blog | GitHub | Twitter (@Adams_Lair)


Top
 Profile  
 
PostPosted: 2017/10/17, 19:58 
Junior Member
Junior Member

Joined: 2016/08/03, 22:07
Posts: 45
Location: Florida
Role: Hobbyist
I didn't even think to look at the renderer for this. Thanks; I'll get it updated in my code. Appreciate the response. :1+:


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC + 1 hour [ DST ]


Who is online

Users browsing this forum: No registered users and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group