using System.Collections;
using System.Collections.Generic;
using UnityEngine;
///
/// Logic block which deals with jumping a character in a direction
///
[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
///
/// Implementation of BlockLogic, moves the player forward
///
/// Player to move
protected override void BlockLogic(Character player, float animationTime)
{
player.Jump(direction, animationTime);
}
///
/// Returns the block that the character will endUp on after they use this logic element
///
/// block character is on
/// layers to ignore
/// block which character will finish on after performing this function
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 hit; //output of block detection
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;
//If block at Position is walkable set it to last known position
if (Block.isBlockAtPosition(position, 1, layerMask, out hit) && hit.isWalkable(layerMask))
retVal = hit;
//else if block down one is walkable
else if (Block.isBlockAtPosition(position + Vector3.down, 1, layerMask, out hit) && hit.isWalkable(layerMask))
retVal = hit;
}
return retVal;
}
///
/// Jumps up obstacle of specific height. Jumps as high as possible
///
/// Direction of obstacle
/// max height
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
}