You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

301 lines
11 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
//控制选中取消角色与角色的移动
public class GameManager : MonoBehaviour
{
public GameObject menuUI; // 调出菜单
public float speed = 3.0f; // 镜头移动速度
Character selected; // 记录选中的棋子
Character maincharacter; // 记录主角
bool menuActive = false; // 是否显示菜单
bool exploreMode = false; // true时为探索模式false时为战斗模式
public GameObject[] pieces; // 记录所有的棋子
public int[] groupCount; // 记录敌我双方棋子数量groupCount[0]为友方groupCount[1]为敌方
public int currentGroup = 0; // 当前的回合是哪一方的,0是友军,1是敌方
float horizontal; // 移动镜头
float vertical; // 移动镜头
public GameObject turnObject; // 回合UI
public GameObject turnText; // 记录敌我回合的文本框
int enemyNumber; // 关卡中敌人总数
public GameObject myBag; // 背包
bool isOpen; // 背包是否打开
public GameObject gameSettlement; // 结算界面
public bool settlementFlag; // 是否需要结算界面
// 加点界面
public GameObject strAdd;
public GameObject dexAdd;
public GameObject sanAdd;
public GameObject inteAdd;
public GameObject defAdd;
public GameObject vigAdd;
Rigidbody2D rigidbody2d;
public Inventory myInventory;
void Start()
{
SearchPieces();
SetCount();
SetExploreMode();
rigidbody2d = GetComponent<Rigidbody2D>();
if (GetExploreMode()) // **
{
turnObject.SetActive(false); // ****
settlementFlag = false; // **** 初始状态中无敌人时不需要结算界面
enemyNumber = 0; // **** 初始状态敌人数量为0
}
else
{
turnObject.SetActive(true); // ****
settlementFlag = true; // **** 初始状态中有敌人时需要结算界面
enemyNumber = groupCount[1]; // **** 记录初始状态关卡中敌人总数
}
}
void Update()
{
if(!menuActive)
{
CheckIsUsingSkill();
ShowSettlement();
ShowTurn(); // ** 不显示菜单时先判断是否显示敌我回合
ShowBag(); // **** 不显示菜单时可以打开背包
// 选中角色时,镜头移动到角色上,方向键控制角色走动
if (selected != null)
{
// 取消选择
if(Input.GetMouseButtonDown(1))
{
selected.Cancel();
return;
}
MoveCharacter();
// 选中角色在lookDirection方向上与NPC对话
if (exploreMode && Input.GetKeyDown(KeyCode.X))
selected.Dialogue();
// 选中角色在lookDirection方向上进入下一场景
if (exploreMode && selected == maincharacter)
selected.EnterNextScene();
// 选中角色时按f捡起物品
if (Input.GetKeyDown("f"))
selected.Pickup();
if(!exploreMode)
{
// 选中角色时可切换武器
if(Input.GetKeyDown("c"))
selected.GetNextWeapon();
if(Input.GetKeyDown("e"))
selected.weaponManager.SetWeaponsGUI();
if(Input.GetKeyDown("z"))
{
selected.skillManager.SetSkillsGUI();
bool value = !selected.skillManager.GetGameObjectValue();
//selected.weaponManager.gameObject.SetActive(value);
}
if(Input.GetKeyDown("x") && !selected.GetIsUsingSkill())
{
selected.Attack();
}
}
}
}
CallMenu();
}
//移动镜头
void MoveCamera()
{
horizontal = Input.GetAxis("Horizontal");
vertical = Input.GetAxis("Vertical");
Vector2 position = transform.position;
position.x += speed*horizontal*Time.deltaTime;
position.y += speed*vertical*Time.deltaTime;
transform.position = position;
}
// 移动角色
void MoveCharacter()
{
transform.position = selected.transform.position;
// 角色走动
if(Input.GetKeyDown("up"))
{
selected.Move(Vector2.up);
}
if(Input.GetKeyDown("down"))
{
selected.Move(Vector2.down);
}
if(Input.GetKeyDown("left"))
{
selected.Move(Vector2.left);
}
if(Input.GetKeyDown("right"))
{
selected.Move(Vector2.right);
}
// 角色转向
if(Input.GetKeyDown("w"))
{
selected.Turn(Vector2.up);
}
if(Input.GetKeyDown("s"))
{
selected.Turn(Vector2.down);
}
if(Input.GetKeyDown("a"))
{
selected.Turn(Vector2.left);
}
if(Input.GetKeyDown("d"))
{
selected.Turn(Vector2.right);
}
}
// 呼出菜单
void CallMenu()
{
if(Input.GetKeyDown("escape"))
{
menuActive = !menuActive;
menuUI.SetActive(menuActive);
}
}
// 改变选中的棋子
public void SetSelected(Character character)
{
selected = character;
}
// 返回选中的棋子
public Character GetSelected()
{
return selected;
}
// 回合结束
public void TurnEnd()
{
if(exploreMode) return;
if(currentGroup == 0)
currentGroup = 1;
else
currentGroup = 0; //交换回合
foreach (var piece in pieces)
{
if(piece.GetComponent<Character>().groupNumber == currentGroup)
piece.GetComponent<Character>().ReSet();
}
if(selected != null)
selected.Cancel();
}
// 搜寻棋子
public void SearchPieces()
{
pieces = GameObject.FindGameObjectsWithTag("Pieces");
}
// 数敌我双方棋子数
void SetCount()
{
groupCount = new int[3];
foreach (var piece in pieces)
{
Character character = piece.GetComponent<Character>();
groupCount[character.groupNumber] ++ ;
//Debug.Log("gruopCount[" + character.groupNumber + "] = " + groupCount[character.groupNumber]);
}
}
public bool GetExploreMode()
{
return exploreMode;
}
// 当敌人数量为0时状态切换为探索模式
public void SetExploreMode()
{
if(groupCount[1] == 0)
{
selected.Cancel();
exploreMode = true;
Debug.Log("exploreMode");
currentGroup = 0;
foreach (var piece in pieces)
{
piece.GetComponent<Character>().ReSet();
}
}
}
// 战斗结算
void MakeSettlement()
{
for (int i = 0; i < enemyNumber; i++)
{
maincharacter.money += 10;
maincharacter.exp += 10;
while (maincharacter.exp >= maincharacter.levelUp[maincharacter.level - 1])
{
maincharacter.exp -= maincharacter.levelUp[maincharacter.level - 1];
maincharacter.level++;
maincharacter.points += 5;
}
}
}
// 显示结算界面
void ShowSettlement()
{
if (settlementFlag) // **** 初始状态有敌人才需要结算
{
if (exploreMode) // **** 战斗模式结束时显示结算界面
{
MakeSettlement(); // **** 击杀所有敌人后获得经验和金钱
gameSettlement.SetActive(true);
// 是否显示加点系统
if(maincharacter.points > 0)
{
strAdd.SetActive(true);
dexAdd.SetActive(true);
sanAdd.SetActive(true);
inteAdd.SetActive(true);
defAdd.SetActive(true);
vigAdd.SetActive(true);
}
settlementFlag = false; // **** 此场景中不再需要结算界面
}
}
}
void ShowTurn() // **
{ // **
if (exploreMode) // ** 探索模式下不显示敌我回合
turnObject.SetActive(false); // **
else // **
{ // **
if (currentGroup != 0) // **
turnText.GetComponent<TMP_Text>().text = "敌方回合"; // **
else // **
turnText.GetComponent<TMP_Text>().text = "我方回合"; // **
turnObject.SetActive(true); // ** 战斗模式下始终显示敌我回合
} // **
}
void ShowBag() // 是否显示背包
{
isOpen = myBag.activeSelf; // 防止鼠标点击退出背包后按两次I才能再次打开
if(Input.GetKeyDown("i")) // 按I键打开背包
{
isOpen = !isOpen;
myBag.SetActive(isOpen);
InventoryManager.RefreshItem();
}
}
}