summaryrefslogtreecommitdiff
path: root/Valheim_v202102/Valheim/assembly_valheim/Pickable.cs
blob: 23d83dbde3bf5743e6cd17614df948d82d28082a (plain)
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
using System;
using UnityEngine;

public class Pickable : MonoBehaviour, Hoverable, Interactable
{
	public GameObject m_hideWhenPicked;

	public GameObject m_itemPrefab;

	public int m_amount = 1;

	public DropTable m_extraDrops = new DropTable();

	public string m_overrideName = "";

	public int m_respawnTimeMinutes;

	public float m_spawnOffset = 0.5f;

	public EffectList m_pickEffector = new EffectList();

	public bool m_pickEffectAtSpawnPoint;

	public bool m_useInteractAnimation;

	private ZNetView m_nview;

	private bool m_picked;

	private void Awake()
	{
		m_nview = GetComponent<ZNetView>();
		ZDO zDO = m_nview.GetZDO();
		if (zDO != null)
		{
			m_nview.Register<bool>("SetPicked", RPC_SetPicked);
			m_picked = zDO.GetBool("picked");
			SetPicked(m_picked);
			if (m_respawnTimeMinutes > 0)
			{
				InvokeRepeating("UpdateRespawn", UnityEngine.Random.Range(1f, 5f), 60f);
			}
		}
	}

	public string GetHoverText()
	{
		if (m_picked)
		{
			return "";
		}
		return Localization.instance.Localize(GetHoverName() + "\n[<color=yellow><b>$KEY_Use</b></color>] $inventory_pickup");
	}

	public string GetHoverName()
	{
		if (!string.IsNullOrEmpty(m_overrideName))
		{
			return m_overrideName;
		}
		return m_itemPrefab.GetComponent<ItemDrop>().m_itemData.m_shared.m_name;
	}

	private void UpdateRespawn()
	{
		if (m_nview.IsValid() && m_nview.IsOwner() && m_picked)
		{
			long @long = m_nview.GetZDO().GetLong("picked_time", 0L);
			DateTime dateTime = new DateTime(@long);
			if ((ZNet.instance.GetTime() - dateTime).TotalMinutes > (double)m_respawnTimeMinutes)
			{
				m_nview.InvokeRPC(ZNetView.Everybody, "SetPicked", false);
			}
		}
	}

	private void RPC_SetPicked(long sender, bool picked)
	{
		SetPicked(picked);
	}

	private void SetPicked(bool picked)
	{
		m_picked = picked;
		if ((bool)m_hideWhenPicked)
		{
			m_hideWhenPicked.SetActive(!picked);
		}
		if (!m_nview.IsOwner())
		{
			return;
		}
		if (m_respawnTimeMinutes > 0 || m_hideWhenPicked != null)
		{
			m_nview.GetZDO().Set("picked", m_picked);
			if (picked && m_respawnTimeMinutes > 0)
			{
				DateTime time = ZNet.instance.GetTime();
				m_nview.GetZDO().Set("picked_time", time.Ticks);
			}
		}
		else if (picked)
		{
			m_nview.Destroy();
		}
	}

	public bool Interact(Humanoid character, bool repeat)
	{
		if (m_picked)
		{
			return false;
		}
		Vector3 pos = (m_pickEffectAtSpawnPoint ? (base.transform.position + Vector3.up * m_spawnOffset) : base.transform.position);
		m_pickEffector.Create(pos, Quaternion.identity);
		int num = 0;
		for (int i = 0; i < m_amount; i++)
		{
			Drop(m_itemPrefab, num++, 1);
		}
		if (!m_extraDrops.IsEmpty())
		{
			foreach (ItemDrop.ItemData dropListItem in m_extraDrops.GetDropListItems())
			{
				Drop(dropListItem.m_dropPrefab, num++, dropListItem.m_stack);
			}
		}
		m_nview.InvokeRPC(ZNetView.Everybody, "SetPicked", true);
		return m_useInteractAnimation;
	}

	private void Drop(GameObject prefab, int offset, int stack)
	{
		Vector2 vector = UnityEngine.Random.insideUnitCircle * 0.2f;
		Vector3 position = base.transform.position + Vector3.up * m_spawnOffset + new Vector3(vector.x, 0.5f * (float)offset, vector.y);
		Quaternion rotation = Quaternion.Euler(0f, UnityEngine.Random.Range(0, 360), 0f);
		GameObject obj = UnityEngine.Object.Instantiate(prefab, position, rotation);
		obj.GetComponent<ItemDrop>().m_itemData.m_stack = stack;
		obj.GetComponent<Rigidbody>().velocity = Vector3.up * 4f;
	}

	public bool UseItem(Humanoid user, ItemDrop.ItemData item)
	{
		return false;
	}
}