Roguelike Grid Based Targeting

Latest Experimental Build

Question

Hello, This is my first ever question so be gentle :slight_smile:

I am a new programmer making a Roguelike game. Think Tome or Caves of Qud. I’m having an issue with the best way to do my targeting. Right now my plan is to create a “SkillPreview” class that is a control node with a ColorRect attached to it with the following code:

extends Control
class_name PreviewControl


@onready var PreviewArea: ColorRect = $PreviewTargetArea

 

func TargetPreview(TargetType):
	match TargetType:
		Enums.TARGETTYPES.TARGETOUTOFRANGE:
			PreviewArea.Color(0.00, 0.72, 0.00, 0.19)
		Enums.TARGETTYPES.TARGETINRANGE:
			PreviewArea.Color(1.00, 0.00, 0.00, 0.48)
		Enums.TARGETTYPES.OUTOFRANGE:
			PreviewArea.Color(0.23, 0.08, 0.25, 0.36)
		Enums.TARGETTYPES.INRANGE:
			PreviewArea.Color(0.21, 0.43, 0.21, 0.36)

The first step is having my player press a key on their keyboard to call a try cast function:

	if Input.is_action_just_pressed("Cast1"):
		if CurrentCastKey == 1:
			ConfirmCast()
		else:
			var SkillRange = PlayerSkillSheet.skills[0].SkillMaxRange
			TryCast(1, SkillRange)

The Skill range is just a value that represents the amount of squares the skill can travel, For example 1 or 2 = 1 square or 2 squares on the grid.

My question is, How do I calculate the distance between two characters as tiles on a grid? I know that the function I’m using returns them as a Vector2 but how do I translate that into the amount of tiles in a grid? I saw someone talking about using Lerp? But I didn’t full understand the concept and it was a locked thread on another website from 3 years ago. What is the standard way people would do this? Or a best practice that people use that differs from the way im trying to do it im my head?

func CalculateTileDistance(playerPosition: Vector2, enemyPosition: Vector2, tileSize: int = 48) -> int:
	var distanceInPixels = playerPosition.distance_to(enemyPosition)
	var distanceInTiles = round(distanceInPixels / tileSize)
	return distanceInTiles

Sorry, I figured it out. I was just misreading some of the documentation. Here is my solution if anyone is having a similar issue :slight_smile: (you can replace the tile size based of your own tilemap size, Mine is 16x16 but i scale by 3 for visibility which equals 48 for my tilesize.)