Refactor conversation stuff
This commit is contained in:
@@ -34,12 +34,21 @@ func addCallable(params:Dictionary) -> int:
|
|||||||
return params["position"]
|
return params["position"]
|
||||||
|
|
||||||
|
|
||||||
func addConversation(conversation:Array[ConversationElement]) -> Array[int]:
|
func addCallables(params:Dictionary) -> Array[int]:
|
||||||
|
assert(params.has("functions"))
|
||||||
var indexes:Array[int] = []
|
var indexes:Array[int] = []
|
||||||
for element in conversation:
|
for element in params['functions']:
|
||||||
indexes.append(addCallable({ 'function': element.start }))
|
var newDict = element.merged(params, false)
|
||||||
|
newDict.erase('functions')
|
||||||
|
indexes.append(addCallable(newDict))
|
||||||
return indexes
|
return indexes
|
||||||
|
|
||||||
|
func addConversation(resources:Array[ConversationResource]) -> Array[int]:
|
||||||
|
var callables:Array[Dictionary] = []
|
||||||
|
for resource in resources:
|
||||||
|
callables.append(resource.toCallable())
|
||||||
|
return addCallables({ 'functions': callables })
|
||||||
|
|
||||||
func start() -> void:
|
func start() -> void:
|
||||||
if queue.size() == 0:
|
if queue.size() == 0:
|
||||||
return
|
return
|
||||||
@@ -47,7 +56,7 @@ func start() -> void:
|
|||||||
assert(!running)
|
assert(!running)
|
||||||
running = true
|
running = true
|
||||||
index = 0
|
index = 0
|
||||||
while true:
|
while index < queue.size():
|
||||||
var queueItem = queue[index]
|
var queueItem = queue[index]
|
||||||
var result = await queueItem['function'].call(queueItem)
|
var result = await queueItem['function'].call(queueItem)
|
||||||
|
|
||||||
@@ -61,7 +70,7 @@ func start() -> void:
|
|||||||
_:
|
_:
|
||||||
index = result
|
index = result
|
||||||
|
|
||||||
if index >= queue.size() || index < 0:
|
if index < 0:
|
||||||
break
|
break
|
||||||
|
|
||||||
running = false
|
running = false
|
||||||
|
|||||||
12
cutscene/conversation/ConversationAction.gd
Normal file
12
cutscene/conversation/ConversationAction.gd
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
class_name ConversationAction
|
||||||
|
|
||||||
|
static func textboxCallable(params:Dictionary) -> int:
|
||||||
|
assert(params.has('label'))
|
||||||
|
await UI.TEXTBOX.setTextAndWait(params['label'])
|
||||||
|
return Cutscene.CUTSCENE_CONTINUE
|
||||||
|
|
||||||
|
static func getTextboxCallable(label:String) -> Dictionary:
|
||||||
|
return {
|
||||||
|
"function": textboxCallable,
|
||||||
|
"label": label
|
||||||
|
}
|
||||||
1
cutscene/conversation/ConversationAction.gd.uid
Normal file
1
cutscene/conversation/ConversationAction.gd.uid
Normal file
@@ -0,0 +1 @@
|
|||||||
|
uid://dng7tgnex55wp
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
extends Resource
|
|
||||||
class_name ConversationElement
|
|
||||||
|
|
||||||
@export_node_path("Entity") var entity:NodePath
|
|
||||||
@export_multiline var label: String
|
|
||||||
|
|
||||||
func start(_params:Dictionary) -> int:
|
|
||||||
await UI.TEXTBOX.setTextAndWait(label)
|
|
||||||
return Cutscene.CUTSCENE_CONTINUE
|
|
||||||
9
cutscene/conversation/ConversationResource.gd
Normal file
9
cutscene/conversation/ConversationResource.gd
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
extends Resource
|
||||||
|
class_name ConversationResource
|
||||||
|
const ConversationAction = preload("res://cutscene/conversation/ConversationAction.gd")
|
||||||
|
|
||||||
|
@export_node_path("Entity") var entity:NodePath
|
||||||
|
@export_multiline var label: String
|
||||||
|
|
||||||
|
func toCallable() -> Dictionary:
|
||||||
|
return ConversationAction.getTextboxCallable(label)
|
||||||
1
cutscene/conversation/ConversationResource.gd.uid
Normal file
1
cutscene/conversation/ConversationResource.gd.uid
Normal file
@@ -0,0 +1 @@
|
|||||||
|
uid://yn7kxdargafx
|
||||||
16
cutscene/item/ItemAction.gd
Normal file
16
cutscene/item/ItemAction.gd
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
class_name ItemAction
|
||||||
|
const ConversationAction = preload("res://cutscene/conversation/ConversationAction.gd")
|
||||||
|
|
||||||
|
static func itemGetCallable(params:Dictionary) -> int:
|
||||||
|
assert(params.has('stack'))
|
||||||
|
PARTY.BACKPACK.addStack(params['stack'])
|
||||||
|
params['cutscene'].addCallable(ConversationAction.getTextboxCallable('TEST').merged({
|
||||||
|
'position': Cutscene.CUTSCENE_ADD_NEXT,
|
||||||
|
}))
|
||||||
|
return Cutscene.CUTSCENE_CONTINUE
|
||||||
|
|
||||||
|
static func getItemCallable(itemStack:ItemStack) -> Dictionary:
|
||||||
|
return {
|
||||||
|
"function": itemGetCallable,
|
||||||
|
"stack": itemStack
|
||||||
|
}
|
||||||
1
cutscene/item/ItemAction.gd.uid
Normal file
1
cutscene/item/ItemAction.gd.uid
Normal file
@@ -0,0 +1 @@
|
|||||||
|
uid://de04o21rytdqk
|
||||||
@@ -9,12 +9,12 @@ enum InventorySortType {
|
|||||||
ITEM_KEY
|
ITEM_KEY
|
||||||
}
|
}
|
||||||
|
|
||||||
signal itemAdded(item:Item)
|
signal itemAdded(item:Item.ItemId)
|
||||||
signal itemRemoved(item:Item)
|
signal itemRemoved(item:Item.ItemId)
|
||||||
signal itemQuantityChanged(item:Item, quantity:int)
|
signal itemQuantityChanged(item:Item.ItemId, quantity:int)
|
||||||
signal inventorySorted(sortBy:InventorySortType, reverse:bool)
|
signal inventorySorted(sortBy:InventorySortType, reverse:bool)
|
||||||
|
|
||||||
func setItem(item:Item, quantity:int) -> void:
|
func setItem(item:Item.ItemId, quantity:int) -> void:
|
||||||
if quantity < 0:
|
if quantity < 0:
|
||||||
push_error("Cannot set item quantity to negative value, using 0")
|
push_error("Cannot set item quantity to negative value, using 0")
|
||||||
quantity = 0
|
quantity = 0
|
||||||
@@ -42,20 +42,23 @@ func setItem(item:Item, quantity:int) -> void:
|
|||||||
itemAdded.emit(item)
|
itemAdded.emit(item)
|
||||||
itemQuantityChanged.emit(item, quantity)
|
itemQuantityChanged.emit(item, quantity)
|
||||||
|
|
||||||
func getItemQuantity(item:Item) -> int:
|
func getItemQuantity(item:Item.ItemId) -> int:
|
||||||
for itemStack in items:
|
for itemStack in items:
|
||||||
if itemStack.item == item:
|
if itemStack.item == item:
|
||||||
return itemStack.quantity
|
return itemStack.quantity
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
func addItem(item:Item, quantity:int = 1) -> void:
|
func addItem(item:Item.ItemId, quantity:int = 1) -> void:
|
||||||
# Add can only take positive quantities, otherwise use set or remove
|
# Add can only take positive quantities, otherwise use set or remove
|
||||||
if quantity <= 0:
|
if quantity <= 0:
|
||||||
push_error("Cannot add non-positive item quantity")
|
push_error("Cannot add non-positive item quantity")
|
||||||
return
|
return
|
||||||
self.setItem(item, self.getItemQuantity(item) + quantity)
|
self.setItem(item, self.getItemQuantity(item) + quantity)
|
||||||
|
|
||||||
func removeItem(item:Item) -> void:
|
func addStack(stack:ItemStack) -> void:
|
||||||
|
self.addItem(stack.item, stack.quantity)
|
||||||
|
|
||||||
|
func removeItem(item:Item.ItemId) -> void:
|
||||||
self.setItem(item, 0)
|
self.setItem(item, 0)
|
||||||
|
|
||||||
func sort(sortBy:InventorySortType, reverse:bool = false) -> void:
|
func sort(sortBy:InventorySortType, reverse:bool = false) -> void:
|
||||||
@@ -72,7 +75,7 @@ func sort(sortBy:InventorySortType, reverse:bool = false) -> void:
|
|||||||
|
|
||||||
# Sorters
|
# Sorters
|
||||||
func _sortByItemType(a:ItemStack, b:ItemStack) -> int:
|
func _sortByItemType(a:ItemStack, b:ItemStack) -> int:
|
||||||
return int(a.item.itemType) - int(b.item.itemType)
|
return int(Item.getItemById(a.item).itemType) - int(Item.getItemById(b.item).itemType)
|
||||||
|
|
||||||
func _sortByItemKey(a:ItemStack, b:ItemStack) -> int:
|
func _sortByItemKey(a:ItemStack, b:ItemStack) -> int:
|
||||||
return a.item.key.casecmp_to(b.item.key)
|
return Item.getItemById(a.item).key.casecmp_to(Item.getItemById(b.item).key)
|
||||||
15
item/Item.gd
15
item/Item.gd
@@ -8,6 +8,11 @@ enum ItemType {
|
|||||||
KEY_ITEM,
|
KEY_ITEM,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
enum ItemId {
|
||||||
|
NULL,
|
||||||
|
POTION,
|
||||||
|
}
|
||||||
|
|
||||||
# Properties
|
# Properties
|
||||||
var itemType:ItemType
|
var itemType:ItemType
|
||||||
var key:String
|
var key:String
|
||||||
@@ -22,5 +27,11 @@ func getName() -> String:
|
|||||||
return self.key
|
return self.key
|
||||||
|
|
||||||
# Item Table
|
# Item Table
|
||||||
static var NULL:Item = Item.new("NULL", ItemType.NULL)
|
static var ITEM_DEFINITIONS = {
|
||||||
static var POTION:Item = Item.new("POTION", ItemType.MEDICINE)
|
[ItemId.NULL]: Item.new("NULL", ItemType.NULL),
|
||||||
|
[ItemId.POTION]: Item.new("POTION", ItemType.MEDICINE),
|
||||||
|
}
|
||||||
|
|
||||||
|
static func getItemById(itemId:ItemId) -> Item:
|
||||||
|
assert(ITEM_DEFINITIONS.has(itemId))
|
||||||
|
return ITEM_DEFINITIONS[itemId]
|
||||||
13
item/ItemResource.gd
Normal file
13
item/ItemResource.gd
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
extends Resource
|
||||||
|
class_name ItemResource
|
||||||
|
const Item = preload("res://item/Item.gd")
|
||||||
|
const ItemStack = preload("res://item/ItemStack.gd")
|
||||||
|
|
||||||
|
@export var item:Item.ItemId = Item.ItemId.NULL
|
||||||
|
@export var quantity:int = 0
|
||||||
|
|
||||||
|
func toItemStack() -> ItemStack:
|
||||||
|
var stack = ItemStack.new()
|
||||||
|
stack.item = item
|
||||||
|
stack.quantity = quantity
|
||||||
|
return stack
|
||||||
1
item/ItemResource.gd.uid
Normal file
1
item/ItemResource.gd.uid
Normal file
@@ -0,0 +1 @@
|
|||||||
|
uid://38ya6vphm5bu
|
||||||
@@ -1,6 +1,5 @@
|
|||||||
class_name ItemStack
|
class_name ItemStack
|
||||||
|
|
||||||
const Item = preload("res://item/Item.gd")
|
const Item = preload("res://item/Item.gd")
|
||||||
|
|
||||||
var item:Item = Item.NULL
|
var item:Item.ItemId = Item.ItemId.NULL
|
||||||
var quantity:int = 0
|
var quantity:int = 0
|
||||||
@@ -11,6 +11,7 @@ enum MovementType {
|
|||||||
enum InteractType {
|
enum InteractType {
|
||||||
NONE,
|
NONE,
|
||||||
CONVERSATION,
|
CONVERSATION,
|
||||||
|
ONE_TIME_ITEM,
|
||||||
TEST_BATTLE
|
TEST_BATTLE
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -27,7 +28,10 @@ var button := func():
|
|||||||
# Interaction settings
|
# Interaction settings
|
||||||
@export_category("Interactions")
|
@export_category("Interactions")
|
||||||
@export var interactType:InteractType = InteractType.NONE
|
@export var interactType:InteractType = InteractType.NONE
|
||||||
@export var conversation:Array[ConversationElement] = []
|
@export var conversation:Array[ConversationResource] = []
|
||||||
|
|
||||||
|
@export_category("One-Time Item")
|
||||||
|
@export var oneTimeItem:ItemResource = null
|
||||||
|
|
||||||
# TEST BATTLE
|
# TEST BATTLE
|
||||||
@export_category("Test Battle")
|
@export_category("Test Battle")
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ size = Vector3(1.3, 1.3, 1.3)
|
|||||||
|
|
||||||
[node name="Entity" type="CharacterBody3D"]
|
[node name="Entity" type="CharacterBody3D"]
|
||||||
script = ExtResource("1_8e8ef")
|
script = ExtResource("1_8e8ef")
|
||||||
|
entityId = "35ce980f-3df9-4e09-b365-1a228948cf78"
|
||||||
metadata/_custom_type_script = "uid://c8146flooxeue"
|
metadata/_custom_type_script = "uid://c8146flooxeue"
|
||||||
|
|
||||||
[node name="CollisionShape3D" type="CollisionShape3D" parent="."]
|
[node name="CollisionShape3D" type="CollisionShape3D" parent="."]
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
class_name EntityInteractableArea extends Area3D
|
class_name EntityInteractableArea extends Area3D
|
||||||
# const BattleStartAction = preload("res://cutscene/battle/BattleStartAction.gd")
|
const ItemAction = preload("res://cutscene/item/ItemAction.gd")
|
||||||
|
|
||||||
@export var entity:Entity
|
@export var entity:Entity
|
||||||
|
|
||||||
@@ -17,6 +17,15 @@ func isInteractable() -> bool:
|
|||||||
if entity.interactType == Entity.InteractType.TEST_BATTLE:
|
if entity.interactType == Entity.InteractType.TEST_BATTLE:
|
||||||
return true
|
return true
|
||||||
|
|
||||||
|
if entity.interactType == Entity.InteractType.ONE_TIME_ITEM:
|
||||||
|
if entity.oneTimeItem == null:
|
||||||
|
return false
|
||||||
|
if entity.oneTimeItem.quantity <= 0:
|
||||||
|
return false
|
||||||
|
if entity.oneTimeItem.item == Item.ItemId.NULL:
|
||||||
|
return false
|
||||||
|
return true
|
||||||
|
|
||||||
return false
|
return false
|
||||||
|
|
||||||
func _onConversationInteract(_other:Entity) -> void:
|
func _onConversationInteract(_other:Entity) -> void:
|
||||||
@@ -24,6 +33,13 @@ func _onConversationInteract(_other:Entity) -> void:
|
|||||||
cutscene.addConversation(entity.conversation)
|
cutscene.addConversation(entity.conversation)
|
||||||
cutscene.start()
|
cutscene.start()
|
||||||
|
|
||||||
|
func _onItemInteract(_other:Entity) -> void:
|
||||||
|
assert(entity.oneTimeItem != null)
|
||||||
|
var cutscene:Cutscene = Cutscene.new()
|
||||||
|
cutscene.addCallable(ItemAction.getItemCallable(entity.oneTimeItem.toItemStack()))
|
||||||
|
cutscene.addCallable(ConversationAction.getTextboxCallable('This should be last'))
|
||||||
|
cutscene.start()
|
||||||
|
|
||||||
func onInteract(other:Entity) -> void:
|
func onInteract(other:Entity) -> void:
|
||||||
if entity.interactType == Entity.InteractType.NONE:
|
if entity.interactType == Entity.InteractType.NONE:
|
||||||
return
|
return
|
||||||
@@ -33,6 +49,10 @@ func onInteract(other:Entity) -> void:
|
|||||||
_onConversationInteract(other)
|
_onConversationInteract(other)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
Entity.InteractType.ONE_TIME_ITEM:
|
||||||
|
_onItemInteract(other)
|
||||||
|
return
|
||||||
|
|
||||||
Entity.InteractType.TEST_BATTLE:
|
Entity.InteractType.TEST_BATTLE:
|
||||||
var cutscene:Cutscene = Cutscene.new()
|
var cutscene:Cutscene = Cutscene.new()
|
||||||
cutscene.addCallable(BattleStartAction.getStartBattleCallable({
|
cutscene.addCallable(BattleStartAction.getStartBattleCallable({
|
||||||
|
|||||||
@@ -1,15 +1,22 @@
|
|||||||
[gd_scene load_steps=6 format=3 uid="uid://d0ywgijpuqy0r"]
|
[gd_scene load_steps=8 format=3 uid="uid://d0ywgijpuqy0r"]
|
||||||
|
|
||||||
[ext_resource type="Script" uid="uid://xe6pcuq741xi" path="res://overworld/map/TestMap.gd" id="1_6ms5s"]
|
[ext_resource type="Script" uid="uid://xe6pcuq741xi" path="res://overworld/map/TestMap.gd" id="1_6ms5s"]
|
||||||
[ext_resource type="PackedScene" uid="uid://cluuhtfjeodwb" path="res://overworld/map/TestMapBase.tscn" id="1_ox0si"]
|
[ext_resource type="PackedScene" uid="uid://cluuhtfjeodwb" path="res://overworld/map/TestMapBase.tscn" id="1_ox0si"]
|
||||||
[ext_resource type="PackedScene" uid="uid://by4a0r2hp0w6s" path="res://overworld/entity/Entity.tscn" id="2_jmygs"]
|
[ext_resource type="PackedScene" uid="uid://by4a0r2hp0w6s" path="res://overworld/entity/Entity.tscn" id="2_jmygs"]
|
||||||
[ext_resource type="Script" uid="uid://b40rstjkpompc" path="res://cutscene/conversation/ConversationElement.gd" id="3_p7git"]
|
[ext_resource type="Script" uid="uid://yn7kxdargafx" path="res://cutscene/conversation/ConversationResource.gd" id="3_p7git"]
|
||||||
|
[ext_resource type="Script" uid="uid://38ya6vphm5bu" path="res://item/ItemResource.gd" id="4_xf0pb"]
|
||||||
|
|
||||||
[sub_resource type="Resource" id="Resource_p7git"]
|
[sub_resource type="Resource" id="Resource_xf0pb"]
|
||||||
script = ExtResource("3_p7git")
|
script = ExtResource("3_p7git")
|
||||||
entity = NodePath(".")
|
entity = NodePath(".")
|
||||||
label = "Hello!"
|
label = "Test"
|
||||||
metadata/_custom_type_script = "uid://b40rstjkpompc"
|
metadata/_custom_type_script = "uid://yn7kxdargafx"
|
||||||
|
|
||||||
|
[sub_resource type="Resource" id="Resource_125nt"]
|
||||||
|
script = ExtResource("4_xf0pb")
|
||||||
|
item = 1
|
||||||
|
quantity = 2
|
||||||
|
metadata/_custom_type_script = "uid://38ya6vphm5bu"
|
||||||
|
|
||||||
[node name="TestMap" type="Node3D"]
|
[node name="TestMap" type="Node3D"]
|
||||||
script = ExtResource("1_6ms5s")
|
script = ExtResource("1_6ms5s")
|
||||||
@@ -18,12 +25,13 @@ script = ExtResource("1_6ms5s")
|
|||||||
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0.00883961, 1.11219, 0.0142021)
|
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0.00883961, 1.11219, 0.0142021)
|
||||||
entityId = "bcabec96-8d33-4c16-a997-3bb3b0562b33"
|
entityId = "bcabec96-8d33-4c16-a997-3bb3b0562b33"
|
||||||
interactType = 1
|
interactType = 1
|
||||||
conversation = Array[ExtResource("3_p7git")]([SubResource("Resource_p7git")])
|
conversation = Array[ExtResource("3_p7git")]([SubResource("Resource_xf0pb")])
|
||||||
|
|
||||||
[node name="NotPlayer2" parent="." instance=ExtResource("2_jmygs")]
|
[node name="NotPlayer2" parent="." instance=ExtResource("2_jmygs")]
|
||||||
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0.00883961, 1.11219, 4.34543)
|
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0.00883961, 1.11219, 4.34543)
|
||||||
entityId = "ad5a1504-7fbf-45d6-b1bf-6e7af6314066"
|
entityId = "ad5a1504-7fbf-45d6-b1bf-6e7af6314066"
|
||||||
interactType = 2
|
interactType = 2
|
||||||
|
oneTimeItem = SubResource("Resource_125nt")
|
||||||
|
|
||||||
[node name="TestMapBase" parent="." instance=ExtResource("1_ox0si")]
|
[node name="TestMapBase" parent="." instance=ExtResource("1_ox0si")]
|
||||||
|
|
||||||
|
|||||||
@@ -93,3 +93,4 @@ func setText(text:String) -> void:
|
|||||||
func setTextAndWait(text:String) -> void:
|
func setTextAndWait(text:String) -> void:
|
||||||
self.setText(text);
|
self.setText(text);
|
||||||
await self.textboxClosing
|
await self.textboxClosing
|
||||||
|
await get_tree().process_frame
|
||||||
|
|||||||
Reference in New Issue
Block a user