|
|
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; //记录选中的棋子
|
|
|
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; // ** 敌我回合的文本框
|
|
|
|
|
|
public GameObject myBag; // **** 背包
|
|
|
bool isOpen; // **** 背包是否打开
|
|
|
|
|
|
void Start()
|
|
|
{
|
|
|
SearchPieces();
|
|
|
SetCount();
|
|
|
if (GetExploreMode()) // **
|
|
|
turnObject.SetActive(false); // ** 探索模式下不显示敌我回合
|
|
|
else // **
|
|
|
turnObject.SetActive(true); // **
|
|
|
}
|
|
|
void Update()
|
|
|
{
|
|
|
if(!menuActive)
|
|
|
{
|
|
|
ShowTurn(); // ** 不显示菜单时先判断是否显示敌我回合
|
|
|
ShowBag(); // **** 不显示菜单时可以打开背包
|
|
|
//选中角色时,镜头移动到角色上,方向键控制角色走动
|
|
|
if (selected != null)
|
|
|
{
|
|
|
//取消选择
|
|
|
if(Input.GetMouseButtonDown(1))
|
|
|
{
|
|
|
selected.Cancel();
|
|
|
}
|
|
|
MoveCharacter();
|
|
|
//选中角色在lookDirection方向上与NPC对话
|
|
|
if (exploreMode && Input.GetKeyDown(KeyCode.X))
|
|
|
selected.Dialogue();
|
|
|
// **** 按x捡起物品
|
|
|
if (Input.GetKeyDown("x"))
|
|
|
selected.Pickup();
|
|
|
//选中角色时可切换武器
|
|
|
if(Input.GetKeyDown("c"))
|
|
|
selected.GetNextWeapon();
|
|
|
}
|
|
|
//未选中角色时,方向键控制镜头移动
|
|
|
else
|
|
|
MoveCamera();
|
|
|
}
|
|
|
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 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(); // ****
|
|
|
}
|
|
|
}
|
|
|
}
|