Guest User

Untitled

a guest
Jan 21st, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.20 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using UnityEngine.SceneManagement;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.IO;
  8.  
  9. [CreateAssetMenu(fileName = "NewAttack", menuName = "Attacks/Basic - Empty", order = 1)]
  10. public class BasicAttack : AttackTypeBase {
  11.  
  12. [SerializeField, Tooltip("The point of attack based on attacker's position")]
  13. private Vector2Int _AttackPoint;
  14.  
  15. [SerializeField, Tooltip("The range of the attack starting from attack point")]
  16. private int _AttackRange;
  17.  
  18. [SerializeField, Tooltip("Decrease damage over distance")]
  19. private bool _DecreaseDamage;
  20.  
  21. [SerializeField, Tooltip("The base hp change for teammates")]
  22. private int _TeamHealthAdditive;
  23.  
  24. [SerializeField, Tooltip("The base hp change for all other characters")]
  25. private int _EnemyHealthAdditive;
  26.  
  27. [SerializeField, Tooltip("Does the attack affect the attacker himself")]
  28. private bool _AffectsSelf;
  29.  
  30. [SerializeField, Tooltip("Knockback distance in attacking direction for teammates")]
  31. private int _TeamKnockBack;
  32.  
  33. [SerializeField, Tooltip("Knockback distance in attacking direction for enemys")]
  34. private int _EnemyKnockBack;
  35.  
  36. [SerializeField, Tooltip("Does the attacker move to the point of attack (Knockback has to be higher than 0)")]
  37. private bool _TravelToPiont;
  38.  
  39. private Vector2Int _DirectionVector;
  40.  
  41. public override bool Attack(CharacterStats owner) {
  42. if (owner.currentTile.occupant == null || owner.currentTile.occupant == owner) {
  43. Vector2Int pointOfEffect = new Vector2Int(owner.currentTile.position.x + (_AttackPoint.x * _DirectionVector.x), owner.currentTile.position.y + (_AttackPoint.y * _DirectionVector.y));
  44.  
  45. if (pointOfEffect.x >= 0 && pointOfEffect.x <= owner.fieldManager.gridSize.x - 1 && pointOfEffect.y >= 0 && pointOfEffect.y <= owner.fieldManager.gridSize.y - 1) {
  46. List<TileNode> currentLevel = new List<TileNode>();
  47. List<CharacterStats> damaged = new List<CharacterStats>();
  48.  
  49. currentLevel.Add(owner.fieldManager.grid[pointOfEffect.x, pointOfEffect.y]);
  50. if (_AffectsSelf == false) {
  51. damaged.Add(owner);
  52. }
  53.  
  54. if (currentLevel[0].occupant != null && damaged.Contains(currentLevel[0].occupant) == false) {
  55. damaged.Add(currentLevel[0].occupant);
  56.  
  57. if (currentLevel[0].occupant.playSide == owner.playSide) {
  58. currentLevel[0].occupant.health += _TeamHealthAdditive;
  59.  
  60. if (currentLevel[0].occupant != owner && currentLevel[0].position.x >= _TeamKnockBack * -_DirectionVector.x && currentLevel[0].position.x <= (owner.fieldManager.gridSize.x - 1) - (_TeamKnockBack * _DirectionVector.x) && currentLevel[0].position.y >= _TeamKnockBack * -_DirectionVector.y && currentLevel[0].position.y <= (owner.fieldManager.gridSize.y - 1) - (_TeamKnockBack * _DirectionVector.y)) {
  61. if (owner.fieldManager.grid[currentLevel[0].position.x + (_TeamKnockBack * _DirectionVector.x), currentLevel[0].position.y + (_TeamKnockBack * _DirectionVector.y)].occupant == null) {
  62. currentLevel[0].occupant.transform.position = owner.fieldManager.grid[currentLevel[0].position.x + (_TeamKnockBack * _DirectionVector.x), currentLevel[0].position.y + (_TeamKnockBack * _DirectionVector.y)].transform.position;
  63. currentLevel[0].occupant.UpdateTilePosition();
  64. }
  65. }
  66. }
  67.  
  68. else {
  69. currentLevel[0].occupant.health += _EnemyHealthAdditive;
  70.  
  71. if (currentLevel[0].occupant != owner && currentLevel[0].position.x >= _EnemyKnockBack * -_DirectionVector.x && currentLevel[0].position.x <= (owner.fieldManager.gridSize.x - 1) - (_EnemyKnockBack * _DirectionVector.x) && currentLevel[0].position.y >= _EnemyKnockBack * -_DirectionVector.y && currentLevel[0].position.y <= (owner.fieldManager.gridSize.y - 1) - (_EnemyKnockBack * _DirectionVector.y)) {
  72. if (owner.fieldManager.grid[currentLevel[0].position.x + (_EnemyKnockBack * _DirectionVector.x), currentLevel[0].position.y + (_EnemyKnockBack * _DirectionVector.y)].occupant == null) {
  73. currentLevel[0].occupant.transform.position = owner.fieldManager.grid[currentLevel[0].position.x + (_EnemyKnockBack * _DirectionVector.x), currentLevel[0].position.y + (_EnemyKnockBack * _DirectionVector.y)].transform.position;
  74. currentLevel[0].occupant.UpdateTilePosition();
  75. }
  76. }
  77. }
  78. }
  79.  
  80. for (int l = 0; l < _AttackRange; ++l) {
  81. List<TileNode> nextLevel = new List<TileNode>();
  82.  
  83. foreach (TileNode curLink in currentLevel) {
  84. if (curLink.occupant != null && damaged.Contains(curLink.occupant) == false) {
  85. Vector2Int knockbackDirection = curLink.position - pointOfEffect;
  86. knockbackDirection.x = Mathf.Clamp(knockbackDirection.x, -1, 1);
  87. knockbackDirection.y = Mathf.Clamp(knockbackDirection.y, -1, 1);
  88.  
  89. damaged.Add(curLink.occupant);
  90.  
  91. if (curLink.occupant.playSide == owner.playSide) {
  92. curLink.occupant.health += _TeamHealthAdditive - (_TeamHealthAdditive / _AttackRange) * l;
  93.  
  94. if (curLink.occupant != owner && curLink.position.x >= _TeamKnockBack * -knockbackDirection.x && curLink.position.x <= (owner.fieldManager.gridSize.x - 1) - (_TeamKnockBack * knockbackDirection.x) && curLink.position.y >= _TeamKnockBack * -knockbackDirection.y && curLink.position.y <= (owner.fieldManager.gridSize.y - 1) - (_TeamKnockBack * knockbackDirection.y)) {
  95. if (owner.fieldManager.grid[curLink.position.x + (_TeamKnockBack * knockbackDirection.x), curLink.position.y + (_TeamKnockBack * knockbackDirection.y)].occupant == null) {
  96. curLink.occupant.transform.position = owner.fieldManager.grid[curLink.position.x + (_TeamKnockBack * knockbackDirection.x), curLink.position.y + (_TeamKnockBack * knockbackDirection.y)].transform.position;
  97. curLink.occupant.UpdateTilePosition();
  98. }
  99. }
  100. }
  101.  
  102. else {
  103. curLink.occupant.health += _EnemyHealthAdditive - (_EnemyHealthAdditive / _AttackRange) * l;
  104.  
  105. if (curLink.occupant != owner && curLink.position.x >= _EnemyKnockBack * -knockbackDirection.x && curLink.position.x <= (owner.fieldManager.gridSize.x - 1) - (_EnemyKnockBack * knockbackDirection.x) && curLink.position.y >= _EnemyKnockBack * -knockbackDirection.y && curLink.position.y <= (owner.fieldManager.gridSize.y - 1) - (_EnemyKnockBack * knockbackDirection.y)) {
  106. if (owner.fieldManager.grid[curLink.position.x + (_EnemyKnockBack * knockbackDirection.x), curLink.position.y + (_EnemyKnockBack * knockbackDirection.y)].occupant == null) {
  107. curLink.occupant.transform.position = owner.fieldManager.grid[curLink.position.x + (_EnemyKnockBack * knockbackDirection.x), curLink.position.y + (_EnemyKnockBack * knockbackDirection.y)].transform.position;
  108. curLink.occupant.UpdateTilePosition();
  109. }
  110. }
  111. }
  112. }
  113.  
  114. foreach (TileNode nextLink in curLink.links) {
  115. if (nextLink.occupant != null && damaged.Contains(nextLink.occupant) == false) {
  116. Vector2Int knockbackDirection = nextLink.position - pointOfEffect;
  117. knockbackDirection.x = Mathf.Clamp(knockbackDirection.x, -1, 1);
  118. knockbackDirection.y = Mathf.Clamp(knockbackDirection.y, -1, 1);
  119.  
  120. damaged.Add(nextLink.occupant);
  121.  
  122. if (nextLink.occupant.playSide == owner.playSide) {
  123. nextLink.occupant.health += _TeamHealthAdditive - (_TeamHealthAdditive / _AttackRange) * l; ;
  124.  
  125. if (nextLink.occupant != owner && nextLink.position.x >= _TeamKnockBack * -knockbackDirection.x && nextLink.position.x <= (owner.fieldManager.gridSize.x - 1) - (_TeamKnockBack * knockbackDirection.x) && nextLink.position.y >= _TeamKnockBack * -knockbackDirection.y && nextLink.position.y <= (owner.fieldManager.gridSize.y - 1) - (_TeamKnockBack * knockbackDirection.y)) {
  126. if (owner.fieldManager.grid[nextLink.position.x + (_TeamKnockBack * knockbackDirection.x), nextLink.position.y + (_TeamKnockBack * knockbackDirection.y)].occupant == null) {
  127. nextLink.occupant.transform.position = owner.fieldManager.grid[nextLink.position.x + (_TeamKnockBack * knockbackDirection.x), nextLink.position.y + (_TeamKnockBack * knockbackDirection.y)].transform.position;
  128. nextLink.occupant.UpdateTilePosition();
  129. }
  130. }
  131. }
  132.  
  133. else {
  134. nextLink.occupant.health += _EnemyHealthAdditive - (_EnemyHealthAdditive / _AttackRange) * l;
  135.  
  136. if (nextLink.occupant != owner && nextLink.position.x >= _EnemyKnockBack * -knockbackDirection.x && nextLink.position.x <= (owner.fieldManager.gridSize.x - 1) - (_EnemyKnockBack * knockbackDirection.x) && nextLink.position.y >= _EnemyKnockBack * -knockbackDirection.y && nextLink.position.y <= (owner.fieldManager.gridSize.y - 1) - (_EnemyKnockBack * knockbackDirection.y)) {
  137. if (owner.fieldManager.grid[nextLink.position.x + (_EnemyKnockBack * knockbackDirection.x), nextLink.position.y + (_EnemyKnockBack * knockbackDirection.y)].occupant == null) {
  138. nextLink.occupant.transform.position = owner.fieldManager.grid[nextLink.position.x + (_EnemyKnockBack * knockbackDirection.x), nextLink.position.y + (_EnemyKnockBack * knockbackDirection.y)].transform.position;
  139. nextLink.occupant.UpdateTilePosition();
  140. }
  141. }
  142. }
  143. }
  144.  
  145. nextLevel.Add(nextLink);
  146. }
  147. }
  148.  
  149. currentLevel = nextLevel;
  150. }
  151.  
  152. if (_TravelToPiont == true && owner.fieldManager.grid[pointOfEffect.x, pointOfEffect.y].occupant == null) {
  153. owner.transform.position = owner.fieldManager.grid[pointOfEffect.x, pointOfEffect.y].transform.position;
  154. owner.UpdateTilePosition();
  155. }
  156. }
  157.  
  158. return true;
  159. }
  160.  
  161. else {
  162. return false;
  163. }
  164. }
  165.  
  166. public override void UpdateTileFeedback(CharacterStats owner) {
  167. switch (owner.moveDirection) {
  168. case CharacterStats.MoveDirection.Up:
  169. _DirectionVector = new Vector2Int(0, 1);
  170. break;
  171.  
  172. case CharacterStats.MoveDirection.UpRight:
  173. _DirectionVector = new Vector2Int(1, 1);
  174. break;
  175.  
  176. case CharacterStats.MoveDirection.Right:
  177. _DirectionVector = new Vector2Int(1, 0);
  178. break;
  179.  
  180. case CharacterStats.MoveDirection.DownRight:
  181. _DirectionVector = new Vector2Int(1, -1);
  182. break;
  183.  
  184. case CharacterStats.MoveDirection.Down:
  185. _DirectionVector = new Vector2Int(0, -1);
  186. break;
  187.  
  188. case CharacterStats.MoveDirection.DownLeft:
  189. _DirectionVector = new Vector2Int(-1, -1);
  190. break;
  191.  
  192. case CharacterStats.MoveDirection.Left:
  193. _DirectionVector = new Vector2Int(-1, 0);
  194. break;
  195.  
  196. case CharacterStats.MoveDirection.UpLeft:
  197. _DirectionVector = new Vector2Int(-1, 1);
  198. break;
  199. }
  200.  
  201. Vector2Int pointOfEffect = new Vector2Int(owner.currentTile.position.x + (_AttackPoint.x * _DirectionVector.x), owner.currentTile.position.y + (_AttackPoint.y * _DirectionVector.y));
  202.  
  203. if (pointOfEffect.x >= 0 && pointOfEffect.x <= owner.fieldManager.gridSize.x - 1 && pointOfEffect.y >= 0 && pointOfEffect.y <= owner.fieldManager.gridSize.y - 1) {
  204. List<TileNode> currentLevel = new List<TileNode>();
  205. List<TileNode> covered = new List<TileNode>();
  206.  
  207. currentLevel.Add(owner.fieldManager.grid[pointOfEffect.x, pointOfEffect.y]);
  208. if (_AffectsSelf == false) {
  209. covered.Add(owner.currentTile);
  210. }
  211.  
  212. if (covered.Contains(currentLevel[0]) == false) {
  213. covered.Add(currentLevel[0]);
  214. currentLevel[0].tilePhase = TileNode.Phase.Closed;
  215. }
  216.  
  217. for (int l = 0; l < _AttackRange; ++l) {
  218. List<TileNode> nextLevel = new List<TileNode>();
  219.  
  220. foreach (TileNode curLink in currentLevel) {
  221. if (covered.Contains(curLink) == false) {
  222. covered.Add(curLink);
  223. curLink.tilePhase = TileNode.Phase.Closed;
  224. }
  225.  
  226. foreach (TileNode nextLink in curLink.links) {
  227. if (covered.Contains(nextLink) == false) {
  228. covered.Add(nextLink);
  229. nextLink.tilePhase = TileNode.Phase.Closed;
  230. }
  231.  
  232. nextLevel.Add(nextLink);
  233. }
  234. }
  235.  
  236. currentLevel = nextLevel;
  237. }
  238. }
  239. }
  240. }
Add Comment
Please, Sign In to add comment