Godsim/scripts/Tile.gd

98 lines
3.4 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,
2024-06-04 13:12:27 +03:00
Forest,
Lake
2024-05-30 06:55:13 +03:00
}
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!
2024-06-03 22:25:56 +03:00
preload("res://materials/empty_tile.tres"), # Empty
preload("res://materials/plain_tile.tres"), # Plain
2024-06-04 13:12:27 +03:00
preload("res://materials/plain_tile.tres"), # Forest
preload("res://materials/lake_tile.tres") # Lake
2024-06-03 02:45:29 +03:00
]
2024-06-04 00:30:57 +03:00
@export var decorations = {
"Forest": {
"min": 5,
"max": 10,
"object": preload("res://scenes/models/tree.tscn")
}
}
2024-06-03 02:45:29 +03:00
@export var tile_type: TileType = TileType.Empty
2024-06-03 22:25:56 +03:00
@onready var visible_mesh = $VisibleMesh
2024-06-07 04:52:30 +03:00
@onready var God = get_tree().root.get_node("world/God")
2024-06-03 02:53:59 +03:00
2024-06-04 00:30:57 +03:00
var maximum_decoration_objects = 0
func generate_predictable_random_int(min, max, salt):
2024-06-04 00:30:57 +03:00
seed((str(World.world_info.seed) + str(position) + str(salt)).hash())
return randi() % (max + 1) + min
func generate_predictable_random_float(min, max, salt):
seed((str(World.world_info.seed) + str(position) + str(salt)).hash())
return clamp(randf(), min, max)
2024-06-04 00:30:57 +03:00
func generate_decoration_objects():
match tile_type:
TileType.Forest:
var key = TileType.keys()[tile_type]
maximum_decoration_objects = generate_predictable_random_int(decorations[key]["min"], decorations[key]["max"], "");
2024-06-04 00:30:57 +03:00
for i in range(0, maximum_decoration_objects):
var decoration = decorations[TileType.keys()[tile_type]]["object"].instantiate()
var pos = Vector3(generate_predictable_random_int(0, 10, str(i) + "x") - 5, 0, generate_predictable_random_int(0, 10, str(i) + "z") - 5)
var scale = generate_predictable_random_float(0.05, 0.15, str(i) + "scale")
var y_rot = generate_predictable_random_int(0, 360, str(i) + "rot_y")
2024-06-04 01:35:22 +03:00
decoration.rotation.y = y_rot
2024-06-04 00:30:57 +03:00
decoration.position = pos
decoration.scale = Vector3(scale, scale, scale)
2024-06-04 00:30:57 +03:00
decoration.add_to_group("Decoration")
add_child(decoration)
2024-06-04 01:32:46 +03:00
2024-05-30 06:55:13 +03:00
func update_tile():
2024-06-04 00:30:57 +03:00
for decoration in get_children():
if(decoration.is_in_group("Decoration")):
decoration.queue_free()
2024-06-03 22:25:56 +03:00
visible_mesh.set_surface_override_material(0, tile_materials[tile_type])
2024-06-04 00:30:57 +03:00
generate_decoration_objects()
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
2024-06-03 22:25:56 +03:00
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))
2024-05-31 22:46:56 +03:00
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")
2024-06-03 02:26:23 +03:00
if !result_east: empty_tiles_around.push_back("east")
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
}
2024-06-07 04:52:30 +03:00
func _process(delta):
if (tile_type == TileType.Empty): visible = (position.distance_to(God.position) < 100)