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.

77 lines
2.5 KiB

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