|
|
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
-
- /// <summary>
- /// Logic block which deals with jumping a character in a direction
- /// </summary>
- [CreateAssetMenu(menuName = "Major Project/Jump Block")]
- [System.Serializable]
- public class Jump : LogicBlock
- {
- [SerializeField]
- [Tooltip("Direction to Jump")]
- protected Direction direction = Direction.Forward;
-
- [SerializeField]
- [Tooltip("Max distance Jump can go")]
- protected int Distance = 2;
-
- [SerializeField]
- [Tooltip("Max height Jump can go")]
- protected int Height = 1;
-
- #region Class Functions
- /// <summary>
- /// Implementation of BlockLogic, moves the player forward
- /// </summary>
- /// <param name="player">Player to move</param>
- protected override IEnumerator BlockLogic(Character player, float animationTime, bool useBlockDirection = false)
- {
- player.justMoved = true;
-
- List<Block> jumpBlocks = new List<Block>(); //Blocks to jump forward between
- List<Block> bounceBlocks = new List<Block>(); //Blocks to bounce backward across
-
- //Debug.Log("<b>" + player.name + " jumping from " + player.transform.position + "<\b>");
-
- Block endBlock = GetEndBlock(player.CurrentBlock, player.transform, ~player.Ignore);
- //Debug.Log("Initial end block: " + endBlock.transform.position);
-
- jumpBlocks.Add(endBlock);
-
- while (endBlock.CurrentPlayer != null && endBlock.CurrentPlayer != player)
- {
- //Debug.Log("End block occupied! Time to bounce");
- Block oldEndBlock = endBlock;
- //Debug.Log("oldEndBlock = " + oldEndBlock.transform.position);
- endBlock = GetEndBlock(endBlock, player.transform, ~player.Ignore);
- //Debug.Log("new endBlock = " + endBlock.transform.position);
- if (endBlock == oldEndBlock) //Make sure we're not bouncing on the spot
- {
- //Debug.Log("We are bouncing in place, stop here");
- break;
- }
- //Debug.Log("Added block to list");
- jumpBlocks.Add(endBlock);
- }
-
- //If we've ended on a block containing another player, we'll need to bounce backwards until we find an open square
- while (endBlock.CurrentPlayer != null && endBlock.CurrentPlayer != player)
- {
- //Debug.Log("Block at " + endBlock.transform.position + " still occupied, we must bounce back");
- Vector3 position = endBlock.position + (Direction.Back.ToVector(player.transform));
- endBlock = Block.GetOrCreateBlockAtPosition(position, 1, ~player.Ignore);
- //Debug.Log("New endBlock = " + endBlock.transform.position);
- bounceBlocks.Add(endBlock);
- }
-
- //Jump forward, bouncing across every player
- //If there are multi-jumps to be done, they'll happen here
- for (int i = 0; i < jumpBlocks.Count - 1; i++)
- {
- //Debug.Log("Jumping to: " + jumpBlocks[i].transform.position);
- yield return player.StartCoroutine(player.AnimateToPosition(jumpBlocks[i].VisualPosition + Vector3.up * 0.5f, Character.Animation.Jump, 0.8f));
- }
-
- //If we're at the last queued jump and there are no bouncebacks in the queue, then we've reached an open square, and can officially move here
- if (bounceBlocks.Count == 0)
- {
- //Debug.Log("Final jump to: " + endBlock.transform.position);
- yield return player.StartCoroutine(player.MoveToBlock(endBlock, Character.Animation.Jump, animationTime));
- }
- else //Otherwise, we have to bounce back first
- {
- yield return player.StartCoroutine(player.AnimateToPosition(jumpBlocks[jumpBlocks.Count - 1].VisualPosition + Vector3.up * 0.5f, Character.Animation.Jump, 0.8f));
- for (int i = 0; i < bounceBlocks.Count - 1; i++)
- {
- //Debug.Log("Bouncing to: " + bounceBlocks[i].transform.position);
- yield return player.StartCoroutine(player.AnimateToPosition(bounceBlocks[i].VisualPosition + Vector3.up * 0.5f, Character.Animation.Jump, 0.8f));
- }
- //Debug.Log("Final bounce to: " + endBlock.transform.position);
- yield return player.StartCoroutine(player.MoveToBlock(endBlock, Character.Animation.Jump, animationTime));
- }
-
- /*while (endBlock.CurrentPlayer != null && endBlock.CurrentPlayer != player)
- {
- yield return player.StartCoroutine(player.AnimateToPosition(endBlock.VisualPosition + Vector3.up * 0.5f, Character.Animation.Jump, 0.8f));
- endBlock.CurrentPlayer.StartAnimation(Character.Animation.Hit);
- endBlock = GetEndBlock(endBlock, player.transform, ~player.Ignore);
- }
- yield return player.StartCoroutine(player.MoveToBlock(endBlock, Character.Animation.Jump, animationTime));*/
- }
-
- /// <summary>
- /// Returns the block that the character will endUp on after they use this logic element
- /// </summary>
- /// <param name="startBlock">block character is on</param>
- /// <param name="layerMask">layers to ignore</param>
- /// <returns>block which character will finish on after performing this function </returns>
- public override Block GetEndBlock(Block startBlock, Transform transform, LayerMask layerMask)
- {
- //if there is a block infront JumpUp else LongJump
- if (Block.isBlockAtPosition(startBlock.position + direction.ToVector(transform) + Vector3.up, 1, layerMask))
- return JumpUp(startBlock, transform, layerMask);
- else
- return JumpLong(startBlock, transform, layerMask);
- }
- #endregion Class Functions
-
- #region Helper Functions
- private Block JumpLong(Block StartBlock, Transform transform, LayerMask layerMask)
- {
- //setting up variables
- Vector3 position; // position wanted
- Block retVal = StartBlock; //block we'll actually move to
-
- //Check blocks in front until we hit an obstruction or went the distance
- for (int i = 1; i <= Distance; i++)
- {
- //Next position to MoveTo
- position = StartBlock.position + (direction.ToVector(transform) * i);
-
- //if jump is obstructed, stop and go to last known block
- if (Block.isBlockAtPosition(position + Vector3.up, 1, layerMask))
- break;
-
- retVal = Block.GetOrCreateBlockAtPosition(position, 1, layerMask);
- }
-
- return retVal;
- }
-
- /// <summary>
- /// Jumps up obstacle of specific height. Jumps as high as possible
- /// </summary>
- /// <param name="direction">Direction of obstacle</param>
- /// <param name="height">max height</param>
- private Block JumpUp(Block StartBlock, Transform transform, LayerMask layerMask)
- {
- //setting up variables
- Vector3 position; // position wanted
- Block hit; //output of block detection
- Block retVal = StartBlock; //block we'll actually move to
-
- //Check blocks in going up then move to the heighest walkable one
- for (int i = 0; i <= Height; i++)
- {
- //next position up the tower
- position = StartBlock.position + direction.ToVector(transform) + (Vector3.up * i);
-
- //if block is walkable set it to last known position
- if (Block.isBlockAtPosition(position, 1, layerMask, out hit) && hit.isWalkable(layerMask))
- retVal = hit;
- }
- return retVal;
- }
- #endregion Helper Functions
-
- #region Serialisation functions
- public override void CopyToken(BlockToken token)
- {
- base.CopyToken(token);
- direction = ((DirectionToken)token).direction;
- }
-
- public override BlockToken ToToken(BlockToken token = null)
- {
- if (token == null)
- token = new DirectionToken(this);
-
- DirectionToken retVal = (DirectionToken)base.ToToken(token);
- retVal.direction = direction;
-
- return retVal;
- }
- #endregion Serialisation functions
- }
|