Add food and yam interaction

This commit is contained in:
snider committed 2022-08-14 05:40:14 -07:00
1 parent 2874c11658
commit d20642e267
41 files changed
+2747 -42

No files matched your search

+10
View File
@@ -0,0 +1,10 @@
extends Resource
class_name ItemType
export var name: String
export var visual_scene: PackedScene
func spawn_node() -> Spatial:
var node = preload("res://scenes/item.tscn").instance()
node.set_item_type(self)
return node
+6 -3
View File
@@ -87,7 +87,7 @@ func try_trigger_interact(area):
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)
$"%ItemSpawn".remove_child(item_in_hand)
item_in_hand = null
return item_dropped
@@ -96,10 +96,13 @@ func pick_up_item(item):
if item_in_hand != null:
item_dropped = drop_item_in_hand()
item_in_hand = item
$ItemSpawn.add_child(item_in_hand)
$"%ItemSpawn".add_child(item_in_hand)
print("player picked up ", item_in_hand.item_name)
return item_dropped
func has_item() -> bool:
return item_in_hand != null
func get_item_in_hand():
var item_to_return = null
if item_in_hand != null:
+6
View File
@@ -0,0 +1,6 @@
extends Resource
class_name Recipe
export var item_type_in_1: Resource
export var item_type_in_2: Resource
export var item_type_out: Resource
+10
View File
@@ -0,0 +1,10 @@
extends Resource
class_name RecipeDB
export(Array, Resource) var recipes
func find_recipe(item_type_1: Resource, item_type_2: Resource) -> Resource:
for recipe in recipes:
pass
# TODO, find recipe
return recipes[0] # This is just stubbed; remove later
+34 -1
View File
@@ -2,6 +2,36 @@ extends Spatial
export var grow_time: float = 2.0
class GrowerStateIdle:
func _on_coin_machine_coin_requirement_met(player):
pass
func _on_grow_timer_timeout():
pass
func _on_item_holder_item_changed(item):
pass
class GrowerStateGrowing:
func _on_coin_machine_coin_requirement_met(player):
pass
func _on_grow_timer_timeout():
pass
func _on_item_holder_item_changed(item):
pass
class GrowerStateBlocked:
func _on_coin_machine_coin_requirement_met(player):
pass
func _on_grow_timer_timeout():
pass
func _on_item_holder_item_changed(item):
pass
func _ready():
$grow_timer.wait_time = grow_time
@@ -10,7 +40,7 @@ func _on_coin_machine_coin_requirement_met(player):
$coin_machine.enabled = false
$grow_timer.start()
func _on_Timer_timeout():
func _on_grow_timer_timeout():
var yam = preload("res://scenes/item_pickup.tscn").instance()
yam.item_name = "yam"
yam.item_amount = 1
@@ -25,3 +55,6 @@ func _on_yam_item_tree_exited():
func _process(delta):
$"%status_label".text = "%f" % $grow_timer.time_left
func _on_item_holder_item_changed(item):
pass # Replace with function body.
@@ -0,0 +1,32 @@
extends Spatial
const StateIdle = preload("res://scripts/hydroponics_station/state/idle.gd")
const StateGrowing = preload("res://scripts/hydroponics_station/state/growing.gd")
const StateBlocked = preload("res://scripts/hydroponics_station/state/blocked.gd")
export var grow_time: float = 2.0
var state = null
func _ready():
$grow_timer.wait_time = grow_time
state = StateIdle.new()
state.ctx = self
state.enter_from(null)
print("hydroponics_station: NULL -> ", state.NAME)
func change_state(new_state):
print("hydroponics_station: ", state.NAME, " -> ", new_state.NAME)
new_state.ctx = self
state.exit_to(new_state)
new_state.enter_from(state)
state = new_state
func _on_coin_machine_coin_requirement_met(player):
state.on_coin_machine_coin_requirement_met(player)
func _on_grow_timer_timeout():
state.on_grow_timer_timeout()
func _on_item_holder_item_changed(item):
state.on_item_holder_item_changed(item)
@@ -0,0 +1,25 @@
extends Reference
const NAME = "blocked"
var ctx = null
func _init():
pass
func enter_from(state):
ctx.get_node("coin_machine").enabled = false
ctx.get_node("item_holder").add_enabled = true
func exit_to(state):
pass
func on_coin_machine_coin_requirement_met(player):
assert(false)
func on_grow_timer_timeout():
assert(false)
func on_item_holder_item_changed(item):
if item == null:
ctx.change_state(ctx.StateIdle.new())
@@ -0,0 +1,30 @@
extends Reference
const NAME = "growing"
const raw_yam = preload("res://item_types/raw_yam.tres")
var ctx = null
var mat: SpatialMaterial = SpatialMaterial.new()
func _init():
mat.albedo_color = Color.green
func enter_from(state):
ctx.get_node("coin_machine").enabled = false
ctx.get_node("item_holder").add_enabled = false
ctx.get_node("grow_timer").start()
ctx.get_node("MeshInstance").set_surface_material(0, mat)
func exit_to(state):
ctx.get_node("MeshInstance").set_surface_material(0, null)
func on_coin_machine_coin_requirement_met(player):
assert(false, "coin machine should be disabled while growing")
func on_grow_timer_timeout():
var item_holder = ctx.get_node("item_holder")
item_holder.spawn_item(raw_yam)
ctx.change_state(ctx.StateBlocked.new())
func on_item_holder_item_changed(item):
assert(item.item_type == raw_yam)
+25
View File
@@ -0,0 +1,25 @@
extends Reference
const NAME = "idle"
var ctx = null
func _init():
pass
func enter_from(state):
ctx.get_node("coin_machine").enabled = true
ctx.get_node("item_holder").add_enabled = true
func exit_to(state):
pass
func on_coin_machine_coin_requirement_met(player):
ctx.change_state(ctx.StateGrowing.new())
func on_grow_timer_timeout():
assert(false, "grow timer shouldn't trigger while idle")
func on_item_holder_item_changed(item):
assert(item != null)
ctx.change_state(ctx.StateBlocked.new())
+12 -5
View File
@@ -1,8 +1,15 @@
extends Spatial
export var item_name : String = ""
export var item_type: Resource setget set_item_type
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
func set_item_type(new_value: Resource):
item_type = new_value
if not is_inside_tree():
yield(self, "ready")
item_name = item_type.name
$ItemLabel.text = item_type.name
for child in $visual.get_children():
child.queue_free()
$visual.add_child(item_type.visual_scene.instance())
+65 -6
View File
@@ -1,17 +1,21 @@
extends Spatial
signal item_changed(item)
export var start_with_item : String = ""
export var start_with_item : Resource
export var remove_enabled := true
export var add_enabled := true
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)
if start_with_item != null:
add_item(start_with_item.spawn_node())
func remove_item():
func has_item() -> bool:
return item_in_hold != null
func remove_item() -> Node:
assert(item_in_hold != null)
var removed_item = item_in_hold
$ItemSpawn.remove_child(item_in_hold)
@@ -23,7 +27,62 @@ func add_item(item):
$ItemSpawn.add_child(item)
item_in_hold = item
func spawn_item(item_type: Resource):
assert(!has_item())
assert(item_type is ItemType)
add_item(item_type.spawn_node())
emit_signal("item_changed", item_in_hold)
func destroy_item():
var removed_item = item_in_hold
$ItemSpawn.remove_child(item_in_hold)
item_in_hold = null
removed_item.queue_free()
emit_signal("item_changed", item_in_hold)
func swap_with_player(player) -> bool:
assert(has_item() and player.has_item())
if not remove_enabled or not add_enabled:
return false
var player_item = player.drop_item_in_hand()
player.pick_up_item(remove_item())
add_item(player_item)
$AudioStreamPlayer.play()
emit_signal("item_changed", item_in_hold)
return true
func give_to_player(player) -> bool:
assert(has_item() and not player.has_item())
if not remove_enabled:
return false
var player_item = player.pick_up_item(remove_item())
$AudioStreamPlayer.play()
emit_signal("item_changed", item_in_hold)
return true
func take_from_player(player) -> bool:
assert(not has_item() and player.has_item())
if not add_enabled:
return false
add_item(player.drop_item_in_hand())
$AudioStreamPlayer.play()
emit_signal("item_changed", item_in_hold)
return true
func on_player_interact(player) -> bool:
if has_item() and player.has_item():
return swap_with_player(player)
elif has_item() and not player.has_item():
return give_to_player(player)
elif not has_item() and player.has_item():
return take_from_player(player)
var player_item = player.get_item_in_hand()
if item_in_hold == null:
if player_item == null:
+32
View File
@@ -0,0 +1,32 @@
extends Spatial
const StateIdle = preload("res://scripts/oven/state/idle.gd")
const StateCooking = preload("res://scripts/oven/state/cooking.gd")
const StateHoldingItem = preload("res://scripts/oven/state/holding_item.gd")
export var cook_time: float = 2.0
var state = null
func _ready():
$cook_timer.wait_time = cook_time
state = StateIdle.new()
state.ctx = self
state.enter_from(null)
print("hydroponics_station: NULL -> ", state.NAME)
func change_state(new_state):
print("hydroponics_station: ", state.NAME, " -> ", new_state.NAME)
new_state.ctx = self
state.exit_to(new_state)
new_state.enter_from(state)
state = new_state
func _on_coin_machine_coin_requirement_met(player):
state.on_coin_machine_coin_requirement_met(player)
func _on_cook_timer_timeout():
state.on_cook_timer_timeout()
func _on_item_holder_item_changed(item):
state.on_item_holder_item_changed(item)
+33
View File
@@ -0,0 +1,33 @@
extends Reference
const NAME = "cooking"
const cooked_yam = preload("res://item_types/cooked_yam.tres")
var ctx = null
var mat: SpatialMaterial = SpatialMaterial.new()
func _init():
mat.albedo_color = Color.red
func enter_from(state):
ctx.get_node("coin_machine").enabled = false
ctx.get_node("%item_holder").add_enabled = false
ctx.get_node("%item_holder").remove_enabled = false
ctx.get_node("cook_timer").start()
ctx.get_node("%coil").set_surface_material(0, mat)
func exit_to(state):
ctx.get_node("%coil").set_surface_material(0, null)
func on_coin_machine_coin_requirement_met(player):
assert(false, "coin machine should be disabled while growing")
func on_cook_timer_timeout():
var item_holder = ctx.get_node("%item_holder")
item_holder.destroy_item()
item_holder.spawn_item(cooked_yam)
ctx.change_state(ctx.StateHoldingItem.new())
func on_item_holder_item_changed(item):
assert(item == null || item.item_type == cooked_yam)
+35
View File
@@ -0,0 +1,35 @@
extends Reference
const NAME = "holding_item"
const raw_yam = preload("res://item_types/raw_yam.tres")
var ctx = null
func _init():
pass
func enter_from(state):
if ctx.get_node("%item_holder").item_in_hold.item_type == raw_yam:
ctx.get_node("coin_machine").enabled = true
ctx.get_node("%item_holder").add_enabled = false
ctx.get_node("%item_holder").remove_enabled = false
else:
ctx.get_node("coin_machine").enabled = false
ctx.get_node("%item_holder").add_enabled = true
ctx.get_node("%item_holder").remove_enabled = true
func exit_to(state):
pass
func on_coin_machine_coin_requirement_met(player):
if ctx.get_node("%item_holder").item_in_hold.item_type == raw_yam:
ctx.change_state(ctx.StateCooking.new())
func on_cook_timer_timeout():
assert(false)
func on_item_holder_item_changed(item):
if item == null:
ctx.change_state(ctx.StateIdle.new())
else:
ctx.change_state(ctx.StateHoldingItem.new())
+26
View File
@@ -0,0 +1,26 @@
extends Reference
const NAME = "idle"
var ctx = null
func _init():
pass
func enter_from(state):
ctx.get_node("coin_machine").enabled = false
ctx.get_node("%item_holder").add_enabled = true
ctx.get_node("%item_holder").remove_enabled = false # not possible
func exit_to(state):
pass
func on_coin_machine_coin_requirement_met(player):
assert(false, "coin machine should be disabled while idle")
func on_cook_timer_timeout():
assert(false, "cook timer shouldn't trigger while idle")
func on_item_holder_item_changed(item):
assert(item != null, "there should be no item to remove while idle")
ctx.change_state(ctx.StateHoldingItem.new())