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.
 
 
 
 
 
 

93 lines
2.9 KiB

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// Logic block which deals with moving a character in a direction
/// </summary>
[CreateAssetMenu(menuName = "Major Project/Move Block")]
[System.Serializable]
public class Move : LogicBlock
{
[SerializeField]
protected Direction direction = Direction.Forward;
#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;
Block endBlock;
if (useBlockDirection)
endBlock = GetEndBlock(player.CurrentBlock, player.CurrentBlock.transform, ~player.Ignore);
else
endBlock = GetEndBlock(player.CurrentBlock, player.transform, ~player.Ignore);
yield return player.StartCoroutine(player.MoveToBlock(endBlock, Character.Animation.Walk, 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></returns>
public override Block GetEndBlock(Block startBlock,Transform tranform, LayerMask layerMask)
{
DebugExtensions.DrawCube(startBlock.position, Vector3.one / 2, Color.red, 3, false);
Vector3 wantedPosition = startBlock.position + direction.ToVector(tranform); // position wanted
Block hit; //output of block detection
Block retVal = startBlock; //Block we'll move to
//If block at Position is walkable set it to moveTo
if (Block.isBlockAtPosition(wantedPosition, 1, layerMask, out hit) && hit.isWalkable(layerMask))
{
retVal = hit;
}
//else if block down one is walkable
else if (Block.isBlockAtPosition(wantedPosition + Vector3.down, 1, layerMask, out hit) && hit.isWalkable(layerMask))
{
//and it isn't obstructed
if (Block.isBlockAtPosition(wantedPosition + Vector3.up, 1, layerMask, out hit))
retVal = hit;
}
return retVal;
}
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 Class Functions
}
[System.Serializable]
public class DirectionToken : BlockToken
{
public Direction direction;
public DirectionToken(LogicBlock block) : base(block) { }
}