//切换武器 void SwitchWeapon() { int curWeaponNum = PureTranslateTool.WeaponTypeToInt(curWeapon); int lastWeaponNum = PureTranslateTool.WeaponTypeToInt(lastWeapon); if (Input.GetKeyDown(KeyCode.Alpha1) && isHaveWeapon[0] && curWeapon != WeaponType.Rifle) { OnWeaponChange(weapon[curWeaponNum], weapon[0], weaponPostion[0]); } if (Input.GetKeyDown(KeyCode.Alpha2) && isHaveWeapon[1] && curWeapon != WeaponType.Pistol) { OnWeaponChange(weapon[curWeaponNum], weapon[1], weaponPostion[1]); } if (Input.GetKeyDown(KeyCode.Alpha3) && isHaveWeapon[2] && curWeapon != WeaponType.Knife) { OnWeaponChange(weapon[curWeaponNum], weapon[2], weaponPostion[2]); } if (Input.GetKeyDown(KeyCode.Alpha4) && isHaveWeapon[3] && curWeapon != WeaponType.Grenade) { OnWeaponChange(weapon[curWeaponNum], weapon[3], weaponPostion[3]); } if (Input.GetKeyDown(KeyCode.Alpha5) && isHaveWeapon[4] && curWeapon != WeaponType.Bomb) { OnWeaponChange(weapon[curWeaponNum], weapon[4], weaponPostion[4]); } if (Input.GetKeyDown(KeyCode.Q) && lastWeapon != WeaponType.None) { OnWeaponChange(weapon[curWeaponNum], weapon[lastWeaponNum], weaponPostion[lastWeaponNum]); } }
void OnCollisionEnter(Collision other) { if (other.transform.tag == "LocalPlayer") { PlayerWeapon pw = other.transform.GetComponent <PlayerWeapon>(); int i = PureTranslateTool.WeaponTypeToInt(bw.WeaponClass); if (pw.isHaveWeapon[i]) { return; } pw.GetWeapon(bw); //发送消息删除该掉落 RequestSender.Instance.DeleteDropWeaponRequest(this); Destroy(gameObject); } }
//丢弃武器 void DropWeapon() { if (Input.GetKeyDown(KeyCode.G) && (curWeapon == WeaponType.Rifle || curWeapon == WeaponType.Pistol || curWeapon == WeaponType.Bomb)) { int i = PureTranslateTool.WeaponTypeToInt(curWeapon); GameObject dropWeaponModel = Instantiate(Resources.Load("Prefab/w_Weapon/" + weapon[i].bw.WeaponName) as GameObject); //读取掉落武器 DroppedWeapon temp = dropWeaponModel.GetComponent <DroppedWeapon>(); //获取丢弃武器信息脚本 temp.throwDir = transform.forward; temp.SetBaseWeapon(weapon[i].bw); //设置武器信息] PanelManager.Instance.playerInfoPanel.OnWeaponLose(temp.bw); //更新UI dropWeaponModel.transform.position = transform.localPosition + transform.forward * 2f; RequestSender.Instance.SendSyncDropWeaponRequest(temp, dropWeaponModel.transform.position); //向服务器发送 Destroy(weapon[i].gameObject); //销毁扔掉的武器 weaponPostion[i].gameObject.SetActive(false); //关闭武器点 isHaveWeapon[i] = false; //标识武器没有了 BIndWeaponControl(); //更新武器控制脚本 //查找第一个还有的武器并切换出来 SwitchFirstHave(); } }
//获得武器 public void GetWeapon(BaseWeapon baseWeapon) { int i = PureTranslateTool.WeaponTypeToInt(baseWeapon.WeaponClass); if (isHaveWeapon[i]) { return; //已经有该类型武器不继续获得 } int j = PureTranslateTool.WeaponTypeToInt(curWeapon); GameObject weaponPrefab = Resources.Load("Prefab/v_Weapon/" + baseWeapon.WeaponName) as GameObject; GameObject newWeapon = GameObject.Instantiate(weaponPrefab, weaponPostion[i].position, Quaternion.identity); Weapon w = newWeapon.GetComponent <Weapon>(); w.SetBaseWeapon(baseWeapon); PanelManager.Instance.playerInfoPanel.SetWeaponImage(w.bw); //改变UI newWeapon.transform.SetParent(weaponPostion[i]); //设置位置 newWeapon.transform.localEulerAngles = Vector3.zero; isHaveWeapon[i] = true; // 标记已有 BIndWeaponControl(); //绑定控制脚本 OnWeaponChange(weapon[j], weapon[i], weaponPostion[i]); }