Type Object 写 物品属性

又一段时间没写博客啦,或许是太懒?

还是前段时间试着模仿自由幻想写了个自己玩的游戏,没有任何贴图、模型等。

虽然自己玩得很好。但我的愿意是用项目练手。所以准备推翻重构啦。


为啥重构?唔,给一段我写的代码的贴图你就知啦


唔,相信你们也看到了这段代码是有多么的糟糕。

我刚写完打算添加属性时才发觉自己做了多么蠢的一件事。

如果我是策划,那么这里一定会经常改

所以不可能直接就在这里写上所有属性

每一次更改都是一次大修

完全没提高技能嘛,一直在描述需求难过


我重新对物品进行了分类



所以准备推翻重来。重新拿起设计模式观看。

我发现 Type Object 可以很好的满足我的需求

它的使用一般用于不知道最终会有多少种类型的对象

它使用一个类去创建另一个类,每一个类都是一个新的对象

就是说我用它进行使用相同的函数生产不同的装备

如果需要修改的时候,只需要更改对应类,而无需把代码写死


看描述,首先它是一个物品,然后它分为道具类型和装备类型。

然后道具类型又分为合成类、使用类以及卡片类


它看起来应该会是这样


public class Article{
    private ArticleBreed breed;
    public Article( ArticleBreed breed){
        this.breed = breed;
    }
    public ArticleType Breed{get{return breed;}}
}

public class ArticleBreed
{
    private int id;
    private string name;
    private Sprite icon;
    private int weight;
    private int level;
    public ArticleType(int id, string name, Sprite icon, int weight, int level){
        this.id=id;
        this.name=name;
        this.icon=icon;
        this.weight=weight;
        this.level=level;
    }
    public Article newArticle()
    {
        return new Article(this);
    }
    public int Id{get{return id;}}
    public string Name{get{return name;}}
    public Sprite Icon{get{return icon;}}
    public int Weight{get{return weight;}}
    public int Level{get{return level;}}
}
public class StageProperty:ArticleBreed
{
    private StagePropertyBreed breed;
    public StageProperty(int id,string name, Sprite icon, int weight, int level, StagePropertyBreed breed)
    :base(id,name,icon,weight,level)
    {
        this.breed = breed;
    }
    public StagePropertyBreed Breed{get{return breed;}}
}
public class StagePropertyBreed
{
    private string explain;
    public string Explain{get{return explain;}}
    public StagePropertyBreed(string explain){
        this.explain = explain;
    }
    public StageProperty newStageProperty(int id,string name, Sprite icon, int weight, int level)
    {
        return new StageProperty( id, name, icon, weight, level, this);
    }
}
public class SynthesisStageProperty:StagePropertyBreed
{
    private int id;
    private int count;
    public int Id{get{return id;}}

    public SynthesisStageProperty(string explain,int id, int count):base(explain)
    {
        this.id = id;
        this.count = count;
    }
    public int Synthesis(SynthesisStageProperty[]array)
    {
        if(array.Length >= count)
            return id;
        return -1;
    }
}
public class UseStageProperty:StagePropertyBreed
{
    private int value;
    public UseStageProperty(string explain,int value):base(explain)
    {
        this.value = value;
    }
    public void Use(out int value)
    {
        value += this.value;
    }
}
public class CardStageProperty:StagePropertyBreed
{
    private EquipType type;
    private EffectType effectType;
    private int minLevel;
    private int min;
    private int max;
    private int value;

    public CardStageProperty(string explain,EquipType equipType,EffectType effectType, int minLevel, int max, int value):base(explain)
    {
        type = equipType;
        this.effectType = effectType;
        this.minLevel = minLevel;
        this.min = min;
        this.max = max+1;
        this.value = value;
    }
    public bool Inlay(Equip equip)
    {
        var breed = equip.Breed;
        if(breed.Type == type){
            if(breed.EquipLevel >= minLevel){
                int level = Random.Range(min,max);
                //获得表对应类型对应等级的值
                //value = table.get(rand);
                breed.Inlay( effectType,value);
                return true;
            }
        }
        return false;
    }
}
public class Equip:ArticleBreed
{
    private EquipBreed breed;
    public EquipBreed Breed{get{return breed;}}
    public Equip(int id,string name, Sprite icon, int weight, int level, EquipBreed breed)
    :base(id,name,icon,weight,level)
    {
        this.breed = breed;
    }
}

public class EquipBreed
{
    private EquipType type;
    private int equipLevel;
    private int processingLevel=0;
    private List<Effect> holeAttribute = new List<Effect>();
    public EquipType Type{get{return type;}}
    public int EquipLevel{get{return equipLevel;}}
    public List<Effect> HoleAttribute{get{return holeAttribute;}}
    public struct Effect
    {
        public EffectType type;
        public int value;
        public Effect(EffectType type, int value){
            this.type = type;
            this.value = value;
        }
    }
   public Equip newEquip(int id,string name, Sprite icon, int weight, int level)
    {
        return new Equip(id, name, icon, weight, level, this);
    }
    public void Inlay(EffectType type, int value){
        holeAttribute.Add(Effect(type,value));
    }
    public void Strike(int index){
        holeAttribute.RemoveAt(index);
    }
    public bool Processing(){
        if( Random.Range(0,processingLevel*10) ){
            return false;
        }
        return true;
    }
}

public enum EquipType
{
    Arms,
    Shoes,
    Clothes,
    Glove,
    Helmet,
    Mask,
    Necklace,
    Knapsack,
    Ring,
    Shield
}
public enum EffectType
{
    Hp,
    Mp,
    Speed
}




它看起来会是这样使用。唔,这样读,创建一个合成类,然后初始化道具的属性,最后返回的将会是一个物品类

Sprite icon;
new SynthesisStageProperty("",2,15).newStageProperty(0,"",icon,1,0).newArticle();



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小鱼游戏开发

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值