From fb54a67393a2f1f6336994a272f074ddc81ffea3 Mon Sep 17 00:00:00 2001 From: Ichicoro Date: Sun, 8 Jan 2023 13:47:44 +0100 Subject: [PATCH] Use singleton for Player access and despawn pineapple on Player collision --- Singletons.gd | 9 +++++++++ island/MoveTowardsPlayer.gd | 18 +++++++++++++----- island/Pineapple.tscn | 34 ++++++++++++++++++++++++++++------ island/PineappleMesh.tscn | 2 +- project.godot | 3 +++ 5 files changed, 54 insertions(+), 12 deletions(-) create mode 100644 Singletons.gd diff --git a/Singletons.gd b/Singletons.gd new file mode 100644 index 0000000..13726c8 --- /dev/null +++ b/Singletons.gd @@ -0,0 +1,9 @@ +extends Node + +var player: Player + +func _ready() -> void: + pass + +func _process(delta: float) -> void: + pass diff --git a/island/MoveTowardsPlayer.gd b/island/MoveTowardsPlayer.gd index 9a7f0a6..19f06e9 100644 --- a/island/MoveTowardsPlayer.gd +++ b/island/MoveTowardsPlayer.gd @@ -2,11 +2,12 @@ extends Node @export var force_multiplier: float @export var target: RigidBody3D +@export var capture_radius: float = 1.5 -# Must be BELOW the player node to work! @onready var magnet_area: Area3D = $MagnetArea -@onready var player: CharacterBody3D = get_tree().root.find_child("Player", true, false) - +@onready var capture_area: Area3D = $CaptureArea +@onready var pop: AudioStreamPlayer3D = $"../Pop" +@onready var mesh: MeshInstance3D = $"../PineappleMesh" var captured = false @@ -17,12 +18,19 @@ func capture(): func _physics_process(delta): if captured: - var direction = player.position - target.position + var direction = Singletons.player.position - target.position var force = direction.normalized() * force_multiplier * delta target.apply_force(force) func _on_magnet_area_body_entered(body: Node3D): - if body.name == "Player": + if body is Player: print("Player captured pickup!") capture() + + +func _on_capture_area_body_entered(body: Node3D) -> void: + mesh.visible = false + pop.play() + await get_tree().create_timer(1).timeout + target.queue_free() diff --git a/island/Pineapple.tscn b/island/Pineapple.tscn index 40bdfd7..61a6265 100644 --- a/island/Pineapple.tscn +++ b/island/Pineapple.tscn @@ -1,28 +1,40 @@ -[gd_scene load_steps=5 format=3 uid="uid://v7bv81krh12e"] +[gd_scene load_steps=8 format=3 uid="uid://v7bv81krh12e"] [ext_resource type="PackedScene" uid="uid://bwwpi0itor6cw" path="res://island/PineappleMesh.tscn" id="1_ndmmp"] [ext_resource type="Script" path="res://island/MoveTowardsPlayer.gd" id="2_d3itl"] +[ext_resource type="AudioStream" uid="uid://lkftwgdun745" path="res://assets/pop.wav" id="3_b3ttd"] +[ext_resource type="Script" path="res://island/Pop.gd" id="4_rn5e2"] -[sub_resource type="CylinderShape3D" id="CylinderShape3D_t5bjw"] +[sub_resource type="CylinderShape3D" id="CylinderShape3D_xodvb"] height = 0.9 radius = 0.3 -[sub_resource type="CylinderShape3D" id="CylinderShape3D_m5gp0"] +[sub_resource type="CylinderShape3D" id="CylinderShape3D_fghe6"] height = 6.0 radius = 3.0 +[sub_resource type="CylinderShape3D" id="CylinderShape3D_j5xpy"] +height = 1.4 +radius = 0.7 + [node name="Pineapple" type="RigidBody3D"] collision_layer = 2 -collision_mask = 3 +collision_mask = 5 lock_rotation = true linear_damp = 1.0 [node name="Shape" type="CollisionShape3D" parent="."] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.45, 0) -shape = SubResource("CylinderShape3D_t5bjw") +shape = SubResource("CylinderShape3D_xodvb") [node name="PineappleMesh" parent="." instance=ExtResource("1_ndmmp")] +[node name="Pop" type="AudioStreamPlayer3D" parent="."] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0.5, 0.1, 0.5) +stream = ExtResource("3_b3ttd") +attenuation_filter_cutoff_hz = 20500.0 +script = ExtResource("4_rn5e2") + [node name="MoveTowardsPlayer" type="Node3D" parent="." node_paths=PackedStringArray("target")] script = ExtResource("2_d3itl") force_multiplier = 1250.0 @@ -34,6 +46,16 @@ monitorable = false [node name="Shape" type="CollisionShape3D" parent="MoveTowardsPlayer/MagnetArea"] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.5, 0) -shape = SubResource("CylinderShape3D_m5gp0") +shape = SubResource("CylinderShape3D_fghe6") + +[node name="CaptureArea" type="Area3D" parent="MoveTowardsPlayer"] +collision_layer = 0 +collision_mask = 0 +monitorable = false + +[node name="Shape" type="CollisionShape3D" parent="MoveTowardsPlayer/CaptureArea"] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.5, 0) +shape = SubResource("CylinderShape3D_j5xpy") [connection signal="body_entered" from="MoveTowardsPlayer/MagnetArea" to="MoveTowardsPlayer" method="_on_magnet_area_body_entered"] +[connection signal="body_entered" from="MoveTowardsPlayer/CaptureArea" to="MoveTowardsPlayer" method="_on_capture_area_body_entered"] diff --git a/island/PineappleMesh.tscn b/island/PineappleMesh.tscn index ba6f352..86a1c40 100644 --- a/island/PineappleMesh.tscn +++ b/island/PineappleMesh.tscn @@ -8,7 +8,7 @@ transparency = 1 shading_mode = 0 albedo_texture = ExtResource("1_x2h5q") texture_filter = 0 -billboard_mode = 2 +billboard_mode = 1 [sub_resource type="ArrayMesh" id="ArrayMesh_sjes8"] _surfaces = [{ diff --git a/project.godot b/project.godot index 9136e38..e6ccd89 100644 --- a/project.godot +++ b/project.godot @@ -118,6 +118,9 @@ fire={ 3d_physics/layer_1="Default" 3d_physics/layer_2="Pickups" +3d_physics/layer_3="Player" +3d_physics/layer_4="Terrain" +3d_physics/layer_5="Planted seed" [rendering]