diff --git a/COGITO/CogitoObjects/cogito_player.gd b/COGITO/CogitoObjects/cogito_player.gd index b316a455..4c2f0322 100644 --- a/COGITO/CogitoObjects/cogito_player.gd +++ b/COGITO/CogitoObjects/cogito_player.gd @@ -407,6 +407,7 @@ func get_params(transform3d, motion): params.recovery_as_collision = true return params + #region Sittable Interaction Handling #Sittable Vars @@ -426,20 +427,23 @@ func toggle_sitting(): _stand_up() else: _sit_down() - + + func _on_sit_requested(sittable: Node): if not is_sitting: _sit_down() + func _on_stand_requested(): if is_sitting: _stand_up() + func _on_seat_move_requested(sittable: Node): moving_seat = true _sit_down() - + func handle_sitting_look(event): #TODO - Fix for vehicles by handling dynamic look marker, Fix for controller support var neck_position = neck.global_transform.origin @@ -482,6 +486,7 @@ func handle_sitting_look(event): # TODO replace with dynamic vertical look range based on look marker head.rotation.x = clamp(head.rotation.x, deg_to_rad(-sittable.vertical_look_angle), deg_to_rad(sittable.vertical_look_angle)) + func _sit_down(): standing_collision_shape.disabled = true crouching_collision_shape.disabled = true @@ -516,7 +521,8 @@ func _sit_down(): var tween = create_tween() tween.tween_property(self, "global_transform", sittable.sit_position_node.global_transform, sittable.tween_duration) tween.tween_callback(Callable(self, "_sit_down_finished")) - + + func _sit_down_finished(): is_sitting = true set_physics_process(true) @@ -529,7 +535,7 @@ func _sit_down_finished(): var target_transform = neck.global_transform.looking_at(sittable_look_marker, Vector3.UP) tween.tween_property(neck, "global_transform:basis", target_transform.basis, sittable.rotation_tween_duration) - + func _stand_up(): var sittable = CogitoSceneManager._current_sittable_node if sittable: @@ -552,6 +558,7 @@ func _stand_up(): #Functions to handle Exit types + #Return player to Original position func _move_to_original_position(sittable): currently_tweening = true @@ -559,6 +566,8 @@ func _move_to_original_position(sittable): tween.tween_property(self, "global_transform", original_position, sittable.tween_duration) tween.tween_property(neck, "global_transform:basis", original_neck_basis, sittable.rotation_tween_duration) tween.tween_callback(Callable(self, "_stand_up_finished")) + + #Return player to Leave node position func _move_to_leave_node(sittable): currently_tweening = true @@ -573,6 +582,7 @@ func _move_to_leave_node(sittable): print("No leave node found. Returning to Original position") _move_to_original_position(sittable) + #Find location using navmesh to place player func _move_to_nearby_location(sittable): print("Attempting to find available locations to move player to") @@ -625,6 +635,7 @@ func _move_to_nearby_location(sittable): print("No available location found after ",attempts, " tries. Testing for leave node.") _move_to_leave_node(sittable) + func _move_to_displacement_position(sittable): var tween = create_tween() var new_position = sittable.global_transform.origin - displacement_position @@ -727,27 +738,42 @@ var jumped_from_slide = false var was_in_air = false +##Sittables Process +func _process_on_sittable(delta): + var sittable = CogitoSceneManager._current_sittable_node + # Processing analog stick mouselook TODO Rewrite for Look angle marker support + if joystick_h_event: + if abs(joystick_h_event.get_axis_value()) > JOY_DEADZONE: + if INVERT_Y_AXIS: + head.rotate_x(deg_to_rad(joystick_h_event.get_axis_value() * JOY_H_SENS)) + else: + head.rotate_x(-deg_to_rad(joystick_h_event.get_axis_value() * JOY_H_SENS)) + head.rotation.x = clamp(head.rotation.x, deg_to_rad(-sittable.horizontal_look_angle), deg_to_rad(sittable.horizontal_look_angle)) + + if joystick_v_event: + if abs(joystick_v_event.get_axis_value()) > JOY_DEADZONE: + neck.rotate_y(deg_to_rad(-joystick_v_event.get_axis_value() * JOY_V_SENS)) + neck.rotation.y = clamp(neck.rotation.y, deg_to_rad(-180), deg_to_rad(180)) + + #avoids instantly moving to seat before tween is finished + if not currently_tweening: + self.global_transform = sittable.sit_position_node.global_transform + #Check if the player should be ejected, is_ejected is flag to prevent multiple calls + if sittable.eject_on_fall == true and not is_ejected: + var chair_up_vector = sittable.global_transform.basis.y + var global_up_vector = Vector3(0, 1, 0) + # Calculate angle between chair's up vector and the global up vector + var angle_to_up = rad_to_deg(chair_up_vector.angle_to(global_up_vector)) + # If the angle is greater than a threshold of 45 degrees, the chair has fallen over + if angle_to_up > sittable.eject_angle: + is_ejected = true # Set the flag to avoid repeated ejections + CogitoSceneManager._current_sittable_node.interact(player_interaction_component) #Interact with sittable to reset state and eject + func _physics_process(delta): #if is_movement_paused: #return if is_sitting: - var sittable = CogitoSceneManager._current_sittable_node - #Update Player location if Chair has moved - if sittable: - #avoids instantly moving to seat before tween is finished - if not currently_tweening: - self.global_transform = sittable.sit_position_node.global_transform - #Check if the player should be ejected, is_ejected is flag to prevent multiple calls - if sittable.eject_on_fall == true and not is_ejected: - var chair_up_vector = sittable.global_transform.basis.y - var global_up_vector = Vector3(0, 1, 0) - # Calculate angle between chair's up vector and the global up vector - var angle_to_up = rad_to_deg(chair_up_vector.angle_to(global_up_vector)) - # If the angle is greater than a threshold of 45 degrees, the chair has fallen over - if angle_to_up > sittable.eject_angle: - is_ejected = true # Set the flag to avoid repeated ejections - CogitoSceneManager._current_sittable_node.interact(player_interaction_component) #Interact with sittable to reset state and eject - + _process_on_sittable(delta) return # Store current velocity for the next frame diff --git a/COGITO/CogitoObjects/cogito_sittable.gd b/COGITO/CogitoObjects/cogito_sittable.gd index 00065d8e..53db475c 100644 --- a/COGITO/CogitoObjects/cogito_sittable.gd +++ b/COGITO/CogitoObjects/cogito_sittable.gd @@ -117,6 +117,7 @@ var cogito_properties : CogitoProperties = null var interaction_component_state:bool = false #endregion + func _ready(): #find player node player_node = CogitoSceneManager._current_player_node @@ -156,6 +157,7 @@ func _ready(): find_cogito_properties() + func get_child_carryable_components() -> Array: var components = [] # iterate through child nodes and check for CogitoCarryableComponent @@ -171,8 +173,8 @@ func displace_sit_marker(): var adjusted_transform = sit_position_node.transform adjusted_transform.origin.y -= sit_marker_displacement sit_position_node.transform = adjusted_transform - - + + func _sit_down(): if animation_player: @@ -203,7 +205,8 @@ func _sit_down(): node.hide() is_occupied = true - + + func _stand_up(): if animation_player: @@ -235,6 +238,7 @@ func _stand_up(): is_occupied = false + func switch(): if is_occupied: _stand_up() @@ -263,6 +267,7 @@ func set_state(): else: _stand_up() + func _on_sit_area_body_entered(body): if body == player_node: player_in_sit_area = true @@ -280,7 +285,8 @@ func _on_sit_area_body_entered(body): SitAreaBehaviour.NONE: BasicInteraction.is_disabled = false interaction_component_state = true - + + func _on_sit_area_body_exited(body): if body == player_node: player_in_sit_area = false @@ -292,6 +298,7 @@ func _on_sit_area_body_exited(body): BasicInteraction.is_disabled = true interaction_component_state = false + func find_cogito_properties(): var property_nodes = find_children("","CogitoProperties",true) #Grabs all attached property components if property_nodes: @@ -299,6 +306,7 @@ func find_cogito_properties(): var interact_cooldown = 0.0 + func interact(player_interaction_component): #Prevent entering fallen chair @@ -353,6 +361,7 @@ func interact(player_interaction_component): var object = get_node(nodepath) object.interact(player_interaction_component) + func _is_fallen() -> bool: var current_rotation = global_transform.basis.get_euler() var pitch_angle = rad_to_deg(abs(current_rotation.x)) @@ -361,6 +370,7 @@ func _is_fallen() -> bool: return true return false + func save(): var state_dict = { "node_path" : self.get_path(), @@ -390,8 +400,8 @@ func save(): state_dict["angular_velocity_y"] = rigid_body.angular_velocity.y state_dict["angular_velocity_z"] = rigid_body.angular_velocity.z return state_dict - - + + func find_rigid_body() -> RigidBody3D: var current = self while current: diff --git a/COGITO/CogitoObjects/cogito_vehicle.gd b/COGITO/CogitoObjects/cogito_vehicle.gd index 81f7515e..82461e5b 100644 --- a/COGITO/CogitoObjects/cogito_vehicle.gd +++ b/COGITO/CogitoObjects/cogito_vehicle.gd @@ -19,6 +19,7 @@ func _ready(): super._ready() physics_sittable = true + func _physics_process(delta): if player_node and player_node.is_sitting and CogitoSceneManager._current_sittable_node == self: @@ -31,6 +32,7 @@ func _physics_process(delta): vehicle_moved.emit() + func handle_input(delta): var input_vector = Vector3.ZERO @@ -50,6 +52,7 @@ func handle_input(delta): acceleration = local_direction * move_speed * delta rotation_momentum += rotation_input * rotation_speed + func apply_momentum(delta): if rotation_momentum != 0: rotation_momentum *= momentum_damping