-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathDropRewardSO.cs
More file actions
79 lines (67 loc) · 1.97 KB
/
DropRewardSO.cs
File metadata and controls
79 lines (67 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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();
}
}