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.

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