Diwali_Rifle Code Implementation😁


Overview

The Diwali_Rifle class implements the IWeapon interface, providing the functionality for a fully-automatic firearm weapon with a rapid-fire feature and projectile-based damage mechanics. This weapon supports both automatic and single-shot firing modes, with realistic visual and sound effects.

Class Breakdown

Variables

  1. weaponManager: Manages the equipped weapon and associated actions.

  2. weaponData: Stores details about the weapon .

  3. bulletSpeed: A constant value defining the speed of the bullet.

  4. isAutoFireOn: Boolean to toggle between automatic and single-shot firing modes.

  5. boolGiveDamageOnRayCastHit: Controls whether damage is applied upon a successful raycast hit.

Methods

1. EquipWeapon

  • Purpose: Prepares the DiwaliRifle for use by equipping it, initializing its data, and enabling raycast-based damage.

  • Parameters:

    • weaponManager: Reference to the weapon manager.

    • weaponUserType: Specifies who is equipping the weapon (e.g., player or enemy).

    • weaponSwitchType: Specifies the type of weapon switch (e.g., swap or drop-pick).

  • Key Actions:

    • Replaces the current weapon with the DiwaliRifle.

    • Retrieves weapon data and enables raycast damage.

2. UnEquipWeapon

  • Purpose: Handles unequipping the weapon, resetting necessary variables.

  • Parameters:

    • weaponUserType: Specifies who is unequipping the weapon.

  • Key Actions:

    • Logs the unequip action for debugging.

3. Shoot

  • Purpose: Handles firing logic, supporting both automatic and single-shot modes.

  • Actions:

    • Calls AutoShoot coroutine for rapid fire when isAutoFireOn is true.

    • Calls SingleShoot for single-shot firing.

4. AutoShoot

  • Purpose: Implements rapid fire by repeatedly calling SingleShoot with a short interval.

  • Key Actions:

    • Iteratively calls SingleShoot while the trigger button is pressed.

5. SingleShoot

  • Purpose: Fires a single bullet, playing audio, visuals, and managing projectile movement.

  • Key Actions:

    • Plays muzzle flash and firing sounds.

    • Spawns bullet caps and applies random force and torque for realism.

    • Uses raycasting to determine hits and applies damage.

    • Spawns projectiles for visual representation of bullets.

6. MoveBullet

  • Purpose: Handles the movement of the projectile towards the hit point, including visuals.

  • Parameters:

    • bullet: Transform of the projectile being moved.

    • hit: RaycastHit data representing the hit target.

    • isFakeHit (optional): Specifies if the hit is simulated.

  • Key Actions:

    • Moves the projectile along a trail towards the target.

    • Spawns a wall hit effect if the hit object is not an enemy or player.

7. GetWeaponData

  • Purpose: Retrieves the weapon data for the DiwaliRifle.

  • Returns: A WeaponData object containing details like damage and range.

Key Features

Dynamic Firing Modes

  • Supports both automatic and single-shot firing mechanisms for versatile combat.

Realistic Effects

  • Muzzle flashes, bullet caps, and wall hit effects enhance the visual experience.

  • 3D audio integration provides immersive sound feedback.

Damage Mechanics

  • Raycasting ensures precise damage application to enemies or players.

  • Projectiles visually simulate bullet movement in the game environment.

Projectile Pooling

  • Efficient resource management using a pool for projectiles, reducing runtime instantiation overhead.

Implementation Code

public class DiwaliRifle : IWeapon
 {
     IWeaponManager weaponManager;
     WeaponData weaponData;
     const float bulletSpeed = 170;
     bool isAutoFireOn = true;
     bool boolGiveDamageOnRayCastHit = false;
     public void EquipWeapon(IWeaponManager weaponManager, WeaponUserType weaponUserType, WeaponSwitchType weaponSwitchType)
     {
         this.weaponManager = weaponManager;
         weaponData = WeaponInventory.instance.weaponsData.Find(ele => ele.weaponName == WeaponName.DiwaliRifle);
         weaponManager.weaponHolder.ReplaceWeapon(weaponData, weaponSwitchType);
         Debug.Log("EquipWeapon: DiwaliRifle");
         boolGiveDamageOnRayCastHit = true;
     }
     public void UnEquipWeapon(WeaponUserType weaponUserType)
     {
         Debug.Log("UnEquipWeapon: DiwaliRifle");
     }
     public void Shoot()
     {
         if (isAutoFireOn)
         {
             weaponManager.StartCoroutine(AutoShoot());
         }
         else
         {
             SingleShoot();
         }
     }
     public IEnumerator AutoShoot()
     {
         while (weaponManager.isPressingTriggerButton)
         {
             SingleShoot();
             yield return new WaitForSeconds(weaponData.autoAttackInterval);
         }
     }
     public void SingleShoot()
     {
         AudioManager2.instance.Play3DAudio(AudioType.RifleFiring, weaponManager.transform.position);
         List<Transform> bulletStartPoints = weaponManager.weaponHolder.GetBulletStartPoints();
         foreach (Transform bulletStartPoint in bulletStartPoints)
         {
             weaponManager.weaponHolder.PlayMuzzleFlash();
             GameObject cap = IWeaponManager.Instantiate(weaponData.projectileCap);
             cap.transform.position = weaponManager.weaponHolder.GetHandPosition(false).position + bulletStartPoint.forward / 2.5f;
             Rigidbody bulletCapRB = cap.GetComponent<Rigidbody>();
             bulletCapRB.AddForce(weaponManager.weaponHolder.transform.right * 3, ForceMode.Impulse);
             Ray ray = new Ray(bulletStartPoint.position, bulletStartPoint.forward);
             RaycastHit hit;
             if (Physics.Raycast(ray, out hit, weaponData.maxDistanceCanCover, weaponData.layerMask))
             {
                 if (boolGiveDamageOnRayCastHit && hit.collider.CompareTag(Consts.enemyTagString))
                 {
                     hit.collider.GetComponent<IEnemy>().TakeDamage(weaponData.damage, weaponManager.gameObject, hit.point);
                 }
             }
         }
     }
     public WeaponData GetWeaponData()
     {
         return weaponData;
     }
 } 

Notes

  1. Ensure WeaponInventory contains the necessary data for the Diwali_Rifle.

  2. Test both firing modes and verify damage application.

  3. Adjust bulletSpeed or autoAttackInterval for balancing.

Leave a comment

Log in with itch.io to leave a comment.