diff options
Diffstat (limited to 'scripts/player.gd')
-rw-r--r-- | scripts/player.gd | 64 |
1 files changed, 50 insertions, 14 deletions
diff --git a/scripts/player.gd b/scripts/player.gd index 73490d3..69ba910 100644 --- a/scripts/player.gd +++ b/scripts/player.gd @@ -23,6 +23,9 @@ var holding: Node3D = null var climbing: bool = false var last_wall_direction: Vector3 = Vector3() +var inital_camera_offset: Vector3 = Vector3() +var camera_rotation: float = 0 + func got_body(body: Node3D): if current_node == null: print("Got body ", body) @@ -46,14 +49,22 @@ func pickup(): holding.freeze = true $Holder.add_child(holding) +func get_camera_fwd() -> Vector3: + var fwd: Vector3 = -Vector3(sin(camera_rotation), 0, cos(camera_rotation)) + return fwd + +func get_camera_right() -> Vector3: + var right: Vector3 = Vector3(cos(camera_rotation), 0, -sin(camera_rotation)) + return right + func throw(): var g_pos = holding.global_position holding.get_parent().remove_child(holding) get_parent().add_child(holding) holding.global_position = g_pos - var fwd = $ThirdPersonCamera.get_front_direction() - var right = Vector3.UP + var fwd = get_camera_fwd() + var right = get_camera_right() var dir = (fwd + right).normalized() holding.freeze = false holding.apply_impulse(THROW_FORCE*dir) @@ -64,14 +75,21 @@ func _ready(): assert(seed_text != null, "Seed text is null") Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED) - $ThirdPersonCamera.mouse_follow = true $Picker.connect("body_entered", got_body) $Picker.connect("body_exited", leave_body) -func _process(_delta): + inital_camera_offset = $Camera3D.global_position - global_position + +func _process(delta: float): stam_bar.value = stamina seed_text.text = str(num_seeds) + + turn_camera(delta) + +func turn_camera(delta: float): + var turn_amt = Input.get_axis("turn_right", "turn_left") + camera_rotation += turn_amt * delta func _physics_process(delta): var speed = SPEED @@ -91,11 +109,8 @@ func _physics_process(delta): if not is_on_floor() and not climbing: velocity.y -= gravity * delta - var fwd = $ThirdPersonCamera.get_front_direction() - var right = $ThirdPersonCamera.get_right_direction() - - var climb_direction_fwd = Vector3() - var climb_direction_right = Vector3() + var fwd = get_camera_fwd() + var right = get_camera_right() var valid_climb = false if is_on_wall(): @@ -110,29 +125,46 @@ func _physics_process(delta): climbing = true elif climbing: climbing = false - + + var climb_direction_fwd = Vector3() + var climb_direction_right = Vector3() if climbing: climb_direction_fwd = (Vector3.UP - Vector3.UP.project(last_wall_direction)).normalized() + + # TODO + # This should not be relative to the camera, climbing should be fixed, I think I might need + # to take a cross product here to do that with the wall normal and the fwd/up direction on the wall climb_direction_right = (right - right.project(last_wall_direction)).normalized() # Handle Jump. if Input.is_action_just_pressed("ui_accept") and is_on_floor(): velocity.y = JUMP_VELOCITY - look_at(position + fwd, Vector3.UP) - + # Make sure player is looking in the forward direction + # TODO player should only start looking at the fwd direction when they start moving + look_at(global_position + fwd) + # 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("left", "right", "backward", "forward") var direction = (input_dir.y * fwd + input_dir.x * right).normalized() var climb_direction: Vector3 = (input_dir.y * climb_direction_fwd + input_dir.x * climb_direction_right).normalized() if climbing: - #print("Climb direction is ", climb_direction_fwd, " ", climb_direction_right, " ", get_wall_normal()) velocity = climb_direction * speed + + # TODO there should be some way to force the player to be locked to the wall without + # just pushing into the wall slightly. velocity += -speed * last_wall_direction if input_dir.length_squared() > 0: stam_consumption += RUN_CONSUMPTION - #print("Velocity is ", velocity) + + # Force player to look toward wall + look_at(global_position - last_wall_direction) + + # Set camera rotation so it continues to face the wall + # TODO player should still be able to move the camera, this should probably just nudge the camera to this + # position while climbing and not controling the camera + camera_rotation = Vector2(last_wall_direction.x, last_wall_direction.z).angle_to(Vector2(inital_camera_offset.x, inital_camera_offset.z)) elif direction: velocity.x = direction.x * speed velocity.z = direction.z * speed @@ -142,6 +174,10 @@ func _physics_process(delta): move_and_slide() + # Position camera + $Camera3D.global_position = global_position + inital_camera_offset.rotated(Vector3.UP, camera_rotation) + $Camera3D.look_at(global_position) + if stam_consumption == 0 and not Input.is_action_pressed("run") and is_on_floor(): stam_consumption = -STAM_REGEN |