private void Update() { if (Input.GetMouseButtonDown(0) && MouseStateRecorder.GetInstance().GetCurMouseState() == MouseStateRecorder.MouseState.normal) { //当鼠标点击站点,且此时的鼠标状态为正常 Vector3 pos = Camera.main.ScreenToWorldPoint(Input.mousePosition); RaycastHit2D hit = Physics2D.Raycast(new Vector2(pos.x, pos.y), Vector2.zero); if (hit.collider) { Land cur = hit.collider.GetComponent <Land>(); if (cur == null || cur.GetPlayer() != GameManager.GetInstance().GetCurPlayer()) { return; } curPointLand = cur; if (OnPointLandChange != null) { OnPointLandChange.Invoke(this, EventArgs.Empty); } transform.position = curPointLand.transform.position; HideAll(); ToShow(allPannels[0]);//展示基本面板 } else { if (!EventSystem.current.IsPointerOverGameObject()) { HideAll(); } } } }
public void Shoot() { curLand = LandPanel.GetInstance().GetCurPointLand(); shooter = curLand.GetBuilding().GetComponent <Shooter>(); StartCoroutine("SelectToLand", shooter); MouseStateRecorder.GetInstance().SetCurMouseState(MouseStateRecorder.MouseState.emitBullet); }
public void BuildPipe() { Land landFrom = LandPanel.GetInstance().GetCurPointLand(); Pipe pipe = Instantiate(pipePrefab.gameObject, landFrom.transform.position, Quaternion.identity).GetComponent <Pipe>(); pipe.SetFrom(landFrom); StartCoroutine("SelectToLand", pipe); MouseStateRecorder.GetInstance().SetCurMouseState(MouseStateRecorder.MouseState.buildPipe); }
IEnumerator SelectToLand(Pipe pipe) { while (true) { Vector2 point = Camera.main.ScreenToWorldPoint(Input.mousePosition); pipe.Stretch(point, pipe.GetLength(point) < maxPipeLen); if (Input.GetMouseButtonDown(0)) { RaycastHit2D hit = Physics2D.Raycast(point, Vector2.zero); if (hit.collider) { Land land = hit.collider.GetComponent <Land>(); float dis = 0; if (land != null && !land.Equals(LandPanel.GetInstance().GetCurPointLand()) && //不建立自己到自己的pipe pipe.GetPlayer().Equals(land.GetPlayer()) && //不建立自己到别人的pipe (dis = pipe.GetLength(point)) < maxPipeLen) //不能太长 { PlayerDataRequire dataRequire = new PlayerDataRequire(PlayerData.PlayerDataEnum.capital, -(int)(pipeCostPerLen * dis)); bool ret = pipe.GetPlayer().GetData().UpdateData(dataRequire, false); if (!ret) { break; //资金不够 } pipe.GetPlayer().GetData().UpdateData(dataRequire, true); pipe.SetToLand(land); break; } } Destroy(pipe.gameObject); break; } yield return(0); } MouseStateRecorder.GetInstance().SetCurMouseState(MouseStateRecorder.MouseState.normal); }
IEnumerator SelectToLand(Shooter shooter) { locatorSprite.SetActive(true); bool isLocate = false; while (true) { locatorSprite.transform.Rotate(Vector3.forward, 1.0f); Vector3 point = Camera.main.ScreenToWorldPoint(Input.mousePosition); locatorSprite.transform.position = (Vector2)point; shooter.TurretLookAt(point); if (Input.GetMouseButtonDown(0)) { RaycastHit2D hit = Physics2D.Raycast(new Vector2(point.x, point.y), Vector2.zero); if (hit.collider) { Land land = hit.collider.GetComponent <Land>(); if (land != null && !land.GetPlayer().Equals(curLand.GetPlayer()) && shooter.IfDisWithin(point)) { locatorSprite.transform.position = land.transform.position; shooter.AutoShoot(land); isLocate = true; break; } } break; } yield return(0); } MouseStateRecorder.GetInstance().SetCurMouseState(MouseStateRecorder.MouseState.normal); if (isLocate) { yield return(new WaitForSeconds(1f)); } locatorSprite.SetActive(false); }