itrpg/scripts/isometric_map_system.gd

69 lines
3.0 KiB
GDScript

extends Node
# Constants for tile types/colors
const MAIN_SOURCE_ID = 1
const BLUE_ISOMETRICTILE_ATLAS_POSITION = Vector2i(0,0)
const RED_ISOMETRICTILE_ATLAS_POSITION = Vector2i(1,0)
const GREEN_ISOMETRICTILE_ATLAS_POSITION = Vector2i(2,0)
const WHITE_ISOMETRICTILE_ATLAS_POSITION = Vector2i(3,0)
const BLACK_ISOMETRICTILE_ATLAS_POSITION = Vector2i(4,0)
const PURPLE_ISOMETRICTILE_ATLAS_POSITION = Vector2i(5,0)
const ORANGE_ISOMETRICTILE_ATLAS_POSITION = Vector2i(6,0)
const LOW_ALPHA_BLOCK_ISOMETRICTILE_ATLAS_POSITION = Vector2i(7,0)
const SELECTED_BLUE_ISOMETRICTILE_ATLAS_POSITION = Vector2i(0,1)
const SELECTED_RED_ISOMETRICTILE_ATLAS_POSITION = Vector2i(1,1)
const SELECTED_GREEN_ISOMETRICTILE_ATLAS_POSITION = Vector2i(2,1)
const SELECTED_WHITE_ISOMETRICTILE_ATLAS_POSITION = Vector2i(3,1)
const SELECTED_BLACK_ISOMETRICTILE_ATLAS_POSITION = Vector2i(4,1)
const SELECTED_PURPLE_ISOMETRICTILE_ATLAS_POSITION = Vector2i(5,1)
const SELECTED_ORANGE_ISOMETRICTILE_ATLAS_POSITION = Vector2i(6,1)
# Debug flags
var debug_enabled = true
# Debug function that only prints when debug is enabled
func debug_print(message):
if debug_enabled:
print("[IsometricMapSystem] " + str(message))
# Utility functions for conversion between coordinate systems
func iso_to_world(tile_x, tile_y, player_z_layer = 0, tile_width = 32, tile_height = 16):
# Calculate the world position from isometric coordinates
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, tile_width = 32, tile_height = 16):
# 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))
# Create a new tile data structure with default values
func create_tile_data(coord_x, coord_y, z, atlas_base_position = null, atlas_highlight_position = null):
# Ensure we have valid default positions to avoid null reference errors
var base_pos = atlas_base_position if atlas_base_position != null else BLUE_ISOMETRICTILE_ATLAS_POSITION
var highlight_pos = atlas_highlight_position if atlas_highlight_position != null else WHITE_ISOMETRICTILE_ATLAS_POSITION
return {
"x": coord_x,
"y": coord_y,
"z": z,
"atlas_base_position": null,
"atlas_highlight_position": null,
"highlighted": false,
"selected": false,
"hp": 100,
"armour": 0,
"destroyable": false,
"visibility": false,
"unit": null,
"unit_type": null, # PC (more options e.g. can move it) or NPC (can only view it)
"gravity": null,
}