public void StartMove(PlatformMoveType type, int units) { switch (type) { case PlatformMoveType.AxisX: { Vector3 target = transform.position + units * Vector3.right * gridSystem.cellSize; IsMoving = true; StartCoroutine(MoveCoroutine(target, type)); //else break; } case PlatformMoveType.AxisZ: { Vector3 target = transform.position + units * Vector3.forward * gridSystem.cellSize; IsMoving = true; StartCoroutine(MoveCoroutine(target, type)); //else break; } default: throw new NotImplementedException(); } }
private IEnumerator MoveCoroutine(Vector3 target, PlatformMoveType type) { sound.StartMoveSound(); while (Vector3.Distance(transform.position, target) > truncateDist) { transform.position = Vector3.SmoothDamp(transform.position, target, ref currVelocity, transitionTime); yield return(null); } transform.position = target; IsMoving = false; sound.endMoveSound(); }
public void StartMoveGroup(PlatformMoveType type, Int2 playerIdx) { if (gridSystem.ExistMovingGroup) { return; } int units = ComputeMoveUnits(type, playerIdx); //HashSet<PlatformGroup> colGroups = ComputeNeighborGroups(); if (units == 0) { HashSet <PlatformGroup> colGroups = ComputeNeighborGroups(); colGroups.Add(this); PlatformGroup.Combine(colGroups, playerIdx); return; } gridSystem.ExistMovingGroup = true; StartCoroutine(MoveGroupCoroutine(type, units, playerIdx)); }
private IEnumerator MoveCoroutine(Vector3 target, PlatformMoveType type) { sound.StartMoveSound(); while (Vector3.Distance(transform.position, target) > truncateDist) { transform.position = Vector3.SmoothDamp(transform.position, target, ref currVelocity, transitionTime); yield return null; } transform.position = target; IsMoving = false; sound.endMoveSound(); }
//private private IEnumerator MoveGroupCoroutine(PlatformMoveType type, int units, Int2 playerIdx) { foreach (Platform pf in container) pf.StartMove(type, units); bool waitAll = true; while (waitAll) { waitAll = false; foreach (Platform pf in container) { if (pf.IsMoving) waitAll = true; } yield return null; } //maintain gridSystem foreach (Platform pf in container) gridSystem[pf.index] = null; switch (type) { case PlatformMoveType.AxisX: { foreach (Platform pf in container) { pf.index._x += units; gridSystem[pf.index] = pf; } break; } case PlatformMoveType.AxisZ: { foreach (Platform pf in container) { pf.index._z += units; gridSystem[pf.index] = pf; } break; } default: throw new NotImplementedException(); } //trigger combination HashSet<PlatformGroup> colGroups = ComputeNeighborGroups(); colGroups.Add(this); //should update player's platform group here PlatformGroup.Combine(colGroups, playerIdx); //consider using event instead //LevelController.platformMoved = true; if (OnGroupMoved != null) OnGroupMoved(); gridSystem.ExistMovingGroup = false; }
//private private int ComputeMoveUnits(PlatformMoveType type, Int2 playerIdx) { //theoretically shouldn't generate out of bound exception Int2 lower,upper; ComputeAABB(out lower, out upper); switch (type) { case PlatformMoveType.AxisX: { if (playerIdx._x >= lower._x && playerIdx._x <= upper._x) return 0; else if (playerIdx._x < lower._x) { //player is at left, move left then //find the leftmost platforms from THIS group on each z int[] thisLeftmostIdx = new int[upper._z - lower._z + 1]; for (int i = 0; i < thisLeftmostIdx.Length; i++) thisLeftmostIdx[i] = upper._x; foreach (Platform pf in container) { if (thisLeftmostIdx[pf.index._z - lower._z] > pf.index._x) thisLeftmostIdx[pf.index._z - lower._z] = pf.index._x; } //find the rightmost platform from OTHER group on each z (if any) int[] otherRightmostIdx = new int[upper._z - lower._z + 1]; for (int i = 0; i < otherRightmostIdx.Length; i++) { otherRightmostIdx[i] = playerIdx._x - 1; } for (int z = 0; z < upper._z - lower._z + 1; z++) { for (int x = thisLeftmostIdx[z] - 1; x >= playerIdx._x; x--) { if (gridSystem[x, z + lower._z] != null) { otherRightmostIdx[z] = x; break; } } } //now determine the moving units of the longest possible move //record groups that this will collide with (find all min values) int maxUnit = lower._x - playerIdx._x; int[] candidates = new int[upper._z - lower._z + 1]; for (int i = 0; i < upper._z - lower._z + 1; i++) { candidates[i] = thisLeftmostIdx[i] - otherRightmostIdx[i] - 1; if ( candidates[i] < maxUnit) maxUnit = candidates[i]; } return - maxUnit; //negative since we are moving left } else { //player is at left, move right then //find the rightmost platforms from THIS group on each z int[] thisRightmostIdx = new int[upper._z - lower._z + 1]; for (int i = 0; i < thisRightmostIdx.Length; i++) thisRightmostIdx[i] = lower._x; foreach (Platform pf in container) { if (thisRightmostIdx[pf.index._z - lower._z] < pf.index._x) thisRightmostIdx[pf.index._z - lower._z] = pf.index._x; } //find the leftmost platforms from OTHER groups on each z (if any) int[] otherLeftmostIdx = new int[upper._z - lower._z + 1]; for (int i = 0; i < otherLeftmostIdx.Length; i++) { otherLeftmostIdx[i] = playerIdx._x + 1; } for (int z = 0; z < upper._z - lower._z + 1; z++) { for (int x = thisRightmostIdx[z] + 1; x <= playerIdx._x; x++) { if (gridSystem[x, z + lower._z] != null) { otherLeftmostIdx[z] = x; break; } } } //now determine the moving units of the longest possible move //record groups that this will collide with (find all min values) int maxUnit = playerIdx._x - upper._x; int[] candidates = new int[upper._z - lower._z + 1]; for (int i = 0; i < upper._z - lower._z + 1; i++) { candidates[i] = otherLeftmostIdx[i] - thisRightmostIdx[i] - 1; if (candidates[i] < maxUnit) maxUnit = candidates[i]; } return maxUnit; } } case PlatformMoveType.AxisZ: { if (playerIdx._z >= lower._z && playerIdx._z <= upper._z) return 0; else if (playerIdx._z < lower._z) { //player is at back, move back then //find the backmost(?) platforms of THIS group on each x int[] thisBackmostIdx = new int[upper._x - lower._x + 1]; for (int i = 0; i < thisBackmostIdx.Length; i++) thisBackmostIdx[i] = upper._z; foreach (Platform pf in container) { if (pf.index._z < thisBackmostIdx[pf.index._x - lower._x]) thisBackmostIdx[pf.index._x - lower._x] = pf.index._z; } //find the foremost platforms of OTHER groups on each x if any int[] otherForemostIdx = new int[upper._x - lower._x + 1]; for (int i = 0; i < otherForemostIdx.Length; i++) { otherForemostIdx[i] = playerIdx._z - 1; } for (int x = 0; x < thisBackmostIdx.Length; x++) { for (int z = thisBackmostIdx[x] - 1; z >= playerIdx._z; z--) { if (gridSystem[x + lower._x, z] != null) { otherForemostIdx[x] = z; break; } } } //now determine the moving units of the longest possible move //record groups that this will collide with (find all min values) int maxUnit = lower._z - playerIdx._z; int[] candidates = new int[upper._x - lower._x + 1]; for (int i = 0; i < upper._x - lower._x + 1; i++) { candidates[i] = thisBackmostIdx[i] - otherForemostIdx[i] - 1; if (candidates[i] < maxUnit) maxUnit = candidates[i]; } return - maxUnit; //negative since we are moving back } else { //player is in front, move front then //find the foremost platform of THIS group on each x int[] thisForemostIdx = new int[upper._x - lower._x + 1]; for (int i = 0; i < thisForemostIdx.Length; i++) thisForemostIdx[i] = lower._z; foreach (Platform pf in container) { if (thisForemostIdx[pf.index._x - lower._x] < pf.index._z) thisForemostIdx[pf.index._x - lower._x] = pf.index._z; } //find the backmost platform of OTHER group on each x if any int[] otherBackmostIdx = new int[upper._x - lower._x + 1]; for (int i = 0; i < otherBackmostIdx.Length; i++) { otherBackmostIdx[i] = playerIdx._z + 1; } for (int x = 0; x < thisForemostIdx.Length; x++) { for (int z = thisForemostIdx[x] + 1; z <= playerIdx._z; z++) { if (gridSystem[x + lower._x, z] != null) { otherBackmostIdx[x] = z; break; } } } //now determine the moving units of the longest possible move int maxUnit = playerIdx._z - upper._z; int[] candidates = new int[upper._x - lower._x + 1]; for (int i = 0; i < upper._x - lower._x + 1; i++) { candidates[i] = otherBackmostIdx[i] - thisForemostIdx[i] - 1; if (candidates[i] < maxUnit) maxUnit = candidates[i]; } return maxUnit; } } default:throw new NotImplementedException(); } }
public void StartMoveGroup(PlatformMoveType type, Int2 playerIdx) { if (gridSystem.ExistMovingGroup) return; int units = ComputeMoveUnits(type, playerIdx); //HashSet<PlatformGroup> colGroups = ComputeNeighborGroups(); if (units == 0) { HashSet<PlatformGroup> colGroups = ComputeNeighborGroups(); colGroups.Add(this); PlatformGroup.Combine(colGroups, playerIdx); return; } gridSystem.ExistMovingGroup = true; StartCoroutine(MoveGroupCoroutine(type, units, playerIdx)); }
//private private IEnumerator MoveGroupCoroutine(PlatformMoveType type, int units, Int2 playerIdx) { foreach (Platform pf in container) { pf.StartMove(type, units); } bool waitAll = true; while (waitAll) { waitAll = false; foreach (Platform pf in container) { if (pf.IsMoving) { waitAll = true; } } yield return(null); } //maintain gridSystem foreach (Platform pf in container) { gridSystem[pf.index] = null; } switch (type) { case PlatformMoveType.AxisX: { foreach (Platform pf in container) { pf.index._x += units; gridSystem[pf.index] = pf; } break; } case PlatformMoveType.AxisZ: { foreach (Platform pf in container) { pf.index._z += units; gridSystem[pf.index] = pf; } break; } default: throw new NotImplementedException(); } //trigger combination HashSet <PlatformGroup> colGroups = ComputeNeighborGroups(); colGroups.Add(this); //should update player's platform group here PlatformGroup.Combine(colGroups, playerIdx); //consider using event instead //LevelController.platformMoved = true; if (OnGroupMoved != null) { OnGroupMoved(); } gridSystem.ExistMovingGroup = false; }
//private private int ComputeMoveUnits(PlatformMoveType type, Int2 playerIdx) { //theoretically shouldn't generate out of bound exception Int2 lower, upper; ComputeAABB(out lower, out upper); switch (type) { case PlatformMoveType.AxisX: { if (playerIdx._x >= lower._x && playerIdx._x <= upper._x) { return(0); } else if (playerIdx._x < lower._x) { //player is at left, move left then //find the leftmost platforms from THIS group on each z int[] thisLeftmostIdx = new int[upper._z - lower._z + 1]; for (int i = 0; i < thisLeftmostIdx.Length; i++) { thisLeftmostIdx[i] = upper._x; } foreach (Platform pf in container) { if (thisLeftmostIdx[pf.index._z - lower._z] > pf.index._x) { thisLeftmostIdx[pf.index._z - lower._z] = pf.index._x; } } //find the rightmost platform from OTHER group on each z (if any) int[] otherRightmostIdx = new int[upper._z - lower._z + 1]; for (int i = 0; i < otherRightmostIdx.Length; i++) { otherRightmostIdx[i] = playerIdx._x - 1; } for (int z = 0; z < upper._z - lower._z + 1; z++) { for (int x = thisLeftmostIdx[z] - 1; x >= playerIdx._x; x--) { if (gridSystem[x, z + lower._z] != null) { otherRightmostIdx[z] = x; break; } } } //now determine the moving units of the longest possible move //record groups that this will collide with (find all min values) int maxUnit = lower._x - playerIdx._x; int[] candidates = new int[upper._z - lower._z + 1]; for (int i = 0; i < upper._z - lower._z + 1; i++) { candidates[i] = thisLeftmostIdx[i] - otherRightmostIdx[i] - 1; if (candidates[i] < maxUnit) { maxUnit = candidates[i]; } } return(-maxUnit); //negative since we are moving left } else { //player is at left, move right then //find the rightmost platforms from THIS group on each z int[] thisRightmostIdx = new int[upper._z - lower._z + 1]; for (int i = 0; i < thisRightmostIdx.Length; i++) { thisRightmostIdx[i] = lower._x; } foreach (Platform pf in container) { if (thisRightmostIdx[pf.index._z - lower._z] < pf.index._x) { thisRightmostIdx[pf.index._z - lower._z] = pf.index._x; } } //find the leftmost platforms from OTHER groups on each z (if any) int[] otherLeftmostIdx = new int[upper._z - lower._z + 1]; for (int i = 0; i < otherLeftmostIdx.Length; i++) { otherLeftmostIdx[i] = playerIdx._x + 1; } for (int z = 0; z < upper._z - lower._z + 1; z++) { for (int x = thisRightmostIdx[z] + 1; x <= playerIdx._x; x++) { if (gridSystem[x, z + lower._z] != null) { otherLeftmostIdx[z] = x; break; } } } //now determine the moving units of the longest possible move //record groups that this will collide with (find all min values) int maxUnit = playerIdx._x - upper._x; int[] candidates = new int[upper._z - lower._z + 1]; for (int i = 0; i < upper._z - lower._z + 1; i++) { candidates[i] = otherLeftmostIdx[i] - thisRightmostIdx[i] - 1; if (candidates[i] < maxUnit) { maxUnit = candidates[i]; } } return(maxUnit); } } case PlatformMoveType.AxisZ: { if (playerIdx._z >= lower._z && playerIdx._z <= upper._z) { return(0); } else if (playerIdx._z < lower._z) { //player is at back, move back then //find the backmost(?) platforms of THIS group on each x int[] thisBackmostIdx = new int[upper._x - lower._x + 1]; for (int i = 0; i < thisBackmostIdx.Length; i++) { thisBackmostIdx[i] = upper._z; } foreach (Platform pf in container) { if (pf.index._z < thisBackmostIdx[pf.index._x - lower._x]) { thisBackmostIdx[pf.index._x - lower._x] = pf.index._z; } } //find the foremost platforms of OTHER groups on each x if any int[] otherForemostIdx = new int[upper._x - lower._x + 1]; for (int i = 0; i < otherForemostIdx.Length; i++) { otherForemostIdx[i] = playerIdx._z - 1; } for (int x = 0; x < thisBackmostIdx.Length; x++) { for (int z = thisBackmostIdx[x] - 1; z >= playerIdx._z; z--) { if (gridSystem[x + lower._x, z] != null) { otherForemostIdx[x] = z; break; } } } //now determine the moving units of the longest possible move //record groups that this will collide with (find all min values) int maxUnit = lower._z - playerIdx._z; int[] candidates = new int[upper._x - lower._x + 1]; for (int i = 0; i < upper._x - lower._x + 1; i++) { candidates[i] = thisBackmostIdx[i] - otherForemostIdx[i] - 1; if (candidates[i] < maxUnit) { maxUnit = candidates[i]; } } return(-maxUnit); //negative since we are moving back } else { //player is in front, move front then //find the foremost platform of THIS group on each x int[] thisForemostIdx = new int[upper._x - lower._x + 1]; for (int i = 0; i < thisForemostIdx.Length; i++) { thisForemostIdx[i] = lower._z; } foreach (Platform pf in container) { if (thisForemostIdx[pf.index._x - lower._x] < pf.index._z) { thisForemostIdx[pf.index._x - lower._x] = pf.index._z; } } //find the backmost platform of OTHER group on each x if any int[] otherBackmostIdx = new int[upper._x - lower._x + 1]; for (int i = 0; i < otherBackmostIdx.Length; i++) { otherBackmostIdx[i] = playerIdx._z + 1; } for (int x = 0; x < thisForemostIdx.Length; x++) { for (int z = thisForemostIdx[x] + 1; z <= playerIdx._z; z++) { if (gridSystem[x + lower._x, z] != null) { otherBackmostIdx[x] = z; break; } } } //now determine the moving units of the longest possible move int maxUnit = playerIdx._z - upper._z; int[] candidates = new int[upper._x - lower._x + 1]; for (int i = 0; i < upper._x - lower._x + 1; i++) { candidates[i] = otherBackmostIdx[i] - thisForemostIdx[i] - 1; if (candidates[i] < maxUnit) { maxUnit = candidates[i]; } } return(maxUnit); } } default: throw new NotImplementedException(); } }