|
@ -7,7 +7,7 @@ using UnityEngine.SceneManagement; |
|
|
public class Character : MonoBehaviour |
|
|
public class Character : MonoBehaviour |
|
|
{ |
|
|
{ |
|
|
public string nextScene; |
|
|
public string nextScene; |
|
|
Animator characterAnimator; |
|
|
|
|
|
|
|
|
Animator characterAnimator; |
|
|
|
|
|
|
|
|
#region Inspector Fields
|
|
|
#region Inspector Fields
|
|
|
|
|
|
|
|
@ -27,6 +27,9 @@ public class Character : MonoBehaviour |
|
|
[Tooltip("Character to display")] |
|
|
[Tooltip("Character to display")] |
|
|
private string CharacterModel = "Bear"; |
|
|
private string CharacterModel = "Bear"; |
|
|
|
|
|
|
|
|
|
|
|
[SerializeField] |
|
|
|
|
|
private TMPro.TextMeshPro BlockTitlePrefab; |
|
|
|
|
|
|
|
|
#endregion Inspector Fields
|
|
|
#endregion Inspector Fields
|
|
|
|
|
|
|
|
|
#region Read Only
|
|
|
#region Read Only
|
|
@ -38,7 +41,7 @@ public class Character : MonoBehaviour |
|
|
private void Start() |
|
|
private void Start() |
|
|
{ |
|
|
{ |
|
|
if (Inventory != null && CloneInventoryOnStart) |
|
|
if (Inventory != null && CloneInventoryOnStart) |
|
|
Inventory = Inventory.Clone(Inventory); |
|
|
|
|
|
|
|
|
Inventory = Inventory.Clone(Inventory); |
|
|
|
|
|
|
|
|
//If no starting block find one below it
|
|
|
//If no starting block find one below it
|
|
|
if (_currentBlock == null) |
|
|
if (_currentBlock == null) |
|
@ -54,74 +57,87 @@ public class Character : MonoBehaviour |
|
|
characterAnimator = GetComponentInChildren<Animator>(); |
|
|
characterAnimator = GetComponentInChildren<Animator>(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endregion Unity Functions
|
|
|
#endregion Unity Functions
|
|
|
|
|
|
|
|
|
#region Class Implementation
|
|
|
#region Class Implementation
|
|
|
|
|
|
|
|
|
public void Initialise(Block startingBlock, Inventory inventory, string Character) { |
|
|
|
|
|
|
|
|
public void Initialise(Block startingBlock, Inventory inventory, string Character) |
|
|
|
|
|
{ |
|
|
_currentBlock = startingBlock; |
|
|
_currentBlock = startingBlock; |
|
|
Inventory = inventory; |
|
|
Inventory = inventory; |
|
|
CharacterModel = Character; |
|
|
CharacterModel = Character; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public void DisplayBlock(LogicBlock block) |
|
|
|
|
|
{ |
|
|
|
|
|
if (BlockTitlePrefab == null) |
|
|
|
|
|
return; |
|
|
|
|
|
|
|
|
|
|
|
TMPro.TextMeshPro temp = Instantiate(BlockTitlePrefab.gameObject).GetComponent<TMPro.TextMeshPro>(); |
|
|
|
|
|
temp.text = block.DisplayName; |
|
|
|
|
|
temp.color = block.Color; |
|
|
|
|
|
|
|
|
|
|
|
temp.transform.position = transform.position + (Vector3.one * 0.25f); |
|
|
|
|
|
temp.transform.rotation = Quaternion.LookRotation(temp.transform.position - Camera.main.transform.position); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
public Vector3 yValue(float time, float heightMultiplier) |
|
|
public Vector3 yValue(float time, float heightMultiplier) |
|
|
{ |
|
|
{ |
|
|
float y = Mathf.Sin((Mathf.PI*time)) * heightMultiplier; |
|
|
|
|
|
|
|
|
float y = Mathf.Sin((Mathf.PI * time)) * heightMultiplier; |
|
|
return new Vector3(0, y, 0); |
|
|
return new Vector3(0, y, 0); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
IEnumerator JumpCoroutine(Block Target, Transform Current, float time, float heightMax) |
|
|
|
|
|
{ |
|
|
|
|
|
float elapsedTime = 0; |
|
|
|
|
|
Vector3 startPosition = Current.position; |
|
|
|
|
|
time *= 0.8f; |
|
|
|
|
|
|
|
|
IEnumerator JumpCoroutine(Block Target, Transform Current, float time, float heightMax) |
|
|
|
|
|
{ |
|
|
|
|
|
float elapsedTime = 0; |
|
|
|
|
|
Vector3 startPosition = Current.position; |
|
|
|
|
|
time *= 0.8f; |
|
|
characterAnimator.Play("Jump Inplace"); |
|
|
characterAnimator.Play("Jump Inplace"); |
|
|
yield return new WaitForSeconds(0.15f); |
|
|
yield return new WaitForSeconds(0.15f); |
|
|
while (elapsedTime < time) |
|
|
while (elapsedTime < time) |
|
|
{ |
|
|
|
|
|
|
|
|
{ |
|
|
transform.position = Vector3.Lerp(startPosition, Target.VisualPosition, (elapsedTime / time)); |
|
|
transform.position = Vector3.Lerp(startPosition, Target.VisualPosition, (elapsedTime / time)); |
|
|
transform.position += yValue((elapsedTime / time), heightMax); |
|
|
|
|
|
yield return new WaitForEndOfFrame(); |
|
|
|
|
|
elapsedTime += Time.deltaTime; |
|
|
|
|
|
} |
|
|
|
|
|
transform.position = Target.VisualPosition; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
transform.position += yValue((elapsedTime / time), heightMax); |
|
|
|
|
|
yield return new WaitForEndOfFrame(); |
|
|
|
|
|
elapsedTime += Time.deltaTime; |
|
|
|
|
|
} |
|
|
|
|
|
transform.position = Target.VisualPosition; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
IEnumerator MoveCoroutine(Block Target, Transform Current, float time, float heightMax) |
|
|
IEnumerator MoveCoroutine(Block Target, Transform Current, float time, float heightMax) |
|
|
{ |
|
|
{ |
|
|
float elapsedTime = 0; |
|
|
float elapsedTime = 0; |
|
|
Vector3 startPosition = Current.position; |
|
|
Vector3 startPosition = Current.position; |
|
|
time *= 0.8f; |
|
|
time *= 0.8f; |
|
|
characterAnimator.Play("Walk"); |
|
|
|
|
|
yield return new WaitForSeconds(0.05f); |
|
|
|
|
|
|
|
|
characterAnimator.Play("Walk"); |
|
|
|
|
|
yield return new WaitForSeconds(0.05f); |
|
|
while (elapsedTime < time) |
|
|
while (elapsedTime < time) |
|
|
{ |
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
transform.position = Vector3.Lerp(startPosition, Target.VisualPosition, (elapsedTime / time)); |
|
|
transform.position = Vector3.Lerp(startPosition, Target.VisualPosition, (elapsedTime / time)); |
|
|
yield return new WaitForEndOfFrame(); |
|
|
|
|
|
elapsedTime += Time.deltaTime; |
|
|
|
|
|
|
|
|
yield return new WaitForEndOfFrame(); |
|
|
|
|
|
elapsedTime += Time.deltaTime; |
|
|
} |
|
|
} |
|
|
transform.position = Target.VisualPosition; |
|
|
transform.position = Target.VisualPosition; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
IEnumerator MoveDownCoroutine(Block Target, Transform Current, float time, float heightMax) |
|
|
|
|
|
{ |
|
|
|
|
|
float elapsedTime = 0; |
|
|
|
|
|
Vector3 startPosition = Current.position; |
|
|
|
|
|
time *= 0.8f; |
|
|
|
|
|
characterAnimator.Play("Walk"); |
|
|
|
|
|
while (elapsedTime < time) |
|
|
|
|
|
{ |
|
|
|
|
|
transform.position = Vector3.Lerp(startPosition, Target.VisualPosition, (elapsedTime / time)); |
|
|
|
|
|
transform.position += yValue((elapsedTime / time), heightMax); |
|
|
|
|
|
yield return new WaitForEndOfFrame(); |
|
|
|
|
|
elapsedTime += Time.deltaTime; |
|
|
|
|
|
} |
|
|
|
|
|
transform.position = Target.VisualPosition; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
IEnumerator MoveDownCoroutine(Block Target, Transform Current, float time, float heightMax) |
|
|
|
|
|
{ |
|
|
|
|
|
float elapsedTime = 0; |
|
|
|
|
|
Vector3 startPosition = Current.position; |
|
|
|
|
|
time *= 0.8f; |
|
|
|
|
|
characterAnimator.Play("Walk"); |
|
|
|
|
|
while (elapsedTime < time) |
|
|
|
|
|
{ |
|
|
|
|
|
transform.position = Vector3.Lerp(startPosition, Target.VisualPosition, (elapsedTime / time)); |
|
|
|
|
|
transform.position += yValue((elapsedTime / time), heightMax); |
|
|
|
|
|
yield return new WaitForEndOfFrame(); |
|
|
|
|
|
elapsedTime += Time.deltaTime; |
|
|
|
|
|
} |
|
|
|
|
|
transform.position = Target.VisualPosition; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
IEnumerator rotateCoroutine(Direction direction, Transform Current, float time, float heightMax) |
|
|
IEnumerator rotateCoroutine(Direction direction, Transform Current, float time, float heightMax) |
|
|
{ |
|
|
{ |
|
@ -132,7 +148,7 @@ public class Character : MonoBehaviour |
|
|
Vector3 startPosition = transform.position; |
|
|
Vector3 startPosition = transform.position; |
|
|
while (elapsedTime < time) |
|
|
while (elapsedTime < time) |
|
|
{ |
|
|
{ |
|
|
characterAnimator.Play("Jump"); |
|
|
|
|
|
|
|
|
characterAnimator.Play("Jump"); |
|
|
transform.forward = Vector3.Slerp(startDirection, endDirection, (elapsedTime / time)); |
|
|
transform.forward = Vector3.Slerp(startDirection, endDirection, (elapsedTime / time)); |
|
|
yield return new WaitForEndOfFrame(); |
|
|
yield return new WaitForEndOfFrame(); |
|
|
elapsedTime += Time.deltaTime; |
|
|
elapsedTime += Time.deltaTime; |
|
@ -147,7 +163,7 @@ public class Character : MonoBehaviour |
|
|
/// <param name="direction">direction to walk</param>
|
|
|
/// <param name="direction">direction to walk</param>
|
|
|
/// <remarks>Technically is same as JumpLong(1) but kept seperate to avoid confusion</remarks>
|
|
|
/// <remarks>Technically is same as JumpLong(1) but kept seperate to avoid confusion</remarks>
|
|
|
public void Move(Direction direction, float speed) |
|
|
public void Move(Direction direction, float speed) |
|
|
{ |
|
|
|
|
|
|
|
|
{ |
|
|
//setting up variables
|
|
|
//setting up variables
|
|
|
Vector3 position = _currentBlock.position + direction.ToVector(transform); // position wanted
|
|
|
Vector3 position = _currentBlock.position + direction.ToVector(transform); // position wanted
|
|
|
Block hit; //output of block detection
|
|
|
Block hit; //output of block detection
|
|
@ -159,16 +175,19 @@ public class Character : MonoBehaviour |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//If block at Position is walkable set it to moveTo
|
|
|
//If block at Position is walkable set it to moveTo
|
|
|
if (Block.isBlockAtPosition(position, 1, ~Ignore, out hit) && hit.isWalkable(~Ignore)){ |
|
|
|
|
|
|
|
|
if (Block.isBlockAtPosition(position, 1, ~Ignore, out hit) && hit.isWalkable(~Ignore)) |
|
|
|
|
|
{ |
|
|
moveTo = hit; |
|
|
moveTo = hit; |
|
|
_currentBlock = moveTo; |
|
|
|
|
|
StartCoroutine(MoveCoroutine(_currentBlock, transform, speed, 0.3f)); } |
|
|
|
|
|
|
|
|
_currentBlock = moveTo; |
|
|
|
|
|
StartCoroutine(MoveCoroutine(_currentBlock, transform, speed, 0.3f)); |
|
|
|
|
|
} |
|
|
//else if block down one is walkable
|
|
|
//else if block down one is walkable
|
|
|
else if (Block.isBlockAtPosition(position + Vector3.down, 1, ~Ignore, out hit) && hit.isWalkable(~Ignore)){ |
|
|
|
|
|
|
|
|
else if (Block.isBlockAtPosition(position + Vector3.down, 1, ~Ignore, out hit) && hit.isWalkable(~Ignore)) |
|
|
|
|
|
{ |
|
|
moveTo = hit; |
|
|
moveTo = hit; |
|
|
_currentBlock = moveTo; |
|
|
|
|
|
StartCoroutine(MoveDownCoroutine(_currentBlock, transform, speed, 0.3f)); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
_currentBlock = moveTo; |
|
|
|
|
|
StartCoroutine(MoveDownCoroutine(_currentBlock, transform, speed, 0.3f)); |
|
|
|
|
|
} |
|
|
//set current block && move
|
|
|
//set current block && move
|
|
|
//CurrentBlock = moveTo;
|
|
|
//CurrentBlock = moveTo;
|
|
|
//StartCoroutine(MoveCoroutine(CurrentBlock, transform, speed, 0.3f));
|
|
|
//StartCoroutine(MoveCoroutine(CurrentBlock, transform, speed, 0.3f));
|
|
@ -176,10 +195,10 @@ public class Character : MonoBehaviour |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
/// <summary>
|
|
|
/// Upon collision with a floating block, collect its
|
|
|
|
|
|
/// Upon collision with the end portal, end of level
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="other">name of collided object</param>
|
|
|
|
|
|
|
|
|
/// Upon collision with a floating block, collect its
|
|
|
|
|
|
/// Upon collision with the end portal, end of level
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="other">name of collided object</param>
|
|
|
void OnTriggerEnter(Collider other) |
|
|
void OnTriggerEnter(Collider other) |
|
|
{ |
|
|
{ |
|
|
Collectable collectable = other.GetComponentInChildren<Collectable>(); |
|
|
Collectable collectable = other.GetComponentInChildren<Collectable>(); |
|
@ -188,11 +207,12 @@ public class Character : MonoBehaviour |
|
|
{ |
|
|
{ |
|
|
collectable.OnCollect(this); |
|
|
collectable.OnCollect(this); |
|
|
} |
|
|
} |
|
|
if (other.gameObject.name == "collect_sphere"){ |
|
|
|
|
|
other.gameObject.SetActive(false); |
|
|
|
|
|
//player.collected +=1;
|
|
|
|
|
|
|
|
|
if (other.gameObject.name == "collect_sphere") |
|
|
|
|
|
{ |
|
|
|
|
|
other.gameObject.SetActive(false); |
|
|
|
|
|
//player.collected +=1;
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
if (other.gameObject.name == "End Portal") |
|
|
if (other.gameObject.name == "End Portal") |
|
|
{ |
|
|
{ |
|
|
other.GetComponent<Collider>().enabled = false; |
|
|
other.GetComponent<Collider>().enabled = false; |
|
@ -249,10 +269,10 @@ public class Character : MonoBehaviour |
|
|
if (Block.isBlockAtPosition(position, 1, ~Ignore, out hit) && hit.isWalkable(~Ignore)) |
|
|
if (Block.isBlockAtPosition(position, 1, ~Ignore, out hit) && hit.isWalkable(~Ignore)) |
|
|
moveTo = hit; |
|
|
moveTo = hit; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//set current block && move
|
|
|
//set current block && move
|
|
|
_currentBlock = moveTo; |
|
|
_currentBlock = moveTo; |
|
|
StartCoroutine(JumpCoroutine(_currentBlock, transform, speed, 0.5f)); |
|
|
|
|
|
|
|
|
StartCoroutine(JumpCoroutine(_currentBlock, transform, speed, 0.5f)); |
|
|
//transform.position = CurrentBlock.VisualPosition;
|
|
|
//transform.position = CurrentBlock.VisualPosition;
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -287,10 +307,10 @@ public class Character : MonoBehaviour |
|
|
moveTo = hit; |
|
|
moveTo = hit; |
|
|
|
|
|
|
|
|
}//end for
|
|
|
}//end for
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//Set Current Block and move
|
|
|
//Set Current Block and move
|
|
|
_currentBlock = moveTo; |
|
|
_currentBlock = moveTo; |
|
|
StartCoroutine(JumpCoroutine(_currentBlock, transform, speed, 0.5f)); |
|
|
|
|
|
|
|
|
StartCoroutine(JumpCoroutine(_currentBlock, transform, speed, 0.5f)); |
|
|
//transform.position = CurrentBlock.VisualPosition;
|
|
|
//transform.position = CurrentBlock.VisualPosition;
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|