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.

239 lines
10 KiB

using System.Net.Mime;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro; // **
public class Character : MonoBehaviour
{
//角色等级
int level = 1;
//角色ID
int id = 0;
//角色阵营序列号
public int groupNumber;
//角色可加点属性
int str = 1;
int dex = 1;
int san = 1;
int inte = 1;
int def = 1;
int vig = 1;
//角色衍生属性
int maxHealth;
int maxPoise;
int maxMovement ;
int currentHealth;
int currentMovement;
int currentPoise;
//调用其他类
WeaponManager weaponManager;
CharacterUIManager characterUIManager;
MovementBar movementBar;
GameObject currentWeaponObject;
Weapon currentWeapon;
GameManager gameManager;
Animator animator;
Vector2 lookDirection = new Vector2(1, 0); // ** 初始化lookDirection
//初始化
void Start()
{
animator = GetComponent<Animator>();
gameManager = GameObject.Find("GameManager").GetComponent<GameManager>();
//初始化角色战斗属性
maxMovement = (int)(dex * 2 + san * 2 + 8);
maxHealth = 10 + vig * 10;
maxPoise = (int)(str * 4 + vig * 2);
currentMovement = maxMovement;
currentHealth = maxHealth;
currentPoise = maxPoise;
//初始化角色UI
characterUIManager = transform.GetChild(1).GetComponent<CharacterUIManager>();
movementBar = transform.GetChild(1).GetChild(2).GetComponent<MovementBar>();
characterUIManager.ChangePoints(0, 1.0f, 0);
characterUIManager.ChangePoints(0, 1.0f, 1);
movementBar.ChangeMovement(0, currentMovement, 1);
//初始化角色位置
RaycastHit2D hit = Physics2D.Raycast(transform.position,Vector2.up,1.0f,LayerMask.GetMask("Cells"));
hit.collider.GetComponent<Cell>().SetOccupied(true);
hit.collider.GetComponent<Cell>().SetCurrentCharacter(this);
//初始化角色武器
weaponManager = transform.GetChild(0).GetComponent<WeaponManager>();
}
//鼠标点击角色
private void OnMouseDown()
{
if(gameManager.GetSelected() != null)
gameManager.GetSelected().Cancel();
if(currentMovement >= 0)
Selected();
}
//选中角色
public void Selected()
{
if(gameManager.currentGroup != groupNumber || currentMovement < 1)
return;
animator.SetFloat("selected", 1);
gameManager.SetSelected(gameObject.GetComponent<Character>());
if(!gameManager.GetExploreMode())
{
weaponManager.SetCurrnetWeaponActive(true);
movementBar.SetMovementActive(true);
}
}
//取消选中角色
public void Cancel()
{
animator.SetFloat("selected", 0);
gameManager.SetSelected(null);
weaponManager.SetCurrnetWeaponActive(false);
if(!gameManager.GetExploreMode())
movementBar.SetMovementActive(false);
}
//移动角色
public void Move(Vector2 deviation)
{
//角色朝向
Turn(deviation);
//判断角色是否能够前进
Vector2 temp = new Vector2(deviation.x + transform.position.x, deviation.y + transform.position.y);
RaycastHit2D[] hits = Physics2D.RaycastAll(transform.position,deviation,1.0f,LayerMask.GetMask("Cells"));
if(hits.Length <= 1)
{
Debug.Log("Not Accessed");
}
else
{
Cell currentCell = hits[0].collider.GetComponent<Cell>();
Cell cell = hits[1].collider.GetComponent<Cell>();
if(cell.GetOccupied()) Debug.Log("Not Accessed");
else if(temp.x == cell.transform.position.x && temp.y == cell.transform.position.y)
{
//判断行动点数是否够用
if(currentMovement >= cell.cost)
{
if(!gameManager.GetExploreMode())
ChangeMovement(-cell.cost);
//改变棋子所占用的单元格
transform.position = cell.transform.position;
currentCell.SetOccupied(false);
currentCell.SetCurrentCharacter(null);
cell.SetCurrentCharacter(this);
cell.SetOccupied(true);
}
else
{
OutOfMovement();
}
//陷阱地形对角色造成伤害
if(cell.damage > 0)
ChangeHealth(-cell.damage);
}
}
}
//调节生命值,在血量减少或回复时调用
public void ChangeHealth(int amount)
{
currentHealth = Mathf.Clamp(currentHealth + amount, 0 ,maxHealth);
characterUIManager.ChangePoints(amount,currentHealth / (float)maxHealth, 0); //生命值改变后血条显示改变
}
//调节韧性点数
public void ChangePoise(int amount)
{
currentPoise = Mathf.Clamp(currentPoise + amount, 0 ,maxPoise);
characterUIManager.ChangePoints(amount,currentPoise / (float)maxPoise, 1); //韧性值改变后韧性条显示改变
}
//调节行动点数
public void ChangeMovement(int amount)
{
currentMovement += amount;
movementBar.ChangeMovement(amount,currentMovement,currentMovement / (float)maxMovement);
}
//结束回合时重制角色行动点数
public void ReSet()
{
if(currentPoise <= 0)
{
currentPoise = maxPoise;
characterUIManager.ChangePoints(0, 1, 1);
currentMovement = (int)(maxMovement * 0.5);
}
else
currentMovement = maxMovement;
ChangeMovement(0);
}
//获取当前行动点数
public int GetCurrentMovement()
{
return currentMovement;
}
//获取当前生命值
public int GetCurrentHealth()
{
return currentHealth;
}
//角色转向
public void Turn(Vector2 direction)
{
lookDirection = direction;
weaponManager.weapons[weaponManager.currentIndex].SetRotation(direction);
animator.SetFloat("lookX", direction.x);
animator.SetFloat("lookY", direction.y);
}
public void Dialogue()
{
Vector2 position = new Vector2(transform.position.x, transform.position.y); // ** 以Vector2形式保存当前位置 // **
RaycastHit2D hit = Physics2D.Raycast(position + Vector2.up * 0.2f,
lookDirection, 1.5f,LayerMask.GetMask("NPC")); // ** 判断是否接触NPC
if (hit.collider != null) // **
{ // **
NonPlayerCharacter character = hit.collider.GetComponent<NonPlayerCharacter>(); // **
if (character != null) // **
{ // **
character.DisplayDialog(); // **
} // ** // **
} // **
}
//显示行动点不足 // **
public void OutOfMovement()
{
characterUIManager.OutOfMovement();
}
//切换武器
public void GetNextWeapon()
{
weaponManager.weapons[weaponManager.currentIndex].gameObject.SetActive(false);
weaponManager.currentIndex += 1;
weaponManager.currentIndex %= 2;
weaponManager.weapons[weaponManager.currentIndex].gameObject.SetActive(true);
weaponManager.weapons[weaponManager.currentIndex].SetRotation(lookDirection);
}
private void OnDestroy()
{
//Debug.Log("OnDestroy was called");
RaycastHit2D hit = Physics2D.Raycast(transform.position,Vector2.up,1.0f,LayerMask.GetMask("Cells"));
hit.collider.GetComponent<Cell>().SetOccupied(false);
gameManager.SearchPieces();
gameManager.groupCount[groupNumber] -= 1;
gameManager.SetExploreMode();
}
// **** 捡起物品
public void Pickup()
{
Vector2 position = new Vector2(transform.position.x, transform.position.y); // **** 以Vector2形式保存当前位置 // **
RaycastHit2D hit = Physics2D.Raycast(position + Vector2.up * 0.2f,
lookDirection, 1.5f, LayerMask.GetMask("Item")); // **** 判断是否接触物品
if (hit.collider != null) // ****
{ // ****
ItemInMap item = hit.collider.GetComponent<ItemInMap>(); // ****
if (item != null) // ****
{ // ****
item.AddNewItem(); // ****
} // **** // **
} // ****
}
}