#遊戲unity-音之國度#戰鬥系統中的回合制
轉載需標明出處
在上一篇博客中,我們介紹了實現回合制的代碼中的新內容——協程,這篇博客我們就真正的來實現一個回合制遊戲;首先,一般的回合制遊戲都是自己與敵人接連出招,然後當一方血量爲0是終止戰鬥,而我們的音符獸(主角)有不同的類型,相對應的就有不同的種類特點——例如,有的就會在受到攻擊後,自動進行反擊,而反擊的血量在一個較低的數值範圍內隨機產生,基於這樣的思想,我們可以增加一定機率的敵人的反擊算法。
主要的運行流程就是在對戰開始後,幾秒後自動進入己方出戰,出來技能框,選擇技能,然後主要的一點就是交換攻擊權限,由對方攻擊;而這個攻擊權限就由一個布爾值來控制,如下:
我用的是Animation組件
好了廢話少說上綁在主角身上的腳本Player
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour
{
public int HP = 100;
//等待玩家操作
public bool isWaitPlayer = true;
public bool ifUIshow = true;
//動畫組件
private Animation mAnim;
// Use this for initialization
void Start()
{
mAnim = this.GetComponent<Animation>();
mAnim.Play("Walk");
}
//傷害
void OnDamage(int mValue)
{
HP -= mValue;
}
public void OnGUI()
{
//如果處於等待狀態,則顯示操作窗口
if (isWaitPlayer && ifUIshow)
{
GUI.Window(0, new Rect(Screen.width / 2 + 150, Screen.height / 2 + 150, 200, 200), InitWindow, "請選擇技能");
// mAnim.Play("skill");
mAnim.Play("Walk");
}
}
void InitWindow(int ID)
{
if (GUI.Button(new Rect(0, 20, 200, 30), "攻擊"))
{
mAnim.Play("Run");
OnDamage(10);
//交換操作權
isWaitPlayer = false;
ifUIshow = false;
}
if (GUI.Button(new Rect(0, 50, 200, 30), "法術"))
{
OnDamage(10);
mAnim.Play("Dying");
//交換操作權
isWaitPlayer = false;
ifUIshow = false;
}
}
// Update is called once per frame
void Update()
{
}
}
綁在敵人身上的腳本
using UnityEngine;
using System.Collections;
public class Monster : MonoBehaviour
{
public int HP = 100;
public bool isWaitPlayer = true;
//動畫組件
private Animation mAnim;
// Use this for initialization
void Start()
{
mAnim = this.GetComponent<Animation>();
mAnim.Play("Walk");
}
void OnDamage(int mValue)
{
HP -= mValue;
}
//敵人AI算法
public void StartAI()
{
if (!isWaitPlayer)
{
if (HP > 20)
{
//80%
if (Random.Range(1, 5) % 5 != 1)
{
mAnim.Play("Dying");
// mAnim.SetBool("enemyAttack", true);
//ondamage
isWaitPlayer = true;
}
//20%
else
{
mAnim.Play("Dying");
isWaitPlayer = true;
}
}
else
{
switch (Random.Range(1, 5) % 5)
{
case 0:
mAnim.Play("Dying");
isWaitPlayer = true;
break;
}
}
}
}
// Update is called once per frame
void Update()
{
if (isWaitPlayer)
{
mAnim.Play("Walk");
}
}
}
後臺控制的總腳本control——其中就有上一篇所講到的協程函數的運用
using UnityEngine;
using System.Collections;
public class control : MonoBehaviour
{
//動畫組件
private Animation mAnim;
//定義玩家及敵人
public Transform mPlayer;
public Transform mEnemy;
//定義玩家及敵人腳本類
private Player playerScript;
private Monster enemyScript;
//默認操作狀態爲玩家操作
private OperatorState mState = OperatorState.Player;
//定義操作狀態枚舉
public enum OperatorState
{
Quit,
EnemyAI,
Player
}
// Use this for initialization
void Start()
{
mAnim = mPlayer.GetComponent<Animation>();
//獲取玩家及敵人腳本類
playerScript = mPlayer.GetComponent<Player>();
enemyScript = mEnemy.GetComponent<Monster>();
playerScript.OnGUI();
}
//UI延遲4.5秒調出
IEnumerator WaitUI()
{
yield return new WaitForSeconds(1F);
enemyScript.isWaitPlayer = true;
playerScript.ifUIshow = true;
}
//
IEnumerator WaitAI()
{
yield return new WaitForSeconds(2.0F);
enemyScript.isWaitPlayer = false;
}
//爲怪物AI延遲3秒
IEnumerator UpdateLater()
{
yield return new WaitForSeconds(3.0F);
//敵人停止等待
enemyScript.isWaitPlayer = false;
//敵人執行AI
enemyScript.StartAI();
}
// Update is called once per frame
void Update()
{
//如果敵我雙方有一方生命值爲0,則遊戲結束
if (playerScript.HP == 0)
{
mState = OperatorState.Quit;
Debug.Log("遊戲失敗");
}
else if (enemyScript.HP == 0)
{
mState = OperatorState.Quit;
Debug.Log("遊戲勝利");
}
else
{
switch (mState)
{
case OperatorState.Player:
{
if (!playerScript.isWaitPlayer)
{
StartCoroutine("UpdateLater");
StartCoroutine("WaitUI");
mState = OperatorState.EnemyAI;
}
break;
}
case OperatorState.EnemyAI:
{
if (enemyScript.isWaitPlayer)
{
StartCoroutine("WaitAI");
playerScript.isWaitPlayer = true;
mState = OperatorState.Player;
mAnim.Play("Walk");
}
break;
}
}
}
}
}
至此,回合制對戰的基本代碼書寫完成。