/*登录 */ public void LogIn() { if (isInLogIn || isInRegister) { return; //若已点击了登陆按钮或注册按钮则不响应 } StopCoroutine("ResetPrompt"); promptText.text = ""; userNameInput.interactable = false; passwordInput.interactable = false; isInLogIn = true; //test // LogInSuccess(); ByteBuilder builder = new ByteBuilder(); builder.Add(System.BitConverter.GetBytes(Network.LOGIN)); builder.Add(System.BitConverter.GetBytes(userNameInput.text.Length)); builder.Add(System.BitConverter.GetBytes(passwordInput.text.Length)); builder.Add(System.Text.Encoding.ASCII.GetBytes(userNameInput.text)); builder.Add(System.Text.Encoding.ASCII.GetBytes(passwordInput.text)); Global.network.Send(builder.GetByes()); // Debug.Log("Set"); LogInOutCor = StartCoroutine(LogInTimeout()); }
/*结束游戏 */ public void OverGame() { Debug.Log("OVer"); isDead = true; promptText.gameObject.SetActive(true); promptText.text = "同步中"; int score = int.Parse(scoreText.text); Global.killSum += score; if (aliveTime > Global.maxAliveTime) { Global.maxAliveTime = aliveTime; } ByteBuilder builder = new ByteBuilder(); builder.Add(System.BitConverter.GetBytes(Network.MATCH_INFO)); builder.Add(System.BitConverter.GetBytes(Global.killSum)); builder.Add(System.BitConverter.GetBytes((double)aliveTime)); //double builder.Add(System.BitConverter.GetBytes(Global.userName.Length)); builder.Add(System.Text.Encoding.ASCII.GetBytes(Global.userName)); Global.network.Send(builder.GetByes()); StartCoroutine(TimeOut()); }
/*注册 */ public void Register() { if (isInLogIn || isInRegister) { return; //若已点击了登陆按钮或注册按钮则不响应 } userNameInput.interactable = false; passwordInput.interactable = false; StopCoroutine("ResetPrompt"); promptText.text = ""; isInRegister = true; ByteBuilder builder = new ByteBuilder(); builder.Add(System.BitConverter.GetBytes(Network.REGISTER)); builder.Add(System.BitConverter.GetBytes(userNameInput.text.Length)); builder.Add(System.BitConverter.GetBytes(passwordInput.text.Length)); builder.Add(System.Text.Encoding.ASCII.GetBytes(userNameInput.text)); builder.Add(System.Text.Encoding.ASCII.GetBytes(passwordInput.text)); Global.network.Send(builder.GetByes()); RegisterOutCor = StartCoroutine(RegisterTimeout()); }
// Update is called once per frame void Update() { //创建敌人 if (detectors.Count != 0) { foreach (GameObject obj in detectors) { if (!obj.activeSelf) { deletes.AddLast(obj); } } foreach (GameObject obj in deletes) { detectors.Remove(obj); GameObject.Destroy(obj); } foreach (GameObject obj in detectors) { GameObject rival = Instantiate(rivalPrefab, new Vector3(obj.transform.position.x, rivalPrefab.transform.position.y, obj.transform.position.z), Quaternion.identity); rival.transform.Rotate(0f, Random.Range(-180f, 180f), 0f, Space.Self); rivalList.AddLast(rival); RivalController controller = rival.GetComponent <RivalController>(); controller.DelegateDie(new DieCallback(OnRivalDie)); controller.id = idNext; ++idNext; GameObject.Destroy(obj); ++completeNum; } detectors.Clear(); deletes.Clear(); } //询问服务器是否需新创建敌人 if (isCreateOver && createRivalNum <= 0 && isRecv) { StartCoroutine("ResetRecv"); byteBuilder.Add(System.BitConverter.GetBytes(Network.RIVAL_NUM)); byteBuilder.Add(System.BitConverter.GetBytes(rivalList.Count)); Global.network.Send(byteBuilder.GetByes()); byteBuilder.Clear(); isRecv = false; } else if (!isCreateOver && requestNum == createRivalNum) //检查是否创建成功 { createRivalNum = requestNum - completeNum; //开始将未创建成功的创建 for (int i = 0; i < createRivalNum; ++i) { CreateDetector(); } requestNum = 0; completeNum = 0; } if (!isCreateOver && createRivalNum <= 0) //检查是否全部创建完成 { completeNum = 0; requestNum = 0; createRivalNum = 0; isCreateOver = true; } if (isCreateOver && createRivalNum > 0) //检查是否有待创建的敌人 { //开始将未创建成功的创建 for (int i = 0; i < createRivalNum; ++i) { CreateDetector(); } isCreateOver = false; requestNum = 0; completeNum = 0; } }
// Update is called once per frame void Update() { //死亡则慢慢下沉消失 if (humanController.health <= 0 && !isDead) { if (dieCallbacks != null) { dieCallbacks(gameObject); //死亡回调 } isDead = true; Destroy(GetComponent <CharacterController>()); Destroy(GetComponent <Rigidbody>()); Destroy(GetComponent <SphereCollider>()); StopAllCoroutines(); StartCoroutine(Die()); return; } if (humanController.health != humanController.MaxHealth && humanController.health > 0 && !isTrace && !isShoot) { isTrace = true; isPatrol = false; isShoot = false; } if (isInRotate) { return; } // Debug.Log("Next"); if (!isDead) { //移动 if (isPatrol) { // Debug.Log("WALK"); // nav.enabled = false; // StopCoroutine("Trace"); humanController.Idle(); if (Vector3.Angle(patrolForward, transform.forward) > 5.0f) //判断是否需要转向 { // Debug.Log("ChangeTo:"+patrolForward + transform.forward + (patrolForward == transform.forward) +" "+); // StopCoroutine("RotateByFrame"); RotateTowards(patrolForward); return; } humanController.Walk(walkSpeed, 0f); } else if (isTrace) //追踪 { if (playerLastPosition == Vector3.zero || (Global.player.transform.position - playerLastPosition).magnitude > 3f) { /*向服务器发送路径计算请求 */ byteBuilder.Add(System.BitConverter.GetBytes(Network.RIVAL_REQUEST)); byteBuilder.Add(System.BitConverter.GetBytes(id)); byteBuilder.Add(System.BitConverter.GetBytes(Network.SEARCH_ROAD)); byteBuilder.Add(System.BitConverter.GetBytes((int)(transform.position.x + 0.5))); byteBuilder.Add(System.BitConverter.GetBytes((int)(transform.position.z + 0.5))); byteBuilder.Add(System.BitConverter.GetBytes((int)(Global.player.transform.position.x + 0.5))); byteBuilder.Add(System.BitConverter.GetBytes((int)(Global.player.transform.position.z + 0.5))); Global.network.Send(byteBuilder.GetByes()); byteBuilder.Clear(); // nav.enabled = true; // nav.SetDestination(Global.player.transform.position); // humanController.Run(0, 0); playerLastPosition = Global.player.transform.position; } } else if (vertical != 0 || horizontal != 0) { humanController.Run(vertical * runSpeed, horizontal * runSpeed); // nav.enabled = false; StopCoroutine("Trace"); humanController.Idle(); } else { // humanController.Idle(); // nav.enabled = false; // StopCoroutine("Trace"); StopAllCoroutines(); humanController.Idle(); } //设置视线 Ray tar = new Ray(transform.position + shootRectify, transform.forward); if (Physics.Raycast(tar, out sightHit)) { lookAtPoint = sightHit.point; } //检测是否玩家在视线内 Vector3 targetV = Global.player.transform.position - transform.position; // Debug.Log(Vector3.Angle(transform.forward, targetV)); if (Vector3.Angle(transform.forward, targetV) <= sightAngle) { Ray detect = new Ray(transform.position + shootRectify, targetV); RaycastHit h; if (Physics.Raycast(detect, out h)) { // Debug.Log("Hit:"+h.collider.gameObject.layer+" "+h.collider.name); // Debug.DrawRay(transform.position, targetV, Color.black); if (h.collider.gameObject.layer == 11) { // Debug.Log("Human"); float dis = (transform.position - Global.player.transform.position).magnitude; if (!isTrace && !isShoot) //若在巡逻状态 { Debug.Log("In Sight"); //在视线内 byteBuilder.Add(System.BitConverter.GetBytes(Network.RIVAL_REQUEST)); byteBuilder.Add(System.BitConverter.GetBytes(id)); byteBuilder.Add(System.BitConverter.GetBytes(Network.PLAYER_IN_SIGHT)); Global.network.Send(byteBuilder.GetByes()); byteBuilder.Clear(); } else if (!isShoot && dis < weaponController.maxAttackDis) //若不在射击状态 { byteBuilder.Add(System.BitConverter.GetBytes(Network.RIVAL_REQUEST)); byteBuilder.Add(System.BitConverter.GetBytes(id)); byteBuilder.Add(System.BitConverter.GetBytes(Network.PLAYER_IN_SHOOT)); Global.network.Send(byteBuilder.GetByes()); byteBuilder.Clear(); } else if (isShoot && dis > weaponController.maxAttackDis) //若在射击状态但不在攻击距离内 { byteBuilder.Add(System.BitConverter.GetBytes(Network.RIVAL_REQUEST)); byteBuilder.Add(System.BitConverter.GetBytes(id)); byteBuilder.Add(System.BitConverter.GetBytes(Network.PLAYER_OUT_SHOOT)); Global.network.Send(byteBuilder.GetByes()); byteBuilder.Clear(); } } else if (isShoot) //出视线外 { byteBuilder.Add(System.BitConverter.GetBytes(Network.RIVAL_REQUEST)); byteBuilder.Add(System.BitConverter.GetBytes(id)); byteBuilder.Add(System.BitConverter.GetBytes(Network.PLAYER_OUT_SHOOT)); Global.network.Send(byteBuilder.GetByes()); byteBuilder.Clear(); } } } //shoot if (isShoot) { Debug.Log("SHOOT"); //若无子弹则换弹 if (weaponController.bulletNumNow <= 0) { weaponController.bulletSum = weaponController.bulletMaxHold + 100; //设置子弹无限 humanController.Reload(); } else { transform.LookAt(Global.player.transform, transform.up); if (humanController.Attack1() && weaponController.Shoot()) { Ray ray = new Ray(transform.position + shootRectify, transform.forward + transform.right * Random.Range(-shootBias, shootBias) + transform.up * Random.Range(-shootBias, shootBias)); if (Physics.Raycast(ray, out hit)) { //若击中环境,刀子无击中效果 if (hit.collider.gameObject.layer == 9 && weaponController.category != WeaponCategory.Knife) { GameObject shootSmoke = Instantiate(shootSmokePrefab, hit.point, Quaternion.identity); shootSmoke.transform.localRotation.SetLookRotation(hit.normal); //获取击中物体的颜色 Color color = hit.collider.gameObject.GetComponent <MeshRenderer>().material.color; ParticleSystem particleSystem = shootSmoke.GetComponent <ParticleSystem>(); //设置起始颜色 ParticleSystem.MainModule main = particleSystem.main; main.startColor = color; //设置颜色渐变 ParticleSystem.ColorOverLifetimeModule col = particleSystem.colorOverLifetime; col.enabled = true; Gradient grad = new Gradient(); grad.SetKeys(new GradientColorKey[] { new GradientColorKey(color, 0.4f), new GradientColorKey(color, 1.0f) }, new GradientAlphaKey[] { new GradientAlphaKey(1.0f, 0.0f), new GradientAlphaKey(0.0f, 1.0f) }); col.color = grad; GameObject.Destroy(shootSmoke, 0.5f); //设置0.2s后销毁烟雾效果 } //若击中人 else if (hit.collider.gameObject.layer == 11) { hit.collider.gameObject.GetComponent <HumanAnimController>().Damaged(weaponController.damage, hit.point); } } } } } } }