Godsim/scripts/Tile.gd

56 lines
1.8 KiB
GDScript3
Raw Normal View History

2024-05-30 02:14:08 +03:00
extends StaticBody3D
2024-05-30 06:55:13 +03:00
enum TileType {
Empty,
Plain,
Forest
}
2024-05-30 02:14:08 +03:00
2024-06-03 02:45:29 +03:00
var tile_materials = [ # must be in the same order as an enum above!
preload("res://materials/empty_tile.tres"),
preload("res://materials/plain_tile.tres")
]
2024-05-30 06:55:13 +03:00
2024-06-03 02:45:29 +03:00
@export var tile_type: TileType = TileType.Empty
@onready var visible_mesh = $"CollisionMesh/VisibleMesh"
@onready var plus_sprite = $"CollisionMesh/plus_sprite"
2024-06-03 02:53:59 +03:00
2024-05-30 06:55:13 +03:00
func update_tile():
2024-06-03 02:45:29 +03:00
visible_mesh.material_override = tile_materials[tile_type]
plus_sprite.visible = tile_type == TileType.Empty
2024-05-30 02:14:08 +03:00
2024-05-30 06:55:13 +03:00
func _ready():
update_tile()
2024-05-30 02:14:08 +03:00
2024-06-03 02:45:29 +03:00
func get_empty_dirs_around():
var empty_tiles_around = []
2024-06-03 02:26:23 +03:00
2024-05-31 22:46:56 +03:00
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)
2024-06-03 02:45:29 +03:00
if !result_north: empty_tiles_around.push_back("north")
if !result_south: empty_tiles_around.push_back("south")
if !result_west: empty_tiles_around.push_back("west")
if !result_east: empty_tiles_around.push_back("east")
2024-06-03 02:26:23 +03:00
2024-06-03 02:45:29 +03:00
return empty_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
}