Skip to content

Commit

Permalink
Merge pull request #340 from Phazorknight/auto-pick-up-zone
Browse files Browse the repository at this point in the history
Adds an AutoPickUpZone to the player
  • Loading branch information
OvercastInteractive authored Dec 4, 2024
2 parents 2821a4d + 911c65d commit 4c1df66
Show file tree
Hide file tree
Showing 6 changed files with 185 additions and 1 deletion.
13 changes: 13 additions & 0 deletions addons/cogito/Assets/Graphics/Editor/Icon_AutoPickUpZone.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
[remap]

importer="texture"
type="CompressedTexture2D"
uid="uid://bbt1hw8oekefc"
path="res://.godot/imported/Icon_AutoPickUpZone.svg-d67018c19bfb70773473a113052db674.ctex"
metadata={
"vram_texture": false
}

[deps]

source_file="res://addons/cogito/Assets/Graphics/Editor/Icon_AutoPickUpZone.svg"
dest_files=["res://.godot/imported/Icon_AutoPickUpZone.svg-d67018c19bfb70773473a113052db674.ctex"]

[params]

compress/mode=0
compress/high_quality=false
compress/lossy_quality=0.7
compress/hdr_compression=1
compress/normal_map=0
compress/channel_pack=0
mipmaps/generate=false
mipmaps/limit=-1
roughness/mode=0
roughness/src_normal=""
process/fix_alpha_border=true
process/premult_alpha=false
process/normal_map_invert_y=false
process/hdr_as_srgb=false
process/hdr_clamp_exposure=false
process/size_limit=0
detect_3d/compress_to=1
svg/scale=1.0
editor/scale_with_editor_scale=false
editor/convert_colors_with_editor_theme=false
15 changes: 15 additions & 0 deletions addons/cogito/CogitoObjects/cogito_projectile.gd
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ var damage_amount : int = 0
@export var stick_on_impact : bool = false
## Array of Scenes that will get spawned on parent position on death.
@export var spawn_on_death : Array[PackedScene] = []
## This prevents being able to auto pick up projectiles that have just been fired
@export_range(0.1, 3.0, 0.1, "or_greater") var pick_up_delay: float = 0.5
var can_pick_up: bool = false


func _ready():
add_to_group("interactable")
Expand All @@ -24,6 +28,17 @@ func _ready():

if lifespan:
lifespan.timeout.connect(on_timeout)

_pick_up_timer()


func _pick_up_timer() -> void:
if lifespan and (lifespan as Timer).wait_time < pick_up_delay:
# The projectile cannot be picked up before it dies, so don't create the timer
return
await get_tree().create_timer(pick_up_delay).timeout
can_pick_up = true


func on_timeout():
die()
Expand Down
79 changes: 79 additions & 0 deletions addons/cogito/Components/AutoPickUpZone.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
@icon("res://addons/cogito/Assets/Graphics/Editor/Icon_AutoPickUpZone.svg")
class_name AutoPickUpZone
extends Area3D

signal auto_picked_up_item()

# This is useful when you have pick up items that are frustrating, tedious, or required
# and provides a smoother experience than only using the default raycasting + input interaction.

## Items listed here will be picked up automatically when they enter the area
@export var auto_pick_up_items: Array[InventoryItemPD] = []
@export var _player_interaction_component: PlayerInteractionComponent
var player: CogitoPlayer
@onready var standing_collider: CollisionShape3D = $StandingZone
@onready var crouching_collider: CollisionShape3D = $CrouchingZone


func _ready() -> void:
player = get_parent()


func _on_body_entered(body: Node3D) -> void:
_attempt_pick_up(body)


func _physics_process(_delta) -> void:
_update_shape()


func _update_shape() -> void:
# Uses a shorter collision cylinder when crouching
if player.is_crouching:
if !standing_collider.disabled:
standing_collider.disabled = true
if crouching_collider.disabled:
crouching_collider.disabled = false
else:
if standing_collider.disabled:
standing_collider.disabled = false
if !crouching_collider.disabled:
crouching_collider.disabled = true


func _attempt_pick_up(body: Node3D) -> void:
var child_count: int = body.get_child_count()
# search recursively as PickupComponents tend to be childed near the back
for i in range(child_count-1, -1, -1):
if body.get_child(i) is not PickupComponent:
continue

var pick_up_component = body.get_child(i) as PickupComponent
# This prevents cogito projectiles
if body is CogitoProjectile and !(body as CogitoProjectile).can_pick_up:
#print("can't pick up projectile as it has just been fired")
continue

if auto_pick_up_items.has(pick_up_component.slot_data.inventory_item):
pick_up_component.interact(_player_interaction_component)
auto_picked_up_item.emit()
# Exit loop after detecting a pick up component (should only be one)
break


# TODO: may require save functionality to properly implement altering the item list
#func new_item_list(new_items: Array[InventoryItemPD]) -> void:
#auto_pick_up_items = new_items.duplicate()
#CogitoGlobals.debug_log(true,"AutoPickUpZone.gd", "Created a new auto pick up list")
#
#
#func add_item_to_list(item: InventoryItemPD) -> void:
#if !auto_pick_up_items.has(item):
#auto_pick_up_items.append(item)
#CogitoGlobals.debug_log(true,"AutoPickUpZone.gd", "Added %s to auto pick up list" % item.name)
#
#
#func remove_item_from_list(item: InventoryItemPD) -> void:
#if auto_pick_up_items.has(item):
#auto_pick_up_items.erase(item)
#CogitoGlobals.debug_log(true,"AutoPickUpZone.gd", "Removed %s from auto pick up list" % item.name)
32 changes: 32 additions & 0 deletions addons/cogito/Components/AutoPickUpZone.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
[gd_scene load_steps=8 format=3 uid="uid://cga0lxak2u2oq"]

[ext_resource type="Script" path="res://addons/cogito/Components/AutoPickUpZone.gd" id="1_nbis2"]
[ext_resource type="Script" path="res://addons/cogito/InventoryPD/CustomResources/InventoryItemPD.gd" id="2_ulsma"]
[ext_resource type="Resource" uid="uid://bqhbrpnp2tt08" path="res://addons/cogito/InventoryPD/Items/Cogito_FoamBullets.tres" id="3_jp7wc"]
[ext_resource type="Resource" uid="uid://cvupo3p844nh2" path="res://addons/cogito/InventoryPD/Items/Cogito_LaserAmmo.tres" id="4_mlplg"]
[ext_resource type="Resource" uid="uid://cupby6uqaftvv" path="res://addons/cogito/InventoryPD/Items/Cogito_Battery.tres" id="5_3whxy"]

[sub_resource type="CylinderShape3D" id="CylinderShape3D_cn2h2"]
height = 1.5
radius = 0.75

[sub_resource type="CylinderShape3D" id="CylinderShape3D_fawea"]
height = 1.0
radius = 0.75

[node name="AutoPickUpZone" type="Area3D"]
collision_layer = 0
collision_mask = 2
script = ExtResource("1_nbis2")
auto_pick_up_items = Array[ExtResource("2_ulsma")]([ExtResource("3_jp7wc"), ExtResource("4_mlplg"), ExtResource("5_3whxy")])

[node name="StandingZone" type="CollisionShape3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -0.25, 0)
shape = SubResource("CylinderShape3D_cn2h2")

[node name="CrouchingZone" type="CollisionShape3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -0.5, 0)
shape = SubResource("CylinderShape3D_fawea")

[connection signal="body_entered" from="." to="." method="_on_body_entered"]
[connection signal="body_exited" from="." to="." method="_on_body_exited"]
Loading

0 comments on commit 4c1df66

Please sign in to comment.