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

Expanded upon throw and drop behavior #335

Merged
merged 1 commit into from
Nov 25, 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
63 changes: 57 additions & 6 deletions addons/cogito/Components/PlayerInteractionComponent.gd
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,34 @@ var carried_object = null: # Used for carryable handling.

var is_carrying: bool:
get: return carried_object != null
## Power with which object are thrown (opposed to being dropped)
@export var throw_power: float = 10
var is_changing_wieldables: bool = false # Used to avoid any input acitons while wieldables are being swapped

# Power with which object are thrown (opposed to being dropped)
#@export var throw_power: float = 10
@export_group("Throw Settings")
## The maximum power you can apply to a thrown object
@export var max_throw_power: float = 25.0
## Multiplied with the mass of the thrown object, up to the Max Throw Power. Prevents throwing lightweight objects incredibly fast.
@export var throw_power_mass_multiplier: float = 10.0
## Drain stamina if calculated throw power exceeds this value
@export var throw_stamina_threshold: float = 20.0
@export var throw_stamina_drain: float = 5.0
## When stamina is below cost on a throw attempt, drop instead
@export var drop_when_cant_throw: bool = true
## Leave empty to ignore stamina cost evaluation when throwing
@export var stamina_attribute: CogitoAttribute
var player: CogitoPlayer

@export_group("Drop Settings")
## The maximum power you can use when dropping objects
@export var max_drop_power: float = 1.0
## Multiplied by the mass of the thrown object, up to the Max Drop Power
@export var drop_power_mass_multiplier: float = 1.0

@export_group("Wieldable Settings")
## List of Wieldable nodes
@export var wieldable_nodes: Array[Node]
@export var wieldable_container: Node3D
var is_changing_wieldables: bool = false # Used to avoid any input acitons while wieldables are being swapped
# Various variables used for wieldable handling
var equipped_wieldable_item: WieldableItemPD = null
var equipped_wieldable_node = null
Expand All @@ -42,7 +63,8 @@ var is_wielding: bool:
var player_rid

func _ready():
pass
player = get_parent() as CogitoPlayer
#pass


func exclude_player(rid: RID):
Expand All @@ -62,7 +84,8 @@ func _input(event: InputEvent) -> void:

if is_carrying and !get_parent().is_movement_paused and is_instance_valid(carried_object):
if Input.is_action_just_pressed("action_primary"):
carried_object.throw(throw_power)
_attempt_throw()
#carried_object.throw(throw_power)


# Wieldable primary Action Input
Expand All @@ -86,7 +109,8 @@ func _handle_interaction(action: String) -> void:
# if carrying an object, drop it.
if is_carrying:
if is_instance_valid(carried_object) and carried_object.input_map_action == action:
carried_object.throw(1)
_drop_carried_object()
#carried_object.throw(1)
return
elif !is_instance_valid(carried_object):
stop_carrying()
Expand Down Expand Up @@ -374,3 +398,30 @@ func _rebuild_interaction_prompts() -> void:
nothing_detected.emit() # Clears the prompts
if interactable != null:
interactive_object_detected.emit(interactable.interaction_nodes) # Builds the prompts


func _attempt_throw() -> void:
if !is_carrying:
return
var carried_object_mass: float = (carried_object.get_parent() as RigidBody3D).mass
var throw_force: float = carried_object_mass * throw_power_mass_multiplier
throw_force = clamp(throw_force, 0, max_throw_power)

if stamina_attribute and throw_force >= throw_stamina_threshold:
if stamina_attribute.value_current < throw_stamina_drain:
if drop_when_cant_throw:
_drop_carried_object()
return
else:
player.decrease_attribute(stamina_attribute.attribute_name, throw_stamina_drain)

carried_object.throw(throw_force)


func _drop_carried_object() -> void:
if !is_carrying:
return
var carried_object_mass: float = (carried_object.get_parent() as RigidBody3D).mass
var drop_force: float = carried_object_mass * drop_power_mass_multiplier
drop_force = clamp(drop_force, 0, max_drop_power)
carried_object.throw(drop_force)
4 changes: 3 additions & 1 deletion addons/cogito/PackedScenes/cogito_player.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,10 @@ landing_material_library = ExtResource("19_pc36t")
wait_time = 0.5
one_shot = true

[node name="PlayerInteractionComponent" parent="." node_paths=PackedStringArray("carryable_position", "wieldable_container") instance=ExtResource("20_4f25o")]
[node name="PlayerInteractionComponent" parent="." node_paths=PackedStringArray("interaction_raycast", "carryable_position", "stamina_attribute", "wieldable_container") instance=ExtResource("20_4f25o")]
interaction_raycast = NodePath("../Body/Neck/Head/Eyes/Camera/InteractionRaycast")
carryable_position = NodePath("../Body/Neck/Head/Eyes/Camera/CarryablePosition")
stamina_attribute = NodePath("../StaminaAttribute")
wieldable_container = NodePath("../Body/Neck/Head/Wieldables")

[node name="Player_HUD" parent="." node_paths=PackedStringArray("player") instance=ExtResource("21_j3p88")]
Expand Down