-
Notifications
You must be signed in to change notification settings - Fork 0
/
Weapon.cs
150 lines (140 loc) · 4.53 KB
/
Weapon.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
146
147
148
149
150
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//This class is attached to the player object and handle the shooting.
public class Weapon : MonoBehaviour
{
public GameObject bulletPrefab1;
public GameObject bulletPrefab2;
public GameObject image1;
public GameObject image2;
public Transform firePoint;
public static Vector2 mousePos;
public AudioClip shootingAudio;
private float bulletForce;
private int interval1;
private int interval2;
private int counter1 = 0;
private int counter2 = 0;
private bool canfire1 = true;
private bool canfire2 = true;
private bool difficulty;
private int weaponstate = 0;
public static Queue<GameObject> Pool;
//This function is called before the first frame to initialize the bullet speed and firing rate.
//The function also instantiate a few bullet objects into a queue for object pooling.
void Start()
{
difficulty = setvol.isHard;
bulletForce = 10;
if(difficulty == true)
{
interval1 = 20;
interval2 = 100;
}
else
{
interval1 = 15;
interval2 = 80;
}
Pool = new Queue<GameObject>();
for (int i = 0; i < 3; i++)
{
GameObject ammoObject = Instantiate(bulletPrefab2);
ammoObject.SetActive(false);
Physics2D.IgnoreCollision(ammoObject.GetComponent<Collider2D>(), GetComponent<Collider2D>());
Pool.Enqueue(ammoObject);
}
}
//This function is called every frame to check from left click or right click, where left click fires and right click switch bullet.
void Update()
{
if (Input.GetButtonDown("Fire2"))
{
if (weaponstate == 0)
{
image1.SetActive(false);
image2.SetActive(true);
weaponstate = 1;
}
else if (weaponstate == 1)
{
image2.SetActive(false);
image1.SetActive(true);
weaponstate = 0;
}
}
if (Input.GetButtonDown("Fire1"))
{
if (weaponstate == 0)
{
if (canfire1 == true)
{
canfire1 = false;
counter1 = 0;
AudioSource.PlayClipAtPoint(shootingAudio, Camera.main.transform.position);
shoot();
}
}
else
{
if (canfire2 == true)
{
canfire2 = false;
counter2 = 0;
AudioSource.PlayClipAtPoint(shootingAudio, Camera.main.transform.position);
FireAmmo();
}
}
}
}
//This function is called with a fixed interval to serve as a timer for fire rate.
void FixedUpdate()
{
if (counter1 == interval1)
{
canfire1 = true;
}
if (counter2 == interval2)
{
canfire2 = true;
}
if (canfire1 == false)
{
counter1 += 1;
}
if (canfire2 == false)
{
counter2 += 1;
}
}
//Set the bullet object as active when fired and return the object.
private GameObject SpawnAmmo()
{
GameObject ammo = Pool.Dequeue();
ammo.transform.position = firePoint.position;
ammo.transform.rotation = firePoint.rotation;
ammo.SetActive(true);
Pool.Enqueue(ammo);
return ammo;
}
//This function is called when the second type of bullet is fired.
private void FireAmmo()
{
mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
GameObject ammo = SpawnAmmo();
}
//Destroy all bullet objects if player object is destroyed.
void OnDestroy()
{
Pool = null;
}
//This function is called when the first type of bullet is fired, to instantiate the bullet and add force to its.
void shoot()
{
GameObject bullet = Instantiate(bulletPrefab1, firePoint.position, firePoint.rotation);
Physics2D.IgnoreCollision(bullet.GetComponent<Collider2D>(), GetComponent<Collider2D>());
Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>();
rb.AddForce(firePoint.up * bulletForce, ForceMode2D.Impulse);
}
}