学习游戏制作记录(制作系统与物品掉落系统)8.16

1.制作系统

在游戏里我们可以通过材料来制作装备,当我们点击合成表中的装备时,它会检测我们是否有足够的材料数量来制作

创建相应的物品槽

ItemData_Equipment脚本:

Header("Crafting Requiring")
public List<InventoryItem> craftingMaterials;//需要材料的列表

Inventory脚本:

public bool canCraft(ItemData_Equipment _itemToCraft,List<InventoryItem> _requireMaterials)//是否可以制作
{
List<InventoryItem> MaterialToRemove = new List<InventoryItem>();//用来保存删除的材料

for(int i =0;i<_requireMaterials.Count;i++)
{
if (stashItemsDictionary.TryGetValue(_requireMaterialsi.data, out InventoryItem stashValue))//检查玩家材料列表
{
if(stashValue.stackSize < _requireMaterialsi.stackSize)
{
Debug.Log("Not enough Materials");
return false;
}
else//如果有足够的材料
{
MaterialToRemove.Add(_requireMaterialsi);//添加到删除列表
}
}
else
{
Debug.Log("Not enough Materials");
return false;
}

}

for(int i=0;i<MaterialToRemove.Count;i++)//删除
{
if (stashItemsDictionary.TryGetValue(MaterialToRemovei.data, out InventoryItem value))
{
value.stackSize -= MaterialToRemovei.stackSize;

if (value.stackSize < 1)
{
stash.Remove(value);
stashItemsDictionary.Remove(MaterialToRemovei.data);
}
}

}
UpdataSlotUI();

AddItem(_itemToCraft);//将合成的装备添加到装备列表
Debug.Log("here is" + _itemToCraft.ItemName);
return true;
}

创建UI_craftSlot脚本:

private void OnEnable()
{
UpdataSlot(Item);//更新
}

public override void OnPointerDown(PointerEventData eventData)
{
if (Item == null)
{
return;
}
if (eventData.pointerCurrentRaycast.gameObject == gameObject)
{
ItemData_Equipment craftData = Item.data as ItemData_Equipment;

Inventory.instance.canCraft(craftData, craftData.craftingMaterials);//调用合成
}
}

2.实现敌人掉落指定物品

为物品添加一个子对象名为触发器,它专门负责拾取

为物品本身添加2D刚体,我们希望物品掉落时有随机速度

添加Item层级确保人物不会和物品碰撞

制作为预制体

ItemObject脚本:

SerializeField private Rigidbody2D rb;

SerializeField private Vector2 velocity;
SerializeField private ItemData itemData;

private void SetupVisuals()
{
if (itemData == null)
return;
GetComponent<SpriteRenderer>().sprite = itemData.icon;

gameObject.name = "Item Object -" + itemData.ItemName;
}

public void SetupItem(ItemData _itemData,Vector2 velocity)//设置物品数据和速度
{
itemData = _itemData;
rb.velocity = velocity;

SetupVisuals();
}
public void PickupObject()//拾取函数
{
Inventory.instance.AddItem(itemData);
Destroy(gameObject);
}

创建ItemObject_Trigger脚本:

private ItemObject myItemObject =>GetComponentInParent<ItemObject>();//获取物品脚本

private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.GetComponent<Player>() != null)
{
myItemObject.PickupObject();//实现拾取
}
}

创建ItemDrop脚本挂载在敌人上:

SerializeField private GameObject dropprefeb;
SerializeField private ItemData item;//数据

public void DropItem()
{
GameObject newDrop =Instantiate(dropprefeb,transform.position,Quaternion.identity);

Vector2 randomVelocity = new Vector2(Random.Range(-5, 5), Random.Range(15, 20));//设置随机速度

newDrop.GetComponent<ItemObject>().SetupItem(item, randomVelocity);

}

EnemyStats脚本:

private ItemDrop myDropSystem;//获取掉落系统

{
ApplyLevelModifier();
base.Start();

enemy = GetComponent<Enemy>();
myDropSystem = GetComponent<ItemDrop>();
}

protected override void Die()//死亡时触发掉落函数
{
base.Die();

enemy.Die();

myDropSystem.DropItem();
}

3.实现敌人的随机掉落

ItemData脚本:

Range(0, 100)
public float dropChance;//添加掉落几率

ItemDrop脚本:

SerializeField private int possibleItemDrop;//掉落的数量
SerializeField private ItemData\[\] possibleDrop;//可能掉落的物品
private List<ItemData> dropList =new List<ItemData>();//实际上掉落物品的列表

public void GenerateDrop()//生成掉落
{
for(int i = 0; i < possibleDrop.Length; i++)//从可能掉落的物品中筛选
{
if (Random.Range(0, 100) < possibleDropi.dropChance)
{
dropList.Add(possibleDropi);//加入到掉落列表
}
}

for(int i = 0;i < dropList.Count; i++)
{
ItemData newItem = dropListRandom.Range(0, dropList.Count - i);//随机掉落
dropList.Remove(newItem);

DropItem(newItem);//生成物品
}
}

public void DropItem(ItemData _item)//修改传入参数
{
GameObject newDrop = Instantiate(dropprefeb, transform.position, Quaternion.identity);

Vector2 randomVelocity = new Vector2(Random.Range(-5, 5), Random.Range(15, 20));

newDrop.GetComponent<ItemObject>().SetupItem(_item, randomVelocity);

}

EnemyStats脚本:

protected override void Die()
{
base.Die();

enemy.Die();

myDropSystem.GenerateDrop();//死亡调用
}

相关推荐
劈星斩月12 分钟前
监督学习算法 之 决策树
学习·算法·决策树
丁小未18 分钟前
Unity 几种常见合批手段的要求
游戏·unity·合批·srpbatcher·动态合批·静态合批
AOwhisky23 分钟前
Python 学习笔记(第五期)——组合数据类型:列表、元组、集合与字典精讲——核心知识点自测与详解
开发语言·笔记·python·学习·云计算
czhc114007566333 分钟前
722:零侵入;DBG;
c#
woshihuanglaoshi38 分钟前
数据迁移与版本管理 - Flutter在鸿蒙平台实现数据库升级策略
数据库·学习·flutter·华为·harmonyos·鸿蒙·鸿蒙系统
johnny2331 小时前
UI新革命?GenUI、WanDB-OpenUI、thesysdev-OpenUI、A2UI、AG-UI、MCP-UI
ui
-银雾鸢尾-10 小时前
C#中的StringBuilder相关方法
开发语言·c#
-银雾鸢尾-10 小时前
C#中结构体与类的区别;抽象类与接口的区别
开发语言·c#
天天进步201510 小时前
UI-TARS 源码解析 #10:parse_action 源码解析:如何用 AST 安全解析模型生成的函数调用?
安全·ui
世人万千丶11 小时前
参数管理_Flutter在鸿蒙平台路由参数最佳实践
学习·flutter·华为·harmonyos·鸿蒙