Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | a human |
I was wondering how I could make it so when you clicked on the screen a block would go to location of the mouse and then lock onto other blocks
Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | a human |
I was wondering how I could make it so when you clicked on the screen a block would go to location of the mouse and then lock onto other blocks
Reply From: | Dlean Jeans |
TileMap
has a world_to_map() function which you could pass in the mouse position and get the cell position:
var block_position = $TileMap.world_to_map(get_global_mouse_position())
$TileMap.set_cellv(block_position, GRASS_BLOCK)
With that, I guess you can place a block mid-air so you need to check if there’s any blocks around that:
const DIRECTIONS = [Vector2.UP, Vector2.DOWN, Vector2.LEFT, Vector2.RIGHT]
func exists_block_around(block_position:Vector2):
for dir in DIRECTIONS:
if $TileMap.get_cellv(block_position + dir) != TileMap.INVALID_CELL:
return true
return false
var block_position = $TileMap.world_to_map(get_global_mouse_position())
if exists_block_around(block_position):
$TileMap.set_cellv(block_position, GRASS_BLOCK)
You’ll also need to check the mouse distance to player but I think that’s easy enough for anyone to write.