コード例 #1
0
ファイル: Weapon.cs プロジェクト: stpkforce/SanAndreasUnity
        private static Weapon AddWeaponComponent(GameObject go, WeaponData data)
        {
            // find type which inherits Weapon class, and whose name matches the one in data

            string typeName = data.weaponType.Replace("_", "");

            var type = s_weaponTypes.Where(t => 0 == string.Compare(t.Name, typeName, true)).FirstOrDefault();

            if (type != null)
            {
                return((Weapon)go.AddComponent(type));
            }
            else
            {
                return(go.AddComponent <Weapon> ());
            }
        }
コード例 #2
0
        public void AddRandomWeapons()
        {
            WeaponSlot[] slots = new WeaponSlot[] { WeaponSlot.Pistol, WeaponSlot.Shotgun, WeaponSlot.Submachine,
                                                    WeaponSlot.Machine, WeaponSlot.Rifle, WeaponSlot.Heavy };

            var groups = WeaponData.LoadedWeaponsData.Where(wd => slots.Contains((WeaponSlot)wd.weaponslot))
                         .GroupBy(wd => wd.weaponslot);

            foreach (var grp in groups)
            {
                int count = grp.Count();
                if (count < 1)
                {
                    continue;
                }

                int        index            = Random.Range(0, count - 1);
                WeaponData chosenWeaponData = grp.ElementAt(index);

                this.SetWeaponAtSlot(chosenWeaponData.modelId1, grp.Key);
            }
        }
コード例 #3
0
        public static Weapon Load(int modelId)
        {
            WeaponDef def = Item.GetDefinition <WeaponDef> (modelId);

            if (null == def)
            {
                return(null);
            }

            WeaponData weaponData = WeaponData.LoadedWeaponsData.FirstOrDefault(wd => wd.modelId1 == def.Id);

            if (null == weaponData)
            {
                return(null);
            }

            var geoms = Geometry.Load(def.ModelName, def.TextureDictionaryName);

            if (null == geoms)
            {
                return(null);
            }

            if (null == s_weaponsContainer)
            {
                s_weaponsContainer = new GameObject("Weapons");
                //	weaponsContainer.SetActive (false);
            }

            GameObject go = new GameObject(def.ModelName);

            go.transform.SetParent(s_weaponsContainer.transform);

            geoms.AttachFrames(go.transform, MaterialFlags.Default);

            Weapon weapon = AddWeaponComponent(go, weaponData);

            weapon.definition = def;
            weapon.data       = weaponData;
            // cache gun aiming offset
            if (weapon.data.gunData != null)
            {
                weapon.gunAimingOffset = weapon.data.gunData.aimingOffset;
            }

            // load hud texture
            try {
                weapon.HudTexture = TextureDictionary.Load(def.TextureDictionaryName).GetDiffuse(def.TextureDictionaryName + "icon").Texture;
            } catch {
                Debug.LogErrorFormat("Failed to load hud icon for weapon: model {0}, txd {1}", def.ModelName, def.TextureDictionaryName);
            }

            // weapon sound
            F.RunExceptionSafe(() => {
                if (weaponSoundIndexes.ContainsKey(modelId))
                {
                    var audioSource         = go.GetOrAddComponent <AudioSource> ();
                    audioSource.playOnAwake = false;
                    Debug.LogFormat("loading weapon sound, bank index {0}", weaponSoundIndexes [modelId]);
                    var audioClip = Audio.AudioManager.CreateAudioClipFromSfx("GENRL", 136, 0,
                                                                              Audio.AudioManager.SfxGENRL137Timings[weaponSoundIndexes [modelId]]);
                    audioSource.clip     = audioClip;
                    weapon.m_audioSource = audioSource;
                }
            });

            weapon.InitWeapon();

            return(weapon);
        }