Godsim/scripts/Tile.gd

54 lines
1.6 KiB
GDScript

extends StaticBody3D
enum TileType {
Empty,
Plain,
Forest
}
@export var tile_type: TileType = TileType.Empty;
func update_tile():
var children = find_children("*tile");
for child in children:
if TileType.keys()[tile_type] == child.name.split("_")[0]:
child.visible = true
else:
child.visible = false
func _ready():
update_tile()
func get_free_dirs_around():
var free_tiles_around = []
var space_state = get_world_3d().direct_space_state
var query_north = PhysicsRayQueryParameters3D.create(self.position + Vector3(0, 2.5, 0), self.position + Vector3(10, 2.5, 0))
var query_south = PhysicsRayQueryParameters3D.create(self.position + Vector3(0, 2.5, 0), self.position + Vector3(-12, 2.5, 0))
var query_west = PhysicsRayQueryParameters3D.create(self.position + Vector3(0, 2.5, 0), self.position + Vector3(0, 2.5, -10))
var query_east = PhysicsRayQueryParameters3D.create(self.position + Vector3(0, 2.5, 0), self.position + Vector3(0, 2.5, 10))
var result_north = space_state.intersect_ray(query_north)
var result_south = space_state.intersect_ray(query_south)
var result_west = space_state.intersect_ray(query_west)
var result_east = space_state.intersect_ray(query_east)
if !result_north: free_tiles_around.push_back("north")
if !result_south: free_tiles_around.push_back("south")
if !result_west: free_tiles_around.push_back("west")
if !result_east: free_tiles_around.push_back("east")
return free_tiles_around
func save():
if tile_type == TileType.Empty: return {}
return {
"node": "tile",
"pos_x": position.x,
"pos_y": position.y,
"pos_z": position.z,
"tile_type": tile_type
}