72 lines
1.9 KiB
C#
72 lines
1.9 KiB
C#
using UnityEngine;
|
|
using System.Collections;
|
|
|
|
[ExecuteInEditMode]
|
|
public class Crossbow : Gun {
|
|
public Button fireButton;
|
|
public CrossHair crosshair;
|
|
public GameObject arrowTemplate;
|
|
private GameObject fireSpot;
|
|
private SoundTable sounds;
|
|
public AudioClip singleShotSound;
|
|
public AudioClip reloadSound;
|
|
private AudioSource shootAudioSource;
|
|
private const int cartridgeSize = 30;
|
|
private int loadedAmmo = 0;
|
|
private int availableAmmo = cartridgeSize * 5;
|
|
|
|
// Use this for initialization
|
|
void Start () {
|
|
fireButton.EventDown += new ButtonDelegate (onFire);
|
|
fireSpot = Utils.FindChildRecursive (gameObject, "FireSpot");
|
|
shootAudioSource = fireSpot.AddComponent<AudioSource> ();
|
|
sounds = gameObject.GetComponent<SoundTable> ();
|
|
Reload ();
|
|
}
|
|
|
|
void onFire(Button btn){
|
|
if (loadedAmmo > 0) {
|
|
var shootTarget = crosshair.Target;
|
|
GameObject arrow = (GameObject)Instantiate (arrowTemplate);
|
|
arrow.transform.position = transform.TransformPoint (Vector3.zero);
|
|
CrossbowArrow scr = arrow.GetComponent<CrossbowArrow> ();
|
|
scr.StartPoint = fireSpot.transform.TransformPoint (Vector3.zero);
|
|
scr.EndPoint = shootTarget;
|
|
scr.Speed = 40;
|
|
arrow.SetActive (true);
|
|
|
|
//shootAudioSource.PlayOneShot (singleShotSound);
|
|
loadedAmmo --;
|
|
} else {
|
|
shootAudioSource.PlayOneShot (reloadSound);
|
|
Reload();
|
|
}
|
|
}
|
|
|
|
void Reload(){
|
|
if (loadedAmmo < cartridgeSize && availableAmmo > 0) {
|
|
int emptyCount = cartridgeSize - loadedAmmo;
|
|
int loadCount = emptyCount > availableAmmo ? availableAmmo : emptyCount;
|
|
loadedAmmo += loadCount;
|
|
availableAmmo -= loadCount;
|
|
}
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update () {
|
|
|
|
}
|
|
|
|
public override int GetLoadedAmmo(){
|
|
return loadedAmmo;
|
|
}
|
|
public override int GetAvailableAmmo(){
|
|
return availableAmmo;
|
|
}
|
|
|
|
void OnDrawGizmos(){
|
|
Gizmos.DrawWireSphere (transform.position, 0.5f);
|
|
Gizmos.DrawRay ( new Ray(transform.position, transform.forward * 2));
|
|
}
|
|
}
|