-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayerFire.cs
More file actions
112 lines (84 loc) · 2.63 KB
/
PlayerFire.cs
File metadata and controls
112 lines (84 loc) · 2.63 KB
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
/*======================================================================
* PlayerFire
*
* Programmer : David Torrente
*
* Description: This class controls the firing mechanism for the gun.
* It checks to make sure there is ammunition in the gun, and then
* casts a ray to fire. It also holds the controls for reloading the
* gun.
* ====================================================================*/
using UnityEngine;
using System.Collections;
public class PlayerFire : MonoBehaviour {
private RaycastHit objHit;
private float fireRate;
private float nextShot;
private int damage;
private float range;
private PlayerInfo playerInfo;
private MuzzleBlast muzzleBlast;
private bool reloading;
private float reloadTime;
private AudioSource audioSource;
public AudioClip shotGunBlastSound;
public AudioClip reloadSound;
public AudioClip emptyClipSound;
public GameObject muzzleBlastObj;
public int playerNum;
private EnemyInfo enemyInfo;
void Start ()
{
playerInfo = GetComponentInParent<PlayerInfo> ();
muzzleBlast = muzzleBlastObj.GetComponent<MuzzleBlast> ();
audioSource = GetComponent<AudioSource> ();
damage = 30;
nextShot = 0F;
fireRate = 1F;
range = 100F;
reloadTime = 2F;
reloading = false;
}
void Update ()
{
if (Input.GetAxis ("Fire" + playerNum) != 0 && Time.time > nextShot)
{
fire();
}
if(Input.GetAxis("Reload" + playerNum) != 0 && playerInfo.getCurrentAummunition() != playerInfo.getClipSize())
{
StartCoroutine(reload());
}
}
void fire()
{
playerInfo.reduceCurrentAmmo ();
audioSource.PlayOneShot (shotGunBlastSound);
nextShot = Time.time + fireRate;
muzzleBlast.enabled = true;
StartCoroutine (muzzleBlast.cycleMuzzleBlast ());
int scoreChange = 0;
Debug.DrawRay(transform.position, transform.forward*range, Color.green);
if (Physics.Raycast(transform.position, transform.forward, out objHit, range))
{
Debug.DrawRay(transform.position, transform.forward*range, Color.red);
if (objHit.collider.gameObject.tag == "Enemy")
{
enemyInfo = objHit.collider.gameObject.GetComponentInParent<EnemyInfo> ();
scoreChange = enemyInfo.reportHit (damage, objHit.point);
if ( scoreChange != 0)
{
playerInfo.addPoints (scoreChange);
enemyInfo.death (objHit.point);
}
}
}
}//END fire
private IEnumerator reload()
{
audioSource.PlayOneShot (reloadSound);
reloading = true;
yield return new WaitForSeconds (reloadTime);
reloading = false;
}
}