trò chơi game bóng rổ full code
2 using System;
3 using System.Collections.Generic;
4 public class Shoot : MonoBehaviour
5 {
6 public List<UniMoveController> moves = new List<UniMoveController>();
7 private UniMoveController currentMove;
8
9 public GameObject ball; //reference to the ball prefab, set in editor
10 private Vector3 throwSpeed = new Vector3(3, 10, 0); //This value is a sure basket, we'll modify this using the forcemeter
11 private Vector3 ballPos; //starting ball position
12 private bool thrown = false; //if ball has been thrown, prevents 2 or more balls
13 private GameObject ballClone; //we don't use the original prefab
14
15 public GameObject availableShotsGO; //ScoreText game object reference
16 public int availableShots = 5;
17
18 public GameObject meter; //references to the force meter
19 public GameObject arrow;
20 private float arrowSpeed; //Difficulty, higher value = faster arrow movement
21 public int difficulty = 1; // 1 is easy. 2 is normal. 3 is hard
22 private bool right = true; //used to revers arrow movement
23
24 public GameObject gameOver; //game over text
25
26 private GameObject Player; // Jogador
27
28 private int throwSpeedCount =0;
29 private float throwSpeedMultiplier;
30
31 // public Camera focusCamera;
32
33 private GameObject hoopRing;
34
35 private float leftBorder = 0.089f;
36 private float rightBorder = 0.289f;
37
38 private bool respawnControl = false;
39
40 private bool shootBlocked = false;
41
42 // private GameObject TimerHUD;
43 // private TimerScript timerScript;
44
45 private GameObject varDifficulty;
46
47 void Start()
48 {
49 arrow.transform.position = new Vector3(leftBorder,0.487829f,0);
50
51 /* Increase Gravity */
52 Physics.gravity = new Vector3(0, -20, 0);
53 Player = GameObject.Find ("Player");
54
55 varDifficulty = GameObject.Find ("varDifficulty");
56 if (varDifficulty != null)
57 difficulty = int.Parse (varDifficulty.GetComponent<GUIText> ().text);
58 else
59 difficulty = 3;
60
61 switch (difficulty) {
62 case 1: arrowSpeed=0.001f; break;
63 case 2: arrowSpeed=0.002f; break;
64 case 3: arrowSpeed=0.003f; break;
65 case 4: arrowSpeed=0.004f; break;
66 }
67
68 //TimerHUD = GameObject.Find("TimerHUD");
69 //timerScript = (TimerScript) TimerHUD.GetComponent(typeof(TimerScript));
70 Time.maximumDeltaTime = 0.1f;
71
72 int count = UniMoveController.GetNumConnected();
73
74 // Iterate through all connections (USB and Bluetooth)
75 for (int i = 0; i < count; i++)
76 {
77 UniMoveController move = gameObject.AddComponent<UniMoveController>(); // It's a MonoBehaviour, so we can't just call a constructor
78
79 // Remember to initialize!
80 if (!move.Init(i)) // TENTA INICIALIZAR
81 { // QUANDO RESTARTA O PROGRAMA ELE TENTA INICIALIZAR E NAO CONSEGUE PQ JA ESTA INICIADO
82 // ENTAO ELE DESTROI AS CONEXOES ABERTAS
83 // E POR ISSO DESLIGA O CONTROLE
84 // === EU ACHO... ===
85 Destroy(move); // If it failed to initialize, destroy and continue on
86 continue;
87 }
88
89 // This example program only uses Bluetooth-connected controllers
90 PSMoveConnectionType conn = move.ConnectionType;
91 if (conn == PSMoveConnectionType.Unknown || conn == PSMoveConnectionType.USB)
92 {
93 Destroy(move);
94 }
95 else
96 {
97 moves.Add(move);
98
99 move.OnControllerDisconnected += HandleControllerDisconnected;
100
101 // Start all controllers with a white LED
102 move.SetLED(Color.white);
103 Debug.Log ("PSMove conectado.");
104 }
105 }
106 if (moves.Count<=0) Debug.Log ("No Bluetooth-connected controllers found. Make sure one or more are both paired and connected to this computer.");
107 }
108
109 void FixedUpdate()
110 {
111 /* Move Meter Arrow */
112 if (arrow.transform.position.x < rightBorder && right)
113 {
114 arrow.transform.position += new Vector3(arrowSpeed, 0, 0);
115 throwSpeedCount++;
116 }
117 if (arrow.transform.position.x >= rightBorder)
118 {
119 right = false;
120 }
121 if (right == false)
122 {
123 arrow.transform.position -= new Vector3(arrowSpeed, 0, 0);
124 throwSpeedCount--;
125 }
126 if (arrow.transform.position.x <= leftBorder)
127 {
128 right = true;
129 }
130
131
132 foreach (UniMoveController move in moves) {
133 if (move.Disconnected) {
134 currentMove = null;
135 continue;
136 }
137 currentMove = move;
138 move.SetRumble (move.Trigger);
139 }
140
141 /* Shoot ball on Tap */
142 //if ((Input.GetButton("Fire1") || move.GetButtonDown(PSMoveButton.Trigger)) && !thrown && availableShots > 0 && shootBlocked==false)
143 bool shootButtonDown;
144 if (currentMove == null)
145 shootButtonDown = false;
146 else
147 shootButtonDown = currentMove.GetButtonDown (PSMoveButton.Trigger);
148 if ((Input.GetButton("Fire1") || shootButtonDown) && !thrown && availableShots > 0 && shootBlocked==false)
149 {
150 //Debug.Log(moves[0].Acceleration.x);
151 shootBlocked=true;
152 arrowSpeed=0;
153 thrown = true;
154 availableShots--;
155 //availableShotsGO.GetComponent().text = availableShots.ToString();
156 availableShotsGO.GetComponent<GUIText>().text = availableShots.ToString();
157
158 Vector3 playerPos = Player.transform.position;
159 ballPos.x=playerPos.x+0.234f;
160 ballPos.y=playerPos.y+1.4509f;
161 ballPos.z=playerPos.z-0.0234f;
162 ballClone = Instantiate(ball, ballPos, transform.rotation) as GameObject;
163 respawnControl=false;
164
165 if (throwSpeedCount*difficulty>97 && throwSpeedCount*difficulty<123) // Bem no meio
166 throwSpeedMultiplier=11;
167 else
168 throwSpeedMultiplier=(throwSpeedCount*difficulty)/10;
169
170 if (throwSpeedMultiplier==12) throwSpeedMultiplier=12.5f; // BUG FIX (Gambiarra)
171
172 hoopRing = GameObject.Find ("Ring");
173 Vector3 heading = hoopRing.transform.position - Player.transform.position;
174 throwSpeed.x = heading.x;
175 throwSpeed.z = heading.z + Player.transform.rotation.y/1000;
176 throwSpeed.y = heading.y + throwSpeedMultiplier-2.5f;
177
178 ballClone.GetComponent<Rigidbody>().AddForce(throwSpeed, ForceMode.Impulse);
179
180 GetComponent<AudioSource>().Play(); //play shoot sound
181 }
182
183 /* Remove Ball when it hits the floor */
184 if (ballClone != null && ballClone.transform.position.y < 0.25)
185 { /* Fim da rodada */
186
187 Destroy(ballClone,3);
188 thrown = false;
189 throwSpeed = new Vector3(3, 10, 0);//Reset perfect shot variable
190 switch (difficulty) {
191 case 1: arrowSpeed=0.001f; break;
192 case 2: arrowSpeed=0.002f; break;
193 case 3: arrowSpeed=0.003f; break;
194 case 4: arrowSpeed=0.004f; break;
195 }
196 arrow.transform.position = new Vector3(leftBorder,0.487829f,0);
197 right=true;
198 throwSpeedCount=0;
199
200 //Component TimerScript = GetComponent(Timer);
201
202
203 Respawn ();
204 StartCoroutine(enableShoot()); // Destrava o click
205
206 /* Check if out of shots */
207
208
209
210 if (availableShots == 0)
211 {
212 //arrow.renderer.enabled = false;
213 arrow.GetComponent<GUITexture>().enabled=false;
214 Invoke("restart", 2);
215 }
216 }
217
218 /*if (timerScript.getCurrentTime()<=0){
219 //Debug.Log ("Time Over!");
220 arrow.guiTexture.enabled = false;
221 //timerScript.restartTimer();
222 // TimerHUD = null;
223 // timerScript = null;
224 //Destroy(TimerHUD);
225 //availableShots = 0;
226 timerScript.restartTimer();
227 Invoke ("restart", 2);
228 }*/
229 }
230
231 System.Collections.IEnumerator enableShoot() { // Destrava o click
232 yield return new WaitForSeconds(3f);
233 shootBlocked = false;
234 StopAllCoroutines (); // O certo seria finalizar so esta thread, mas so consegui usando StopAllCoroutines()!
235 }
236
237 void Respawn(){
238 if (respawnControl==false) {
239 //float newX;
240 float newZ;
241 hoopRing = GameObject.Find ("Ring");
242
243 // direçao para olhar
244 // Vector3 heading = hoopRing.transform.position - Player.transform.position;
245 //Vector3 playerAngles = Player.transform.eulerAngles;
246 Vector3 playerPosition = Player.transform.position;
247 newZ = UnityEngine.Random.Range (3.0f,16.0f);
248 Player.transform.position = new Vector3 (playerPosition.x, playerPosition.y, newZ);
249
250 respawnControl=true;
251
252 /* Debug.Log (Player.transform.eulerAngles.y);
253 Player.transform.eulerAngles = new Vector3 (playerAngles.x, playerAngles.y+(heading.z*10), playerAngles.z);
254 respawnControl = true; */
255 }
256 }
257 void HandleControllerDisconnected (object sender, EventArgs e)
258 {
259 // TODO: Remove this disconnected controller from the list and maybe give an update to the player
260 }
261 void restart()
262 {
263 Application.LoadLevel(Application.loadedLevel);
264 }
265 }
public GameObject ball; reference to the ball prefab, set in editor
private Vector3 throwSpeed = new Vector3(3, 10, 0); This value is a sure basket, we'll modify this using the forcemeter
private Vector3 ballPos; starting ball position
private bool thrown = false; if ball has been thrown, prevents 2 or more balls
private GameObject ballClone; we don't use the original prefab
public GameObject availableShotsGO; ScoreText game object reference
public GameObject meter; references to the force meter
private float arrowSpeed; Difficulty, higher value = faster arrow movement
public int difficulty = 1; 1 is easy. 2 is normal. 3 is hard
private bool right = true; used to revers arrow movement
public GameObject gameOver; game over text
private GameObject Player; Jogador
public Camera focusCamera;
private GameObject TimerHUD;
private TimerScript timerScript;
TimerHUD = GameObject.Find("TimerHUD");
timerScript = (TimerScript) TimerHUD.GetComponent(typeof(TimerScript));
Iterate through all connections (USB and Bluetooth)
UniMoveController move = gameObject.AddComponent
Remember to initialize!
if (!move.Init(i)) TENTA INICIALIZAR
{ QUANDO RESTARTA O PROGRAMA ELE TENTA INICIALIZAR E NAO CONSEGUE PQ JA ESTA INICIADO
ENTAO ELE DESTROI AS CONEXOES ABERTAS
E POR ISSO DESLIGA O CONTROLE
=== EU ACHO... ===
Destroy(move); If it failed to initialize, destroy and continue on
This example program only uses Bluetooth-connected controllers
Start all controllers with a white LED
if ((Input.GetButton("Fire1") || move.GetButtonDown(PSMoveButton.Trigger)) && !thrown && availableShots > 0 && shootBlocked==false)
Debug.Log(moves[0].Acceleration.x);
availableShotsGO.GetComponent().text = availableShots.ToString();
if (throwSpeedCount*difficulty>97 && throwSpeedCount*difficulty<123) Bem no meio
if (throwSpeedMultiplier==12) throwSpeedMultiplier=12.5f; BUG FIX (Gambiarra)
GetComponent
throwSpeed = new Vector3(3, 10, 0);Reset perfect shot variable
Component TimerScript = GetComponent(Timer);
StartCoroutine(enableShoot()); Destrava o click
arrow.renderer.enabled = false;
Debug.Log ("Time Over!");
timerScript.restartTimer();
TimerHUD = null;
timerScript = null;
Destroy(TimerHUD);
availableShots = 0;
System.Collections.IEnumerator enableShoot() { Destrava o click
StopAllCoroutines (); O certo seria finalizar so esta thread, mas so consegui usando StopAllCoroutines()!
float newX;
direçao para olhar
Vector3 heading = hoopRing.transform.position - Player.transform.position;
Vector3 playerAngles = Player.transform.eulerAngles;
TODO: Remove this disconnected controller from the list and maybe give an update to the player