Godsim/scripts/Tile.gd

64 lines
1.7 KiB
GDScript

extends StaticBody3D
enum TileType {
Empty,
Plain,
Forest
}
@export var tile_type: TileType = TileType.Empty;
var free_sides = {
"north": false,
"south": false,
"west": false,
"east": false,
}
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 _physics_process(delta):
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)
free_sides = {
"north": result_north == {},
"south": result_south == {},
"west": result_west == {},
"east": result_east == {}
}
func get_free_dirs_around():
var free_tiles_around = []
for side in free_sides.keys():
if free_sides[side]:
free_tiles_around.push_back(side)
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
}