增加任务逻辑
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
using System;
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
namespace TcgEngine
|
||||
@@ -208,4 +208,45 @@ namespace TcgEngine
|
||||
public string last_online_time;
|
||||
}
|
||||
|
||||
// 任务相关请求和响应结构
|
||||
[Serializable]
|
||||
public struct TaskDataResponse
|
||||
{
|
||||
public string id;
|
||||
public string name;
|
||||
public string desc;
|
||||
public int condition;
|
||||
public int value1;
|
||||
public string value2;
|
||||
public string value3;
|
||||
public int[] rewardTypes;
|
||||
public int[] rewardNums;
|
||||
public bool isDailyTask;
|
||||
public int durationHours;
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
public struct PlayerTaskResponse
|
||||
{
|
||||
public string taskId;
|
||||
public long assignedTime;
|
||||
public long expireTime;
|
||||
public int status;
|
||||
public int progress;
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
public struct PlayerTasksResponse
|
||||
{
|
||||
public PlayerTaskResponse[] tasks;
|
||||
public long lastDailyTaskAssigned;
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
public struct PlayerTaskSaveRequest
|
||||
{
|
||||
public PlayerTaskResponse[] tasks;
|
||||
public long lastDailyTaskAssigned;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
116
Assets/TcgEngine/Scripts/Data/TaskData.cs
Normal file
116
Assets/TcgEngine/Scripts/Data/TaskData.cs
Normal file
@@ -0,0 +1,116 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace TcgEngine
|
||||
{
|
||||
/// <summary>
|
||||
/// 任务配置数据,用于定义各种任务的属性和条件
|
||||
/// </summary>
|
||||
[CreateAssetMenu(fileName = "TaskData", menuName = "TcgEngine/TaskData")]
|
||||
public class TaskData : ScriptableObject
|
||||
{
|
||||
public string id;
|
||||
public string name;
|
||||
public string desc;
|
||||
public TaskConditionType condition;
|
||||
public int value1; // 任务进度上限
|
||||
public string value2; // 边界条件1
|
||||
public string value3; // 边界条件2
|
||||
public TaskRewardType[] rewardTypes;
|
||||
public int[] rewardNums;
|
||||
public bool isDailyTask = true; // 是否为每日任务
|
||||
public int durationHours = 24; // 任务有效期(小时)
|
||||
|
||||
// 从服务器响应数据创建任务配置
|
||||
public static TaskData CreateFromResponse(TaskDataResponse response)
|
||||
{
|
||||
TaskData taskData = ScriptableObject.CreateInstance<TaskData>();
|
||||
taskData.id = response.id;
|
||||
taskData.name = response.name;
|
||||
taskData.desc = response.desc;
|
||||
taskData.condition = (TaskConditionType)response.condition;
|
||||
taskData.value1 = response.value1;
|
||||
taskData.value2 = response.value2;
|
||||
taskData.value3 = response.value3;
|
||||
|
||||
// 转换奖励类型数组
|
||||
taskData.rewardTypes = new TaskRewardType[response.rewardTypes.Length];
|
||||
for (int i = 0; i < response.rewardTypes.Length; i++)
|
||||
{
|
||||
taskData.rewardTypes[i] = (TaskRewardType)response.rewardTypes[i];
|
||||
}
|
||||
|
||||
taskData.rewardNums = response.rewardNums;
|
||||
taskData.isDailyTask = response.isDailyTask;
|
||||
taskData.durationHours = response.durationHours;
|
||||
|
||||
return taskData;
|
||||
}
|
||||
}
|
||||
|
||||
public enum TaskConditionType
|
||||
{
|
||||
LoginGame = 1, // 登入游戏
|
||||
PlayGames = 2, // 进行X场对战
|
||||
WinGames = 3, // 胜利X场
|
||||
DefeatHeroWithAttributes = 4, // 击败Y属性和Z属性的英雄X次
|
||||
SummonHeroWithAttributes = 5, // 召唤Y属性和Z属性的英雄X次
|
||||
UseHeroSkillWithAttributes = 6 // 使用Y属性和Z属性英雄的技能X次
|
||||
}
|
||||
|
||||
public enum TaskRewardType
|
||||
{
|
||||
Coins = 0, // 金币
|
||||
// 后续可扩展其他奖励类型
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
public class PlayerTask
|
||||
{
|
||||
public string taskId;
|
||||
public DateTime assignedTime;
|
||||
public DateTime expireTime;
|
||||
public TaskStatus status;
|
||||
public int progress; // 当前进度
|
||||
|
||||
public PlayerTask(TaskData taskConfig)
|
||||
{
|
||||
taskId = taskConfig.id;
|
||||
assignedTime = DateTime.Now;
|
||||
expireTime = assignedTime.AddHours(taskConfig.durationHours);
|
||||
status = TaskStatus.Active;
|
||||
progress = 0;
|
||||
}
|
||||
|
||||
// 从服务器响应数据创建玩家任务
|
||||
public PlayerTask(PlayerTaskResponse response)
|
||||
{
|
||||
taskId = response.taskId;
|
||||
assignedTime = new DateTime(response.assignedTime);
|
||||
expireTime = new DateTime(response.expireTime);
|
||||
status = (TaskStatus)response.status;
|
||||
progress = response.progress;
|
||||
}
|
||||
|
||||
// 转换为服务器响应数据
|
||||
public PlayerTaskResponse ToResponse()
|
||||
{
|
||||
PlayerTaskResponse response = new PlayerTaskResponse();
|
||||
response.taskId = taskId;
|
||||
response.assignedTime = assignedTime.Ticks;
|
||||
response.expireTime = expireTime.Ticks;
|
||||
response.status = (int)status;
|
||||
response.progress = progress;
|
||||
return response;
|
||||
}
|
||||
}
|
||||
|
||||
public enum TaskStatus
|
||||
{
|
||||
Active = 0, // 激活
|
||||
Completed = 1, // 完成
|
||||
Expired = 2, // 过期
|
||||
Claimed = 3 // 已领取
|
||||
}
|
||||
}
|
||||
11
Assets/TcgEngine/Scripts/Data/TaskData.cs.meta
Normal file
11
Assets/TcgEngine/Scripts/Data/TaskData.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 951a52e8fe4069e41963e049a390811f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,43 @@
|
||||
using UnityEngine;
|
||||
using TcgEngine.Client;
|
||||
using TcgEngine.Gameplay;
|
||||
|
||||
namespace TcgEngine.Client
|
||||
{
|
||||
/// <summary>
|
||||
/// 游戏客户端任务集成,用于将任务系统集成到游戏客户端中
|
||||
/// </summary>
|
||||
public class GameClientTaskIntegration : MonoBehaviour
|
||||
{
|
||||
private void Start()
|
||||
{
|
||||
// 在游戏客户端启动时初始化任务系统
|
||||
GameClient client = GameClient.Get();
|
||||
if (client != null)
|
||||
{
|
||||
// 玩家连接到游戏服务器时触发任务检查
|
||||
client.onConnectServer += OnConnectToServer;
|
||||
}
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
// 取消订阅事件
|
||||
GameClient client = GameClient.Get();
|
||||
if (client != null)
|
||||
{
|
||||
client.onConnectServer -= OnConnectToServer;
|
||||
}
|
||||
}
|
||||
|
||||
private void OnConnectToServer()
|
||||
{
|
||||
// 玩家登录时检查任务
|
||||
TaskManager taskManager = TaskManager.Instance;
|
||||
if (taskManager != null)
|
||||
{
|
||||
taskManager.OnPlayerLogin();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 222f46c5f21da1d47a9d2fed11d3d48c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
553
Assets/TcgEngine/Scripts/GameLogic/TaskManager.cs
Normal file
553
Assets/TcgEngine/Scripts/GameLogic/TaskManager.cs
Normal file
@@ -0,0 +1,553 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
using TcgEngine;
|
||||
using TcgEngine.Gameplay;
|
||||
using TcgEngine.Client;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace TcgEngine.Gameplay
|
||||
{
|
||||
/// <summary>
|
||||
/// 任务管理器,负责任务的分配、进度追踪和奖励发放等功能
|
||||
/// </summary>
|
||||
public class TaskManager : MonoBehaviour
|
||||
{
|
||||
public static TaskManager Instance;
|
||||
|
||||
[Header("Player Data")]
|
||||
public List<PlayerTask> playerTasks = new List<PlayerTask>();
|
||||
public DateTime lastDailyTaskAssigned;
|
||||
public int maxTasks = 5; // 玩家最多持有任务数
|
||||
|
||||
private GameLogic gameLogic;
|
||||
private System.Random random = new System.Random();
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
if (Instance == null)
|
||||
{
|
||||
Instance = this;
|
||||
DontDestroyOnLoad(gameObject);
|
||||
}
|
||||
else
|
||||
{
|
||||
Destroy(gameObject);
|
||||
}
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
LoadTasks();
|
||||
LoadPlayerData();
|
||||
}
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
// 订阅游戏事件
|
||||
GameClient client = GameClient.Get();
|
||||
if (client != null)
|
||||
{
|
||||
client.onGameStart += OnGameStart;
|
||||
client.onGameEnd += OnGameEnd;
|
||||
}
|
||||
|
||||
// 移除对GameLogic.Instance的错误引用,改为检查gameLogic变量
|
||||
if (gameLogic != null)
|
||||
{
|
||||
SubscribeToGameEvents();
|
||||
}
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
// 取消订阅游戏事件
|
||||
GameClient client = GameClient.Get();
|
||||
if (client != null)
|
||||
{
|
||||
client.onGameStart -= OnGameStart;
|
||||
client.onGameEnd -= OnGameEnd;
|
||||
}
|
||||
|
||||
// 移除对GameLogic.Instance的错误引用,改为检查gameLogic变量
|
||||
if (gameLogic != null)
|
||||
{
|
||||
UnsubscribeFromGameEvents();
|
||||
}
|
||||
}
|
||||
|
||||
public void SetGameLogic(GameLogic logic)
|
||||
{
|
||||
gameLogic = logic;
|
||||
SubscribeToGameEvents();
|
||||
}
|
||||
|
||||
private void SubscribeToGameEvents()
|
||||
{
|
||||
if (gameLogic != null)
|
||||
{
|
||||
gameLogic.onCardPlayed += OnCardPlayed;
|
||||
gameLogic.onCardSummoned += OnCardSummoned;
|
||||
gameLogic.onAbilityStart += OnAbilityUsed;
|
||||
gameLogic.onCardDiscarded += OnCardDiscarded; // 用于检测击败英雄
|
||||
}
|
||||
}
|
||||
|
||||
private void UnsubscribeFromGameEvents()
|
||||
{
|
||||
if (gameLogic != null)
|
||||
{
|
||||
gameLogic.onCardPlayed -= OnCardPlayed;
|
||||
gameLogic.onCardSummoned -= OnCardSummoned;
|
||||
gameLogic.onAbilityStart -= OnAbilityUsed;
|
||||
gameLogic.onCardDiscarded -= OnCardDiscarded;
|
||||
}
|
||||
}
|
||||
|
||||
private async void LoadTasks()
|
||||
{
|
||||
// 从服务器加载所有任务配置
|
||||
await LoadTasksFromServer();
|
||||
}
|
||||
|
||||
private async Task LoadTasksFromServer()
|
||||
{
|
||||
if (ApiClient.Get() != null && ApiClient.Get().IsLoggedIn())
|
||||
{
|
||||
// 从服务器API获取任务配置
|
||||
string url = ApiClient.ServerURL + "/tasks";
|
||||
WebResponse res = await ApiClient.Get().SendGetRequest(url);
|
||||
|
||||
if (res.success)
|
||||
{
|
||||
// 解析任务配置数据
|
||||
try
|
||||
{
|
||||
TaskDataResponse[] taskResponses = ApiTool.JsonToObject<TaskDataResponse[]>(res.data);
|
||||
Debug.Log("Loaded " + taskResponses.Length + " tasks from server");
|
||||
// 在实际项目中,这里应该将服务器数据转换为TaskData对象并存储在内存中
|
||||
// 供后续使用,而不是每次都从Resources加载
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Debug.LogError("Failed to parse task data from server: " + e.Message);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogWarning("Failed to load tasks from server: " + res.error);
|
||||
}
|
||||
}
|
||||
|
||||
// 如果无法从服务器加载,则使用本地Resources作为后备
|
||||
TaskData[] localTasks = Resources.LoadAll<TaskData>("Tasks");
|
||||
Debug.Log("Loaded " + localTasks.Length + " tasks from local resources as fallback");
|
||||
}
|
||||
|
||||
private async void LoadPlayerData()
|
||||
{
|
||||
// 从服务器加载玩家任务数据
|
||||
await LoadPlayerTasksFromServer();
|
||||
}
|
||||
|
||||
private async Task LoadPlayerTasksFromServer()
|
||||
{
|
||||
if (ApiClient.Get() != null && ApiClient.Get().IsLoggedIn())
|
||||
{
|
||||
// 从服务器获取玩家任务数据
|
||||
string url = ApiClient.ServerURL + "/users/" + ApiClient.Get().UserID + "/tasks";
|
||||
WebResponse res = await ApiClient.Get().SendGetRequest(url);
|
||||
|
||||
if (res.success)
|
||||
{
|
||||
// 解析玩家任务数据
|
||||
try
|
||||
{
|
||||
PlayerTasksResponse response = ApiTool.JsonToObject<PlayerTasksResponse>(res.data);
|
||||
lastDailyTaskAssigned = new DateTime(response.lastDailyTaskAssigned);
|
||||
|
||||
// 清除现有任务
|
||||
playerTasks.Clear();
|
||||
|
||||
// 转换并添加任务
|
||||
foreach (var taskResponse in response.tasks)
|
||||
{
|
||||
PlayerTask playerTask = new PlayerTask(taskResponse);
|
||||
playerTasks.Add(playerTask);
|
||||
}
|
||||
|
||||
Debug.Log("Player tasks loaded from server: " + playerTasks.Count + " tasks");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Debug.LogError("Failed to parse player tasks from server: " + e.Message);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogWarning("Failed to load player tasks from server: " + res.error);
|
||||
}
|
||||
}
|
||||
|
||||
// 简化处理,首次登录时分配任务
|
||||
if (playerTasks.Count == 0)
|
||||
{
|
||||
AssignDailyTaskIfNeeded();
|
||||
}
|
||||
}
|
||||
|
||||
public async void SavePlayerData()
|
||||
{
|
||||
// 将玩家任务数据保存到服务器
|
||||
if (ApiClient.Get() != null && ApiClient.Get().IsLoggedIn())
|
||||
{
|
||||
// 准备要发送的数据
|
||||
PlayerTasksResponse saveData = new PlayerTasksResponse();
|
||||
|
||||
// 转换任务数据
|
||||
PlayerTaskResponse[] taskResponses = new PlayerTaskResponse[playerTasks.Count];
|
||||
for (int i = 0; i < playerTasks.Count; i++)
|
||||
{
|
||||
taskResponses[i] = playerTasks[i].ToResponse();
|
||||
}
|
||||
|
||||
saveData.tasks = taskResponses;
|
||||
saveData.lastDailyTaskAssigned = lastDailyTaskAssigned.Ticks;
|
||||
|
||||
string json = ApiTool.ToJson(saveData);
|
||||
string url = ApiClient.ServerURL + "/users/" + ApiClient.Get().UserID + "/tasks";
|
||||
WebResponse res = await ApiClient.Get().SendPostRequest(url, json);
|
||||
|
||||
if (res.success)
|
||||
{
|
||||
Debug.Log("Player tasks saved to server");
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogWarning("Failed to save player tasks to server: " + res.error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 每日任务分配
|
||||
public void AssignDailyTaskIfNeeded()
|
||||
{
|
||||
// 检查是否应该分配新任务(每日一次)
|
||||
DateTime now = DateTime.Now;
|
||||
if ((now - lastDailyTaskAssigned).TotalHours < 24)
|
||||
return;
|
||||
|
||||
// 检查玩家是否已达到最大任务数
|
||||
if (playerTasks.Count >= maxTasks)
|
||||
return;
|
||||
|
||||
// 获取所有每日任务
|
||||
TaskData[] allTasks = Resources.LoadAll<TaskData>("Tasks");
|
||||
var dailyTasks = allTasks
|
||||
.Where(t => t.isDailyTask &&
|
||||
t.condition != 0 &&
|
||||
!string.IsNullOrEmpty(t.id))
|
||||
.ToArray();
|
||||
|
||||
if (dailyTasks.Length == 0)
|
||||
return;
|
||||
|
||||
// 避免重复分配相同任务
|
||||
int maxRetries = 3;
|
||||
TaskData selectedTask = null;
|
||||
HashSet<string> triedTasks = new HashSet<string>();
|
||||
|
||||
while (maxRetries-- > 0 && selectedTask == null)
|
||||
{
|
||||
// 随机选择一个任务
|
||||
TaskData candidateTask = dailyTasks[random.Next(dailyTasks.Length)];
|
||||
|
||||
// 如果任务已经尝试过则跳过
|
||||
if (triedTasks.Contains(candidateTask.id))
|
||||
continue;
|
||||
|
||||
triedTasks.Add(candidateTask.id);
|
||||
|
||||
// 检查任务是否有效(未被玩家完成过或未在进度中)
|
||||
if (!playerTasks.Any(pt => pt.taskId == candidateTask.id))
|
||||
{
|
||||
selectedTask = candidateTask;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// 如果没有找到合适的任务则返回
|
||||
if (selectedTask == null)
|
||||
{
|
||||
Debug.LogWarning("Failed to assign daily task after multiple retries");
|
||||
return;
|
||||
}
|
||||
|
||||
// 创建玩家任务实例
|
||||
PlayerTask playerTask = new PlayerTask(selectedTask);
|
||||
playerTasks.Add(playerTask);
|
||||
|
||||
lastDailyTaskAssigned = now;
|
||||
SavePlayerData();
|
||||
|
||||
Debug.Log($"Assigned daily task: {selectedTask.name}");
|
||||
}
|
||||
|
||||
// 检查并更新过期任务
|
||||
public void CheckExpiredTasks()
|
||||
{
|
||||
DateTime now = DateTime.Now;
|
||||
foreach (var task in playerTasks)
|
||||
{
|
||||
if (task.status == TaskStatus.Active && now > task.expireTime)
|
||||
{
|
||||
task.status = TaskStatus.Expired;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 检查任务是否完成
|
||||
public bool CheckTaskCompletion(PlayerTask task)
|
||||
{
|
||||
TaskData taskConfig = GetTaskConfig(task.taskId);
|
||||
if (taskConfig == null)
|
||||
return false;
|
||||
|
||||
return task.progress >= taskConfig.value1;
|
||||
}
|
||||
|
||||
// 领取任务奖励
|
||||
public bool ClaimTaskReward(PlayerTask task)
|
||||
{
|
||||
if (task.status != TaskStatus.Completed)
|
||||
return false;
|
||||
|
||||
TaskData taskConfig = GetTaskConfig(task.taskId);
|
||||
if (taskConfig == null)
|
||||
return false;
|
||||
|
||||
// 发放奖励
|
||||
for (int i = 0; i < taskConfig.rewardTypes.Length && i < taskConfig.rewardNums.Length; i++)
|
||||
{
|
||||
GiveReward(taskConfig.rewardTypes[i], taskConfig.rewardNums[i]);
|
||||
}
|
||||
|
||||
task.status = TaskStatus.Claimed;
|
||||
SavePlayerData();
|
||||
return true;
|
||||
}
|
||||
|
||||
private void GiveReward(TaskRewardType rewardType, int amount)
|
||||
{
|
||||
// 根据奖励类型发放奖励
|
||||
switch (rewardType)
|
||||
{
|
||||
case TaskRewardType.Coins:
|
||||
// 增加金币(需要与游戏现有金币系统集成)
|
||||
UserData userData = Authenticator.Get().UserData;
|
||||
if (userData != null)
|
||||
{
|
||||
userData.coins += amount;
|
||||
Debug.Log($"Gave {amount} coins as reward");
|
||||
|
||||
// 保存用户数据更新
|
||||
if (ApiClient.Get() != null && ApiClient.Get().IsLoggedIn())
|
||||
{
|
||||
Authenticator.Get().SaveUserData();
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private TaskData GetTaskConfig(string taskId)
|
||||
{
|
||||
TaskData[] allTasks = Resources.LoadAll<TaskData>("Tasks");
|
||||
return allTasks.FirstOrDefault(t => t.id == taskId);
|
||||
}
|
||||
|
||||
// 更新任务进度
|
||||
private void UpdateTaskProgress(TaskConditionType conditionType, string parameter1 = null, string parameter2 = null)
|
||||
{
|
||||
bool progressUpdated = false;
|
||||
|
||||
foreach (var task in playerTasks)
|
||||
{
|
||||
if (task.status != TaskStatus.Active)
|
||||
continue;
|
||||
|
||||
TaskData taskConfig = GetTaskConfig(task.taskId);
|
||||
if (taskConfig == null || taskConfig.condition != conditionType)
|
||||
continue;
|
||||
|
||||
bool shouldUpdate = false;
|
||||
|
||||
// 根据任务类型检查条件
|
||||
switch (conditionType)
|
||||
{
|
||||
case TaskConditionType.LoginGame:
|
||||
case TaskConditionType.PlayGames:
|
||||
case TaskConditionType.WinGames:
|
||||
// 这些任务类型直接增加进度
|
||||
shouldUpdate = true;
|
||||
break;
|
||||
|
||||
case TaskConditionType.DefeatHeroWithAttributes:
|
||||
case TaskConditionType.SummonHeroWithAttributes:
|
||||
case TaskConditionType.UseHeroSkillWithAttributes:
|
||||
// 检查参数是否匹配(支持多个参数匹配)
|
||||
if (!string.IsNullOrEmpty(parameter1) &&
|
||||
(!string.IsNullOrEmpty(taskConfig.value2) && taskConfig.value2 == parameter1 ||
|
||||
!string.IsNullOrEmpty(taskConfig.value3) && taskConfig.value3 == parameter1))
|
||||
{
|
||||
shouldUpdate = true;
|
||||
}
|
||||
else if (!string.IsNullOrEmpty(parameter2) &&
|
||||
(!string.IsNullOrEmpty(taskConfig.value2) && taskConfig.value2 == parameter2 ||
|
||||
!string.IsNullOrEmpty(taskConfig.value3) && taskConfig.value3 == parameter2))
|
||||
{
|
||||
shouldUpdate = true;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
if (shouldUpdate && task.progress < taskConfig.value1)
|
||||
{
|
||||
task.progress++;
|
||||
progressUpdated = true;
|
||||
|
||||
// 检查任务是否完成
|
||||
if (CheckTaskCompletion(task))
|
||||
{
|
||||
task.status = TaskStatus.Completed;
|
||||
Debug.Log($"Task completed: {taskConfig.name}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (progressUpdated)
|
||||
{
|
||||
SavePlayerData();
|
||||
}
|
||||
}
|
||||
|
||||
// 事件处理方法
|
||||
private void OnGameStart()
|
||||
{
|
||||
// 进行对战任务进度+1
|
||||
UpdateTaskProgress(TaskConditionType.PlayGames);
|
||||
}
|
||||
|
||||
private void OnGameEnd(int winner)
|
||||
{
|
||||
int player_id = GameClient.Get().GetPlayerID();
|
||||
|
||||
// 胜利任务进度+1
|
||||
if (winner == player_id)
|
||||
{
|
||||
UpdateTaskProgress(TaskConditionType.WinGames);
|
||||
}
|
||||
|
||||
// 检查是否有任务完成并自动领取奖励
|
||||
CheckAndClaimCompletedTasks();
|
||||
}
|
||||
|
||||
private void OnCardPlayed(Card card, Slot slot)
|
||||
{
|
||||
// 召唤特定属性英雄任务
|
||||
if (card.CardData.type == CardType.Character)
|
||||
{
|
||||
string cardCamp = card.CardData.camp.ToString();
|
||||
UpdateTaskProgress(TaskConditionType.SummonHeroWithAttributes, cardCamp);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnCardSummoned(Card card, Slot slot)
|
||||
{
|
||||
// 召唤特定属性英雄任务
|
||||
if (card.CardData.type == CardType.Character)
|
||||
{
|
||||
string cardCamp = card.CardData.camp.ToString();
|
||||
UpdateTaskProgress(TaskConditionType.SummonHeroWithAttributes, cardCamp);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnAbilityUsed(AbilityData ability, Card caster)
|
||||
{
|
||||
// 使用特定属性英雄技能任务
|
||||
string cardCamp = caster.CardData.camp.ToString();
|
||||
UpdateTaskProgress(TaskConditionType.UseHeroSkillWithAttributes, cardCamp);
|
||||
}
|
||||
|
||||
private void OnCardDiscarded(Card card)
|
||||
{
|
||||
// 击败特定属性英雄任务
|
||||
if (card.CardData.type == CardType.Character)
|
||||
{
|
||||
string cardCamp = card.CardData.camp.ToString();
|
||||
UpdateTaskProgress(TaskConditionType.DefeatHeroWithAttributes, cardCamp);
|
||||
}
|
||||
}
|
||||
|
||||
// 检查并自动领取已完成任务的奖励
|
||||
public void CheckAndClaimCompletedTasks()
|
||||
{
|
||||
bool claimedAny = false;
|
||||
foreach (var task in playerTasks)
|
||||
{
|
||||
if (task.status == TaskStatus.Completed)
|
||||
{
|
||||
if (ClaimTaskReward(task))
|
||||
{
|
||||
claimedAny = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (claimedAny)
|
||||
{
|
||||
SavePlayerData();
|
||||
}
|
||||
}
|
||||
|
||||
// 玩家登录时检查任务
|
||||
public void OnPlayerLogin()
|
||||
{
|
||||
// 登录任务完成
|
||||
UpdateTaskProgress(TaskConditionType.LoginGame);
|
||||
|
||||
// 检查是否有任务完成并自动领取奖励
|
||||
CheckAndClaimCompletedTasks();
|
||||
|
||||
// 分配每日任务(如果需要)
|
||||
AssignDailyTaskIfNeeded();
|
||||
}
|
||||
|
||||
// 获取活跃任务
|
||||
public List<PlayerTask> GetActiveTasks()
|
||||
{
|
||||
return playerTasks.Where(t => t.status == TaskStatus.Active).ToList();
|
||||
}
|
||||
|
||||
// 获取已完成任务
|
||||
public List<PlayerTask> GetCompletedTasks()
|
||||
{
|
||||
return playerTasks.Where(t => t.status == TaskStatus.Completed).ToList();
|
||||
}
|
||||
|
||||
// 获取所有任务
|
||||
public List<PlayerTask> GetAllTasks()
|
||||
{
|
||||
return playerTasks;
|
||||
}
|
||||
}
|
||||
|
||||
// 玩家任务保存数据结构
|
||||
[Serializable]
|
||||
public struct PlayerTaskSaveData
|
||||
{
|
||||
public PlayerTask[] tasks;
|
||||
public DateTime lastDailyTaskAssigned;
|
||||
}
|
||||
}
|
||||
11
Assets/TcgEngine/Scripts/GameLogic/TaskManager.cs.meta
Normal file
11
Assets/TcgEngine/Scripts/GameLogic/TaskManager.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 37abf64a943e2bb43a818a56f330d168
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user