EnemyShooting.cs
Description
The enemy shooting script, controlling the weapons used by the enemy.
Script
using System;
using UnityEngine;
using Weapons.Enemy;
namespace AI
{
public class EnemyShooting : MonoBehaviour
{
private enum WeaponType
{
Pistol,
Shotgun
}
[SerializeField] private WeaponType weaponType;
[SerializeField] private EnemyPistol pistol;
[SerializeField] private EnemyShotgun shotgun;
public EnemyBaseWeapon CurrentWeapon { get; private set; }
public bool CanAttack { get; set; }
public void Start()
{
CanAttack = true;
switch (weaponType)
{
case WeaponType.Pistol:
EquipWeapon(pistol);
break;
case WeaponType.Shotgun:
EquipWeapon(shotgun);
break;
default:
throw new ArgumentOutOfRangeException();
}
}
private void EquipWeapon(EnemyBaseWeapon newWeapon)
{
CurrentWeapon = newWeapon;
CurrentWeapon.CurrentPrimaryAmmo = CurrentWeapon.maxPrimaryAmmo;
CurrentWeapon.CurrentSecondaryAmmo = CurrentWeapon.maxSecondaryAmmo;
}
public void Reload()
{
if (!CurrentWeapon) return;
CurrentWeapon.Reload();
}
public void Fire()
{
if (!CurrentWeapon || !CanAttack) return;
CurrentWeapon.Fire();
}
}
}
Private Methods
- EquipWeapon()
Equips a new weapon. Sets the current weapon to the new weapon, and updates the ammo according to the values set by the weapon script.
Public Methods
- Fire()
Calls the fire function on the current weapon the enemy has.
- Reload()
Calls the reload function on the current weapon the enemy has.
- Start()
States that the enemy is able to attack. Checks what weapon type the enemy has been configured to be, and equips the according weapon.
Variables
- CanAttack
Can the enemy currently attack.
- CurrentWeapon
The currently equipped weapon.
- pistol
The pistol prefab to be equipped by the enemy.
- shotgun
The shotgun prefab to be equipped by the enemy.
- weaponType
The weapon type to be used by the enemy.
Last modified: 30 April 2024