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.

96 lines
3.0 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. return Block.GetOrCreateBlockAtPosition(wantedPosition, 1, layerMask);
  41. //If block at Position is walkable set it to moveTo
  42. if (Block.isBlockAtPosition(wantedPosition, 1, layerMask, out hit) && hit.isWalkable(layerMask))
  43. {
  44. retVal = hit;
  45. }
  46. //else if block down one is walkable
  47. else if (Block.isBlockAtPosition(wantedPosition + Vector3.down, 1, layerMask, out hit) && hit.isWalkable(layerMask))
  48. {
  49. //and it isn't obstructed
  50. if (Block.isBlockAtPosition(wantedPosition + Vector3.up, 1, layerMask, out hit))
  51. retVal = hit;
  52. }
  53. return retVal;
  54. }
  55. public override void CopyToken(BlockToken token)
  56. {
  57. base.CopyToken(token);
  58. direction = ((DirectionToken)token).direction;
  59. }
  60. public override BlockToken ToToken(BlockToken token = null)
  61. {
  62. if (token == null)
  63. token = new DirectionToken(this);
  64. DirectionToken retVal = (DirectionToken) base.ToToken(token);
  65. retVal.direction = direction;
  66. return retVal;
  67. }
  68. #endregion Class Functions
  69. }
  70. [System.Serializable]
  71. public class DirectionToken : BlockToken
  72. {
  73. public Direction direction;
  74. public DirectionToken(LogicBlock block) : base(block) { }
  75. }