Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sittable controller support #304

Merged
merged 2 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 47 additions & 21 deletions COGITO/CogitoObjects/cogito_player.gd
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,7 @@ func get_params(transform3d, motion):
params.recovery_as_collision = true
return params


#region Sittable Interaction Handling

#Sittable Vars
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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:
Expand All @@ -552,13 +558,16 @@ func _stand_up():

#Functions to handle Exit types


#Return player to Original position
func _move_to_original_position(sittable):
currently_tweening = true
var tween = create_tween()
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
Expand All @@ -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")
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
22 changes: 16 additions & 6 deletions COGITO/CogitoObjects/cogito_sittable.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -203,7 +205,8 @@ func _sit_down():
node.hide()

is_occupied = true



func _stand_up():

if animation_player:
Expand Down Expand Up @@ -235,6 +238,7 @@ func _stand_up():

is_occupied = false


func switch():
if is_occupied:
_stand_up()
Expand Down Expand 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
Expand All @@ -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
Expand All @@ -292,13 +298,15 @@ 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:
cogito_properties = property_nodes[0]

var interact_cooldown = 0.0


func interact(player_interaction_component):

#Prevent entering fallen chair
Expand Down Expand Up @@ -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))
Expand All @@ -361,6 +370,7 @@ func _is_fallen() -> bool:
return true
return false


func save():
var state_dict = {
"node_path" : self.get_path(),
Expand Down Expand Up @@ -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:
Expand Down
3 changes: 3 additions & 0 deletions COGITO/CogitoObjects/cogito_vehicle.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -31,6 +32,7 @@ func _physics_process(delta):

vehicle_moved.emit()


func handle_input(delta):

var input_vector = Vector3.ZERO
Expand All @@ -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
Expand Down