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.

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