function to convert player pos to isometric and back, should now handle it correctly

This commit is contained in:
Jonas 2025-03-29 23:21:15 +01:00
parent db749e0df2
commit 11acfcd892
3 changed files with 47 additions and 41 deletions

View File

@ -618,10 +618,15 @@ func init_player():
tile["visibility"] = true tile["visibility"] = true
tile["unit"] = player_node tile["unit"] = player_node
tile["unit_type"] ="PC" tile["unit_type"] ="PC"
player_node.set_unit_position(tile["x"], tile["y"], tile["z"]) var init_player_pos = iso_to_world(tile["x"], tile["y"], tile["z"])
tile["unit"].set_unit_position(init_player_pos.x, init_player_pos.y, tile["z"])
func _move_up_or_down(up_or_down = "up"):
func _on_player_z_layer_change_pressed() -> void: var z_offset = 0
if up_or_down == "up":
z_offset = 1
elif up_or_down == "down":
z_offset = -1
var new_z = 0 var new_z = 0
var new_y = 0 var new_y = 0
var new_x = 0 var new_x = 0
@ -631,7 +636,7 @@ func _on_player_z_layer_change_pressed() -> void:
for y in GRID_SIZE_LENGTH: for y in GRID_SIZE_LENGTH:
tile = debug_map[x][y][z] tile = debug_map[x][y][z]
if tile["unit_type"] == "PC": if tile["unit_type"] == "PC":
new_z = z + 1 new_z = z + z_offset
new_y = y new_y = y
new_x = x new_x = x
break break
@ -649,42 +654,42 @@ func _on_player_z_layer_change_pressed() -> void:
new_player_tile["unit"] = player_node new_player_tile["unit"] = player_node
new_player_tile["visibility"] = true new_player_tile["visibility"] = true
new_player_tile["unit_type"] ="PC" new_player_tile["unit_type"] ="PC"
player_node.set_unit_position(new_player_tile["x"], new_player_tile["y"], new_player_tile["z"]) var new_player_pos = iso_to_world(tile["x"], tile["y"], tile["z"])
new_player_tile["unit"].set_unit_position(new_player_pos.x, new_player_pos.y, tile["z"])
print(new_player_tile["x"], new_player_tile["y"], new_player_tile["z"])
func iso_to_world(tile_x, tile_y, player_z_layer = 0):
# Get the tile size from your tilemap
var tile_width = isometric_map_layers[player_z_layer].tile_set.tile_size.x
var tile_height = isometric_map_layers[player_z_layer].tile_set.tile_size.y
# Calculate the world position
var world_x = (tile_x - tile_y) * (tile_width / 2)
var world_y = (tile_x + tile_y) * (tile_height / 2) - (player_z_layer * tile_height / 2)
return Vector2(world_x, world_y)
# Converts world position to isometric tile coordinates
func world_to_iso(world_pos, player_z_layer = 0):
# Get the tile size from your tilemap
var tile_width = isometric_map_layers[player_z_layer].tile_set.tile_size.x
var tile_height = isometric_map_layers[player_z_layer].tile_set.tile_size.y
# Adjust y position based on z-layer
var adjusted_y = world_pos.y + (player_z_layer * tile_height / 2)
# Calculate the tile coordinates
var tile_x = (world_pos.x / (tile_width / 2) + adjusted_y / (tile_height / 2)) / 2
var tile_y = (adjusted_y / (tile_height / 2) - world_pos.x / (tile_width / 2)) / 2
return Vector2i(round(tile_x), round(tile_y))
func _on_player_z_layer_change_pressed() -> void:
_move_up_or_down("up")
func _on_player_z_layer_change_down_pressed() -> void: func _on_player_z_layer_change_down_pressed() -> void:
var new_z = 0 _move_up_or_down("down")
var new_y = 0
var new_x = 0
var tile = debug_map[0][0][0]
for z in GRID_SIZE_HEIGHT:
for x in GRID_SIZE_WIDTH:
for y in GRID_SIZE_LENGTH:
tile = debug_map[x][y][z]
if tile["unit_type"] == "PC":
new_z = z - 1
new_y = 1
new_x = 1
break
if tile["unit_type"] == "PC":
print("y ", tile)
break
if tile["unit_type"] == "PC":
print("x ", tile)
break
if tile["unit_type"] == "PC":
print("z ", tile)
break
print("tile ", tile)
print("x ", new_x, " y ", new_y, " z ", new_z)
tile["unit"] = null
tile["unit_type"] = null
tile["visibility"] = null
var new_player_tile = debug_map[new_x][new_y][new_z]
new_player_tile["visibility"] = true
new_player_tile["unit"] = player_node
new_player_tile["unit_type"] ="PC"
player_node.set_unit_position(new_player_tile["x"], new_player_tile["y"], new_player_tile["z"])

View File

@ -15,6 +15,7 @@ var player_data = {
} }
func set_unit_position(x, y, z): func set_unit_position(x, y, z):
var z_layer = z var z_layer = z
var x_position = x var x_position = x