From abd602756be5d1317b48a7007f503b21bc9c9702 Mon Sep 17 00:00:00 2001 From: "s3607057 (Angus Niven)" Date: Wed, 6 Nov 2019 19:51:20 +1100 Subject: [PATCH] Rewrote the jumping logic to avoid infinite loops when jumping onto a character in front of a wall - characters will now jump back to the first open square available. --- Assets/Scripts/Logic/Blocks/Jump.cs | 66 ++++++++++++++++++++++++++++- 1 file changed, 64 insertions(+), 2 deletions(-) diff --git a/Assets/Scripts/Logic/Blocks/Jump.cs b/Assets/Scripts/Logic/Blocks/Jump.cs index eb27d4d..5bb0d97 100644 --- a/Assets/Scripts/Logic/Blocks/Jump.cs +++ b/Assets/Scripts/Logic/Blocks/Jump.cs @@ -1,4 +1,5 @@ using System.Collections; +using System.Collections.Generic; using UnityEngine; /// @@ -28,15 +29,76 @@ public class Jump : LogicBlock protected override IEnumerator BlockLogic(Character player, float animationTime, bool useBlockDirection = false) { player.justMoved = true; + + List jumpBlocks = new List(); //Blocks to jump forward between + List bounceBlocks = new List(); //Blocks to bounce backward across + + //Debug.Log("" + 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) + /*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)); + yield return player.StartCoroutine(player.MoveToBlock(endBlock, Character.Animation.Jump, animationTime));*/ } ///