// 当前单位接收伤害 public void ReceiveDamage(Unit AttackUnit, int DamageAmount) { // 伤害响应部分 // 实际伤害结算 int CurrentHealth = GetCurrentHealth(); if (CurrentHealth - DamageAmount <= 0) { CurrentHealth = 0; IsAlive = false; // 每当一个棋子包含的单位不在存活就需要检查棋子是否还存活 PieceStateController TmpPieceStateController = LocatePiece.GetComponentInChildren <PieceStateController>(); // 如果棋子包含的存活单位为0则判定棋子不再存活 if (TmpPieceStateController.GetAliveUnitNum() == 0) { Debug.Log("Piece is not alive."); // 调用棋子亡语函数 TmpPieceStateController.PieceDead(AttackUnit); } } else { CurrentHealth -= DamageAmount; } SetCurrentHealth(CurrentHealth); }
public void InitializePieceUI() { //Debug.Log("sasasasasa"); PieceUIBase = Instantiate(PieceUIBasePrefab); //PieceUIBase.transform.parent = GameObject.Find("Canvas").gameObject.transform; PieceUIBase.transform.SetParent(GameObject.Find("Canvas/PieceUILayer").gameObject.transform); // 在父对象的子对象中查找目标物体 HealBar = PieceUIBase.transform.Find("HealthBar").gameObject; ThisPieceStateController = this.GetComponent <PieceStateController>(); }
// Use this for initialization void Start() { ClientManager = GameObject.Find("ClientManager"); ThisPieceStateController = this.GetComponent <PieceStateController>(); ThisPieceInfoController = this.GetComponent <PieceInfoController>(); //ChessBoardManager = GameObject.Find("ChessBoardManager"); //CheckerBoardSize = LocateChessboard.GetComponent<ChessBoardInfoController>().CheckerBoardSize; //ChessBoardSize = LocateChessboard.GetComponent<ChessBoardInfoController>().ChessBoardSize; //GeneratePositionX = LocateChessboard.GetComponent<ChessBoardInfoController>().ChessBoardOuterPosition.x; //GeneratePositionZ = LocateChessboard.GetComponent<ChessBoardInfoController>().ChessBoardOuterPosition.y; //InitPosition = LocateChessboard.GetComponent<ChessBoardInfoController>().InitPosition; // 载入可操作目标预制体 OperatbleTargetPrefab = (GameObject)Resources.Load("Prefabs/OperatbleTarget"); HintTargetPrefab = (GameObject)Resources.Load("Prefabs/HintTarget"); //ThisPieceStateController = this.transform.parent.GetComponent<PieceStateController>(); if (OperatbleTargetPrefab == null) { Debug.Log("OperatbleTargetPrefab load fail"); } // TODO: 动态调整可操作目标数组的长度 OperatblePreviews = new GameObject[10]; HintPreviews = new GameObject[20]; AttackTargets = new GameObject[20]; MoveDirectionX[0] = 1; MoveDirectionY[0] = 1; MoveDirectionX[1] = 1; MoveDirectionY[1] = 0; MoveDirectionX[2] = 1; MoveDirectionY[2] = -1; MoveDirectionX[3] = 0; MoveDirectionY[3] = -1; MoveDirectionX[4] = -1; MoveDirectionY[4] = -1; MoveDirectionX[5] = -1; MoveDirectionY[5] = 0; MoveDirectionX[6] = -1; MoveDirectionY[6] = 1; MoveDirectionX[7] = 0; MoveDirectionY[7] = 1; }
public void SetPieceEntity(GameObject NewPieceEntity) { if (NewPieceEntity != null) { PieceEntity = NewPieceEntity; ThisPieceStateController = NewPieceEntity.GetComponentInChildren <PieceStateController>(); if (ThisPieceStateController.GetUnitIconImage() == null) { //Debug.Log("====================================="); } SetUnitIconImage(ThisPieceStateController.GetUnitIconImage()); } else { PieceEntity = null; ThisPieceStateController = null; } }
// 判断棋子ParaPiece在当前时刻是否决定撤退 public bool PieceDecideRetreat(GameObject ParaPiece) { PieceStateController ParaPieceStateController = ParaPiece.GetComponentInChildren <PieceStateController>(); float LostHealthInBattle = ParaPieceStateController.GetCurrentPieceHealthBeforeBattle() - ParaPieceStateController.GetPieceCurrentHealth(); float CurrentHealthInBattle = ParaPieceStateController.GetPieceCurrentHealth(); if (LostHealthInBattle > 0) { // 如果在本场战斗中已经损失的生命值超过了该棋子 (最大生命值 X 棋子的撤退阈值(默认撤退阈值为25%)) 则触发撤退机制 if (LostHealthInBattle / ParaPieceStateController.GetPieceMaxHealth() >= ParaPieceStateController.GetRetreatThreshold()) { float RandomFloat = Random.Range(0, 100); float X = LostHealthInBattle / (LostHealthInBattle + CurrentHealthInBattle); //TODO: 此处应该使用棋子自带的撤退策略来判断是否撤退 // 在本场战斗损失生命值已经超过棋子生命值25%的前提下, 本场战斗损失生命值越多,撤退概率越大 if (RandomFloat <= 50 + 50 * X) { Debug.Log("player:" + ParaPieceStateController.GetOwner() + "retreat."); return(true); } } } // 重要TODO // 执行具体的撤退操作并且移除战斗单位数组中的对应单位引用以及战斗序列中的对应操作 // 棋子死亡算作撤退 if (CurrentHealthInBattle == 0) { return(true); } return(false); }
public override void InputStateUpdate() { //Debug.Log("HandCardOperation"); // 游戏场景射线检测 GetHoverGameObject(); // GUI射线检测 GetHoverGUI(); GameObject CurrentHoverCheckerBoard = GetHoverCheckerBoard(); GameObject CurrentHoverPiece = GetHoverPiece(); GameObject CurrentHoverHandCardArea = GetHoverHandCardArea(); if (Input.GetMouseButtonUp(0)) { //Debug.Log("mouseUp"); // 如果鼠标左键被抬起 if (GetCurrentOperationHandCard().GetComponent <HandCardController>().GetCard().HandCardSpell() == true) { // 如果当前操作手牌施放成功则更新手牌区 // 当本地卡牌施放判断成功时向游戏控制器告知卡牌使用 MainCardManager.PlyerCastACard(GetCurrentPlayerID(), GetCurrentOperationHandCard().GetComponent <HandCardController>().GetCard()); //HandCardArea.GetComponent<HandCardAreaController>().SpellAHandCard(GetCurrentOperationHandCard()); } else { // 如果当前操作手牌施放失败则显示手牌UI同时将手牌移回手牌区 HandCardArea.GetComponent <HandCardAreaController>().RestoreAHandCard(GetCurrentOperationHandCard()); GetCurrentOperationHandCard().GetComponent <HandCardController>().HandCardDisplayOn(); // 隐藏手牌UI对应卡牌的瞄准模式 GetCurrentOperationHandCard().GetComponent <HandCardController>().GetCard().TargetPreviewOff(); } DisselectCurrentOperationHandCard(); } else { // 如果鼠标左键没有被抬起(即用户一直按着鼠标左键) GetCurrentOperationHandCard().transform.position = Input.mousePosition; if (CurrentHoverHandCardArea != null) { // 当玩家按住鼠标左键拖动卡牌并且卡牌仍处在手牌区中时显示手牌UI并隐藏卡牌瞄准模式 // 显示手牌UI GetCurrentOperationHandCard().GetComponent <HandCardController>().HandCardDisplayOn(); // 隐藏手牌UI对应卡牌的瞄准模式 GetCurrentOperationHandCard().GetComponent <HandCardController>().GetCard().TargetPreviewOff(); //Debug.Log("mouse in hand card area."); } else { // 当玩家按住鼠标左键拖动卡牌并且卡牌不处在手牌区中时隐藏手牌UI并显示卡牌瞄准模式 if (GetCurrentOperationHandCard().GetComponent <HandCardController>().GetCard().HandCardcCostCheck()) { // 如果玩家当前资源足以施放该卡牌则允许将手牌拖动到棋盘上进行释放目标选择 // 这里应该是3个分支 // 1.如果拖动到棋盘格上 // 2.如果拖动到棋子上 // 3.如果拖动到其他物体上 if (CurrentHoverCheckerBoard != null) { // 如果鼠标移动到某个棋盘格上 // 隐藏手牌UI GetCurrentOperationHandCard().GetComponent <HandCardController>().HandCardDisplayOff(); // 显示手牌UI对应卡牌的瞄准模式 GetCurrentOperationHandCard().GetComponent <HandCardController>().GetCard().TargetPreviewOn(CurrentHoverCheckerBoard); } else if (CurrentHoverPiece != null) { // 如果鼠标移动到某个棋子上 // 隐藏手牌UI GetCurrentOperationHandCard().GetComponent <HandCardController>().HandCardDisplayOff(); // 获取棋子所在的棋盘格 PieceStateController TmpPieceStateController = CurrentHoverPiece.GetComponentInChildren <PieceStateController>(); CurrentHoverCheckerBoard = TmpPieceStateController.LocateChessboard.GetComponent <ChessBoardController>().GetCheckerBoard(TmpPieceStateController.ChessBoardInternalCoordinate); //Debug.Log(CurrentHoverCheckerBoard.name); // 显示手牌UI对应卡牌的瞄准模式 GetCurrentOperationHandCard().GetComponent <HandCardController>().GetCard().TargetPreviewOn(CurrentHoverCheckerBoard); } else { // 如果鼠标并没有位于某个棋盘格上 // 显示手牌UI GetCurrentOperationHandCard().GetComponent <HandCardController>().HandCardDisplayOn(); // 隐藏手牌UI对应卡牌的瞄准模式 GetCurrentOperationHandCard().GetComponent <HandCardController>().GetCard().TargetPreviewOff(); } } else { // 如果玩家当前资源不足以施放该卡牌则将卡牌拉回手牌区 HandCardArea.GetComponent <HandCardAreaController>().RestoreAHandCard(GetCurrentOperationHandCard()); GetCurrentOperationHandCard().GetComponent <HandCardController>().HandCardDisplayOn(); // 隐藏手牌UI对应卡牌的瞄准模式 GetCurrentOperationHandCard().GetComponent <HandCardController>().GetCard().TargetPreviewOff(); DisselectCurrentOperationHandCard(); } } } }
// 将一个棋子当前的状态信息装入展示卡牌当中 public void LoadInfoFromPiece(GameObject ParaPiece) { PieceInfoController ParaPieceInfoController = ParaPiece.GetComponentInChildren <PieceInfoController>(); PieceStateController ParaPieceStateController = ParaPiece.GetComponentInChildren <PieceStateController>(); CardNameText.GetComponent <Text>().text = ParaPieceInfoController.GetPieceName(); if (ParaPieceStateController.GetCardMainImage() != null) { MainImage.GetComponent <RawImage>().texture = ParaPieceStateController.GetCardMainImage(); } else { } //MainImage.GetComponent<RawImage>().texture = // 也许在单位数目为1的时候英雄棋子和普通杂兵棋子需要区别对待(英雄类棋子直接隐藏单位数目图标和单位数目文本) PieceUnitNumText.GetComponent <Text>().text = (ParaPieceStateController.GetAliveUnitNum()).ToString(); MineralText.GetComponent <Text>().text = (ParaPieceInfoController.GetPieceCostMineral()).ToString(); GasText.GetComponent <Text>().text = (ParaPieceInfoController.GetPieceCostGas()).ToString(); List <string> CardDescriptionItemList = ParaPieceStateController.GetCardDescriptionItemList(); int CharacterNum = 0; if (CardDescriptionItemList != null) { //Debug.Log("get it"); for (int i = 0; i < CardDescriptionItemList.Count; i++) { CharacterNum += CardDescriptionItemList[i].Length; } // 如果描述项字符总量过多则需要调整文本中的字体大小 if (CharacterNum <= 56) { string Res = ""; // 组装卡牌描述区文本,每个描述项之间用一个空格隔开 for (int i = 0; i < CardDescriptionItemList.Count; i++) { if (i == CardDescriptionItemList.Count - 1) { Res += CardDescriptionItemList[i]; } else { Res += (CardDescriptionItemList[i] + " "); } } // 将卡牌描述区文本设置到text属性 CardDescription.transform.Find("CardDescriptionText").gameObject.GetComponent <Text>().text = Res; } } // 更新生命值状态条 float HealthPercentage = ParaPieceStateController.GetRationOfHealth(); float HealthBarWidth = 145 * HealthPercentage; float HealthBarPosition = 145 * (1 - HealthPercentage); HealthBar.GetComponent <RectTransform>().sizeDelta = new Vector2(HealthBarWidth, 6); HealthBar.GetComponent <RectTransform>().localPosition = new Vector3((float)(HealthBarPosition / 2.0), 0, 0); }
public override bool HandCardSpell() { Ray DetectRay; RaycastHit Hit; GameObject ColliderGameObject; DetectRay = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(DetectRay, out Hit)) { ColliderGameObject = Hit.collider.gameObject; } else { ColliderGameObject = null; return(false); } // 费用检查 //Debug.Log("This Card belong to player " + GetOwner()); int PlayerMineral = GetResourceManager().GetPlayerMineral(GetOwner()); int PlayerGas = GetResourceManager().GetPlayerGas(GetOwner()); int PieceCostMineral = MinionPiece.GetComponentInChildren <PieceInfoController>().GetPieceCostMineral(); int PieceCostGas = MinionPiece.GetComponentInChildren <PieceInfoController>().GetPieceCostGas(); if (PieceCostMineral > PlayerMineral || PieceCostGas > PlayerGas) { // 显示一条提示资源不足的消息 GetGameController().MessageManager.GetComponent <MessageController>().PrivateMessage("资源不足"); // 卡牌施放失败返回false return(false); } if (ColliderGameObject != null && ColliderGameObject.GetComponent <GameObjectPropertyController>().IsCheckerBoard == true) { // 如果鼠标悬停在一个棋盘格上 CheckerBoardInfoController TmpCheckerBoardInfoController = ColliderGameObject.GetComponent <CheckerBoardInfoController>(); // 卡牌释放 GetWorldManager().DeployPieceOnChessBoard(MinionPiece, TmpCheckerBoardInfoController.CheckerBoardInternalCoordinate, TmpCheckerBoardInfoController.CheckerBoardOuterCoordinate); MinionPiece.GetComponentInChildren <PieceAnimationController>().DeployAnimationStart(); // 资源消耗更新 PlayerMineral -= PieceCostMineral; PlayerGas -= PieceCostGas; GetResourceManager().SetPlayerMineral(GetOwner(), PlayerMineral); GetResourceManager().SetPlayerGas(GetOwner(), PlayerGas); return(true); } else if (ColliderGameObject != null && ColliderGameObject.GetComponent <GameObjectPropertyController>() != null && ColliderGameObject.GetComponent <GameObjectPropertyController>().IsPiece == true) { // 如果鼠标悬停在一个棋子上 PieceStateController TmpPieceStateController = ColliderGameObject.GetComponentInChildren <PieceStateController>(); // TODO: 此处应该检查该棋盘格处是否含有可见的敌方棋子 // 如果含有的话,不具有渗透者技能的棋子会部署失败 // 如果含有的话,具有渗透者技能的棋子可以成功部署 // 如果不含有敌方棋子则可以成功部署 GameObject TmpCheckerBoard = TmpPieceStateController.LocateChessboard.GetComponent <ChessBoardController>().GetCheckerBoard(TmpPieceStateController.ChessBoardInternalCoordinate); CheckerBoardInfoController TmpCheckerBoardInfoController = TmpCheckerBoard.GetComponent <CheckerBoardInfoController>(); //Debug.Log(CurrentHoverCheckerBoard.name); GetWorldManager().DeployPieceOnChessBoard(MinionPiece, TmpCheckerBoardInfoController.CheckerBoardInternalCoordinate, TmpCheckerBoardInfoController.CheckerBoardOuterCoordinate); MinionPiece.GetComponentInChildren <PieceAnimationController>().DeployAnimationStart(); // 资源消耗更新 PlayerMineral -= PieceCostMineral; PlayerGas -= PieceCostGas; GetResourceManager().SetPlayerMineral(GetOwner(), PlayerMineral); GetResourceManager().SetPlayerGas(GetOwner(), PlayerGas); return(true); } else { return(false); } }
// 鼠标左键点选棋子后在主场景中显示所有的 可移动位置 和 可攻击位置 public void OperationPreview() { PreviewIndex = 0; ThisPieceStateController = this.GetComponent <PieceStateController>(); ThisPieceInfoController = this.GetComponent <PieceInfoController>(); ThisPieceStateController.DisplayAllUnit(); GameObject LocateChessboard = ThisPieceStateController.LocateChessboard; CheckerBoardSize = LocateChessboard.GetComponent <ChessBoardController>().CheckerBoardSize; ChessBoardSize = LocateChessboard.GetComponent <ChessBoardController>().ChessBoardSize; //GeneratePositionX = LocateChessboard.GetComponent<ChessBoardInfoController>().ChessBoardOuterPosition.x; //GeneratePositionZ = LocateChessboard.GetComponent<ChessBoardInfoController>().ChessBoardOuterPosition.y; //InitPosition = LocateChessboard.GetComponent<ChessBoardInfoController>().InitPosition; //Debug.Log("233"); this.GetComponent <MeshRenderer>().material = PieceSelectedMaterial; if (this.GetComponent <PieceInfoController>().HasOrientation == false) { // 获取该棋子在棋盘中的坐标 Vector2Int CurrentCoordinate = ThisPieceStateController.ChessBoardInternalCoordinate; // 进行常规移动位置计算 for (int i = 0; i < 8; i++) { for (int j = 1; j <= ThisPieceInfoController.BaseMoveAbility[i]; j++) { Vector2Int NewInterCoord = new Vector2Int(); Vector2Int NewOuterCoord = new Vector2Int(); Vector2Int ParaInterCoord = new Vector2Int(CurrentCoordinate.x + j * MoveDirectionX[i], CurrentCoordinate.y + j * MoveDirectionY[i]); if (CheckerBoardAvaliable(ParaInterCoord, out NewInterCoord, out NewOuterCoord) == true) { GenerateHintTarget(NewInterCoord, NewOuterCoord, 1); GenerateOperatableTarget(NewInterCoord, NewOuterCoord); } } } // 进行特殊移动位置计算 // 进行常规攻击位置计算 for (int i = 0; i < 8; i++) { for (int j = 1; j <= ThisPieceInfoController.BaseAttackAbility[i]; j++) { Vector2Int NewInterCoord = new Vector2Int(); Vector2Int NewOuterCoord = new Vector2Int(); Vector2Int ParaInterCoord = new Vector2Int((int)CurrentCoordinate.x + j * MoveDirectionX[i], (int)CurrentCoordinate.y + j * MoveDirectionY[i]); if (CheckerBoardAvaliable(ParaInterCoord, out NewInterCoord, out NewOuterCoord) == true) { GenerateHintTarget(NewInterCoord, NewOuterCoord, 2); //GenerateOperatableTarget(NewInterCoord, NewOuterCoord); GameObject AttackTargetPiece = GetCheckerPiece(NewInterCoord, NewOuterCoord); if (AttackTargetPiece != null) { //Debug.Log(AttackTargetPiece.name); AttackTargets[AttackTargetIndex++] = AttackTargetPiece; AttackTargetPiece.GetComponentInChildren <PieceStateController>().IsAttackTarget = true; } } } } } else { } }
// 显示并刷新参数棋子所在棋盘格对应的棋盘格包含单位信息条 public void RefreshContainUnitBar(GameObject SelectPiece) { PieceStateController SelectPieceStateController = SelectPiece.GetComponent <PieceStateController>(); if (SelectPieceStateController != null) { // 判断如果该棋子所在的棋盘格上有一个以上的棋子则显示并刷新棋盘格包含单位信息条 GameObject SelectLocateChessBoard = SelectPieceStateController.LocateChessboard; int SelectCheckerPieceNum = SelectLocateChessBoard.GetComponent <ChessBoardController>().GetCheckerBoardPlayerAlivePieceNum(SelectPieceStateController.ChessBoardInternalCoordinate, CurrentPlayerID); if (SelectCheckerPieceNum >= 2) { for (int i = 0; i < 10; i++) { LayerOfUnitIcon[i] = i; } // 显示棋盘格内包含单位侧边栏 CheckerBoardContainUnitBarBase.SetActive(true); // 根据该棋盘格内包含的单位调整侧边栏高度 CheckerBoardContainUnitBarBase.GetComponent <RectTransform>().sizeDelta = new Vector2(100, 10 + 90 * SelectCheckerPieceNum); FirstPosition = 0; if (SelectCheckerPieceNum % 2 == 0) { FirstPosition = ((SelectCheckerPieceNum / 2) - 1) * 90 + 45; } else { FirstPosition = (SelectCheckerPieceNum / 2) * 90; } for (int i = 0; i < SelectCheckerPieceNum; i++) { ContainUnitIconBase = GameObject.Instantiate(ContainUnitIconBasePrefab); ContainUnitIconBase.GetComponent <ContainUnitIconController>().SetPieceEntity(SelectLocateChessBoard.GetComponent <ChessBoardController>().GetCheckerBoardPlayerAlivePiece(Vector2ToVector2Int(SelectPieceStateController.ChessBoardInternalCoordinate), CurrentPlayerID, i)); ContainUnitIcanArray.Add(ContainUnitIconBase); ContainUnitIconBase.transform.SetParent(CheckerBoardContainUnitBarBase.transform); ContainUnitIconBase.GetComponent <RectTransform>().localPosition = new Vector3(0, FirstPosition - (i) * 90, 0); ContainUnitIconBase.name = "ContainUnitIconBase.name" + i; EventTrigger trigger = ContainUnitIconBase.GetComponent <EventTrigger>(); trigger.triggers = new List <EventTrigger.Entry>(); EventTrigger.Entry enter = new EventTrigger.Entry(); //3.指定事件触发的类型 enter.eventID = EventTriggerType.PointerClick; //4.指定事件触发的方法 enter.callback = new EventTrigger.TriggerEvent(); //Debug.Log("#######################" + i); int p = i; enter.callback.AddListener(delegate { ClickUnitIconBase(LayerOfUnitIcon[p]); }); trigger.triggers.Add(enter); } } } else { Debug.Log("PieceStateController reference fail"); } }