Added item holder that can hold pickable item instances

This commit is contained in:
akshay 2022-08-14 04:46:50 -04:00
parent 8097a28cf8
commit 138519ea83
10 changed files with 122 additions and 9 deletions

View File

@ -2,11 +2,10 @@
[ext_resource path="res://scripts/coin_machine.gd" type="Script" id=1] [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"] [node name="coin_machine" type="Area"]
script = ExtResource( 1 ) script = ExtResource( 1 )
[node name="CollisionShape" type="CollisionShape" parent="."] [node name="CollisionShape" type="CollisionShape" parent="."]
transform = Transform( 2, 0, 0, 0, 2, 0, 0, 0, 2, 0, 0, 0 )
shape = SubResource( 1 ) shape = SubResource( 1 )

View File

@ -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/player.tscn" type="PackedScene" id=1]
[ext_resource path="res://scenes/moon.tscn" type="PackedScene" id=2] [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/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="dev_akshay" type="Spatial"]
[node name="moon" parent="." instance=ExtResource( 2 )] [node name="moon" parent="." instance=ExtResource( 2 )]
[node name="power_station" parent="." instance=ExtResource( 3 )] [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 time_to_generate = 5.0
[node name="Player" parent="." instance=ExtResource( 1 )] [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"

View File

@ -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://scripts/hydroponics_station.gd" type="Script" id=2]
[ext_resource path="res://scenes/coin_machine.tscn" type="PackedScene" id=3] [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 ) transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.372095, 0 )
shape = SubResource( 2 ) 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 ) 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="."] [node name="grow_timer" type="Timer" parent="."]
one_shot = true one_shot = true

16
scenes/item.tscn Normal file
View File

@ -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

19
scenes/item_holder.tscn Normal file
View File

@ -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 )

View File

@ -29,3 +29,5 @@ shape = SubResource( 2 )
[node name="Camera" type="Camera" parent="."] [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 ) transform = Transform( 1, 0, 0, 0, 0.5, 0.866025, 0, -0.866025, 0.5, 0, 5.44166, 3.39746 )
fov = 50.0 fov = 50.0
[node name="ItemSpawn" type="Spatial" parent="."]

View File

@ -13,7 +13,7 @@ script = ExtResource( 1 )
time_to_generate = 1.0 time_to_generate = 1.0
[node name="coin_machine" parent="." instance=ExtResource( 2 )] [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 coins_required = 0
[node name="MeshInstance" type="MeshInstance" parent="."] [node name="MeshInstance" type="MeshInstance" parent="."]

View File

@ -5,6 +5,8 @@ const SPEED = 3.0
export var inventory = { "ore" : 0 } export var inventory = { "ore" : 0 }
var item_in_hand
# Called when the node enters the scene tree for the first time. # Called when the node enters the scene tree for the first time.
func _ready(): func _ready():
pass # Replace with function body. pass # Replace with function body.
@ -67,3 +69,25 @@ func try_trigger_interact(area):
print("player interacted with ", owner) print("player interacted with ", owner)
return true 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

8
scripts/item.gd Normal file
View File

@ -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

38
scripts/item_holder.gd Normal file
View File

@ -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