-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArrowManager.cs
145 lines (118 loc) · 4.27 KB
/
ArrowManager.cs
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Valve.VR;
public class ArrowManager : MonoBehaviour {
public static ArrowManager instance;
private int playerHP;
private GameObject currentArrow;
public GameObject bow;
public GameObject arrowModel;
public GameObject bowAttachPoint;
public GameObject stringStartPoint;
public GameObject stringAttachPoint;
public GameObject rightHandModel;
public SteamVR_Behaviour_Pose rightController;
public SteamVR_Behaviour_Pose leftController;
public GameObject headset;
public Transform headTransform;
private AudioSource audio;
private bool isAttached = false;
public float speed;
public int score;
// Awake is called before Start
void Awake() {
if (instance == null)
{
instance = this;
}
audio = GetComponent<AudioSource>();
rightHandModel.SetActive(true);
score = 0;
playerHP = 3;
}
void OnDestroy() {
if (instance == this)
{
instance = null;
}
}
public void DecPlayerHP() {
playerHP -= 1;
audio.Play();
}
private void PullString() {
float distance = 11f * (stringStartPoint.transform.position - rightController.transform.position).magnitude;
if (distance > 5.4f)
{
distance = 5.4f;
}
stringAttachPoint.transform.localPosition = stringStartPoint.transform.localPosition + new Vector3(distance, 0f, 0f);
}
public void ReleaseArrow() {
float distance = 11f * (stringStartPoint.transform.position - rightController.transform.position).magnitude;
if (distance > 5.4f)
{
distance = 5.4f;
}
if (currentArrow != null)
{
Rigidbody rBody = currentArrow.GetComponent<Rigidbody>();
currentArrow.transform.parent = null;
rBody.velocity = speed * distance * currentArrow.transform.forward;
rBody.useGravity = true;
isAttached = false;
currentArrow = null;
rightHandModel.SetActive(true);
stringAttachPoint.transform.position = stringStartPoint.transform.position;
}
}
// Update is called once per frame
void Update () {
AttachArrowToHand();
if (isAttached)
{
PullString();
bow.transform.rotation = Quaternion.LookRotation(leftController.transform.position - rightController.transform.position, -leftController.transform.forward);
}
headTransform = headset.transform;
if (playerHP <= 0)
{
playerHP = 3;
ResetGame();
}
}
private void ResetGame() {
int scoreCopy = score;
score = 0;
SpawnEnemies.instance.gameStarted = false;
SpawnEnemies.instance.DespawnAllEnemies();
GameObject startCube = GameObject.FindGameObjectWithTag("StartCube");
startCube.gameObject.GetComponent<MeshRenderer>().enabled = true;
startCube.gameObject.GetComponent<BoxCollider>().enabled = true;
GameObject.FindGameObjectWithTag("StartText").GetComponent<MeshRenderer>().enabled = true;
GameObject scoreText = GameObject.FindGameObjectWithTag("ScoreText");
scoreText.GetComponent<TextMesh>().text = "Previous score: " + scoreCopy;
scoreText.GetComponent<MeshRenderer>().enabled = true;
}
private void AttachArrowToHand() {
if (currentArrow == null) {
if (DrawArrow.instance.inQuiver) {
currentArrow = Instantiate(arrowModel);
currentArrow.transform.parent = rightController.transform;
currentArrow.transform.localPosition = new Vector3(0f, -0.01f, 0.26f);
currentArrow.transform.localRotation = Quaternion.identity;
rightHandModel.SetActive(false);
}
}
}
public void AttachArrowToBow() {
currentArrow.transform.parent = stringAttachPoint.transform;
currentArrow.transform.localPosition = bowAttachPoint.transform.localPosition;
currentArrow.transform.rotation = bowAttachPoint.transform.rotation;
isAttached = true;
}
public void IncScore() {
score += 1;
}
}