Example #1
0
        protected override void OnPostCreate(bool loaded)
        {
            base.OnPostCreate(loaded);

            if (!EntitySystemWorld.Instance.IsEditor())
            {
                //get spawners for later use
                foreach (MapObjectAttachedObject ao in AttachedObjects)
                {
                    MapObjectAttachedMapObject amo = ao as MapObjectAttachedMapObject;

                    if (amo != null)
                    {
                        Spawner s = amo.MapObject as Spawner;

                        if (s != null)
                        {
                            attachedSpawners.Add(s);
                        }
                    }
                }
            }

            AddTimer();
        }
        /// <summary>Overridden from <see cref="Engine.EntitySystem.Entity.OnDeleteSubscribedToDeletionEvent(Entity)"/></summary>
        protected override void OnDeleteSubscribedToDeletionEvent(Entity entity)
        {
            base.OnDeleteSubscribedToDeletionEvent(entity);

            if (activeWeapon == entity)
            {
                activeWeapon.PreFire      -= activeWeapon_PreFire;
                activeWeapon               = null;
                activeWeaponAttachedObject = null;
            }
        }
Example #3
0
        /// <summary>Overridden from <see cref="Engine.EntitySystem.Entity.OnRelatedEntityDelete(Entity)"/></summary>
        protected override void OnRelatedEntityDelete(Entity entity)
        {
            base.OnRelatedEntityDelete(entity);

            if (activeWeapon == entity)
            {
                activeWeapon.PreFire      -= activeWeapon_PreFire;
                activeWeapon               = null;
                activeWeaponAttachedObject = null;
            }
        }
 void CreateActiveWeaponAttachedObject()
 {
     activeWeaponAttachedObject           = new MapObjectAttachedMapObject();
     activeWeaponAttachedObject.MapObject = activeWeapon;
     activeWeaponAttachedObject.BoneSlot  = GetBoneSlotFromAttachedMeshes(
         activeWeapon.Type.CharacterBoneSlot);
     if (activeWeaponAttachedObject.BoneSlot == null)
     {
         activeWeaponAttachedObject.PositionOffset = Type.NotAnimatedWeaponAttachPosition;
     }
     Attach(activeWeaponAttachedObject);
 }
Example #5
0
        private void lstWeaponList_SelectedIndexChange(ListBox sender)
        {
            if (lstWeaponList.SelectedItem == null)
            {
                return;
            }

            //get default weaponType on the selected slot
            AKunit u = spawner.Spawned as AKunit;

            AKunitType.WeaponItem          wi  = cbxWeaponSlots.SelectedItem as AKunitType.WeaponItem;
            AKunitType.AlternateWeaponItem awi = lstWeaponList.SelectedItem as AKunitType.AlternateWeaponItem;

            string selectedBodyPartName  = GetBodyPartNameFromActiveButton();
            int    selectedBodyPartIndex = GetBodyPartIndex(selectedBodyPartName);
            string weaponAlias           = wi.MapObjectAlias;

            MapObjectAttachedObject    o  = u.GetFirstAttachedObjectByAlias(weaponAlias);
            MapObjectAttachedMapObject mo = o as MapObjectAttachedMapObject;

            u.Detach(mo);

            Weapon w = Entities.Instance.Create(awi.WeaponType, Map.Instance) as Weapon;

            w.PostCreate();
            MapObjectAttachedMapObject amo = new MapObjectAttachedMapObject();

            amo.MapObject      = w;
            amo.PositionOffset = mo.PositionOffset;
            amo.Alias          = mo.Alias;
            amo.Body           = mo.Body;
            amo.BoneSlot       = mo.BoneSlot;
            amo.RotationOffset = mo.RotationOffset;
            amo.ScaleOffset    = mo.ScaleOffset;

            u.Attach(amo);

            Gun   g          = w as Gun;
            float multiplier = 1 / g.Type.NormalMode.BetweenFireTime;
            float rateOfFire = multiplier;

            if (g.Type.NormalMode.BulletExpense != 0)
            {
                rateOfFire = rateOfFire * g.Type.NormalMode.BulletExpense;
            }

            txtWeaponInfo.Text = string.Format(
                "Weapon Name: {0}\r\nWeapon Type: {1}\r\nDamage: {2}\r\nHeat: {3}\r\nRate of Fire: {4}\r\nPrice: {5}",
                w.Type.Name, "Not Implemented", g.Type.NormalMode.BulletType.Damage, g.Type.NormalMode.AKGunHeatGeneration, rateOfFire.ToString("F1"), awi.Price);
        }
Example #6
0
        void GunsTryFire(bool alternative)
        {
            foreach (MapObjectAttachedObject attachedObject in AttachedObjects)
            {
                MapObjectAttachedMapObject attachedMapObject = attachedObject as MapObjectAttachedMapObject;
                if (attachedMapObject == null)
                {
                    continue;
                }

                Gun gun = attachedMapObject.MapObject as Gun;

                if (gun != null)
                {
                    gun.TryFire(alternative);
                }
            }
        }
Example #7
0
        private void UpdateWeaponsList()
        {
            List <Weapon> list = new List <Weapon>();

            foreach (MapObjectAttachedObject attachedObject in ControlledObject.AttachedObjects)
            {
                MapObjectAttachedMapObject attachedMapObject = attachedObject as MapObjectAttachedMapObject;
                if (attachedMapObject != null)
                {
                    Weapon weapon = attachedMapObject.MapObject as Weapon;
                    if (weapon != null)
                    {
                        list.Add(weapon);
                    }
                }
            }
            weapons = list.ToArray();
        }
Example #8
0
        protected override void OnAttach()
        {
            if (variant == null)
            {
                return;
            }

            base.OnAttach();

            window = ControlDeclarationManager.Instance.CreateControl("Gui\\VariantSaveWindow.gui");
            Controls.Add(window);

            btnSaveVariant = (Button)window.Controls["SaveVariant"];
            btnBack        = (Button)window.Controls["Back"];
            txtVariantName = (EditBox)window.Controls["VariantName"];
            txtInfo        = (TextBox)window.Controls["Info"];

            txtInfo.Text = "Total Cost of Variant: " + cost.ToString();

            btnBack.Click += new Button.ClickDelegate(btnBack_Click);

            lstWeaponList = (ListBox)window.Controls["WeaponList"];
            lstWeaponList.SelectedIndexChange += new ListBox.SelectedIndexChangeDelegate(lstWeaponList_SelectedIndexChange);

            txtWeaponGroup             = (EditBox)window.Controls["Group"];
            txtWeaponGroup.TextChange += new DefaultEventDelegate(txtWeaponGroup_TextChange);

            btnSaveVariant.Click += new Button.ClickDelegate(btnSaveVariant_Click);

            foreach (TextBlock childBlock in variant.Children)
            {
                foreach (TextBlock grandChildBlock in childBlock.Children)
                {
                    VariantWeaponGroupItem vwgi = new VariantWeaponGroupItem();
                    vwgi.BodyPartName = childBlock.Name;
                    vwgi.WeaponName   = grandChildBlock.Name;
                    MapObjectAttachedMapObject obj = spawned.GetFirstAttachedObjectByAlias(vwgi.WeaponName) as MapObjectAttachedMapObject;
                    vwgi.AttachedMapObjectTypeName = obj.MapObject.Type.Name;
                    vwgi.OldWeaponGroup            = int.Parse(grandChildBlock.Attributes[1].Value);
                    vwgi.NewWeaponGroup            = vwgi.OldWeaponGroup;
                    lstWeaponList.Items.Add(vwgi);
                }
            }
        }
Example #9
0
        public void UpdateInitialWeapons()
        {
            RTSUnit controlledObj = ControlledObject;

            initialWeapons = new List <Weapon>();

            foreach (MapObjectAttachedObject attachedObject in controlledObj.AttachedObjects)
            {
                MapObjectAttachedMapObject attachedMapObject = attachedObject as MapObjectAttachedMapObject;
                if (attachedMapObject != null)
                {
                    Weapon weapon = attachedMapObject.MapObject as Weapon;
                    if (weapon != null)
                    {
                        initialWeapons.Add(weapon);
                    }
                }
            }
        }
Example #10
0
        /// <summary>Overridden from <see cref="Engine.EntitySystem.Entity.OnPostCreate(Boolean)"/>.</summary>
        protected override void OnPostCreate(bool loaded)
        {
            base.OnPostCreate(loaded);

            if (PhysicsModel != null)
            {
                turretBody = PhysicsModel.GetBody("turret");
                baseBody   = PhysicsModel.GetBody("base");
            }

            if (!EntitySystemWorld.Instance.IsEditor())
            {
                if (turretBody == null)
                {
                    Log.Error("Turret: \"turret\" body not exists.");
                }
                if (baseBody == null)
                {
                    Log.Error("Turret: \"base\" body not exists.");
                }
            }

            //mainGun
            foreach (MapObjectAttachedObject attachedObject in AttachedObjects)
            {
                MapObjectAttachedMapObject attachedMapObject = attachedObject as MapObjectAttachedMapObject;
                if (attachedMapObject == null)
                {
                    continue;
                }

                mainGun = attachedMapObject.MapObject as Gun;
                if (mainGun != null)
                {
                    mainGunAttachedObject = attachedMapObject;
                    mainGunOffsetPosition = attachedMapObject.PositionOffset;
                    break;
                }
            }

            SubscribeToTickEvent();
        }
Example #11
0
        private void FindUnitWeapons()
        {
            foreach (MapObjectAttachedObject attachedObject in ControlledObject.AttachedObjects)
            {
                MapObjectAttachedMapObject attachedMapObject =
                    attachedObject as MapObjectAttachedMapObject;
                if (attachedMapObject != null)
                {
                    Weapon weapon = attachedMapObject.MapObject as Weapon;
                    if (weapon != null)
                    {
                        unitWeapons.Add(weapon);
                    }
                }
            }

            foreach (Weapon weapon in unitWeapons)
            {
                SubscribeToDeletionEvent(weapon);
            }
        }
Example #12
0
        private void FindUnitWeapons()
        {
            foreach (MapObjectAttachedObject attachedObject in ControlledObject.AttachedObjects)
            {
                MapObjectAttachedMapObject attachedMapObject =
                    attachedObject as MapObjectAttachedMapObject;
                if (attachedMapObject != null)
                {
                    Weapon weapon = attachedMapObject.MapObject as Weapon;
                    if (weapon != null)
                    {
                        unitWeapons.Add(weapon);
                    }
                }
            }

            foreach (Weapon weapon in unitWeapons)
            {
                AddRelationship(weapon);
            }
        }
Example #13
0
        void UpdateAttachedObjects()
        {
            bool visible = !opened && openDoorOffsetCoefficient == 0;

            foreach (MapObjectAttachedObject attachedObject in AttachedObjects)
            {
                if (attachedObject.Alias == "visibleWhenClosed")
                {
                    attachedObject.Visible = visible;

                    MapObjectAttachedMapObject attachedMapObject = attachedObject as MapObjectAttachedMapObject;
                    if (attachedMapObject != null)
                    {
                        Occluder occluder = attachedMapObject.MapObject as Occluder;
                        if (occluder != null)
                        {
                            occluder.Enabled = visible;
                        }
                    }
                }
            }
        }
        void Client_ReceiveActiveWeapon(RemoteEntityWorld sender, ReceiveDataReader reader)
        {
            uint networkUIN = reader.ReadVariableUInt32();

            if (!reader.Complete())
            {
                return;
            }

            Weapon value = (Weapon)Entities.Instance.GetByNetworkUIN(networkUIN);

            if (activeWeaponAttachedObject != null)
            {
                Detach(activeWeaponAttachedObject);
                activeWeaponAttachedObject = null;
            }

            activeWeapon = value;

            if (activeWeapon != null)
            {
                CreateActiveWeaponAttachedObject();
            }
        }
Example #15
0
		/// <summary>Overridden from <see cref="Engine.EntitySystem.Entity.OnPostCreate(Boolean)"/>.</summary>
		protected override void OnPostCreate( bool loaded )
		{
			base.OnPostCreate( loaded );

			if( PhysicsModel != null )
			{
				turretBody = PhysicsModel.GetBody( "turret" );
				baseBody = PhysicsModel.GetBody( "base" );
			}

			if( !EntitySystemWorld.Instance.IsEditor() )
			{
				if( turretBody == null )
					Log.Error( "Turret: \"turret\" body not exists." );
				if( baseBody == null )
					Log.Error( "Turret: \"base\" body not exists." );
			}

			//mainGun
			foreach( MapObjectAttachedObject attachedObject in AttachedObjects )
			{
				MapObjectAttachedMapObject attachedMapObject = attachedObject as MapObjectAttachedMapObject;
				if( attachedMapObject == null )
					continue;

				mainGun = attachedMapObject.MapObject as Gun;
				if( mainGun != null )
				{
					mainGunAttachedObject = attachedMapObject;
					mainGunOffsetPosition = attachedMapObject.PositionOffset;
					break;
				}
			}

			SubscribeToTickEvent();
		}
 void CreateActiveWeaponAttachedObject()
 {
     activeWeaponAttachedObject = new MapObjectAttachedMapObject();
     activeWeaponAttachedObject.MapObject = activeWeapon;
     activeWeaponAttachedObject.BoneSlot = GetBoneSlotFromAttachedMeshes(
         activeWeapon.Type.BoneSlot );
     if( activeWeaponAttachedObject.BoneSlot == null )
         activeWeaponAttachedObject.PositionOffset = Type.WeaponAttachPosition;
     Attach( activeWeaponAttachedObject );
 }
        void Client_ReceiveActiveWeapon( RemoteEntityWorld sender, ReceiveDataReader reader )
        {
            uint networkUIN = reader.ReadVariableUInt32();
            if( !reader.Complete() )
                return;

            Weapon value = (Weapon)Entities.Instance.GetByNetworkUIN( networkUIN );

            if( activeWeaponAttachedObject != null )
            {
                Detach( activeWeaponAttachedObject );
                activeWeaponAttachedObject = null;
            }

            activeWeapon = value;

            if( activeWeapon != null )
                CreateActiveWeaponAttachedObject();
        }
        /// <summary>Overridden from <see cref="Engine.EntitySystem.Entity.OnRelatedEntityDelete(Entity)"/></summary>
        protected override void OnRelatedEntityDelete( Entity entity )
        {
            base.OnRelatedEntityDelete( entity );

            if( activeWeapon == entity )
            {
                activeWeapon.PreFire -= activeWeapon_PreFire;
                activeWeapon = null;
                activeWeaponAttachedObject = null;
            }
        }
        public bool SetActiveWeapon( int index )
        {
            if( index < -1 || index >= weapons.Count )
                return false;

            if( index != -1 )
                if( !weapons[ index ].exists )
                    return false;

            if( activeWeapon != null )
            {
                if( index != -1 && Type.Weapons[ index ].WeaponType == activeWeapon.Type )
                    return true;

                activeWeapon.PreFire -= activeWeapon_PreFire;

                if( activeWeaponAttachedObject != null )
                {
                    Detach( activeWeaponAttachedObject );
                    activeWeaponAttachedObject = null;
                }

                Gun activeGun = activeWeapon as Gun;
                if( activeGun != null )
                {
                    int activeIndex = GetWeaponIndex( activeWeapon.Type );
                    weapons[ activeIndex ].normalMagazineCount =
                        activeGun.NormalMode.BulletMagazineCount;
                    weapons[ activeIndex ].alternativeMagazineCount =
                        activeGun.AlternativeMode.BulletMagazineCount;
                }

                activeWeapon.SetShouldDelete();
                activeWeapon = null;

                if( EntitySystemWorld.Instance.IsServer() )
                    Server_SendSetActiveWeaponToClients( EntitySystemWorld.Instance.RemoteEntityWorlds );
            }

            if( index != -1 )
            {
                activeWeapon = (Weapon)Entities.Instance.Create(
                    Type.Weapons[ index ].WeaponType, Parent );
                activeWeapon.Server_EnableSynchronizationPositionsToClients = false;

                Gun activeGun = activeWeapon as Gun;

                if( activeGun != null )
                {
                    activeGun.NormalMode.BulletCount = weapons[ index ].NormalBulletCount;
                    activeGun.NormalMode.BulletMagazineCount = weapons[ index ].NormalMagazineCount;

                    activeGun.AlternativeMode.BulletCount = weapons[ index ].AlternativeBulletCount;
                    activeGun.AlternativeMode.BulletMagazineCount =
                        weapons[ index ].AlternativeMagazineCount;
                }

                activeWeapon.PostCreate();

                CreateActiveWeaponAttachedObject();

                activeWeapon.PreFire += activeWeapon_PreFire;

                if( EntitySystemWorld.Instance.IsServer() )
                    Server_SendSetActiveWeaponToClients( EntitySystemWorld.Instance.RemoteEntityWorlds );
            }

            return true;
        }
        private void lstWeaponList_SelectedIndexChange(ListBox sender)
        {
            if (lstWeaponList.SelectedItem == null)
                return;

            //get default weaponType on the selected slot
            AKunit u = spawner.Spawned as AKunit;
            AKunitType.WeaponItem wi = cbxWeaponSlots.SelectedItem as AKunitType.WeaponItem;
            AKunitType.AlternateWeaponItem awi = lstWeaponList.SelectedItem as AKunitType.AlternateWeaponItem;

            string selectedBodyPartName = GetBodyPartNameFromActiveButton();
            int selectedBodyPartIndex = GetBodyPartIndex(selectedBodyPartName);
            string weaponAlias = wi.MapObjectAlias;

            MapObjectAttachedObject o = u.GetFirstAttachedObjectByAlias(weaponAlias);
            MapObjectAttachedMapObject mo = o as MapObjectAttachedMapObject;
            u.Detach(mo);

            Weapon w = Entities.Instance.Create(awi.WeaponType, Map.Instance) as Weapon;
            w.PostCreate();
            MapObjectAttachedMapObject amo = new MapObjectAttachedMapObject();
            amo.MapObject = w;
            amo.PositionOffset = mo.PositionOffset;
            amo.Alias = mo.Alias;
            amo.Body = mo.Body;
            amo.BoneSlot = mo.BoneSlot;
            amo.RotationOffset = mo.RotationOffset;
            amo.ScaleOffset = mo.ScaleOffset;

            u.Attach(amo);

            Gun g = w as Gun;
            float multiplier = 1 / g.Type.NormalMode.BetweenFireTime;
            float rateOfFire = multiplier;

            if (g.Type.NormalMode.BulletExpense != 0)
                rateOfFire = rateOfFire * g.Type.NormalMode.BulletExpense;

            txtWeaponInfo.Text = string.Format(
                "Weapon Name: {0}\r\nWeapon Type: {1}\r\nDamage: {2}\r\nHeat: {3}\r\nRate of Fire: {4}\r\nPrice: {5}",
                w.Type.Name, "Not Implemented", g.Type.NormalMode.BulletType.Damage, g.Type.NormalMode.AKGunHeatGeneration, rateOfFire.ToString("F1"), awi.Price);
        }
Example #21
0
        public virtual void UpdateCurrentItem()
        {
            if (EntitySystemWorld.Instance.WorldSimulationType == WorldSimulationTypes.Editor)
            {
                return;
            }

            //handle my current object if any
            if (activeHeldItem != null)
            {
                if (activeHeldItemAttachedObject != null)
                {
                    Detach(activeHeldItemAttachedObject);
                    activeHeldItemAttachedObject = null;
                }

                activeHeldItem.SetForDeletion(false);
                activeHeldItem = null;

                /* TODO: ONLINE ITEM SUPPORT - TO DO WHEN PORTING
                 * if (EntitySystemWorld.Instance.IsServer())
                 *  Server_SendSetactiveHeldItemToClients(EntitySystemWorld.Instance.RemoteEntityWorlds);*/
            }

            if (GetCurItem != null)
            {
                CreateActiveItem(GetCurItem.ItemType.Name);

                //activeHeldItem.Server_EnableSynchronizationPositionsToClients = false;

                //transfer the info from the itm to the newly created gun
                ConsumableItem consItm = activeHeldItem as ConsumableItem;
                if (consItm != null)
                {
                    consItm.Juice      = GetCurItem.Juice;
                    consItm.ActionMode = GetCurItem.ActionMode;

                    VBWeaponItem wpnItm = consItm as VBWeaponItem;
                    if (wpnItm != null)
                    {
                        wpnItm.AmmoTypeLoaded = GetCurItem.AmmoType;
                    }
                }

                //TODO: PREVENT THE OBJECT ATTACHMENT BUG
                //CreateactiveHeldItemAttachedObject();

                /* ONLINE ITEM SUPPORT - TO DO WHEN PORTING
                 * if (EntitySystemWorld.Instance.IsServer())
                 *  Server_SendSetactiveHeldItemToClients(EntitySystemWorld.Instance.RemoteEntityWorlds);*/
            }
            else
            {
                if (Type.MeleeAttacks != null)
                {
                    if (primaryActive)
                    {
                        CreateActiveItem(Type.MeleeAttacks[0]);
                    }
                    else
                    {
                        CreateActiveItem(Type.MeleeAttacks[1]);
                    }
                }
            }
        }
Example #22
0
        public bool SetActiveWeapon(int index)
        {
            if (index < -1 || index >= weapons.Count)
                return false;

            if (index != -1)
                if (!weapons[index].exists)
                    return false;

            if (activeWeapon != null)
            {
                activeWeapon.PreFire -= activeWeapon_PreFire;

                if (index != -1 && Type.Weapons[index].WeaponType == activeWeapon.Type)
                    return true;

                foreach (MapObjectAttachedObject attachedObject in AttachedObjects)
                {
                    MapObjectAttachedMapObject attachedMapObject = attachedObject as MapObjectAttachedMapObject;
                    if (attachedMapObject == null)
                        continue;

                    Weapon weapon = attachedMapObject.MapObject as Weapon;
                    if (weapon == activeWeapon)
                    {
                        Gun activeGun = activeWeapon as Gun;
                        if (activeGun != null)
                        {
                            int activeIndex = GetWeaponIndex(activeWeapon.Type);
                            weapons[activeIndex].normalMagazineCount =
                                activeGun.NormalMode.BulletMagazineCount;
                            weapons[activeIndex].alternativeMagazineCount =
                                activeGun.AlternativeMode.BulletMagazineCount;
                        }

                        Detach(attachedMapObject);
                        weapon.SetShouldDelete();
                        activeWeapon = null;
                        activeWeaponAttachedObject = null;
                        break;
                    }
                }
            }

            if (index != -1)
            {
                activeWeapon = (Weapon)Entities.Instance.Create(
                    Type.Weapons[index].WeaponType, Parent);

                Gun activeGun = activeWeapon as Gun;

                if (activeGun != null)
                {
                    activeGun.NormalMode.BulletCount = weapons[index].NormalBulletCount;
                    activeGun.NormalMode.BulletMagazineCount = weapons[index].NormalMagazineCount;

                    activeGun.AlternativeMode.BulletCount = weapons[index].AlternativeBulletCount;
                    activeGun.AlternativeMode.BulletMagazineCount =
                        weapons[index].AlternativeMagazineCount;
                }

                activeWeapon.PostCreate();

                CreateActiveWeaponAttachedObject();

                activeWeapon.PreFire += activeWeapon_PreFire;
            }

            return true;
        }
Example #23
0
        public bool RemoveAllWeapon()
        {
            foreach (MapObjectAttachedObject attachedObject in AttachedObjects)
            {
                MapObjectAttachedMapObject attachedMapObject = attachedObject as MapObjectAttachedMapObject;
                if (attachedMapObject == null)
                    continue;

                Weapon weapon = attachedMapObject.MapObject as Weapon;
                if (weapon == activeWeapon)
                {
                    Gun activeGun = activeWeapon as Gun;
                    Detach(attachedMapObject);
                    //weapon.SetShouldDelete();
                    activeWeapon = null;
                    activeWeaponAttachedObject = null;
                    break;
                }
            }

            return true;
        }
        /// <summary>Overridden from <see cref="Engine.EntitySystem.Entity.OnDeleteSubscribedToDeletionEvent(Entity)"/></summary>
        protected override void OnDeleteSubscribedToDeletionEvent(Entity entity)
        {
            base.OnDeleteSubscribedToDeletionEvent(entity);

            if (activeWeapon == entity)
            {
                activeWeapon.PreFire -= activeWeapon_PreFire;
                activeWeapon = null;
                activeWeaponAttachedObject = null;
            }
        }
Example #25
0
        /// <summary>Overridden from <see cref="Engine.EntitySystem.Entity.OnPostCreate(Boolean)"/>.</summary>
        protected override void OnPostCreate( bool loaded )
        {
            base.OnPostCreate( loaded );

            AddTimer();

            if( EntitySystemWorld.Instance.WorldSimulationType != WorldSimulationType.Editor )
            {
                if( PhysicsModel == null )
                {
                    Log.Error( "Tank: Physics model not exists." );
                    return;
                }

                chassisBody = PhysicsModel.GetBody( "chassis" );
                if( chassisBody == null )
                {
                    Log.Error( "Tank: \"chassis\" body not exists." );
                    return;
                }
                towerBody = PhysicsModel.GetBody( "tower" );

                //chassisBody.Collision += chassisBody_Collision;

                foreach( MapObjectAttachedObject attachedObject in AttachedObjects )
                {
                    if( attachedObject.Alias == "leftTrack" )
                        leftTrack.trackHelpers.Add( (MapObjectAttachedHelper)attachedObject );
                    if( attachedObject.Alias == "rightTrack" )
                        rightTrack.trackHelpers.Add( (MapObjectAttachedHelper)attachedObject );
                }

                if( leftTrack.trackHelpers.Count != 0 )
                    tracksPositionYOffset = Math.Abs( leftTrack.trackHelpers[ 0 ].PositionOffset.Y );
            }

            //mainGun
            foreach( MapObjectAttachedObject attachedObject in AttachedObjects )
            {
                MapObjectAttachedMapObject attachedMapObject = attachedObject as MapObjectAttachedMapObject;
                if( attachedMapObject == null )
                    continue;

                mainGun = attachedMapObject.MapObject as Gun;
                if( mainGun != null )
                {
                    mainGunAttachedObject = attachedMapObject;
                    mainGunOffsetPosition = attachedMapObject.PositionOffset;
                    break;
                }
            }

            //towerBodyLocalPosition
            if( towerBody != null )
                towerBodyLocalPosition = PhysicsModel.ModelDeclaration.GetBody( towerBody.Name ).Position;

            //initialize currentGear
            currentGear = Type.Gears.Find( delegate( TankType.Gear gear )
            {
                return gear.Number == 0;
            } );

            //That the body did not fall after loading a map.
            //After loading a map, the physics simulate 5 seconds, that bodies have fallen asleep.
            if( loaded && EntitySystemWorld.Instance.SerializationMode == SerializationModes.Map )
            {
                if( chassisBody != null )
                    chassisBody.Static = true;
                if( towerBody != null )
                    towerBody.Static = true;
            }

            //replace track materials
            if( EntitySystemWorld.Instance.WorldSimulationType != WorldSimulationType.Editor )
                InitTracksAnimation();
        }
Example #26
0
File: Tank.cs Project: whztt07/SDK
        /// <summary>Overridden from <see cref="Engine.EntitySystem.Entity.OnPostCreate(Boolean)"/>.</summary>
        protected override void OnPostCreate( bool loaded )
        {
            base.OnPostCreate( loaded );

            SubscribeToTickEvent();

            if( EngineApp.Instance.ApplicationType != EngineApp.ApplicationTypes.ResourceEditor )
            {
                if( PhysicsModel == null )
                {
                    Log.Error( "Tank: Physics model not exists." );
                    return;
                }

                chassisBody = PhysicsModel.GetBody( "chassis" );
                if( chassisBody == null )
                {
                    Log.Error( "Tank: \"chassis\" body not exists." );
                    return;
                }
                towerBody = PhysicsModel.GetBody( "tower" );

                //chassisBody.Collision += chassisBody_Collision;

                foreach( MapObjectAttachedObject attachedObject in AttachedObjects )
                {
                    if( attachedObject.Alias == "leftTrack" )
                        leftTrack.trackHelpers.Add( (MapObjectAttachedHelper)attachedObject );
                    if( attachedObject.Alias == "rightTrack" )
                        rightTrack.trackHelpers.Add( (MapObjectAttachedHelper)attachedObject );
                }

                if( leftTrack.trackHelpers.Count != 0 )
                    tracksPositionYOffset = Math.Abs( leftTrack.trackHelpers[ 0 ].PositionOffset.Y );
            }

            //mainGun
            foreach( MapObjectAttachedObject attachedObject in AttachedObjects )
            {
                MapObjectAttachedMapObject attachedMapObject = attachedObject as MapObjectAttachedMapObject;
                if( attachedMapObject == null )
                    continue;

                mainGun = attachedMapObject.MapObject as Gun;
                if( mainGun != null )
                {
                    mainGunAttachedObject = attachedMapObject;
                    mainGunOffsetPosition = attachedMapObject.PositionOffset;
                    break;
                }
            }

            //towerBodyLocalPosition
            if( towerBody != null )
                towerBodyLocalPosition = PhysicsModel.ModelDeclaration.GetBody( towerBody.Name ).Position;

            //initialize currentGear
            currentGear = Type.Gears.Find( delegate( TankType.Gear gear )
            {
                return gear.Number == 0;
            } );

            if( EngineApp.Instance.ApplicationType != EngineApp.ApplicationTypes.ResourceEditor )
            {
                InitTracksTextureAnimation();
                InitWheelsSkeletonAnimation();
            }

            //disable contacts between chassisBody and towerBody
            if( chassisBody != null && towerBody != null )
            {
                foreach( Shape shape1 in chassisBody.Shapes )
                {
                    foreach( Shape shape2 in towerBody.Shapes )
                    {
                        PhysicsWorld.Instance.SetShapePairFlags( shape1, shape2,
                            ShapePairFlags.DisableContacts );
                    }
                }
            }
        }
        public bool SetActiveWeapon(int index)
        {
            if (index < -1 || index >= weapons.Count)
            {
                return(false);
            }

            if (index != -1)
            {
                if (!weapons[index].exists)
                {
                    return(false);
                }
            }

            if (activeWeapon != null)
            {
                if (index != -1 && Type.Weapons[index].WeaponType == activeWeapon.Type)
                {
                    return(true);
                }

                activeWeapon.PreFire -= activeWeapon_PreFire;

                if (activeWeaponAttachedObject != null)
                {
                    Detach(activeWeaponAttachedObject);
                    activeWeaponAttachedObject = null;
                }

                Gun activeGun = activeWeapon as Gun;
                if (activeGun != null)
                {
                    int activeIndex = GetWeaponIndex(activeWeapon.Type);
                    weapons[activeIndex].normalMagazineCount =
                        activeGun.NormalMode.BulletMagazineCount;
                    weapons[activeIndex].alternativeMagazineCount =
                        activeGun.AlternativeMode.BulletMagazineCount;
                }

                activeWeapon.Die();
                activeWeapon = null;

                if (EntitySystemWorld.Instance.IsServer())
                {
                    Server_SendSetActiveWeaponToClients(EntitySystemWorld.Instance.RemoteEntityWorlds);
                }
            }

            if (index != -1)
            {
                activeWeapon = (Weapon)Entities.Instance.Create(
                    Type.Weapons[index].WeaponType, Parent);
                activeWeapon.Server_EnableSynchronizationPositionsToClients = false;

                Gun activeGun = activeWeapon as Gun;

                if (activeGun != null)
                {
                    activeGun.NormalMode.BulletCount         = weapons[index].NormalBulletCount;
                    activeGun.NormalMode.BulletMagazineCount = weapons[index].NormalMagazineCount;

                    activeGun.AlternativeMode.BulletCount         = weapons[index].AlternativeBulletCount;
                    activeGun.AlternativeMode.BulletMagazineCount =
                        weapons[index].AlternativeMagazineCount;
                }

                activeWeapon.PostCreate();

                CreateActiveWeaponAttachedObject();

                activeWeapon.PreFire += activeWeapon_PreFire;

                if (EntitySystemWorld.Instance.IsServer())
                {
                    Server_SendSetActiveWeaponToClients(EntitySystemWorld.Instance.RemoteEntityWorlds);
                }
            }

            return(true);
        }