RPG遊戲《黑暗之光》流程介紹與代碼分析之(十五):主角受攻擊效果以及場景切換

十五章:主角受攻擊效果以及場景切換

本篇博客將《黑暗之光》開發的最後工作做完,包括之前未實現的主角被擊效果,以及實際運行中的場景切換。

15.1 主角的受攻擊效果

我們參照WolfBaby中的受攻擊效果(鏈接),製作主角的對應效果
    public float missRate = 0.25f;
    public GameObject HUDTextPrefab;
    private GameObject HUDTextGO;
    public GameObject playerHUDTextGO;
    private HUDText showText;
    private UIFollowTarget followTarget;
    public AudioClip missSound;

    public void BeDamage(int attackValue)
    {
        int defence = Equipment._instance.defense + ps.defense + ps.defense_plus;
        float damage = attackValue * (200 - defence)/200 ;
        //float damage = int.Parse (temp);
        if (damage <= 1)
        {
            damage = 1;
        }
        float value = Random.Range (0f, 1f);
        if (value < 0.5)
        {
            //damage    
        }
        else
        {
            //miss
            AudioSource.PlayClipAtPoint(missSound,transform.position);
            showText.Add("Miss",Color.blue,1);
        }
    }
並對相應物體賦初值

傷害效果的設置如下
    public GameObject playerBody;    //指定角色body
    private Color normalColor;

    void Awake()
    {
        normalColor = playerBody.renderer.material.color;
    }

        if (value < 0.5)
        {
            //damage    
            showText.Add("-" + damage,Color.black,1);
            ps.hp_remain -= damage;
            StartCoroutine (ShowPlayerRed ());
        }
    IEnumerator ShowPlayerRed()
    {
        playerBody.renderer.material.color = Color.red;
        yield return new WaitForSeconds (0.5f);
        playerBody.renderer.material.color = normalColor;
    }

15.2 主角的受傷和死亡效果

接下來實現角色的受傷效果,hp小於0時死亡。首先在PlayerAttack的PlayerState中添加死亡狀態
public enum PlayerState{
    normalWalk,
    normalAttack,
    skillAttack,
    Death    //死亡狀態
}
並在hp小於0時
            if(ps.hp_remain <= 0)
            {
                state = PlayerState.Death;
            }
在Update()中,當state = PlayerState.Death時,我們有
        else if(state == PlayerState.Death)
        {
            animation.CrossFade(aniName_death);
        }

之後我們在WolfBaby的AutoAttack()中攻擊主角並調用主角的BeDamage()函數,
if(aniName_nowAttack == aniName_normalAttack)
                {
                    if(attackTimer >= normalAttackTime)
                    {    
                        target.GetComponent<PlayerAttack>().BeDamage(attackValue);    //訪問Bedamage造成傷害
                        aniName_nowAttack = aniName_idle;
                        animation.CrossFade(aniName_idle);
                    }
                }
                else if(aniName_nowAttack == aniName_crazyAttack)
                {
                    if(attackTimer >= crazyAttackTime)
                    {
                        target.GetComponent<PlayerAttack>().BeDamage(attackValue);
                        aniName_nowAttack = aniName_idle;
                        animation.CrossFade(aniName_idle);
                    }
                }
即實現了主角的受傷效果

在每次攻擊完畢之後,我們要調用人物面板的FaceUI的SetFaceProperty()屬性,以更新狀態。在AutoAttack()函數,我們添加
FaceUI._instance.SetFaceProperty ();    
就可以生效,並且當角色死亡時,我們需要取消怪物的攻擊以及人物的移動功能,因此在AutoAttack中加入
PlayerState playerAttackState = target.GetComponent<PlayerAttack> ().state;    //用playerAttackState,避免與state重名
            if (playerAttackState == PlayerState.Death)
            {
                target = null;
                state = WolfBabyState.Idle;
                return;
            }
以及PlayerDirection的Update()中
if (pa.state == PlayerState.Death) return;
即可實現受傷以及死亡效果。

15.3場景間的切換

最後一步是將整個流程連接起來,通過初始化載入界面——>角色創建界面——>實際遊戲場景,也就是Scene之間的切換。在開始動畫裏,我們有New Game和Load Game兩個功能。
在File——>Build Setting中,導入場景

在控制New Game的按鍵腳本中,添加
    public void PressNewGame()
    {    
        PlayerPrefs.SetInt("dataFromSave",0);
        Application.LoadLevel (1);    //加載上圖紅框中的1場景
    }
即可進入角色創建界面
在角色創建場景中,需要保存所選角色和角色名,因此在OK按鈕腳本中添加
    public void PressConfirmButton()
    {
        PlayerPrefs.SetString ("storeName", nameInput.value);    //存儲名字
        PlayerPrefs.SetInt ("storeCurrentIndex", selectedIndex);    //存儲角色    
        Application.LoadLevel (2);    //加載下一個場景

    }
在GameSetting中創建一個腳本爲LoadRole,控制加載
public class LoadRole : MonoBehaviour {

    public GameObject magicianPrefab;    //獲取兩個角色的prefab
    public GameObject swordmanPrefab;

    void Awake()
    {
        int index = PlayerPrefs.GetInt ("storeCurrentIndex");    //得到輸入的名字和角色
        string name = PlayerPrefs.GetString ("storeName");
        GameObject playerGO = null;
        if(index == 0)
        {
            playerGO = GameObject.Instantiate(magicianPrefab) as GameObject;    //實例化
        }
        else if(index == 1)
        {
            playerGO = GameObject.Instantiate(swordmanPrefab) as GameObject;
        }

        playerGO.GetComponent<PlayerStatus> ().name = name;    //通過實例化的  playerGO修改名字信息
    }

}
就可以正常創建,角色名稱等信息也正常顯示。

總結:至此,遊戲的基本功能都已實現,大致瞭解了整個遊戲的框架以及實現方法。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章