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.

83 lines
2.6 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. if (!Block.isBlockAtPosition(wantedPosition + Vector3.up, 1, layerMask))
  41. return Block.GetOrCreateBlockAtPosition(wantedPosition, 1, layerMask);
  42. else
  43. return startBlock;
  44. }
  45. public override void CopyToken(BlockToken token)
  46. {
  47. base.CopyToken(token);
  48. direction = ((DirectionToken)token).direction;
  49. }
  50. public override BlockToken ToToken(BlockToken token = null)
  51. {
  52. if (token == null)
  53. token = new DirectionToken(this);
  54. DirectionToken retVal = (DirectionToken) base.ToToken(token);
  55. retVal.direction = direction;
  56. return retVal;
  57. }
  58. #endregion Class Functions
  59. }
  60. [System.Serializable]
  61. public class DirectionToken : BlockToken
  62. {
  63. public Direction direction;
  64. public DirectionToken(LogicBlock block) : base(block) { }
  65. }