Skip to content

IWeapon

Nick Stapleton edited this page Jun 10, 2019 · 5 revisions

IWeapon - Weapon Interface

Each piece of geometry has a weapon, think of player in Call of Duty has a gun. Each weapon has an associate IProjectile that is used to spawn new projectiles. If you want to make special ammunition, new weapons, etc., see DotProjectile.cs for an example.

Abstract

Inheritance

  1. IWeapon -> DotWeapon, LineWeapon, ...

Code

namespace Weapon.Command
{
    public interface IWeapon
    {
        void init(IGeo GeoOwner, AudioClip Sound,
                  float Damage = 1, float Pierce = 0, float Rate = 0.125f, float lifeTime = 2.5f);

        // Sets the damage and piercing
        void SetDamage(float Damage, float Piercing);

        // Fires a projectile.
        void Fire(Vector3 movementDir, Vector3 pos, float push, float SpawnOffset);

        // Returns the IGeo owner of the weapon.
        IGeo GetOwner();

        // Sets the owner of the weapon.
        void SetOwner(IGeo geo);
    }
}

Usage

From GeoObject.Start()

If the prefab in the level editor has an Initial Weapon initialized. ie, this is used for the player.

if (InitWeapon != null)
{
    ActiveGun = Instantiate(InitWeapon);
    IWeapon ptr = ActiveGun.GetComponents<IWeapon>()[0];
    ptr.SetOwner(this);
    weapon.Add(ptr);
}

From DotObject.Start()

If the Dot doesn't have an active gun the copy the prefab and initialize it.

if (ActiveGun == null)
{
    // Add the weapon.
    ActiveGun = Instantiate(Resources.Load("Player Items/Dot Gun")) as GameObject;
    DotWeapon dotGun = ActiveGun.GetComponents<IWeapon>()[0] as DotWeapon;
    dotGun.init(this, Resources.Load<AudioClip>(Dot_Shoot_Soundfile), DotDamage, DotPiercing, FireRate);
    weapon.Add(dotGun);
}