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.

93 lines
2.9 KiB

5 years ago
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. /// <summary>
  5. /// Logic block which deals with moving a character in a direction
  6. /// </summary>
  7. [CreateAssetMenu(menuName = "Major Project/Move Block")]
  8. [System.Serializable]
  9. public class Move : LogicBlock
  10. {
  11. [SerializeField]
  12. protected Direction direction = Direction.Forward;
  13. #region Class Functions
  14. /// <summary>
  15. /// Implementation of BlockLogic, moves the player forward
  16. /// </summary>
  17. /// <param name="player">Player to move</param>
  18. protected override IEnumerator BlockLogic(Character player, float animationTime, bool useBlockDirection = false)
  19. {
  20. player.justMoved = true;
  21. Block endBlock;
  22. if (useBlockDirection)
  23. endBlock = GetEndBlock(player.CurrentBlock, player.CurrentBlock.transform, ~player.Ignore);
  24. else
  25. endBlock = GetEndBlock(player.CurrentBlock, player.transform, ~player.Ignore);
  26. yield return player.StartCoroutine(player.MoveToBlock(endBlock, Character.Animation.Walk, animationTime));
  27. }
  28. /// <summary>
  29. /// Returns the block that the character will endUp on after they use this logic element
  30. /// </summary>
  31. /// <param name="startBlock">block character is on</param>
  32. /// <param name="layerMask">layers to ignore</param>
  33. /// <returns></returns>
  34. public override Block GetEndBlock(Block startBlock,Transform tranform, LayerMask layerMask)
  35. {
  36. DebugExtensions.DrawCube(startBlock.position, Vector3.one / 2, Color.red, 3, false);
  37. Vector3 wantedPosition = startBlock.position + direction.ToVector(tranform); // position wanted
  38. Block hit; //output of block detection
  39. Block retVal = startBlock; //Block we'll move to
  40. //If block at Position is walkable set it to moveTo
  41. if (Block.isBlockAtPosition(wantedPosition, 1, layerMask, out hit) && hit.isWalkable(layerMask))
  42. {
  43. retVal = hit;
  44. }
  45. //else if block down one is walkable
  46. else if (Block.isBlockAtPosition(wantedPosition + Vector3.down, 1, layerMask, out hit) && hit.isWalkable(layerMask))
  47. {
  48. //and it isn't obstructed
  49. if (Block.isBlockAtPosition(wantedPosition + Vector3.up, 1, layerMask, out hit))
  50. retVal = hit;
  51. }
  52. return retVal;
  53. }
  54. public override void CopyToken(BlockToken token)
  55. {
  56. base.CopyToken(token);
  57. direction = ((DirectionToken)token).direction;
  58. }
  59. public override BlockToken ToToken(BlockToken token = null)
  60. {
  61. if (token == null)
  62. token = new DirectionToken(this);
  63. DirectionToken retVal = (DirectionToken) base.ToToken(token);
  64. retVal.direction = direction;
  65. return retVal;
  66. }
  67. #endregion Class Functions
  68. }
  69. [System.Serializable]
  70. public class DirectionToken : BlockToken
  71. {
  72. public Direction direction;
  73. public DirectionToken(LogicBlock block) : base(block) { }
  74. }