File size: 1,329 Bytes
defab59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
using Fusion;
using UnityEngine;

public class ItemBox : NetworkBehaviour
{
    public ItemType[] items = new ItemType[] {
        ItemType.Shield, ItemType.Speed, ItemType.Trap, ItemType.Projectile
    };
    public GameObject breakEffectPrefab;
    public float breakEffectLife = 1.2f;

    public override void Spawned()
    {
        if (items == null || items.Length == 0)
            items = new ItemType[] { ItemType.Shield, ItemType.Speed };
    }

    void OnTriggerEnter(Collider other)
    {
        if (!Object.HasStateAuthority)
            return;

        DinoPlayer player = other.GetComponentInParent<DinoPlayer>();
        if (player == null || !player.IsAlive)
            return;

        if (player.HeldItem != 0)
            return;

        ItemType picked = items[Random.Range(0, items.Length)];
        player.GiveItem(picked);

        RpcBreakEffect(transform.position);
        Runner.Despawn(Object);
    }

    [Rpc(RpcSources.StateAuthority, RpcTargets.All)]
    void RpcBreakEffect(Vector3 pos)
    {
        if (breakEffectPrefab == null)
            return;

        GameObject fx = Instantiate(breakEffectPrefab, pos, Quaternion.identity);
        Destroy(fx, breakEffectLife);

        DinoAudio audio = DinoAudio.Instance;
        if (audio != null)
            audio.PlayBoxBreak();
    }
}