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.

90 lines
2.9 KiB

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