private SortOfFire GetWeaponType(GunEnum Sort) { if (Sort == GunEnum.boom) { return(boomObj); } else if (Sort == GunEnum.roket) { return(roketObj); } else if (Sort == GunEnum.set) { return(setObj); } else if (Sort == GunEnum.normal) { return(normal); } else if (Sort == GunEnum.shutgun) { return(shutgun); } else { return(null); } }
public GameObject GetPooledObject(GunEnum Sort) //获取对象池中可以使用的子弹。 { nowbulletType = GetWeaponType(Sort); for (int i = 0; i < nowbulletType.bulletObj.Count; ++i) //把对象池遍历一遍 { //这里简单优化了一下,每一次遍历都是从上一次被使用的子弹的下一个,而不是每次遍历从0开始。 //例如上一次获取了第4个子弹,currentIndex就为5,这里从索引5开始遍历,这是一种贪心算法。 int temI = (nowbulletType.currentIndex + i) % nowbulletType.bulletObj.Count; if (!nowbulletType.bulletObj[temI].activeInHierarchy) //判断该子弹是否在场景中激活。 { nowbulletType.currentIndex = (temI + 1) % nowbulletType.bulletObj.Count; return(nowbulletType.bulletObj[temI]); //找到没有被激活的子弹并返回 } } //如果遍历完一遍子弹库发现没有可以用的,执行下面 if (!lockPoolSize) //如果没有锁定对象池大小,创建子弹并添加到对象池中。 { GameObject obj = Instantiate(nowbulletType.bullet); obj.transform.parent = this.transform; //生成到buttlepool下 nowbulletType.bulletObj.Add(obj); return(obj); } //如果遍历完没有而且锁定了对象池大小,返回空。 return(null); }
void Start() { for (int i = 0; i < bullet_number.Length; i++) { bullet_number[i] = bulletPool_Ins.InitBulletNum(gunType); gunType++; } //自身刚体组件 selfRigidbody = this.GetComponent <Rigidbody>(); //自身动画 selfAnimator = this.GetComponent <Animator>(); normalattack_Ins = PlayerAttack.normal_attack_Instance; gunType = bulletPool_Ins.nowType; //血量初始化 currentHp = totalHP; //奔跑特效 selTrail = gameObject.GetComponent <TrailRenderer>(); selTrail.enabled = false; //枪 biggun = Biggun.Biggun_ins; //血条 hpBar = selfHpbar.transform.Find("Hpbar").GetComponent <Image>(); selEventSystem.EventIns.EventplayerWin.AddListener(StopWalk); selEventSystem.EventIns.EventPickBullet.AddListener(PickupBullet); }
public static BulletsPool bulletsPoolInstance; //子弹池实例 void Awake() { //初始化武器类型 nowType = GunEnum.normal; boomObj.bullet = boom; setObj.bullet = sett; roketObj.bullet = roket; bulletsPoolInstance = this; //把本对象作为实例 //初始化子弹 InitBullet(boomObj); InitBullet(setObj); InitBullet(roketObj); }
//捡到弹药包后 private void PickupBullet() { if (gunType != GunEnum.normal) { //先清空当前武器的子弹数量 bullet_number[gunType.GetHashCode()] = 0; } int weaponType = Random.Range(1, 5); gunType = bulletPool_Ins.RandomChange(weaponType); bullet_number[weaponType] = 10; //显示UI gunnameText.text = bulletPool_Ins.InitGunName(gunType) + " " + bullet_number[gunType.GetHashCode()]; }
public float GetWeaponColdTime(GunEnum Sort) { if (Sort == GunEnum.boom) { return(boomObj.shotting_time); } else if (Sort == GunEnum.roket) { return(roketObj.shotting_time); } else if (Sort == GunEnum.set) { return(setObj.shotting_time); } return(0.2f); }
//减少子弹数量 void DecreasBullet(GunEnum gunType) { bullet_number[gunType.GetHashCode()] -= 1; Debug.Log(bullet_number[gunType.GetHashCode()]); //如果当前武器子弹用完了 if (bullet_number[gunType.GetHashCode()] == 0) { Debug.Log("子弹没了,进来"); this.gunType = GunEnum.normal; gunnameText.text = bulletPool_Ins.InitGunName(this.gunType); } else { gunnameText.text = bulletPool_Ins.InitGunName(gunType) + " " + bullet_number[gunType.GetHashCode()]; } }
private void Awake() { playerInstance = this; bulletPool_Ins = BulletsPool.bulletsPoolInstance; selfHpbar = Instantiate(Resources.Load <GameObject>("Prefab/HpCanvas"), this.transform.position + hpHeight * Vector3.up, Quaternion.identity) as GameObject; //初始化子弹 gunType = GunEnum.normal; gunnameText = selfHpbar.transform.Find("Text").GetComponent <Text>(); shotTransform = gameObject.transform.Find("FirePoint").transform; //初始化音效 attackAudio = Resources.Load <AudioClip>("Sound/gunshot"); roketAudio = Resources.Load <AudioClip>("Sound/roket"); deathAudio = Resources.Load <AudioClip>("Sound/game_over"); hitAudio = Resources.Load <AudioClip>("Sound/Hit"); playerMesh = transform.Find("Mesh").GetComponent <SkinnedMeshRenderer>(); playerMesh.material = Resources.Load <Material>("Materials/Player/Player" + PlayerPrefs.GetInt("PlayerMatID", 0).ToString()); //初始化特效 //skillEffect = transform.Find("LinRenderer").GetComponent<LineRenderer>(); }
public string InitGunName(GunEnum Type) { return(GetWeaponType(Type).name); }
public int InitBulletNum(GunEnum Type) { return(GetWeaponType(Type).max_bullet); }
public GunEnum RandomChange(int type) { nowType = GunEnum.normal + type; return(nowType); }