Here's how I would go about checking if the mouse is in front of the player, written quickly off memory:
# first, convert the player's rotation to a direction
var player_facing_dir = Vector2(cos($PlayerNode.rotation), sin($PlayerNode.rotation))
# get the direction from the player to the mouse
var player_to_mouse = $PlayerNode.global_position.direction_to(get_global_mouse_position())
# Compare the angle difference between the two vectors using the dot product
var player_mouse_angle_dif = player_facing_dir.dot(player_to_mouse)
# In this example, 20 degrees is the maximum angle we will consider in front of the player
if (player_mouse_angle_dif == deg2rad(20)):
# The mouse is in front of the player!
pass
You'll probably want to have a condition to check if the mouse is within a certain radius of the player, so the player cannot affect tiles too far away. Something like this: if (get_global_mouse_position() - $PlayerNode.global_position).length() >= MAX_DISTANCE:
should work. With that taken into account, the code would look something like this:
# make sure the mouse is within the correct radius
if (get_global_mouse_position() - $PlayerNode.global_position).length() <= MAX_DISTANCE:
# first, convert the player's rotation to a direction
var player_facing_dir = Vector2(cos($PlayerNode.rotation), sin($PlayerNode.rotation))
# get the direction from the player to the mouse
var player_to_mouse = $PlayerNode.global_position.direction_to(get_global_mouse_position())
# Compare the angle difference between the two vectors using the dot product
var player_mouse_angle_dif = player_facing_dir.dot(player_to_mouse)
# In this example, 20 degrees is the maximum angle we will consider in front of the player
if (player_mouse_angle_dif == deg2rad(20)):
# The mouse is in front of the player!
pass
else:
# The mouse is out of range!
pass
I think the code above should work, but I wrote it quickly and have not tested it. The gist is that you need to get the direction the player is facing, assuming the player rotates around, and compare that to the angle from the player to the mouse. If the angles are similar, then it means the mouse is in the same direction that the player is facing. Especially if the mouse position is checked to be within a certain radius, it is very likely that if the angles are similar and the mouse is close enough, that a tile in front of the player has been clicked on.
Hopefully this helps!