这是第一个 Unity 开放项目的repo,是 Unity 和社区合作创建的一个小型开源游戏演示,第一款游戏是一款名为 Chop Chop 的动作冒险游戏。
您最多选择25个主题 主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

79 行
2.0 KiB

using UnityEngine;
using UOP1.StateMachine;
using UOP1.StateMachine.ScriptableObjects;
[CreateAssetMenu(fileName = "DropReward", menuName = "State Machines/Actions/Drop Reward")]
public class DropRewardSO : StateActionSO
{
protected override StateAction CreateAction() => new DropReward();
}
public class DropReward : StateAction
{
private DroppableRewardConfigSO _dropRewardConfig;
private Transform _currentTransform;
public override void Awake(StateMachine stateMachine)
{
_dropRewardConfig = stateMachine.GetComponent<Damageable>().DroppableRewardConfig;
_currentTransform = stateMachine.transform;
}
public override void OnUpdate()
{
}
public override void OnStateEnter()
{
DropAllRewards(_currentTransform.position);
}
private void DropAllRewards(Vector3 position)
{
DropGroup specialDropItem = _dropRewardConfig.DropSpecialItem();
if (specialDropItem != null) // drops a special item if any
DropOneReward(specialDropItem, position);
// Drop items
foreach (DropGroup dropGroup in _dropRewardConfig.DropGroups)
{
float randValue = Random.value;
if (dropGroup.DropRate >= randValue)
{
DropOneReward(dropGroup, position);
}
else
{
break;
}
}
}
private void DropOneReward(DropGroup dropGroup, Vector3 position)
{
float dropDice = Random.value;
float _currentRate = 0.0f;
ItemSO item = null;
GameObject itemPrefab = null;
foreach (DropItem dropItem in dropGroup.Drops)
{
_currentRate += dropItem.ItemDropRate;
if (_currentRate >= dropDice)
{
item = dropItem.Item;
itemPrefab = dropItem.Item.Prefab;
break;
}
}
float randAngle = Random.value * Mathf.PI * 2;
GameObject collectibleItem = GameObject.Instantiate(itemPrefab,
position + itemPrefab.transform.localPosition +
_dropRewardConfig.ScatteringDistance * (Mathf.Cos(randAngle) * Vector3.forward + Mathf.Sin(randAngle) * Vector3.right),
Quaternion.identity);
collectibleItem.GetComponent<CollectableItem>().AnimateItem();
}
}