SHOW:
|
|
- or go back to the newest paste.
1 | using System.Collections; | |
2 | using System.Collections.Generic; | |
3 | using UnityEngine; | |
4 | using TMPro; //Wymagane do zarządzania tekstami | |
5 | using Photon.Realtime; | |
6 | using Photon.Pun; | |
7 | using System.IO; | |
8 | ||
9 | public class PongManager : MonoBehaviour | |
10 | { | |
11 | //czy gra trwa | |
12 | public bool gameRun; | |
13 | ||
14 | //Singleton odpowidzialny za zarządzanie | |
15 | public static PongManager instance; | |
16 | ||
17 | //punkty graczy | |
18 | public int player1Points; | |
19 | public int player2Points; | |
20 | ||
21 | //odniesienie do naszych tekstów | |
22 | public TMP_Text p1p; | |
23 | public TMP_Text p2p; | |
24 | ||
25 | //Odniesienie do piłki | |
26 | Ball ball; | |
27 | ||
28 | public Transform p1; | |
29 | public Transform p2; | |
30 | ||
31 | ||
32 | void Awake() | |
33 | { | |
34 | //Podłączenie singletona | |
35 | if(instance == null) | |
36 | { | |
37 | instance = this; | |
38 | } | |
39 | ||
40 | gameRun = false; | |
41 | player1Points = 0; | |
42 | player2Points = 0; | |
43 | UIUpdate(); | |
44 | //znalezienie obiektu z skryptem ball | |
45 | ball = FindObjectOfType<Ball>(); | |
46 | CreatePlayer(); | |
47 | } | |
48 | ||
49 | void CreatePlayer() | |
50 | { | |
51 | //Jeżeli jesteśmy twórcą pokoju to zostajemy graczem 1 | |
52 | if(PhotonNetwork.IsMasterClient) | |
53 | { | |
54 | PhotonNetwork.Instantiate("Paddle", p1.position, Quaternion.identity); | |
55 | ||
56 | } else //w przeciwnym wypadku graczem 2 | |
57 | { | |
58 | PhotonNetwork.Instantiate("Paddle", p2.position, Quaternion.identity); | |
59 | } | |
60 | ||
61 | } | |
62 | ||
63 | void Update() | |
64 | { | |
65 | //Uruchomienie gry, tylko założyciel może uruchomić kulkę | |
66 | if (Input.GetKeyDown(KeyCode.Space) && !gameRun && PhotonNetwork.IsMasterClient) | |
67 | { | |
68 | ball.StartGame(); | |
69 | gameRun = true; | |
70 | } | |
71 | ||
72 | //zamknięcie aplikacji po wciśnięciu Esc | |
73 | if (Input.GetKeyDown(KeyCode.Escape)) | |
74 | { | |
75 | Application.Quit(); | |
76 | } | |
77 | } | |
78 | ||
79 | //Wyświetlanie punktów | |
80 | public void UIUpdate() | |
81 | { | |
82 | p1p.text = player1Points.ToString(); | |
83 | p2p.text = player2Points.ToString(); | |
84 | } | |
85 | ||
86 | //zwiększenie ilości punktów, true dla gracza 1 a false dla gracza 2 | |
87 | public void UpdatePoints(bool player) | |
88 | { | |
89 | if(player) player1Points++; | |
90 | else player2Points++; | |
91 | UIUpdate(); | |
92 | } | |
93 | } | |
94 |