Creature.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using RPG_Fight_Test.Assets.Scripts.Weapons;
  5. using UnityEngine;
  6. using UnityEngine.EventSystems;
  7. using UnityEngine.UI;
  8. public class Creature : MonoBehaviour, ICreatureInterface {
  9. [SerializeField] Sprite creatureImage;
  10. [SerializeField] bool humanControlled;
  11. private int init;
  12. private int dex = 0;
  13. private int creatureHealth;
  14. private List<IAction> actions = new List<IAction>();
  15. internal int CurrentHealth { get; set; }
  16. internal int MaxHealth { get; set; }
  17. Weapon weapon;
  18. int health;
  19. private Vector3Int currentPos;
  20. internal IAction GetFirstAction() {
  21. return firstAction;
  22. }
  23. internal IAction GetSecondAction() {
  24. return secondAction;
  25. }
  26. private IAction firstAction;
  27. private IAction secondAction;
  28. public GameObject InitPanelGameObject { set; get; }
  29. internal int movementRate;
  30. private bool active;
  31. public int Dex { get => dex; set => dex = value; }
  32. public int Init { get => init; set => init = value; }
  33. public int CreatureHealth { get => creatureHealth; set => creatureHealth = value; }
  34. public bool IsCreatureAlive { get; set; }
  35. public Vector3Int CurrentPos { get => currentPos; set => currentPos = value; }
  36. public virtual IWeapon PrimaryWeapon { get; set; }
  37. public virtual int Evade { get; set; }
  38. public int GetInit() {
  39. return Init;
  40. }
  41. public IEnumerator WaitForPlayerAction() {
  42. active = true;
  43. while (!HasFirstAction()) {
  44. yield return null;
  45. }
  46. while (!HasSecondAction()) {
  47. yield return null;
  48. }
  49. active = false;
  50. }
  51. private void OnMouseUp() {
  52. Debug.Log("Mouse up on tile or Creature " + this.name);
  53. SetFirstAction(new AttackAction(this, this));
  54. SetSecondAction(new AttackAction(this, this));
  55. }
  56. internal void SetActive(bool v) {
  57. active = v;
  58. }
  59. internal bool HasSecondAction() {
  60. return secondAction != null;
  61. }
  62. public void RollInit() {
  63. Init = Random.Range(0, 100 - dex);
  64. }
  65. public Sprite GetSprite() {
  66. return creatureImage;
  67. }
  68. public bool IsHumanControlled() {
  69. return humanControlled;
  70. }
  71. public void SetFirstAction(IAction action) {
  72. firstAction = action;
  73. }
  74. public void SetSecondAction(IAction action) {
  75. secondAction = action;
  76. }
  77. internal List<Creature> EnemiesInSquareNextToCreature(Creature creature, List<Creature> combatants) {
  78. List<Creature> enemiesClose = new List<Creature>();
  79. foreach (Creature c in combatants) {
  80. Vector3 distance = c.CurrentPos - creature.CurrentPos;
  81. if (c.name != creature.name && distance.x <= 1 && distance.x >= -1 && distance.y <= 1 && distance.y >= -1) {
  82. enemiesClose.Add(c);
  83. }
  84. }
  85. return enemiesClose;
  86. }
  87. internal void SetPositionInGrid(Vector3Int vector3Int) {
  88. CurrentPos = vector3Int;
  89. }
  90. public bool HasFirstAction() {
  91. return firstAction != null;
  92. }
  93. internal Creature FindClosestEnemy(Creature creature, List<Creature> combatants) {
  94. Creature closestEnemy = null;
  95. float minDist = Mathf.Infinity;
  96. List<Creature> enemies = combatants.FindAll(p => p.IsHumanControlled() != creature.IsHumanControlled());
  97. foreach (Creature e in enemies) {
  98. float dist = Vector3.Distance(e.CurrentPos, creature.CurrentPos);
  99. if (dist < minDist) {
  100. closestEnemy = e;
  101. minDist = dist;
  102. }
  103. Debug.Log("Closest enemy is " + closestEnemy.name);
  104. }
  105. return closestEnemy;
  106. }
  107. public void Death() {
  108. IsCreatureAlive = false;
  109. }
  110. public void TakeDamage(int damage) {
  111. CurrentHealth -= damage;
  112. if (CurrentHealth <= 0) {
  113. Death();
  114. }
  115. InitPanelGameObject.GetComponent<InitCreaturePanelScript>().UpdateHealthBar(MaxHealth / CurrentHealth);
  116. }
  117. public virtual IAction ActionWhenBlocked(Creature blockingCreature) {
  118. throw new System.Exception("The Creature must decide this, add it to the specific creature");
  119. }
  120. public virtual void DecideActions(List<Creature> combatants) {
  121. throw new System.Exception("The Creature must decide this, add it to the specific creature");
  122. }
  123. }