背包管理实例

本文详细介绍了一个基于Unity的游戏物品系统设计,包括物品基类Item及派生的武器Weapon、消耗品Consumable、装备Equipment和材料Materia类。每种类别都具有特定属性,如武器的攻击力和装备的属性加成。
摘要由CSDN通过智能技术生成

1.进行分析,是代码逻辑清晰

在这里插入图片描述

2.创建Item类

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 物品基类
/// </summary>
public class Item
{
    public int ID { get; set; }
    public string Name { get; set; }
    public ItemType Type { get; set; }
    public ItemQuality Quality { get; set; }
    public string Description { get; set; }//描述
    public int Capacity { get; set; }//容量
    public int BuyPrice { get; set; }
    public int SellPrice { get; set; }
    public string Sprite;

    /// <summary>
    /// 空的构造方法
    /// </summary>
    public Item()
    {
        ID = -1;
    }
    /// <summary>
    /// 构造方法
    /// </summary>
    public Item (int id,string name,ItemType type,ItemQuality quality,string des, int capacity,int buyPrice,int sellPrice,string sprite)
    {
        this.ID = id;
        Name = name;
        Type = type;
        Quality = quality;
        Description = des;
        Capacity = capacity;
        BuyPrice = buyPrice;
        SellPrice = sellPrice;
        this.Sprite = sprite;
    }

    /// <summary>
    /// 物品类型
    /// </summary>
    public enum ItemType
    {
        Consumable,//消耗品
        Equipment,//装备类
        Weapon,//武器类
        Material//材料类
    }
    /// <summary>
    /// 物品品质
    /// </summary>
    public enum ItemQuality
    {
        Common,//普通
        Uncmmon,//非凡
        Rare,//稀有
        Epic,//史诗
        Legendary,//传奇
        Artifact//神器
    }
}

3.创建其他继承Item类

1.创建武器类
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    /// <summary>
    /// 武器类
    /// </summary>
    public class Weapon : Item
    {
        public int Damage { get; set; }
        public WeaponType WpType { get; set; }
    
        public Weapon(int id, string name, ItemType type, ItemQuality quality, string des, int capacity, int buyPrice, int sellPrice,string sprite,
        int damage,WeaponType wpType)
          : base(id, name, type, quality, des, capacity, buyPrice, sellPrice,sprite)
        {
            this.Damage = damage;
            this.WpType = wpType;
        }
    
        /// <summary>
        /// 武器类型
        /// </summary>
        public enum WeaponType
        {
            OffHand,
            MainHand
        }

}

2.创建消耗类

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

/// <summary>
/// 消耗类
/// </summary>
public class Consumable : Item
{
    public int HP { get; set; }
    public int MP { get; set; }

    public Consumable(int id, string name, ItemType type, ItemQuality quality, string des, int capacity, int buyPrice, int sellPrice,string sprite,
    int hp,int mp )
      : base(id, name, type, quality, des, capacity, buyPrice, sellPrice,sprite)
    {
        this.HP = hp;
        this.MP = mp;
    }
}
3.创建装备类
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 装备类
/// </summary>
public class Equipment :Item
{
    public int Strength { get; set; }//力量
    public int Intellect { get; set; }//智力
    public int Agility { get; set; }//敏捷
    public int Stamina { get; set; }//体力
    public EquipmentType EquipType { get; set; }//装备类型

    public Equipment(int id, string name, ItemType type, ItemQuality quality, string des, int capacity, int buyPrice, int sellPrice,string sprite,
        int strength,int intellect,int agility,int stamina,EquipmentType equipmentType)
        :base( id,  name,  type,  quality,  des,  capacity,  buyPrice,  sellPrice,sprite)
    {
        this.Strength = strength;
        this.Intellect = intellect;
        this.Agility = agility;
        this.Stamina = stamina;
        this.EquipType = equipmentType;
    }

    public enum EquipmentType
    {
        Head,//头
        Neck,//脖子
        Chest,//胸部
        Ring,//戒指
        Leg,//腿
        Bracer,//护腕
        Boots,//鞋子
        Shoulder,//肩膀
        Belt,//腰带
        OffHand//副手
    }
}
4.创建材料类
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 材料类
/// </summary>
public class Materia : Item
{
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值