Hệ thống Nhiệm vụ Đơn giản
C# Code
/* --- QuestGoal.cs --- */
[System.Serializable]
public class QuestGoal
{
public GoalType goalType;
public int requiredAmount;
public int currentAmount;
public bool IsReached() { return (currentAmount >= requiredAmount); }
public void EnemyKilled() { if (goalType == GoalType.Kill) currentAmount++; }
public void ItemCollected() { if (goalType == GoalType.Gather) currentAmount++; }
}
public enum GoalType { Kill, Gather }
/* --- Quest.cs (ScriptableObject) --- */
using UnityEngine;
[CreateAssetMenu(fileName = "New Quest", menuName = "Quests/Quest")]
public class Quest : ScriptableObject
{
public string title;
public string description;
public int experienceReward;
public bool isCompleted;
public QuestGoal goal;
}
/* --- QuestGiver.cs (Gắn vào NPC) --- */
using UnityEngine;
public class QuestGiver : MonoBehaviour
{
public Quest quest;
public PlayerQuest playerQuest;
public void GiveQuest()
{
playerQuest.quest = quest;
// Mở UI nhiệm vụ và cập nhật thông tin
Debug.Log("New Quest: " + quest.title);
}
}
/* --- PlayerQuest.cs (Gắn vào Player) --- */
using UnityEngine;
public class PlayerQuest : MonoBehaviour
{
public Quest quest;
public void EnemyKilled()
{
if (quest != null && !quest.isCompleted)
{
quest.goal.EnemyKilled();
if (quest.goal.IsReached())
{
CompleteQuest();
}
}
}
void CompleteQuest()
{
quest.isCompleted = true;
// Thưởng cho người chơi
Debug.Log("Quest '" + quest.title + "' completed!");
// Xóa quest hoặc đánh dấu đã hoàn thành trong UI
}
}Một cấu trúc cơ bản để tạo, theo dõi và hoàn thành nhiệm vụ, sử dụng ScriptableObject để định nghĩa nhiệm vụ.