Unity 简单的回合制攻击

本文介绍了一个名为TurnControl的Unity脚本,用于管理回合制游戏中的玩家和敌人操作,包括角色属性设置、攻击逻辑、游戏状态切换以及角色移动。
摘要由CSDN通过智能技术生成

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);
    }

    
}

Unity5.6回合制战斗卡牌游戏源码是一套基于Unity引擎开发的回合制战斗卡牌游戏的源代码。 这套源码提供了游戏的基本框架和核心功能,包括战斗系统、卡牌系统、回合制机制等。 在这个游戏中,玩家可以通过收集和选择不同的卡牌来进行战斗。每个卡牌代表着不同的角色、技能或道具。玩家可以根据自己的策略和需求选择不同的卡牌组合来应对不同的战斗场景。 战斗系统采用回合制机制,玩家和敌人轮流进行行动。每个回合,玩家可以使用卡牌来进行攻击、防御、恢复生命值等操作。玩家和敌人的行动顺序可以根据速度、优先级、技能等进行调整。战斗结果可以通过计算伤害、技能效果、属性相克等来决定。 卡牌系统是游戏的核心机制之一,玩家可以通过游戏内的方式获得、合成和升级卡牌。每个卡牌都有自己的属性、技能战斗效果。玩家可以根据自己的战术和需求来不断调整自己的卡牌组合,以应对不同的挑战。 该源码基于Unity5.6版本开发,使用C#语言编写。它提供了游戏开发的基本框架和良好的可扩展性,开发者可以根据自己的需求和创意来进行二次开发和定制。此外,Unity5.6作为一款强大的游戏引擎,还提供了丰富的资源和工具,方便开发者进行游戏的美术、音效和动画等方面的创作。 总之,Unity5.6回合制战斗卡牌游戏源码是一套功能完善、可定制性强的游戏源代码,适合开发者用于创建自己的回合制战斗卡牌游戏。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值