-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathItemManager.cs
More file actions
162 lines (129 loc) · 4.56 KB
/
ItemManager.cs
File metadata and controls
162 lines (129 loc) · 4.56 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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
using UnityEngine;
public struct PosRotData {
public Vector3 pos;
public Quaternion rot;
public PosRotData(Vector3 pos, Quaternion rot) {
this.pos = pos;
this.rot = rot;
}
}
public class ItemManager : MonoBehaviour {
public static PosRotData[] originalTransforms = new PosRotData[10];
PosRotData almond_water;
PosRotData torch_light;
PosRotData notebook;
PosRotData goofy_notebook;
PosRotData chips;
public Transform handPivot;
public GameObject hand;
public GameObject dropEmpty;
public static GameObject[] hotbarItems = { null, null, null };
public static int[] hotbarItemsUID = { 0, 0, 0 };
public static int hotbarSlot;
public static bool debug = true;
public static void Clear() {
hotbarItems[0] = hotbarItems[1] = hotbarItems[2] = null;
}
private void Start() {
hotbarSlot = 0;
torch_light = new PosRotData(
new Vector3(0, 0, 0),
new Quaternion(0, 0, 0, 1)
);
almond_water = new PosRotData(
new Vector3(0, 0, 0),
new Quaternion(-0.5f, 0.5f, 0.5f, 0.5f)
);
goofy_notebook = new PosRotData(
new Vector3(0, 0, -0.15f),
new Quaternion(0, 0, -0.707106829f, 0.707106829f)
);
notebook = new PosRotData(
new Vector3(0, 0, -0.15f),
new Quaternion(0, 0, -0.707106829f, 0.707106829f)
);
chips = new PosRotData(
new Vector3(-0.15f, 0, 0),
new Quaternion(0, 1, 0, 0)
);
originalTransforms[0] = torch_light;
originalTransforms[1] = almond_water;
originalTransforms[2] = goofy_notebook;
originalTransforms[3] = notebook;
originalTransforms[4] = chips;
}
private void OnGUI() {
if (!debug) return;
GUI.Label(new Rect(10, 1, 300, 300), "Debug:");
GUI.Label(new Rect(10, 31, 300, 300), hotbarSlot.ToString());
GUI.Label(new Rect(10, 46, 300, 300),
string.Format("[{0}, {1}, {2}]", hotbarItems[0], hotbarItems[1], hotbarItems[2])
);
GUI.Label(new Rect(10, 60, 300, 300), string.Format("Amp: {0}, Freq: {1}", HeadBob.amplitude, HeadBob.frequency));
if (hit.collider == null) {
GUI.Label(new Rect(10, 16, 300, 300), "not hit");
return;
}
GUI.Label(new Rect(10, 16, 300, 300), "hit: " + hit.collider.gameObject.layer);
}
RaycastHit hit;
Outline outline;
bool highlight;
private void Update() {
Ray ray = Player.shared.playerCamera.ScreenPointToRay(Input.mousePosition);
if (!highlight && outline != null) {
outline.enabled = false;
outline.OutlineMode = Outline.Mode.OutlineHidden;
}
// Layermask 9: Item
// 512 = 1 << 9
if (Physics.Raycast(ray, out hit, 2, 512)) {
Collider collider = hit.collider;
outline = collider.GetComponent<Outline>();
outline.enabled = true;
outline.OutlineMode = Outline.Mode.OutlineAll;
highlight = true;
if (Input.GetKeyDown(KeyCode.F) && hotbarItems[hotbarSlot] == null) {
outline.enabled = false;
outline.OutlineMode = Outline.Mode.OutlineHidden;
Item item = collider.GetComponent<Item>();
Rigidbody r = item.GetComponent<Rigidbody>();
r.isKinematic = true;
r.useGravity = false;
Item collectedItem = Instantiate(item, hand.transform.position, Quaternion.identity, hand.transform);
collectedItem.transform.SetLocalPositionAndRotation(
originalTransforms[item.ID].pos, originalTransforms[item.ID].rot
);
hotbarItems[hotbarSlot] = collectedItem.gameObject;
Destroy(collider.gameObject);
}
} else highlight = false;
if (Input.GetKeyDown(KeyCode.V) && hotbarItems[hotbarSlot] != null) {
GameObject item = hotbarItems [hotbarSlot];
GameObject clone = Instantiate(item, dropEmpty.transform.position, dropEmpty.transform.rotation);
Destroy(item);
Rigidbody rigidBody = clone.GetComponent<Rigidbody>();
rigidBody.useGravity = true;
rigidBody.isKinematic = false;
rigidBody.WakeUp();
if (hotbarItems[hotbarSlot] != null) // -1 is air
hotbarItems[hotbarSlot] = null;
}
if (Input.mouseScrollDelta.y > 0) {
hotbarSlot++;
if (hotbarSlot > 2) hotbarSlot = 0;
handPivot.localRotation = Quaternion.Euler(60, 0, 0);
}
if (Input.mouseScrollDelta.y < 0) {
hotbarSlot--;
if (hotbarSlot < 0) hotbarSlot = 2;
handPivot.localRotation = Quaternion.Euler(60, 0, 0);
}
SwitchItem(hotbarSlot);
}
public void SwitchItem(int slot) {
for (int i = 0; i < 3; i++)
if (hotbarItems.Length > i && hotbarItems[i] != null)
hotbarItems[i].SetActive(i == slot);
}
}