Exemplo n.º 1
0
        /// <summary>
        /// Creates a new projectile.
        /// </summary>
        /// <param name="type">The type (Guns, Missile, LTS, etc) of the weapon</param>
        /// <param name="target">The projector at which this projectile is being fired</param>
        /// <param name="reachRange">The range of how wide this projectile will consider itself a hit</param>
        /// <returns>The projectile as type Explosive. It will also have been given an ID.</returns>
        private WeaponBase createNewWeapon(WeaponTypes type, Projector target,
                                           Range reachRange)
        {
            //if isLoading is true, ammo count could be 0 but we still have to load weapon
            //since ammo is decreased when weapon is created.
            WeaponBase w = null;

            if (ammunitionFor(type) != 0 || Options.isLoading)
            {
                switch (type)
                {
                case WeaponTypes.guns:
                    validWeapons.Add(w = new Guns(this));
                    break;

                case WeaponTypes.missile:
                    validWeapons.Add(w = new Missile(this));
                    break;

                case WeaponTypes.laserCannonSystem:
                    if ((lCSTarget == null && LTSCount < 1) || Options.isLoading)
                    {
                        //ltsCount will not increment if loaded in from file
                        LTSCount++; //so increment it if we're loading it from a file as well
                        validWeapons.Add(w = new LaserCannonSystem(this));
                    }
                    break;

                case WeaponTypes.cruiseMissile:
                    validWeapons.Add(w = new CruiseMissile(this));
                    break;

                case WeaponTypes.missileInterceptor:
                    Projector iLock = getInterceptorLock();
                    if (iLock == null)
                    {
                        break;
                    }
                    target             = iLock;
                    validWeapons.Add(w = new MissileInterceptor(this));
                    break;

                case WeaponTypes.samMissile:
                    validWeapons.Add(w = new SAMMissile(this));
                    break;

                case WeaponTypes.explosiveMissile:
                    validWeapons.Add(w = new ExplosiveMissile(this));
                    break;

                case WeaponTypes.tankMissile:
                    validWeapons.Add(w = new TankMissile(this));
                    break;

                case WeaponTypes.battleShipGuns:
                    validWeapons.Add(w = new BattleShipGuns(this));
                    break;
                }
            } //if ammo remaining
            if (w != null)
            {
                // Only the client that fired the actual projectile will send info about its fired projectiles.
                // Other clients will just duplicate this projectile, and wait for instructions from this client.
                String id = creator.id + "_" + next
                            + "_" + (nextPos++);
                if (nextID != null)  //received weapon duplicate command from server
                {
                    w.setID(nextID); // command includes assigned ID.

                    /*When firing with guns, only one creation ID will be sent to the server,
                     * signaling the ID of the first shot. We parse this ID to get the IDs of the remaining shots since nextID will be null after this.
                     * See use() to see how gunshots are fired together.
                     * */
                    if (weaponIndex == WeaponTypes.guns)
                    {
                        String[] idParts = nextID.Split(new char[] { '_' });
                        next    = Convert.ToInt32(idParts[1]);
                        nextPos = Convert.ToInt32(idParts[2]) + 1;
                    }                     //if guns
                    nextID = null;
                }
                else                               //If nextID == null IE: not server weapon
                {
                    if (!Options.isLoading)        //weapon's load method will add to object table
                    {
                        w.setID(id, !(w is Guns)); //don't add bullets to object table
                    }
                }

                w.readyToDispose += freeWeapon; //Notify this class when the weapon has completed its execution
                w.eventHit       += eventHit;
                if (target != null)
                {
                    w.lockOn(target);
                }
                w.initRange(reachRange);
                System.Diagnostics.Trace.WriteLineIf(w != null, "WCreated weapon " + w.id);
                if (w.id != null)
                {
                    System.Diagnostics.Trace.WriteLine("WCreated id " + w.id);
                }
                return(w);
            }
            return(null);
        }
Exemplo n.º 2
0
 private String initializeWeapon(WeaponBase e)
 {
     e.use();
     return((creator.isSender()) ? e.id.Replace(creator.id, null) : null);
 }