Coed 1
Coed 1
cs 1
1 using System.Collections;
2 using System.Collections.Generic;
3 using UnityEngine;
4 using UnityEngine.EventSystems;
5 using UnityEngine.UI;
6
7 public class player_movment : MonoBehaviour
8 {
9 private bool onRight = true; // if the character is on the right side
of the map
10 [SerializeField] private Rigidbody2D rb;//rigidbody of the player
11 public float forceX, health, maxDamage;// applied impulse force uppon
user input, health of the player, max damage recived by the player
12 [HideInInspector] public int coins, maxCoins;//total coins possesed by
the player, max coins to get upon impact with the coin gameobject
13 [HideInInspector] public float healthIncrease;//health increase to get
upon collision with health gameobject
14 [HideInInspector] public float score; // score of the player, updates
each fixed frame
15 [HideInInspector] public float scoreFactor; // score boost each fixed
frame
16 [SerializeField] private Text scoreText, coinText, healthText,
end_score_text, end_coin_text, totalBulletsText;// all the ui
elements connected to the core functionalities
17 [SerializeField] private GameObject Pause_Menu, end_menu,
particleSys1; //pause menu gameobject, end meny gameobject, particle
system of the bullet instantiation
18 private bool isPause;// if the player has enabled pause menu
19 private int mag = 1; //direction of the player in mod of vector
20 [SerializeField] private Transform shootPoint; // shooting point of
the player
21 public int range, totalBullets;// total range of the player to shoot
at
22 public bool DoubleTapShoot;
23 [SerializeField] private bool toMouse = false;
24 public int maxBulletsAdd;
25 [HideInInspector] public float shapeSize;
26 [SerializeField] private Transform main_cam;
27 public float effectFactor = 0.01f;
28 public float shakeDuration = 5, shakeAmmount = 0.7f;
29 [SerializeField] private GameObject particles_1;
30
31 private void Awake()
32 {
33 Application.targetFrameRate = 60;
34 player_movment thisData = GetComponent<player_movment>();
35 save_load.load(thisData);
36 transform.localScale = new Vector3(shapeSize / 2, shapeSize / 2,
shapeSize / 2);
C:\Users\yuvra\arcade_game\Assets\scripts\player_movment.cs 2
37 health = health + healthIncrease + 1000;
38 Pause_Menu.SetActive(false);
39 end_menu.SetActive(false);
40 }
41 private bool toShake;
42 private void FixedUpdate()
43 {
44 rb.velocity = new Vector2(mag * forceX, 0);
45 totalBulletsText.text = "" + totalBullets;
46 getScore();
47 }
48
49 private bool ClickedOnUi()
50 {
51
52 PointerEventData eventDataCurrentPosition = new PointerEventData
(EventSystem.current);
53 eventDataCurrentPosition.position = Input.GetTouch(0).position;
54 List<RaycastResult> results = new List<RaycastResult>();
55 EventSystem.current.RaycastAll(eventDataCurrentPosition, results);
56 // return results.Count > 0;
57 foreach (var item in results)
58 {
59 if (item.gameObject.CompareTag("UI"))
60 {
61 return true;
62 }
63 }
64 return false;
65 }
66
67 private void OnMouseDown()
68 {
69 if (Input.touchCount > 0)
70 {
71 if (ClickedOnUi()) return;
72 }
73 }
74
75 private void Update()
76 {
77
78 cameraEffect();
79
80 if (toMouse && Input.GetKeyDown(KeyCode.Mouse0))
81 {
82 toShake = true;
83 changeDir();
84 }
C:\Users\yuvra\arcade_game\Assets\scripts\player_movment.cs 3
85 if (toMouse && Input.GetKeyDown(KeyCode.Mouse1))
86 {
87 shoot();
88 }
89 if (Input.touchCount == 2 && DoubleTapShoot)
90 {
91 Touch touch1 = Input.GetTouch(0);
92 Touch touch2 = Input.GetTouch(1);
93 if (touch1.phase == 0 && touch2.phase == 0)
94 {
95 shoot();
96 return;
97 }
98 }
99 if (Input.touchCount == 1)
100 {
101 if (ClickedOnUi() == true)
102 {
103 return;
104 }
105 Touch touch = Input.GetTouch(0);
106 if (touch.phase == 0)
107 {
108 toShake = true;
109 changeDir();
110 }
111 }
112 takeDamage();
113 }
114
115
116 public void changeDir()
117 {
118 if (onRight)
119 {
120 mag = -1;
121 onRight = false;
122 }
123 else if (!onRight)
124 {
125 mag = 1;
126 onRight = true;
127 }
128 }
129 public void shoot()
130 {
131 if (totalBullets > 0)
132 {
133 RaycastHit2D rayShoot = Physics2D.Raycast(shootPoint.position,
C:\Users\yuvra\arcade_game\Assets\scripts\player_movment.cs 4
Vector2.down, range);
134 Instantiate(particleSys1, transform.position,
Quaternion.identity);
135 totalBullets -= 1;
136 if (rayShoot.collider)
137 {
138
139 if (rayShoot.collider.CompareTag("BLOCK_ENEMY") &&
totalBullets > 0)
140 {
141 Destroy(rayShoot.collider.gameObject);
142 }
143 else if (rayShoot.collider.CompareTag("BLOCK_COIN") &&
totalBullets > 0)
144 {
145 Destroy(rayShoot.collider.gameObject);
146 }
147 }
148 }
149 }
150 IEnumerator cameraShake(float duration, float magnitude)
151 {
152 Vector3 orignalPos = main_cam.localPosition;
153 float elapsedTime = 0.0f;
154 while (elapsedTime < duration)
155 {
156 float xOffset = Random.Range(-1f, 1f) * magnitude;
157 float yOffset = Random.Range(-1f, 1f) * magnitude;
158 main_cam.transform.localPosition = new Vector3(xOffset,
yOffset, orignalPos.z);
159 elapsedTime += Time.deltaTime;
160 yield return null;
161 }
162 main_cam.localPosition = orignalPos;
163 }
164
165
166 private void cameraEffect()
167 {
168 main_cam.position = new Vector3(transform.position.x *
effectFactor, main_cam.transform.position.y,
main_cam.transform.position.z);
169 }
170 public void pause_Menu()
171 {
172 if (isPause)
173 {
174 Pause_Menu.SetActive(false);
175 Time.timeScale = 1;
C:\Users\yuvra\arcade_game\Assets\scripts\player_movment.cs 5
176 isPause = false;
177 }
178 else
179 {
180 Pause_Menu.SetActive(true);
181 Time.timeScale = 0;
182 isPause = true;
183 }
184 }
185 private void takeDamage()
186 {
187 if (health <= 0)
188 {
189 player_movment this_data = GetComponent<player_movment>();
190 save_load.save(this_data);
191 Destroy(this.gameObject);
192 end_menu.SetActive(true);
193 end_score_text.text = "Total Score - " + score;
194 end_coin_text.text = "Total Coins Collected - " + coins;
195 Time.timeScale = 0;
196 return;
197 }
198 healthText.text = "" + health;
199 }
200 private void getScore()
201 {
202 float time = Time.timeSinceLevelLoad;
203 score = coins * scoreFactor / time;
204 scoreText.text = "" + score;
205 }
206
207 private void OnCollisionEnter2D(Collision2D collision)
208 {
209 if (collision.collider.CompareTag("SIDE_LINE") && toShake)
210 {
211 toShake = false;
212 StartCoroutine(cameraShake(shakeDuration, shakeAmmount));
213 Instantiate(particles_1, new Vector3(transform.position.x +
(shapeSize / 2 * mag), transform.position.y, 1),
Quaternion.identity);
214
215 }
216 if (collision.collider.CompareTag("BLOCK_ENEMY"))
217 {
218 health -= Random.Range(40, maxDamage);
219 Destroy(collision.collider.gameObject);
220
221 }
222 else if (collision.collider.CompareTag("BLOCK_COIN"))
C:\Users\yuvra\arcade_game\Assets\scripts\player_movment.cs 6
223 {
224 coins += Random.Range(2, maxCoins);
225 coinText.text = "" + coins;
226 Destroy(collision.collider.gameObject);
227 }
228
229 else if (collision.collider.CompareTag("END_LINE"))
230 {
231 health -= health;
232 }
233
234 else if (collision.collider.CompareTag("BLOCK_BULLET"))
235 {
236 totalBullets += Random.Range(1, maxBulletsAdd);
237 Destroy(collision.gameObject);
238 }
239 }
240 }