1.TurnControl 挂在Play场景上
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
//回合制
public class TurnControl : MonoBehaviour
{
//控制玩家操作及操作窗口是否出现
public bool isWaitForPlayer = true;
//控制怪物操作
public bool isEnemyAction = false;
//获取玩家及敌人脚本的引用
private PlayerControl playerScript;
private EnemyControl enemyScript;
//定义游戏状态枚举
public enum GameState
{
Menu,//游戏开始菜单
Game,//游戏中
Over//游戏结束
}
//游戏初始状态
public GameState currentState = GameState.Menu;
public Slider EnemySlider;
public Slider PlaySlider;
public GameObject[] characterPrefabs; //人物预制体
public GameObject[] placeholderPositions; //人物位置
public GameObject[] enemyPrefabs; //敌方预制体
public GameObject[] enemyPositions; //敌方位置
private GameObject[] myCharacters;
private GameObject[] enemyCharacters;
private int currentPlayerIndex = 0;
private int currentEnemyIndex = 0;
private bool isPlayerTurn = true;
private bool isGameOver = false;
private bool isExecutingTurn; // 标识是否正在执行角色的回合
void Start()
{
defaulMes();
}
private void defaulMes(){
myCharacters = new GameObject[placeholderPositions.Length];
enemyCharacters = new GameObject[placeholderPositions.Length];
// 初始化玩家和敌人的回合
currentPlayerIndex = 0;
currentEnemyIndex = 0;
currentState = GameState.Menu;
isPlayerTurn = true;
isGameOver = false;
LoadCharacters();
}
private void LoadCharacters()
{
if (characterPrefabs.Length != placeholderPositions.Length)
{
Debug.LogError("Character and placeholder counts do not match!");
return;
}
for (int i = 0; i < characterPrefabs.Length; i++)
{
GameObject characterPrefab = characterPrefabs[i];
Transform placeholderPosition = placeholderPositions[i].GetComponent<Transform>();
GameObject admin = placeholderPositions[i].transform.Find("admin").gameObject;
Destroy(admin);
GameObject character = Instantiate(characterPrefab, placeholderPosition.position, placeholderPosition.rotation);
character.transform.SetParent(placeholderPosition);
Transform Ctransform = character.GetComponent<Transform>();
Ctransform.localScale = Vector3.one;
RoleAttributesController roleAttributesController = character.GetComponent<RoleAttributesController>();
if (roleAttributesController != null)
{
// 设置人物属性
roleAttributesController.hp = 100;
roleAttributesController.attackDamage = 20;
roleAttributesController.defence = 10;
}
myCharacters[i] = character;
}
if (enemyPrefabs.Length != enemyPositions.Length)
{
Debug.LogError("Character and placeholder counts do not match!");
return;
}
for (int i = 0; i < enemyPrefabs.Length; i++)
{
GameObject enemyPrefab = enemyPrefabs[i];
Transform enemyPosition = enemyPositions[i].GetComponent<Transform>();
GameObject admin = enemyPositions[i].transform.Find("admin").gameObject;
Destroy(admin);
GameObject character = Instantiate(enemyPrefab, enemyPosition.position, enemyPosition.rotation);
character.transform.SetParent(enemyPosition);
Transform Ctransform = character.GetComponent<Transform>();
Ctransform.localScale = Vector3.one;
RoleAttributesController roleAttributesController = character.GetComponent<RoleAttributesController>();
if (roleAttributesController != null) {
roleAttributesController.hp = 50;
roleAttributesController.attackDamage = 15;
roleAttributesController.defence = 1;
}
enemyCharacters[i] = character;
}
}
void OnGUI()
{
if (currentState == GameState.Menu)
{
GUI.Window(0, new Rect(Screen.width / 2 - 100, Screen.height / 2 - 30, 200, 60), GameStartConfirm, "战斗开始确认");
}
else if (currentState == GameState.Over)
{
GUI.Window(2, new Rect(Screen.width / 2 - 100, Screen.height / 2 - 30, 200, 60), GameRestartConfirm, "战斗结束");
}
}
void GameStartConfirm(int ID)
{
if (GUI.Button(new Rect(50, 30, 100, 20), "开始战斗"))
{
currentState = GameState.Game;
isExecutingTurn = true;
}
}
void GameRestartConfirm(int ID)
{
if (GUI.Button(new Rect(50, 30, 100, 20), "重新开始"))
{
defaulMes();
}
}
void Update()
{
if (IsGameOver())
{
Debug.Log("Game Over");
currentState = GameState.Over;
return;
}else if (currentState == GameState.Game) {
if (isPlayerTurn)
{
if (isExecutingTurn) {
if (currentPlayerIndex >= myCharacters.Length)
{
print(currentEnemyIndex+"-- 转为敌方攻击 ");
currentPlayerIndex = 0;
isPlayerTurn = false;
}
else {
StartCoroutine(PlayerAttack());
}
}
}
else
{
if (isExecutingTurn) {
print(currentEnemyIndex);
if (currentEnemyIndex >= enemyCharacters.Length)
{
print(currentPlayerIndex + " 转为我方攻击");
currentEnemyIndex = 0;
isPlayerTurn = true;
}
else {
StartCoroutine(EnemyAttack());
}
}
}
}
}
private IEnumerator PlayerAttack()
{
isExecutingTurn = false;
GameObject currentPlayer = myCharacters[currentPlayerIndex];
RoleAttributesController attributes = currentPlayer.GetComponent<RoleAttributesController>();
RoleController playerStats = currentPlayer.GetComponent<RoleController>();
print("我方" + currentPlayerIndex);
if (attributes.hp <= 0)
{
currentPlayerIndex++;
isExecutingTurn = true;
yield break;
}else{
GameObject currentEnemy = null;
for (int i = 0; i < enemyCharacters.Length; i++)
{
GameObject obj = enemyCharacters[i];
RoleAttributesController attributesIndex = obj.GetComponent<RoleAttributesController>();
print(attributesIndex.hp);
if (attributesIndex.hp == 0) {
currentEnemy = null;
}
if (attributesIndex.hp > 0)
{
currentEnemy = obj;
break;
}
}
print("-------"+currentEnemy);
if (currentEnemy != null) {
yield return playerStats.Skill1(currentPlayer, currentEnemy);
currentPlayerIndex++;
isExecutingTurn = true;
}
}
}
private IEnumerator EnemyAttack()
{
isExecutingTurn = false;
GameObject currentEnemy = enemyCharacters[currentEnemyIndex];
RoleController enemyStats = currentEnemy.GetComponent<RoleController>();
RoleAttributesController attributes = currentEnemy.GetComponent<RoleAttributesController>();
print("敌方" + currentEnemyIndex);
if (attributes.hp <= 0)
{
currentEnemyIndex++;
isExecutingTurn = true;
yield break;
}
else {
GameObject currentPlayer = null;
for (int i = 0; i < myCharacters.Length; i++)
{
GameObject obj = myCharacters[i];
RoleAttributesController attributesIndex = obj.GetComponent<RoleAttributesController>();
if (attributesIndex.hp > 0)
{
currentPlayer = obj;
break;
}
}
yield return enemyStats.Skill1(currentEnemy, currentPlayer);
currentEnemyIndex++;
isExecutingTurn = true;
}
}
private bool IsGameOver()
{
bool allPlayersDead = true;
bool allEnemiesDead = true;
foreach (GameObject enemyCharacter in enemyCharacters)
{
RoleAttributesController enemyStats = enemyCharacter.GetComponent<RoleAttributesController>();
if (enemyStats.hp > 0)
{
allEnemiesDead = false;
break;
}
}
foreach (GameObject playerCharacter in myCharacters)
{
RoleAttributesController playerStats = playerCharacter.GetComponent<RoleAttributesController>();
if (playerStats.hp > 0)
{
allPlayersDead = false;
break;
}
}
isGameOver = allPlayersDead || allEnemiesDead;
return isGameOver;
}
}
2.挂在脚本人物的叫脚本
using UnityEngine;
using System.Collections;
public class RoleAttributesController : MonoBehaviour
{
public int hp = 100;
public int currentHp;
public int attackDamage = 10;
public float speed = 10f;
public int defence = 1;
public Vector3 startPosition;
private void Start()
{
currentHp = hp;
startPosition = gameObject.GetComponent<Transform>().position;
}
}
using UnityEngine;
using System.Collections;
//回合制
public class RoleController : AbsRoleController
{
public override IEnumerator Skill1(GameObject myteam, GameObject otherTeam)
{
Transform currentTransform = myteam.GetComponent<Transform>();
RoleAttributesController roleAttributesController = myteam.GetComponent<RoleAttributesController>();
Move();
Transform otherTransform = otherTeam.GetComponent<Transform>();
RoleAttributesController otherRoleAttributesController = otherTeam.GetComponent<RoleAttributesController>();
RoleController otherRoleController = otherTeam.GetComponent<RoleController>();
while (Vector3.Distance(currentTransform.position, otherTransform.position) > 1f)
{
currentTransform.position = Vector3.MoveTowards(currentTransform.position, otherTransform.position, roleAttributesController.speed * Time.deltaTime);
yield return null;
}
setAnimation(RoleAnimatorType.ATK2);
int damage = Mathf.Max(0, roleAttributesController.attackDamage - otherRoleAttributesController.defence);
Debug.Log(myteam.name + "使用了普通攻击:" + damage);
otherRoleController.ReceiveDamage(damage);
Skill1Controller skill1 = otherTeam.GetComponent<Skill1Controller>();
StartCoroutine(skill1.Forex(otherTeam));
yield return StartCoroutine(WaitAndReturn(currentTransform, roleAttributesController.startPosition, 1f));
}
private void MoveTowardsTarget(Transform currentTransform, Vector3 startPosition, float speed)
{
currentTransform.position = Vector3.MoveTowards(currentTransform.position, startPosition, speed * Time.deltaTime);
}
private IEnumerator WaitAndReturn(Transform currentTransform, Vector3 startPosition, float waitTime)
{
yield return new WaitForSeconds(waitTime);
Move();
while (Vector3.Distance(currentTransform.position, startPosition) > 0.1f)
{
MoveTowardsTarget(currentTransform, startPosition,10f);
yield return null;
}
// 停止动画
setAnimation(RoleAnimatorType.IDLE);
}
}
using UnityEngine;
using System.Collections;
public class Skill1Controller : MonoBehaviour
{
// Use this for initialization
public GameObject[] allSkillPrefebs; //技能预制体
public float speed = 10f; // 弹道速度
public GameObject ps; // 弹道粒子系统
void Start()
{
}
void Update()
{
}
public IEnumerator Forex(GameObject otherGameObject) {
Transform transform = otherGameObject.GetComponent<Transform>();
int randomIndex = Random.Range(0, allSkillPrefebs.Length);
GameObject character = Instantiate(allSkillPrefebs[randomIndex], transform.position, transform.rotation);
RoleController roleController = otherGameObject.GetComponent<RoleController>();
roleController.Damage();
yield return new WaitForSeconds(2);
Destroy(character);
roleController.stopEvent();
}
[System.Obsolete]
public IEnumerator LaunchProjectile(GameObject myteam, GameObject otherTeam)
{
Transform currentTransform = myteam.GetComponent<Transform>();
Transform otherTransform = otherTeam.GetComponent<Transform>();
GameObject character = Instantiate(ps, otherTransform.position, otherTransform.rotation);
RoleController roleController = otherTransform.GetComponent<RoleController>();
roleController.Damage();
yield return new WaitForSeconds(2);
}
}