当前位置: 首页 > news >正文

Unity类银河恶魔城学习记录11-10 p112 Items drop源代码

Alex教程每一P的教程原代码加上我自己的理解初步理解写的注释,可供学习Alex教程的人参考
此代码仅为较上一P有所改变的代码

【Unity教程】从0编程制作类银河恶魔城游戏_哔哩哔哩_bilibili

 ItemObject_Trigger.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class ItemObject_Trigger : MonoBehaviour
{private ItemObject myItemObject => GetComponentInParent<ItemObject>();private void OnTriggerEnter2D(Collider2D collision){if (collision.GetComponent<Player>() != null){myItemObject.PickupItem();}}
}

ItemObject.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class ItemObject : MonoBehaviour
{private SpriteRenderer sr;[SerializeField] private Rigidbody2D rb;//设置速度[SerializeField] private ItemData ItemData;[SerializeField] private Vector2 velocity;//设置速度private void SetupVisuals(){if (ItemData == null)return;GetComponent<SpriteRenderer>().sprite = ItemData.icon;gameObject.name = ItemData.name;}public void SetupItem(ItemData _itemData,Vector2 _velocity)设置实例函数{ItemData = _itemData;rb.velocity = _velocity;//设置速度SetupVisuals();}public void PickupItem()//拾取函数打包{Inventory.instance.AddItem(ItemData);Destroy(gameObject);}
}
ItemDrop.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;public class ItemDrop : MonoBehaviour
{[SerializeField] private int possibleItemDrop;//设置可能会出现的材料数量[SerializeField] private ItemData[] possibleDrop;private List<ItemData> dropList = new List<ItemData>();[SerializeField] private GameObject dropPrefab;[SerializeField] private ItemData item;public void GenerateDrop()//随机生成物品函数{for(int i = 0;i<possibleDrop.Length;i++)//一个判断可能出现的装备{if(Random.Range(0,100) <= possibleDrop[i].dropChance){dropList.Add(possibleDrop[i]);}       }for (int i = 0; i < possibleItemDrop; i++)//另一个生成可能出现的装备{ItemData randomItem = dropList[Random.Range(0, dropList.Count - 1)];if(possibleItemDrop <= dropList.Count ){dropList.Remove(randomItem);}DropItem(randomItem);}}public void DropItem(ItemData _itemData)//创建实例函数{GameObject newDrop = Instantiate(dropPrefab, transform.position,Quaternion.identity);Vector2 randomVelocity = new Vector2(Random.Range(-5, 5), Random.Range(15, 20));newDrop.GetComponent<ItemObject>().SetupItem(_itemData, randomVelocity);}
}
 EnemyStats.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.WSA;public class EnemyStat : CharacterStats
{private Enemy enemy;private ItemDrop myDropSystem;[Header("Level details")][SerializeField] private int leval = 1;[Range(0f, 1f)]//一个使数值设置成为一定范围的设置[SerializeField] private float percantageModifier = .4f;//设置等级和成长比例public override void DoDamage(CharacterStats _targetStats){base.DoDamage(_targetStats);}protected override void Die(){base.Die();enemy.Die();myDropSystem.GenerateDrop();}protected override void Start(){//改变伤害和生命值//解决初始血量在升级后不满ApplyLevelModifier();enemy = GetComponent<Enemy>();base.Start();myDropSystem = GetComponent<ItemDrop>();}private void ApplyLevelModifier(){Modify(strength);Modify(agility);Modify(intelligence);Modify(vitality);Modify(damage);Modify(critChance);Modify(critPower);Modify(maxHealth);Modify(armor);Modify(evasion);Modify(magicResistance);Modify(fireDamage);Modify(iceDamage);Modify(lightingDamage);}//专门对某个数值进行提升的函数private void Modify(Stat _stat){for(int i =1;i<leval;i++){float modifier = _stat.GetValue() * percantageModifier;_stat.AddModifier(Mathf.RoundToInt(modifier));}}public override void TakeDamage(int _damage){base.TakeDamage(_damage);}
}
ItemData.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public enum ItemType
{Material,Equipment
}[CreateAssetMenu(fileName = "New Item Data", menuName = "Data/Item")]
public class ItemData : ScriptableObject 
{public ItemType itemType;public string itemName;public Sprite icon;//图标[Range(0,100)]public float dropChance;
}

http://www.lryc.cn/news/330870.html

相关文章:

  • EasyExcel 模板导出excel、合并单元格及单元格样式设置。 Freemarker导出word 合并单元格
  • 炫我科技:云渲染领域的佼佼者
  • VsCode正确解决vue3+Eslint+prettier+Vetur的配置冲突
  • 计算机网络—VLAN 间路由配置
  • 微服务篇-C 深入理解第一代微服务(SpringCloud)_VII 深入理解Swagger接口文档可视化管理工具
  • 区块链的应用领域:重塑未来的信任机制
  • 怎么在循环List的时候删除List的元素
  • SpringBoot+thymeleaf完成视频记忆播放功能
  • ES 7.12官网阅读-ILM(index lifecycle management)
  • Jenkins执行策略(图文讲解)
  • 1,static 关键字.Java
  • 网络语义实体对齐(Entity Alignment)相关论文与数据集整理
  • 【自动装箱以及包装类的缓存】⭐️通过具体案例看下每种包装类的不同结果
  • Java(内部类)
  • c++对象指针
  • js 拼接HTML时 onclick方法和传参报错[onject Object] 和 unexpected end of input`
  • 基于springboot实现定时任务,并且添加Event事件处理机制
  • 深入理解数据结构(1):复杂度详解
  • kette介绍-Step之Merge Join
  • 通俗易懂:MySQL中如何设置只读实例并确保数据一致性?
  • 一文了解Java核心知识——线程池
  • Redis热点Key问题分析与解决
  • 深度学习armv8/armv9 cache的原理
  • Python基础之pandas:文件读取与数据处理
  • 基于Springboot旅游网站管理系统设计和实现
  • 深度解析C语言——预处理详解
  • idea2023.2.1 java项目-web项目创建-servlet类得创建
  • Ollama教程——入门:开启本地大型语言模型开发之旅
  • 基于PHP的新闻管理系统(用户发布版)
  • 基础篇3 浅试Python爬虫爬取视频,m3u8标准的切片视频