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.
 
 
 
 
 
 

149 lines
5.0 KiB

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.Jump(direction, animationTime);
player.justMoved = true;
Block endBlock = GetEndBlock(player.CurrentBlock, player.transform, ~player.Ignore);
while(endBlock.CurrentPlayer != null)
{
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 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;
}
/// <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
}