Alex教程每一P的教程原代码加上我自己的理解初步理解写的注释,可供学习Alex教程的人参考
此代码仅为较上一P有所改变的代码
【Unity教程】从0编程制作类银河恶魔城游戏_哔哩哔哩_bilibili
ItemObject_Trigger.cs
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
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
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
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
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;
}