Add Snus Dealer example vendor NPC

pull/37/head
John Montagu, the 4th Earl of Sandvich 2024-06-01 15:43:46 -07:00
parent 3c8ebd8dcd
commit a590aa9209
Signed by: sandvich
GPG Key ID: 9A39BE37E602B22D
4 changed files with 65 additions and 16 deletions

View File

@ -3,6 +3,7 @@
using Godot; using Godot;
using SupaLidlGame.Extensions; using SupaLidlGame.Extensions;
using SupaLidlGame.Items; using SupaLidlGame.Items;
using SupaLidlGame.Utils;
using System; using System;
namespace SupaLidlGame.Characters; namespace SupaLidlGame.Characters;
@ -127,6 +128,37 @@ public partial class NPC : Character
return bestChar; return bestChar;
} }
/// <summary>
/// Finds the best character whose faction aligns with this character's.
/// </summary>
public virtual Character FindBestNeutral()
{
float bestScore = float.MaxValue;
Character bestChar = null;
// NOTE: this relies on all Characters being under the Entities node
foreach (Node node in GetParent().GetChildren())
{
if (node is Character character)
{
bool isFriendly = ((IFaction)this).AlignsWith(character);
if (isFriendly || character.Health <= 0)
{
continue;
}
float score = 0;
score -= Position.DistanceTo(character.Position);
if (score < bestScore)
{
bestScore = score;
bestChar = character;
}
}
}
return bestChar;
}
public override void _Process(double delta) public override void _Process(double delta)
{ {
ThinkerStateMachine.Process(delta); ThinkerStateMachine.Process(delta);

View File

@ -4,14 +4,14 @@
[ext_resource type="Script" path="res://State/Character/CharacterStateMachine.cs" id="2_kynkg"] [ext_resource type="Script" path="res://State/Character/CharacterStateMachine.cs" id="2_kynkg"]
[ext_resource type="Texture2D" uid="uid://bej8thq7ruyty" path="res://Assets/Sprites/Characters/forsen2.png" id="2_s5nik"] [ext_resource type="Texture2D" uid="uid://bej8thq7ruyty" path="res://Assets/Sprites/Characters/forsen2.png" id="2_s5nik"]
[ext_resource type="Script" path="res://State/Character/NPCIdleState.cs" id="3_pcrll"] [ext_resource type="Script" path="res://State/Character/NPCIdleState.cs" id="3_pcrll"]
[ext_resource type="Script" path="res://State/Thinker/IdleState.cs" id="3_rgc42"]
[ext_resource type="Script" path="res://State/Thinker/ThinkerStateMachine.cs" id="4_mo4wj"] [ext_resource type="Script" path="res://State/Thinker/ThinkerStateMachine.cs" id="4_mo4wj"]
[ext_resource type="Script" path="res://State/Thinker/VendorIdle.cs" id="5_oau5d"]
[ext_resource type="PackedScene" uid="uid://dldnp8eunxj3q" path="res://BoundingBoxes/InteractionTrigger.tscn" id="5_sjs24"] [ext_resource type="PackedScene" uid="uid://dldnp8eunxj3q" path="res://BoundingBoxes/InteractionTrigger.tscn" id="5_sjs24"]
[ext_resource type="Script" path="res://Utils/InteractionTriggerDialogue.cs" id="5_yknpw"] [ext_resource type="Script" path="res://Utils/InteractionTriggerDialogue.cs" id="5_yknpw"]
[ext_resource type="Resource" uid="uid://c4n7vhoxybu70" path="res://Dialogue/snus-dealer.dialogue" id="6_isvnq"] [ext_resource type="Resource" uid="uid://c4n7vhoxybu70" path="res://Dialogue/snus-dealer.dialogue" id="6_isvnq"]
[ext_resource type="Script" path="res://Items/Inventory.cs" id="7_vip6b"] [ext_resource type="Script" path="res://Items/Inventory.cs" id="7_vip6b"]
[node name="Character" type="CharacterBody2D" node_paths=PackedStringArray("ThinkerStateMachine", "Sprite", "Inventory", "StateMachine")] [node name="SnusDealer" type="CharacterBody2D" node_paths=PackedStringArray("ThinkerStateMachine", "Sprite", "Inventory", "StateMachine")]
script = ExtResource("1_04gcf") script = ExtResource("1_04gcf")
ThinkerStateMachine = NodePath("Thinker") ThinkerStateMachine = NodePath("Thinker")
Sprite = NodePath("Sprites/Sprite") Sprite = NodePath("Sprites/Sprite")
@ -32,7 +32,7 @@ script = ExtResource("4_mo4wj")
InitialState = NodePath("Idle") InitialState = NodePath("Idle")
[node name="Idle" type="Node" parent="Thinker" node_paths=PackedStringArray("NPC")] [node name="Idle" type="Node" parent="Thinker" node_paths=PackedStringArray("NPC")]
script = ExtResource("3_rgc42") script = ExtResource("5_oau5d")
NPC = NodePath("../..") NPC = NodePath("../..")
[node name="Animations" type="Node" parent="."] [node name="Animations" type="Node" parent="."]
@ -64,7 +64,7 @@ position = Vector2(0, -4)
script = ExtResource("5_yknpw") script = ExtResource("5_yknpw")
InteractionTrigger = NodePath("InteractionTrigger") InteractionTrigger = NodePath("InteractionTrigger")
DialogueResource = ExtResource("6_isvnq") DialogueResource = ExtResource("6_isvnq")
DialogueTitle = "shop" DialogueTitle = "start"
[node name="InteractionTrigger" parent="Interaction" instance=ExtResource("5_sjs24")] [node name="InteractionTrigger" parent="Interaction" instance=ExtResource("5_sjs24")]

View File

@ -1,23 +1,23 @@
~ start ~ start
Snus Dealer: d Snus Dealer: Hey kid, wanna buy some snus?
- Alright. => shop
% => test - No, I don't think so. => END
% => dont_snus - Snus? => dont_snus
=> END
~ test
Snus Dealer: asdadsadasd
=> END => END
~ dont_snus ~ dont_snus
Snus Dealer: You know what they say.
Snus Dealer: If you don't snus... Snus Dealer: If you don't snus...
Snus Dealer: you lose. Snus Dealer: you lose.
- Pepepains
=> start
~ shop
do emit("EnterShop", "res://Items/Shops/SnusDealer.tres")
=> END => END
~ shop

View File

@ -0,0 +1,17 @@
using Godot;
using GodotUtilities;
namespace SupaLidlGame.State.Thinker;
public partial class VendorIdle : ThinkerState
{
public override ThinkerState Think()
{
var bestNeutral = NPC.FindBestNeutral();
if (bestNeutral is not null)
{
NPC.Target = bestNeutral.Position - NPC.Position;
}
return null;
}
}