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.

88 lines
2.8 KiB

5 years ago
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)
  19. {
  20. player.justMoved = true;
  21. //player.Move(direction, animationTime);
  22. Block endBlock = GetEndBlock(player.CurrentBlock, player.transform, ~player.Ignore);
  23. yield return player.StartCoroutine(player.MoveToBlock(endBlock, Character.Animation.Walk, animationTime));
  24. }
  25. /// <summary>
  26. /// Returns the block that the character will endUp on after they use this logic element
  27. /// </summary>
  28. /// <param name="startBlock">block character is on</param>
  29. /// <param name="layerMask">layers to ignore</param>
  30. /// <returns></returns>
  31. public override Block GetEndBlock(Block startBlock,Transform tranform, LayerMask layerMask)
  32. {
  33. DebugExtensions.DrawCube(startBlock.position, Vector3.one / 2, Color.red, 3, false);
  34. Vector3 wantedPosition = startBlock.position + direction.ToVector(tranform); // position wanted
  35. Block hit; //output of block detection
  36. Block retVal = startBlock; //Block we'll move to
  37. //If block at Position is walkable set it to moveTo
  38. if (Block.isBlockAtPosition(wantedPosition, 1, layerMask, out hit) && hit.isWalkable(layerMask))
  39. {
  40. retVal = hit;
  41. }
  42. //else if block down one is walkable
  43. else if (Block.isBlockAtPosition(wantedPosition + Vector3.down, 1, layerMask, out hit) && hit.isWalkable(layerMask))
  44. {
  45. //and it isn't obstructed
  46. if (Block.isBlockAtPosition(wantedPosition + Vector3.up, 1, layerMask, out hit))
  47. retVal = hit;
  48. }
  49. return retVal;
  50. }
  51. public override void CopyToken(BlockToken token)
  52. {
  53. base.CopyToken(token);
  54. direction = ((DirectionToken)token).direction;
  55. }
  56. public override BlockToken ToToken(BlockToken token = null)
  57. {
  58. if (token == null)
  59. token = new DirectionToken(this);
  60. DirectionToken retVal = (DirectionToken) base.ToToken(token);
  61. retVal.direction = direction;
  62. return retVal;
  63. }
  64. #endregion Class Functions
  65. }
  66. [System.Serializable]
  67. public class DirectionToken : BlockToken
  68. {
  69. public Direction direction;
  70. public DirectionToken(LogicBlock block) : base(block) { }
  71. }