123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using QFSW.MOP2;
- using Sirenix.OdinInspector;
- using KairoEngine.Core;
- using KairoEngine.Inventory;
- using KairoEngine.SFX;
- namespace KairoEngine.CharacterSystem
- {
- public class Firearm : MonoBehaviour
- {
- [ShowInInspector]
- public ItemFirearmRef itemFirearmRef;
- public Transform barrel;
- public Transform aim;
- public Transform grip;
- public float camShakeTime = 0.2f;
- public float camShakeIntensity = 0.3f;
-
- private Transform direction;
- [HideInInspector]
- public CharacterController character;
- [ReadOnly] public bool isAutomatic = false;
- [ReadOnly] public float firingSpeed = 1f;
- [ReadOnly] public float soundIntensity = 10f;
- [ReadOnly] public GameObject bulletPrefab;
- [ReadOnly] public bool isReady = true;
- private float counter = 0f;
- private ItemBaseFirearm weapon ;
- private int burstCurrentBullet = 0;
- private string bulletPrefabPool;
- private bool playerCharacter = false;
- private void Start()
- {
- character = gameObject.GetComponentInParent<CharacterController>();
- if(itemFirearmRef == null) return;
- ItemBase item = itemFirearmRef.item;
- weapon = (ItemBaseFirearm)item;
- if(weapon != null)
- {
- isAutomatic = weapon.isAutomatic;
- firingSpeed = weapon.firingSpeed;
- bulletPrefab = weapon.ammoType.bulletPrefab;
- bulletPrefabPool = weapon.ammoType.bulletPrefabPool;
- }
- CharacterAnimator characterAnimator = gameObject.GetComponentInParent<CharacterAnimator>();
- if(characterAnimator!= null) direction = characterAnimator.transform;
- if(character.unique_name == "player-character_") playerCharacter = true;
- else playerCharacter = false;
- }
- private void Update()
- {
- if(character == null || transform.parent == null) return;
- CoolDown();
- Burst();
- }
- private void CoolDown()
- {
- if (!isReady)
- {
- counter += Time.deltaTime;
- if (counter >= firingSpeed)
- {
- isReady = true;
- counter = 0f;
- }
- }
- }
- public void Fire()
- {
- if (isReady)
- {
- isReady = false;
- burstCurrentBullet += 1;
- if(itemFirearmRef.ammo < 1)
- {
- // No ammo
- SoundController.EmmitSound(weapon.emptySound, transform.position);
- }
- else
- {
- // Remove bullet from magazine
- itemFirearmRef.ammo -= 1;
- // Instantiate Bullets
- for (int i = 0; i < weapon.bulletsPerShot; i++)
- {
- GameObject obj = null;
- if(bulletPrefabPool != "") obj = MasterObjectPooler.Instance.GetPool(bulletPrefabPool).GetObject();
- else obj = Instantiate(bulletPrefab);
- Bullet bullet = obj.GetComponent<Bullet>();
- bullet.Initialize(barrel, itemFirearmRef, character, bulletPrefabPool);
- }
- // Create sound
- SoundController.EmmitSound(weapon.shootSound, transform.position);
- // Shake Camera
- if(playerCharacter) GenericEvents.Trigger("CameraShake", camShakeIntensity, camShakeTime);
- }
- }
- }
- private void Burst()
- {
- if(burstCurrentBullet == weapon.burstFire)
- {
- // Burst end
- burstCurrentBullet = 0;
- }
- if(weapon.burstFire > 1)
- {
- if(burstCurrentBullet > 0)
- {
- Fire();
- }
- }
- }
- }
- }
|