主页与Task任务处理

This commit is contained in:
YiHan0621
2025-09-12 11:39:17 +08:00
parent 2151631c59
commit 8506590b12
20 changed files with 579 additions and 60 deletions

View File

@@ -241,7 +241,7 @@ namespace TcgEngine
if (res.success)
{
udata = ApiTool.JsonToObject<UserData>(res.data);
Debug.Log($"获取玩家数据:{res.data}");
// Debug.Log($"获取玩家数据:{res.data}");
}
return udata;

View File

@@ -94,7 +94,8 @@ namespace TcgEngine
public enum TaskRewardType
{
Coins = 0, // 金币
Coins = 0, // 金币
Crystal = 1,
// 后续可扩展其他奖励类型
}

View File

@@ -0,0 +1,44 @@
using System.Collections.Generic;
using UnityEngine;
using TcgEngine.UI;
namespace TcgEngine
{
[System.Serializable]
public class PrizeIconEntry
{
public TaskRewardType type;
public Sprite icon;
}
[CreateAssetMenu(fileName = "NewTaskRewardData", menuName = "TcgEngine/TaskRewardData")]
public class TaskRewardData : ScriptableObject
{
public PrizeIconEntry[] entries;
private Dictionary<TaskRewardType, Sprite> iconDict = new Dictionary<TaskRewardType, Sprite>();
public void Init()
{
iconDict.Clear();
foreach (var entry in entries)
{
Debug.Log($"录入: {entry.type} => {entry.icon?.name}");
iconDict[entry.type] = entry.icon;
}
}
public Sprite GetIcon(TaskRewardType type)
{
if (iconDict.Count == 0) Init();
if (iconDict.TryGetValue(type, out Sprite sprite))
return sprite;
Debug.LogWarning($"未找到 {type} 的图标,请检查 TaskRewardData 的 entries 配置");
return null;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 3b5b19a5996046508584e1eb649f43dd
timeCreated: 1757580421

View File

@@ -422,31 +422,37 @@ namespace TcgEngine.Gameplay
}
task.status = TaskStatus.Claimed;
SavePlayerData();
return true;
}
private void GiveReward(TaskRewardType rewardType, int amount)
{
UserData userData = Authenticator.Get().UserData;
// 根据奖励类型发放奖励
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;
case TaskRewardType.Crystal:
if (userData != null)
{
userData.crystal += amount;
Debug.Log($"获取{amount}个钻石");
}
break;
}
// 保存用户数据更新
if (ApiClient.Get() != null && ApiClient.Get().IsLoggedIn())
{
Authenticator.Get().SaveUserData();
}
}

View File

@@ -37,9 +37,6 @@ namespace TcgEngine.UI
public List<RankLine> lines = new List<RankLine>();
public List<LadderRankLine> ladderLines = new List<LadderRankLine>();
[Header("返回主页")]
public Button returnToHome_button;
private const string DefaultAvatarId = "bear";
@@ -59,28 +56,6 @@ namespace TcgEngine.UI
my_line.onClick += OnClickLine;
my_ladderLine.onClick += OnClickRankLine;
InitLines();
returnToHome_button.onClick.AddListener(OnReturnToHome);
}
/// <summary>
/// 返回主页
/// </summary>
public void OnReturnToHome()
{
isHideObject = !isHideObject;
if (isHideObject)
{
Show();
}
else
{
Hide();
}
foreach (var obj in hideGameObject)
{
obj.SetActive(!isHideObject);
}
}
private void OnDestroy()
@@ -257,7 +232,7 @@ namespace TcgEngine.UI
}
}
//
// private void RankPanelColl()
// {
// if (isLadderRank)

View File

@@ -0,0 +1,29 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace TcgEngine.UI
{
public class UIFrame : UIPanel
{
public Button uiFrameHideButton;
protected override void Awake()
{
base.Awake();
Hide();
}
protected override void Start()
{
uiFrameHideButton.onClick.AddListener(OnUiFrameHide);
}
private void OnUiFrameHide()
{
Hide();
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 9379c33c33779f64a94d441f6a521411
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -17,13 +17,13 @@ namespace TcgEngine.UI
public Text progressBar_text;
public Button reward_button;
public Image reward_icon;
public Image taskEnd_img;
private PlayerTask playerTask;
private TaskData taskConfig;
public GameObject rewardItem;
protected override void Awake()
{
@@ -50,12 +50,25 @@ namespace TcgEngine.UI
reward_button.interactable = task.status == TaskStatus.Completed;
reward_button.onClick.RemoveAllListeners();
reward_button.onClick.AddListener(OnClickReward);
// 奖品
for (int i = 0; i < config.rewardTypes.Length; i++)
{
TaskRewardType rewardType = config.rewardTypes[i];
int rewardNum = (config.rewardNums != null && i < config.rewardNums.Length)
? config.rewardNums[i] : 1; // 没配数量就默认为1
GameObject go = Instantiate(rewardItem, reward_button.transform);
var reward = go.GetComponent<TaskReward>();
reward.SetPrize(rewardType, rewardNum, reward_button.interactable); // 改成传两个参数
}
if (task.status == TaskStatus.Expired || task.status == TaskStatus.Claimed)
{
taskEnd_img.gameObject.SetActive(true);
}
RefreshStatus();
}
@@ -74,13 +87,5 @@ namespace TcgEngine.UI
progressBar_slider.value = playerTask.progress;
progressBar_text.text = playerTask.progress + "/" + taskConfig.value1;
}
private void RewardColl(Sprite icon)
{
if (reward_icon != null)
{
reward_icon.sprite = icon;
}
}
}
}

View File

@@ -0,0 +1,52 @@
using System;
using UnityEngine;
using UnityEngine.UI;
namespace TcgEngine.UI
{
public class TaskReward : MonoBehaviour
{
public TaskRewardData database; // 存放所有奖励图标的 ScriptableObject
public Image rewardIcon; // 图标 Image
public Text amountText; // 数量文本
public float fixedHeight = 80f; // 固定高度
private RectTransform rect;
public void SetPrize(TaskRewardType type, int amount,bool isVis)
{
if (!isVis)
{
rewardIcon.color = Color.gray;
amountText.color = Color.gray;
}
else
{
rewardIcon.color = Color.white;
amountText.color = Color.white;
}
// 图标
Sprite sprite = database.GetIcon(type);
rewardIcon.sprite = sprite;
// 数量
if (amountText != null)
{
amountText.text = amount > 1 ? "x" + amount.ToString() : "";
}
// 高度固定80等比例缩放
if (sprite != null)
{
float ratio = sprite.rect.width / sprite.rect.height;
rect = rewardIcon.GetComponent<RectTransform>();
rect.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, fixedHeight);
rect.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, fixedHeight * ratio);
}
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 55af7dcd22f203d43b3ff26368c1ef5b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: