diff options
Diffstat (limited to 'scripts')
-rw-r--r-- | scripts/fly_camera.gd | 4 | ||||
-rw-r--r-- | scripts/level_gen.gd | 65 | ||||
-rw-r--r-- | scripts/marching_cubes.gd | 14 | ||||
-rw-r--r-- | scripts/player.gd | 39 |
4 files changed, 70 insertions, 52 deletions
diff --git a/scripts/fly_camera.gd b/scripts/fly_camera.gd index 0f4dc53..b342ab6 100644 --- a/scripts/fly_camera.gd +++ b/scripts/fly_camera.gd @@ -1,4 +1,4 @@ -extends Camera3D +extends Node3D # Modifier keys' speed multiplier const SHIFT_MULTIPLIER = 2.5 @@ -113,4 +113,4 @@ func _update_mouselook(): _total_pitch += pitch rotate_y(deg_to_rad(-yaw)) - rotate_object_local(Vector3(1,0,0), deg_to_rad(-pitch)) + #rotate_object_local(Vector3(1,0,0), deg_to_rad(-pitch)) diff --git a/scripts/level_gen.gd b/scripts/level_gen.gd index 184ed26..66f465a 100644 --- a/scripts/level_gen.gd +++ b/scripts/level_gen.gd @@ -24,7 +24,8 @@ func gen_map_grid(): var noise = FastNoiseLite.new() noise.noise_type = FastNoiseLite.TYPE_SIMPLEX noise.frequency = 0.06 - noise.set_seed(0xDEADBEEF) + #noise.set_seed(0xDEADBEEF) + noise.set_seed(random.randi()) for z in depth: for x in width: var mid_x = x - width/2.0 @@ -39,31 +40,19 @@ func gen_map_grid(): for y in gen_h: map_grid[z*height*width + y*width + x] = -100.0 -func add_normal(tri_index: int, triangles: PackedInt32Array, vertices: PackedVector3Array, normal_list: Array[Vector4]): - var new_tri_index: int = 0 - while new_tri_index < len(triangles): - @warning_ignore("integer_division") - var tri_index_1:int = int(new_tri_index/3) - @warning_ignore("integer_division") - var tri_index_2:int = int(tri_index/3) - if tri_index_1 == tri_index_2: - new_tri_index += 3 +func add_normal(tri_index: int, vertices: PackedVector3Array, normal_list: Array[Vector4]): + var vertex_index: int = 0 + while vertex_index < len(vertices): + if vertex_index == tri_index: + vertex_index += 1 continue - var orig_vert = vertices[triangles[tri_index]] - var verts = [ - new_tri_index+0, - new_tri_index+1, - new_tri_index+2] - - for i in verts: - var vert = vertices[triangles[i]] - if i == tri_index: - continue - if vert == orig_vert: - normal_list[triangles[tri_index]] += normal_list[triangles[i]] + var orig_vert = vertices[tri_index] + var vert = vertices[vertex_index] + if orig_vert.is_equal_approx(vert): + normal_list[tri_index] += normal_list[vertex_index] - new_tri_index += 3 + vertex_index += 1 func place_objects(num_objects: int, object: PackedScene): var space = get_world_3d().direct_space_state @@ -95,14 +84,12 @@ func _ready(): gen_map_grid() var vertices = PackedVector3Array() var normals = PackedVector3Array() - var triangles = PackedInt32Array() # Initialize the ArrayMesh. var arr_mesh = ArrayMesh.new() var arrays = [] arrays.resize(Mesh.ARRAY_MAX) arrays[Mesh.ARRAY_VERTEX] = vertices arrays[Mesh.ARRAY_NORMAL] = normals - arrays[Mesh.ARRAY_INDEX] = triangles var cell = MarchingCubes.Cell.new() @@ -127,31 +114,29 @@ func _ready(): cell.value[i] = map_grid_get(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) + MarchingCubes.march_cube(cell, vertices) var tri_index = 0 var normal_list: Array[Vector4] = [] normal_list.resize(len(vertices)) normal_list.fill(Vector4(0,0,0,0)) - while tri_index < len(triangles): - var p1 = vertices[triangles[tri_index + 0]] - var p2 = vertices[triangles[tri_index + 1]] - var p3 = vertices[triangles[tri_index + 2]] + while tri_index < len(vertices): + var p1 = vertices[tri_index + 0] + var p2 = vertices[tri_index + 1] + var p3 = vertices[tri_index + 2] var normal = (p2 - p3).cross(p1 - p3).normalized(); var normal_4 = Vector4(normal.x, normal.y, normal.z, 1) - normal_list[triangles[tri_index + 0]] += normal_4 - normal_list[triangles[tri_index + 1]] += normal_4 - normal_list[triangles[tri_index + 2]] += normal_4 + normal_list[tri_index + 0] += normal_4 + normal_list[tri_index + 1] += normal_4 + normal_list[tri_index + 2] += normal_4 tri_index += 3 - if smooth_normal: - tri_index = 0 - while tri_index < len(triangles): - add_normal(tri_index+0, triangles, vertices, normal_list) - add_normal(tri_index+1, triangles, vertices, normal_list) - add_normal(tri_index+2, triangles, vertices, normal_list) - tri_index += 3 + if smooth_normal: + var vert_index = 0 + while vert_index < len(vertices): + add_normal(vert_index, vertices, normal_list) + vert_index += 1 for normal in normal_list: var real_normal = Vector3(normal.x, normal.y, normal.z) / normal.w diff --git a/scripts/marching_cubes.gd b/scripts/marching_cubes.gd index 6c5296c..cbf278b 100644 --- a/scripts/marching_cubes.gd +++ b/scripts/marching_cubes.gd @@ -12,14 +12,12 @@ class Cell: position.resize(8) value.resize(8) -func march_cube(cell: Cell, triangles: PackedInt32Array, verticies: PackedVector3Array): +func march_cube(cell: Cell, verticies: PackedVector3Array): var cube_index : int = 0 var vertex_list: Array[Vector3] = [] vertex_list.resize(12) - var starting_vertex_count = verticies.size() - for i in 8: if cell.value[i] < 0: cube_index |= (1 << i) @@ -66,16 +64,12 @@ func march_cube(cell: Cell, triangles: PackedInt32Array, verticies: PackedVector local_remap[mc._tri_table[cube_index][tri_index]] = new_vertex_count new_vertex_count += 1 tri_index += 1 - - for i in new_vertex_count: - var vert = new_vertex_list[i] - verticies.push_back(vert) tri_index = 0 while mc._tri_table[cube_index][tri_index] != -1: - triangles.push_back(local_remap[mc._tri_table[cube_index][tri_index + 0]] + starting_vertex_count) - triangles.push_back(local_remap[mc._tri_table[cube_index][tri_index + 1]] + starting_vertex_count) - triangles.push_back(local_remap[mc._tri_table[cube_index][tri_index + 2]] + starting_vertex_count) + verticies.push_back(new_vertex_list[local_remap[mc._tri_table[cube_index][tri_index + 0]]]) + verticies.push_back(new_vertex_list[local_remap[mc._tri_table[cube_index][tri_index + 1]]]) + verticies.push_back(new_vertex_list[local_remap[mc._tri_table[cube_index][tri_index + 2]]]) tri_index += 3 diff --git a/scripts/player.gd b/scripts/player.gd new file mode 100644 index 0000000..49bf1b7 --- /dev/null +++ b/scripts/player.gd @@ -0,0 +1,39 @@ +extends CharacterBody3D + + +const SPEED = 5.0 +const JUMP_VELOCITY = 4.5 + +# Get the gravity from the project settings to be synced with RigidBody nodes. +var gravity = ProjectSettings.get_setting("physics/3d/default_gravity") + +func _ready(): + Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED) + $ThirdPersonCamera.mouse_follow = true + +func _physics_process(delta): + # Add the gravity. + if not is_on_floor(): + velocity.y -= gravity * delta + + # Handle Jump. + if Input.is_action_just_pressed("ui_accept") and is_on_floor(): + velocity.y = JUMP_VELOCITY + + var fwd = $ThirdPersonCamera.get_front_direction() + var right = $ThirdPersonCamera.get_right_direction() + + look_at(position + fwd, Vector3.UP) + + # Get the input direction and handle the movement/deceleration. + # As good practice, you should replace UI actions with custom gameplay actions. + var input_dir = Input.get_vector("ui_left", "ui_right", "ui_down", "ui_up") + var direction = (input_dir.y * fwd + input_dir.x * right).normalized() + if direction: + velocity.x = direction.x * SPEED + velocity.z = direction.z * SPEED + else: + velocity.x = move_toward(velocity.x, 0, SPEED) + velocity.z = move_toward(velocity.z, 0, SPEED) + + move_and_slide() |