Godot -- Drag and Drop (and then SWAP) of 2D Tiles - Design advice?

Hey guys,

I’ve been tinkering with a tile-based game that has “landing zones” for tiles that can be swapped. In general there are three requirements:

  1. The tile that is dragged needs to know which tile is underneath it when released (mouse/touch drag) and which “zone” that new tile is in that it has landed upon. That is so it can swap itself out and then re-evaluate the score in the zone where it landed.
  2. The tile that it has landed upon needs to be swapped into the previous tile’s origination point and that similarly triggers a re-evaluation of that zone’s score that it landed in.
  3. I need to animate the resting/seated tile to “teleport/travel” to the landing area/zone of the dragged tile to show the player that it moved and was swapped.

I was looking around in some of my old apps, and even though it is a “Word Game”(which isn’t exactly my game concept) I actually think Dabble is the best parallel to what I’m describing:

I got the dragging and dropping working with TextureRect’s since they inheret from Control objects (that wasn’t too bad), but I’m now having a hard time conceptually figuring out how to replace the tiles, keep track of the landing zones (though they should be an array of positions/locations on the board which can house and snap-to tiles, basically). And then have to have those tiles “snap to” a grid of slots/zones like you see up in Dabble when the user releases the mouse or lifts finger from device.

Not looking for a solution, but some pseudo-code to get me going down the right path would be super helpful.

I’ve tried Grids, ItemLists, etc. but I’m so new to Godot I’m not sure which is right way to go. :slight_smile:

Thanks in advance for your help! :wink:

had a play around, and not got it all working, but managed to get the tile index of a tilemap for whats under the mouse.

func GetTileUnderPlayer():
	#get players global position
	var _globalPlayerPos = self.global_position
	# get a reference to the tilemap
	var _tileMap = get_node("../Navigation2D/Tilemap")
	# get the global mouse position
	var mousePos = get_global_mouse_position()
	# get the mouse position, transformed to tilemap coords
	var _mousePosTileCoord = _tileMap.world_to_map(mousePos)
	# get the tile index at that tilemap coord
	var _tileIndex = _tileMap.get_cellv(_mousePosTileCoord)
	# now get the tile name at that index
	var tileName = _tileMap.tile_set.tile_get_name(_tileIndex)
	print(str(_tileIndex) + ", Name : " + str(tileName))

its a start tho to find the start and end points :wink:
should be able to swap via the tile index on press and release events