Files
Click-PointRPG/Assets/Scripts/NPC.cs

198 lines
5.6 KiB
C#

using UnityEngine;
public enum NPCType
{
Villager,
Merchant,
Trainer,
Guard,
QuestGiver,
Other
}
public class NPC : Character
{
[Header("NPC Settings")]
public string npcName = "NPC";
public NPCType npcType = NPCType.Villager;
[Header("Interaction Capabilities")]
public bool isQuestGiver = false;
public bool isMerchant = false;
public bool isTrainer = false;
[Header("Fallback Dialogue (if no EasyTalk)")]
[TextArea(3, 10)]
public string greetingText = "Hello, traveler!";
[Header("Quest")]
public string questId = ""; // ID of the quest this NPC offers
[Header("Merchant Settings")]
[Tooltip("Gold required to access merchant services")]
public int merchantGoldRequirement = 0;
[Header("Trainer Settings")]
[Tooltip("Skills this trainer can teach")]
public string[] trainableSkills;
public int trainingCost = 100;
/// <summary>
/// Called when player interacts with this NPC
/// </summary>
public override void OnInteract(GameObject player)
{
// Call base implementation (handles look-at and dialogue)
base.OnInteract(player);
// NPC-specific interactions
if (isQuestGiver && !string.IsNullOrEmpty(questId))
{
//OfferQuest(player);
}
if (isMerchant)
{
OpenMerchantInterface(player);
}
if (isTrainer)
{
OpenTrainingInterface(player);
}
}
/// <summary>
/// Override to add NPC-specific dialogue fallback
/// </summary>
protected override void ShowDialogue(string entryPoint = null)
{
base.ShowDialogue(entryPoint);
// Fallback to simple greeting if no dialogue was shown
if (centralDialogueController == null || dialogue == null)
{
Log($"{npcName}: {greetingText}");
}
}
/// <summary>
/// Offer quest to the player
/// </summary>
protected virtual void OfferQuest(GameObject player)
{
if (QuestManager.Instance == null)
{
Debug.LogWarning("QuestManager not found in scene. Cannot offer quest.", gameObject);
return;
}
if (string.IsNullOrEmpty(questId))
{
Debug.LogWarning($"{npcName} has no questId assigned.", gameObject);
return;
}
// Check if player can accept this quest
if (QuestManager.Instance.CanAcceptQuest(questId))
{
// Start the quest
bool started = QuestManager.Instance.StartQuest(questId);
if (started)
{
Log($"{npcName} gave quest '{questId}' to player!");
}
}
else if (QuestManager.Instance.IsQuestActive(questId))
{
// Quest already active - check if it can be turned in
Quest activeQuest = QuestManager.Instance.GetActiveQuest(questId);
if (activeQuest != null && activeQuest.IsComplete())
{
Log($"{npcName}: You've completed the quest! Turn it in.");
// Player should use CompleteQuestWithNPC method or turn-in dialogue option
}
else
{
Log($"{npcName}: You already have this quest active.");
}
}
else if (QuestManager.Instance.IsQuestCompleted(questId))
{
Log($"{npcName}: You've already completed this quest.");
}
else
{
Log($"{npcName}: You don't meet the requirements for this quest yet.");
}
}
/// <summary>
/// Complete a quest with this NPC (quest turn-in)
/// </summary>
public virtual bool CompleteQuestWithNPC(string questIdToComplete)
{
if (QuestManager.Instance == null)
{
Debug.LogWarning("QuestManager not found in scene.", gameObject);
return false;
}
Quest quest = QuestManager.Instance.GetActiveQuest(questIdToComplete);
if (quest != null && quest.IsComplete())
{
bool completed = QuestManager.Instance.CompleteQuest(questIdToComplete);
if (completed)
{
Log($"{npcName}: Quest complete! Here's your reward.");
return true;
}
}
else
{
Log($"{npcName}: You haven't completed that quest yet.");
}
return false;
}
/// <summary>
/// Open merchant trading interface
/// </summary>
protected virtual void OpenMerchantInterface(GameObject player)
{
Debug.Log($"Opening merchant interface for {npcName}", gameObject);
// TODO: Implement merchant UI
}
/// <summary>
/// Open training interface for learning skills
/// </summary>
protected virtual void OpenTrainingInterface(GameObject player)
{
Debug.Log($"Opening training interface for {npcName}", gameObject);
// TODO: Implement training UI
}
/// <summary>
/// Check if this NPC can perform a specific service
/// </summary>
public bool CanProvideService(NPCType serviceType)
{
return npcType == serviceType ||
(serviceType == NPCType.Merchant && isMerchant) ||
(serviceType == NPCType.Trainer && isTrainer) ||
(serviceType == NPCType.QuestGiver && isQuestGiver);
}
/// <summary>
/// NPCs don't die by default - override for hostile NPCs
/// </summary>
public override void Die()
{
Log($"{npcName} has been defeated!");
// NPCs might drop loot, give quest credit, etc.
base.Die();
}
}