From 138519ea834916e92fadf4bd33b4051387ca9c9f Mon Sep 17 00:00:00 2001 From: akshay Date: Sun, 14 Aug 2022 04:46:50 -0400 Subject: [PATCH] Added item holder that can hold pickable item instances --- scenes/coin_machine.tscn | 3 +-- scenes/dev-akshay.tscn | 13 +++++++++-- scenes/hydroponics_station.tscn | 6 ++---- scenes/item.tscn | 16 ++++++++++++++ scenes/item_holder.tscn | 19 +++++++++++++++++ scenes/player.tscn | 2 ++ scenes/power_station.tscn | 2 +- scripts/Player.gd | 24 +++++++++++++++++++++ scripts/item.gd | 8 +++++++ scripts/item_holder.gd | 38 +++++++++++++++++++++++++++++++++ 10 files changed, 122 insertions(+), 9 deletions(-) create mode 100644 scenes/item.tscn create mode 100644 scenes/item_holder.tscn create mode 100644 scripts/item.gd create mode 100644 scripts/item_holder.gd diff --git a/scenes/coin_machine.tscn b/scenes/coin_machine.tscn index 098f850..5771d45 100644 --- a/scenes/coin_machine.tscn +++ b/scenes/coin_machine.tscn @@ -2,11 +2,10 @@ [ext_resource path="res://scripts/coin_machine.gd" type="Script" id=1] -[sub_resource type="SphereShape" id=1] +[sub_resource type="BoxShape" id=1] [node name="coin_machine" type="Area"] script = ExtResource( 1 ) [node name="CollisionShape" type="CollisionShape" parent="."] -transform = Transform( 2, 0, 0, 0, 2, 0, 0, 0, 2, 0, 0, 0 ) shape = SubResource( 1 ) diff --git a/scenes/dev-akshay.tscn b/scenes/dev-akshay.tscn index 63a8ac6..0f2e4cb 100644 --- a/scenes/dev-akshay.tscn +++ b/scenes/dev-akshay.tscn @@ -1,15 +1,24 @@ -[gd_scene load_steps=4 format=2] +[gd_scene load_steps=5 format=2] [ext_resource path="res://scenes/player.tscn" type="PackedScene" id=1] [ext_resource path="res://scenes/moon.tscn" type="PackedScene" id=2] [ext_resource path="res://scenes/power_station.tscn" type="PackedScene" id=3] +[ext_resource path="res://scenes/item_holder.tscn" type="PackedScene" id=5] [node name="dev_akshay" type="Spatial"] [node name="moon" parent="." instance=ExtResource( 2 )] [node name="power_station" parent="." instance=ExtResource( 3 )] -transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0.00300264, 1.00556, -4.18269 ) +transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, -10.645 ) time_to_generate = 5.0 [node name="Player" parent="." instance=ExtResource( 1 )] + +[node name="item_holder" parent="." instance=ExtResource( 5 )] +transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, -4.04269, 0, -4.32055 ) +start_with_item = "Ore" + +[node name="item_holder2" parent="." instance=ExtResource( 5 )] +transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 3.179, 0, -4.17727 ) +start_with_item = "Roe" diff --git a/scenes/hydroponics_station.tscn b/scenes/hydroponics_station.tscn index 14c4d94..e129c45 100644 --- a/scenes/hydroponics_station.tscn +++ b/scenes/hydroponics_station.tscn @@ -1,6 +1,5 @@ -[gd_scene load_steps=7 format=2] +[gd_scene load_steps=6 format=2] -[ext_resource path="res://scripts/coin_machine.gd" type="Script" id=1] [ext_resource path="res://scripts/hydroponics_station.gd" type="Script" id=2] [ext_resource path="res://scenes/coin_machine.tscn" type="PackedScene" id=3] @@ -39,9 +38,8 @@ billboard = 1 transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.372095, 0 ) shape = SubResource( 2 ) -[node name="coin_machine" type="Area" parent="." instance=ExtResource( 3 )] +[node name="coin_machine" parent="." instance=ExtResource( 3 )] transform = Transform( 0.540026, 0, 0, 0, 0.571038, 0, 0, 0, 0.497953, 0, 0.418593, 0 ) -script = ExtResource( 1 ) [node name="grow_timer" type="Timer" parent="."] one_shot = true diff --git a/scenes/item.tscn b/scenes/item.tscn new file mode 100644 index 0000000..8f730e7 --- /dev/null +++ b/scenes/item.tscn @@ -0,0 +1,16 @@ +[gd_scene load_steps=3 format=2] + +[ext_resource path="res://scripts/item.gd" type="Script" id=1] + +[sub_resource type="CylinderMesh" id=1] +height = 1.0 + +[node name="item" type="Spatial"] +script = ExtResource( 1 ) + +[node name="MeshInstance" type="MeshInstance" parent="."] +mesh = SubResource( 1 ) + +[node name="ItemLabel" type="Label3D" parent="."] +transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0.0275149, 0.721019, 1.58046 ) +billboard = 1 diff --git a/scenes/item_holder.tscn b/scenes/item_holder.tscn new file mode 100644 index 0000000..75f5d3c --- /dev/null +++ b/scenes/item_holder.tscn @@ -0,0 +1,19 @@ +[gd_scene load_steps=4 format=2] + +[ext_resource path="res://assets/sound/handleSmallLeather.ogg" type="AudioStream" id=1] +[ext_resource path="res://scripts/item_holder.gd" type="Script" id=2] + +[sub_resource type="SphereShape" id=4] +radius = 1.4 + +[node name="item_holder" type="Area"] +script = ExtResource( 2 ) + +[node name="TriggerCollider" type="CollisionShape" parent="."] +shape = SubResource( 4 ) + +[node name="ItemSpawn" type="Spatial" parent="."] +transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.372095, 0 ) + +[node name="AudioStreamPlayer" type="AudioStreamPlayer" parent="."] +stream = ExtResource( 1 ) diff --git a/scenes/player.tscn b/scenes/player.tscn index 377d89e..2b5340b 100644 --- a/scenes/player.tscn +++ b/scenes/player.tscn @@ -29,3 +29,5 @@ shape = SubResource( 2 ) [node name="Camera" type="Camera" parent="."] transform = Transform( 1, 0, 0, 0, 0.5, 0.866025, 0, -0.866025, 0.5, 0, 5.44166, 3.39746 ) fov = 50.0 + +[node name="ItemSpawn" type="Spatial" parent="."] diff --git a/scenes/power_station.tscn b/scenes/power_station.tscn index 57043a7..588fa8a 100644 --- a/scenes/power_station.tscn +++ b/scenes/power_station.tscn @@ -13,7 +13,7 @@ script = ExtResource( 1 ) time_to_generate = 1.0 [node name="coin_machine" parent="." instance=ExtResource( 2 )] -transform = Transform( 4, 0, 0, 0, 2, 0, 0, 0, 4, 0, 0, 0 ) +transform = Transform( 1.4, 0, 0, 0, 1, 0, 0, 0, 1.4, 0, 0, 0 ) coins_required = 0 [node name="MeshInstance" type="MeshInstance" parent="."] diff --git a/scripts/Player.gd b/scripts/Player.gd index c45af56..ba63cdd 100644 --- a/scripts/Player.gd +++ b/scripts/Player.gd @@ -5,6 +5,8 @@ const SPEED = 3.0 export var inventory = { "ore" : 0 } +var item_in_hand + # Called when the node enters the scene tree for the first time. func _ready(): pass # Replace with function body. @@ -67,3 +69,25 @@ func try_trigger_interact(area): print("player interacted with ", owner) return true + +func drop_item_in_hand(): + print("player dropped ", item_in_hand.item_name) + var item_dropped = item_in_hand + $ItemSpawn.remove_child(item_in_hand) + item_in_hand = null + return item_dropped + +func pick_up_item(item): + var item_dropped = null + if item_in_hand != null: + item_dropped = drop_item_in_hand() + item_in_hand = item + $ItemSpawn.add_child(item_in_hand) + print("player picked up ", item_in_hand.item_name) + return item_dropped + +func get_item_in_hand(): + var item_to_return = null + if item_in_hand != null: + item_to_return = drop_item_in_hand() + return item_to_return diff --git a/scripts/item.gd b/scripts/item.gd new file mode 100644 index 0000000..e2ccf4a --- /dev/null +++ b/scripts/item.gd @@ -0,0 +1,8 @@ +extends Spatial + +export var item_name : String = "" + +# Called when the node enters the scene tree for the first time. +func _ready(): + assert(item_name != "") + $ItemLabel.text = "Item: %s" % item_name diff --git a/scripts/item_holder.gd b/scripts/item_holder.gd new file mode 100644 index 0000000..8c4c926 --- /dev/null +++ b/scripts/item_holder.gd @@ -0,0 +1,38 @@ +extends Spatial + +export var start_with_item : String = "" + +var item_in_hold + +# Called when the node enters the scene tree for the first time. +func _ready(): + if start_with_item != "": + var item_instance = preload("res://scenes/item.tscn").instance() + item_instance.item_name = start_with_item + add_item(item_instance) + +func remove_item(): + assert(item_in_hold != null) + var removed_item = item_in_hold + $ItemSpawn.remove_child(item_in_hold) + item_in_hold = null + return removed_item + +func add_item(item): + assert(item_in_hold == null) + $ItemSpawn.add_child(item) + item_in_hold = item + +func on_player_interact(player) -> bool: + var player_item = player.get_item_in_hand() + if item_in_hold == null: + if player_item == null: + return false + add_item(player_item) + else: + var item_pickup = remove_item() + player.pick_up_item(item_pickup) + $AudioStreamPlayer.play() + if player_item != null: + add_item(player_item) + return true