对少时步步高RPG回合制游戏的甚是怀念,但是现在找一些当时的游戏来玩发现画面已经完全吸引不了我了,特别是最原始的黑白版伏魔记、侠客行之类的,剧情甚是不错但是却没有了要玩的激情,所以就想在自己业余的时间来尝试研究研究,如果有时间或许能做一个3D版的自己娱乐娱乐。
![](http://img.manew.com/data/attachment/forum/201604/21/180756i1mtzp8c16ock8o5.gif)
3D的游戏,控制移动还是鼠标点击人物朝向移动比较靠谱,本月的训练营有实现奥,不会的可以问我,不多说,本文研究的内容是回合制的简单实现。在网上搜集了一些资料,看了一些代码,发现秦元培博客写的比较简单,但是逻辑稍带有点小bug,我稍加修改了一下,也改了动画的播放方式,不过秦大神的方法简洁易懂,省了我很多功夫,下面就让我来大体介绍一下我这个小Demo。
为了方便,直接把本月训练营赵信的模型动画取过来用了,首先添加动画组件animator,配置一下状态机,给人物添加一个动画,例如下图这样:
![](http://img.manew.com/data/attachment/forum/201604/21/181218jliu83uxjcz00nij.png.thumb.jpg)
![](http://img.manew.com/data/attachment/forum/201604/21/181218cllrfaf1i9ffk33l.png.thumb.jpg)
![](http://img.manew.com/data/attachment/forum/201604/21/181218hcyyg7f5gyce7zj7.png.thumb.jpg)
需要不断重复播放的动画,就是闲置状态,如果可以随时中断的动画则取消勾选Exit Time,还有添加一些状态的参数,animator和animation属性,需要的话自己好好研究一下,我了解的也不是很深刻。
那现在只剩下代码部分了,player部分(放到主角上的脚本):
usingUnityEngine;
usingSystem.Collections;
publicclassPlayer : MonoBehaviour {
publicintHP = 100;
//等待玩家操作
publicboolisWaitPlayer = true;
publicboolifUIshow = true;
//动画组件
privateAnimator mAnim;
// Use this for initialization
voidStart () {
mAnim = GetComponent<Animator>();
mAnim.SetBool ("idle",true);
}
//伤害
voidOnDamage(intmValue){
HP -= mValue;
}
voidOnGUI(){
//如果处于等待状态,则显示操作窗口
if(isWaitPlayer && ifUIshow) {
GUI.Window (0, newRect (Screen.width / 2 + 150, Screen.height / 2 + 150, 200, 200), InitWindow, "请选择技能");
mAnim.SetBool ("skill",false);
mAnim.SetBool ("idle",true);
}
}
voidInitWindow(intID){
if(GUI.Button (newRect (0, 20, 200, 30), "飞剑斩")) {
mAnim.SetBool("idle",false);
mAnim.SetBool ("skill",true);
//交换操作权
isWaitPlayer = false;
ifUIshow = false;
}
if(GUI.Button (newRect (0, 50, 200, 30), "降魔伏法")) {
mAnim.SetBool("idle",false);
mAnim.SetBool ("skill",true);
//交换操作权
isWaitPlayer = false;
ifUIshow = false;
}
}
// Update is called once per frame
voidUpdate () {
}
}
Monster(放到怪物上的脚本):
usingUnityEngine;
usingSystem.Collections;
publicclassMonsterAttackAI : MonoBehaviour {
publicintHP = 100;
publicboolisWaitPlayer = true;
//动画组件
//private Animation mAnimation;
privateAnimator mAnim;
// Use this for initialization
voidStart () {
mAnim = GetComponent<Animator>();
mAnim.SetBool ("enemyIdle",true);
}
voidOnDamage(intmValue){
HP -= mValue;
}
//敌人AI算法
publicvoidStartAI(){
if(!isWaitPlayer) {
if(HP > 20) {
//80%
if(Random.Range (1, 5) % 5 != 1) {
mAnim.SetBool("enemyIdle",false);
mAnim.SetBool("enemyAttack",true);
//ondamage
isWaitPlayer = true;
}
//20%
else{
mAnim.SetBool("enemyIdle",false);
mAnim.SetBool("enemyAttack",true);
isWaitPlayer = true;
}
}else{
switch(Random.Range (1, 5) % 5) {
case0:
mAnim.SetBool("enemyIdle",false);
mAnim.SetBool("enemyAttack",true);
isWaitPlayer = true;
break;
}
}
}
}
// Update is called once per frame
voidUpdate () {
if(isWaitPlayer) {
mAnim.SetBool("enemyIdle",true);
mAnim.SetBool("enemyAttack",false);
}
}
}
剩下的就是综合控制的脚本:
usingUnityEngine;
usingSystem.Collections;
publicclassControl : MonoBehaviour {
//动画组件
privateAnimator mAnim;
//定义玩家及敌人
publicTransform mPlayer;
publicTransform mEnemy;
//定义玩家及敌人脚本类
privatePlayer playerScript;
privateMonsterAttackAI enemyScript;
//默认操作状态为玩家操作
privateOperatorState mState = OperatorState.Player;
//定义操作状态枚举
publicenumOperatorState
{
Quit,
EnemyAI,
Player
}
// Use this for initialization
voidStart () {
mAnim = mPlayer.GetComponent<Animator> ();
//获取玩家及敌人脚本类
playerScript = mPlayer.GetComponent<Player>();
enemyScript = mEnemy.GetComponent<MonsterAttackAI> ();
}
//UI延迟4.5秒调出
IEnumerator WaitUI(){
yieldreturnnewWaitForSeconds (4.5F);
enemyScript.isWaitPlayer = true;
playerScript.ifUIshow = true;
}
//
IEnumerator WaitAI(){
yieldreturnnewWaitForSeconds (2.0F);
enemyScript.isWaitPlayer = false;
}
//为怪物AI延迟3秒
IEnumerator UpdateLater(){
yieldreturnnewWaitForSeconds(3.0F);
//敌人停止等待
enemyScript.isWaitPlayer = false;
//敌人执行AI
enemyScript.StartAI();
}
// Update is called once per frame
voidUpdate () {
//如果敌我双方有一方生命值为0,则游戏结束
if(playerScript.HP == 0) {
mState = OperatorState.Quit;
Debug.Log ("游戏失败");
}elseif(enemyScript.HP == 0) {
mState = OperatorState.Quit;
Debug.Log ("游戏胜利");
}else{
switch(mState) {
caseOperatorState.Player:
if(!playerScript.isWaitPlayer) {
StartCoroutine ("UpdateLater");
StartCoroutine ("WaitUI");
mState = OperatorState.EnemyAI;
}
break;
caseOperatorState.EnemyAI:
if(enemyScript.isWaitPlayer) {
StartCoroutine ("WaitAI");
playerScript.isWaitPlayer = true;
mState = OperatorState.Player;
mAnim.SetBool ("skill",false);
mAnim.SetBool ("idle",true);
}
break;
}
}
}
}
总结:本demo只是为了理一下回合制游戏实现方式的思路,以便以后快速解决这方面的问题,希望小伙伴们多多支持!!!
![](http://www.manew.com/static/image/smiley/default/20119009.gif)
![](http://www.manew.com/static/image/smiley/default/20119009.gif)
![](http://www.manew.com/static/image/smiley/default/20119009.gif)