diff options
author | Lucas Fryzek <lucas.fryzek@fryzekconcepts.com> | 2023-09-10 20:15:31 -0400 |
---|---|---|
committer | Lucas Fryzek <lucas.fryzek@fryzekconcepts.com> | 2023-09-10 20:15:31 -0400 |
commit | 1e694fce7e4ba915861a5be543e40667b9da44ed (patch) | |
tree | abd0929fe486d81b14c4900fc48294fb4544f804 | |
parent | e2af60e3985317e9c4ec15e60f83bd8f5e49110e (diff) |
Add rock and tree prefab
Update world gen to start placing objects on world
-rw-r--r-- | materials/terrain.tres | 3 | ||||
-rw-r--r-- | prefab/rock.tscn | 15 | ||||
-rw-r--r-- | prefab/tree.tscn | 19 | ||||
-rw-r--r-- | scenes/main.tscn | 23 | ||||
-rw-r--r-- | scripts/level_gen.gd | 40 |
5 files changed, 92 insertions, 8 deletions
diff --git a/materials/terrain.tres b/materials/terrain.tres new file mode 100644 index 0000000..f8d2b6a --- /dev/null +++ b/materials/terrain.tres @@ -0,0 +1,3 @@ +[gd_resource type="StandardMaterial3D" format=3 uid="uid://uiwp0gpofjrx"] + +[resource] diff --git a/prefab/rock.tscn b/prefab/rock.tscn new file mode 100644 index 0000000..d5e8828 --- /dev/null +++ b/prefab/rock.tscn @@ -0,0 +1,15 @@ +[gd_scene load_steps=3 format=3 uid="uid://2a3fyuye46hm"] + +[sub_resource type="SphereShape3D" id="SphereShape3D_os5og"] + +[sub_resource type="SphereMesh" id="SphereMesh_l2tu3"] + +[node name="rock" type="Node3D"] + +[node name="StaticBody3D" type="StaticBody3D" parent="."] + +[node name="CollisionShape3D" type="CollisionShape3D" parent="StaticBody3D"] +shape = SubResource("SphereShape3D_os5og") + +[node name="MeshInstance3D" type="MeshInstance3D" parent="StaticBody3D/CollisionShape3D"] +mesh = SubResource("SphereMesh_l2tu3") diff --git a/prefab/tree.tscn b/prefab/tree.tscn new file mode 100644 index 0000000..855920d --- /dev/null +++ b/prefab/tree.tscn @@ -0,0 +1,19 @@ +[gd_scene load_steps=3 format=3 uid="uid://b10phvcuy488p"] + +[sub_resource type="CylinderShape3D" id="CylinderShape3D_wmmj8"] +height = 10.0 + +[sub_resource type="CylinderMesh" id="CylinderMesh_bbh53"] +height = 10.0 + +[node name="Tree" type="Node3D"] + +[node name="StaticBody3D" type="StaticBody3D" parent="."] + +[node name="CollisionShape3D" type="CollisionShape3D" parent="StaticBody3D"] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 5, 0) +shape = SubResource("CylinderShape3D_wmmj8") + +[node name="MeshInstance3D" type="MeshInstance3D" parent="StaticBody3D/CollisionShape3D"] +mesh = SubResource("CylinderMesh_bbh53") +skeleton = NodePath("../../..") diff --git a/scenes/main.tscn b/scenes/main.tscn index 782933b..2b25fc4 100644 --- a/scenes/main.tscn +++ b/scenes/main.tscn @@ -1,11 +1,30 @@ -[gd_scene load_steps=3 format=3 uid="uid://bb55ocpenupao"] +[gd_scene load_steps=6 format=3 uid="uid://bb55ocpenupao"] [ext_resource type="Script" path="res://scripts/level_gen.gd" id="1_sfx65"] [ext_resource type="Script" path="res://scripts/fly_camera.gd" id="2_ba28s"] +[ext_resource type="Material" uid="uid://uiwp0gpofjrx" path="res://materials/terrain.tres" id="3_nsy4g"] + +[sub_resource type="BoxMesh" id="BoxMesh_2l1xl"] +material = ExtResource("3_nsy4g") + +[sub_resource type="Environment" id="Environment_w27kj"] +background_color = Color(0.658824, 0.658824, 0.658824, 1) +ambient_light_source = 2 +ambient_light_color = Color(1, 1, 1, 1) [node name="Node3D" type="Node3D"] script = ExtResource("1_sfx65") [node name="Camera3D" type="Camera3D" parent="."] -transform = Transform3D(1, 0, 0, 0, 0.805763, 0.592238, 0, -0.592238, 0.805763, 1.36517, 2.86316, 6.85865) +transform = Transform3D(1, 0, 0, 0, 0.805763, 0.592238, 0, -0.592238, 0.805763, 1.365, 30, 6.859) script = ExtResource("2_ba28s") + +[node name="DirectionalLight3D" type="DirectionalLight3D" parent="."] +transform = Transform3D(1, 0, 0, 0, -4.37114e-08, 1, 0, -1, -4.37114e-08, 0, 30, 0) + +[node name="MeshInstance3D" type="MeshInstance3D" parent="."] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 10, 0) +mesh = SubResource("BoxMesh_2l1xl") + +[node name="WorldEnvironment" type="WorldEnvironment" parent="."] +environment = SubResource("Environment_w27kj") diff --git a/scripts/level_gen.gd b/scripts/level_gen.gd index 0e4cb21..b86da73 100644 --- a/scripts/level_gen.gd +++ b/scripts/level_gen.gd @@ -4,6 +4,10 @@ extends Node3D @export var height: int = 16 @export var depth: int = 16 +@export var scale_x: int = 10 +@export var scale_y: int = 10 +@export var scale_z: int = 10 + var map_grid: Array[float] = [] func map_grid_get(x: int, y: int, z: int): @@ -14,10 +18,10 @@ func gen_map_grid(): for z in depth: for y in height: for x in width: - if y <= 5: + if y <= 2: map_grid[z*height*width + y*width + x] = -1.0 else: - map_grid[z*height*width + y*width + x] = 0.0 + map_grid[z*height*width + y*width + x] = 10.0 # Called when the node enters the scene tree for the first time. func _ready(): @@ -49,15 +53,17 @@ func _ready(): for i in grid.size(): if grid[i][0] >= width or grid[i][1] >= height or grid[i][2] >= depth or \ grid[i][0] < 0 or grid[i][1] < 0 or grid[i][2] < 0: - cell.value[i] = 0.0 + cell.value[i] = 10.0 else: cell.value[i] = map_grid_get(grid[i][0], grid[i][1], grid[i][2]) - cell.position[i] = Vector3(grid[i][0], grid[i][1], grid[i][2]) + cell.position[i] = Vector3(grid[i][0]*scale_x, grid[i][1]*scale_y, grid[i][2]*scale_z) MarchingCubes.march_cube(cell, triangles, vertices) # Create the Mesh. arr_mesh.add_surface_from_arrays(Mesh.PRIMITIVE_TRIANGLES, arrays) + var material = preload("res://materials/terrain.tres") + arr_mesh.surface_set_material(0, material) var m = MeshInstance3D.new() m.mesh = arr_mesh @@ -66,9 +72,31 @@ func _ready(): var collision_shape = CollisionShape3D.new() collision_shape.set_shape(col_mesh) - m.add_child(collision_shape) + var static_body = StaticBody3D.new() + + static_body.add_child(collision_shape) + static_body.add_child(m) + add_child(static_body) + + var tree_prefab = preload("res://prefab/tree.tscn") + + var space = get_world_3d().direct_space_state + + var random = RandomNumberGenerator.new() + random.randomize() + var num_trees = random.randi_range(5, 15) - add_child(m) + for i in num_trees: + var cast_pos = Vector3(random.randf_range(0, width*scale_x), height*scale_y, random.randf_range(0, depth*scale_z)) + var cast_dest = Vector3(cast_pos.x, 0, cast_pos.z) + var query = PhysicsRayQueryParameters3D.create(cast_pos, cast_dest) + var result = space.intersect_ray(query) + + if result: + var pos = result.position + var new_tree = tree_prefab.instantiate() + new_tree.position = pos + add_child(new_tree) # Called every frame. 'delta' is the elapsed time since the previous frame. func _process(_delta): |