我需要帮助在Unity中定义我的卡牌游戏的架构



我目前正在开发一款受《鼠城》(Ratropolis)启发的游戏,这意味着玩家必须从手中打出纸牌才能在游戏世界中完成某些动作,比如获得一些钱或类似的东西。

我不确定如何实现这一点,我很困惑,至少可以说。基本上,到目前为止,我一直在使用一个名为CardData的可脚本对象,它看起来像这样:(请注意,cardseeffect行直到昨天才出现,这就是我感到困惑的地方)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Serialization;

[CreateAssetMenu]
public class CardData : ScriptableObject
{
//This will contain all the cards information.
public string cardName, description;
public int moneyCost, natureCost, foodCost;
public Texture2D texture; //this is for prototyping only, textures will be replaced by a prefab.
public CardsEffect cardEffect; // CardsEffect is a public class that I created for my spells class to Inherit from. I am not sure about this tho
}
在网上搜索时,我看到许多人建议创建一个公共类来保存效果的空白,这样我就可以为继承它的每张卡片创建一个类。这就是我所做的:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

[System.Serializable]
public class CardsEffect
{
private GameObject resourceController;
public CardsEffect()
{

}

public bool unplayable;

[Header("When played")]
public int moneyBonus;
public int natureBonus;
public int foodBonus;

[Header("When drawn")]
public int drawMoneyBonus;
public int drawNatureBonus;
public int drawFoodBonus;

public void OnDraw()
{

}

public void OnPlay()
{

}
}

但我意识到我真的不知道如何使用它。基本上,我想的是在cardseeffect类中创建带有参数的公共void,就像这样,这样我可以很容易地从继承类调用void,但我不确定这是否好。

DrawCards(int cardsToDraw)
{
target player draws cardsToDraw cards;
}

另外,我不明白如何实现这些类,因为它们不从MonoBehaviour继承,我不能将它们附加到我的ScriptableObject,我不能使用void来制作cardseeffect。所以正如你所看到的,我很困惑,我希望你能给我一些建议。

谢谢你读我的书。

问好

我的建议是把你的数据和游戏逻辑分成不同的类。逻辑类说CardCtrl继承自MonoBehaviour,而数据类没有。CardCtrl被附加到Card游戏对象上。卡片数据只是内存中的实例。可参考CardCtrl

您需要创建一个DataManager类来初始化或加载那些卡片数据实例。不要害怕创建太多的类。

简单的示例:

// put each class in a new `cs` script with proper name(same with class name).
public class CardData
{
public int key;
public int attack;
public int cost;
// other data you need
}
public class CardDataMgr: MonoBehaviour  // attach this to a game object
{
Dictionary<int, CardData> dataDict = new Dictionary<int, CardData>(); // the int here is card data key
private void Awake()
{
LoadData();
}
public void LoadData()
{
// load from files or codes or server
dataDict.Add(1, new CardData() { key = 1, attack = 100, cost = 100 }); // besure the keys are the same
dataDict.Add(2, new CardData() { key = 2, attack = 100, cost = 100 });
}
public CardData GetCardData(int key)
{
return dataDict[key];
}
}
public class CardCtrl: MonoBehaviour // attach this to the card prefab or card game object
{
int key;
int cost;
int attack;
public void InitWithCardKey(CardDataMgr mgr, int key) // call this when you need a new Card, pass in the CardDataMgr in the scene(or you could make it into a singleton)
{
this.key = key; // take a record of key in case for future usage
var data = mgr.GetCardData(key);
this.cost = data.cost;
this.attack = data.attack;
}
// following are other logic that you need
public void OnDraw()
{
}
public void OnKilled()
{
}
}

相关内容

  • 没有找到相关文章

最新更新