private void AssignNewHead() { if (_centipedeTailsList.Count <= 0) { GameClient.Get <ICentipedeManager>().RemoveCentipede(this); _centipedeHead = null; return; } HorizontalDirection horizontalDirection = _centipedeHead.HorizontalDirection; VerticalDirection verticalDirection = _centipedeHead.VerticalDirection; // make next tail to new head CentipedeTail tail = _centipedeTailsList[0]; tail.GetTailObject().name = "Head_" + CentipedeManager.CentipedeCount; _centipedeHead = new CentipedeHead(); _centipedeHead.Init(); _centipedeHead.SetNewHeadGameObject(tail.GetTailObject(), tail.LastPosition, horizontalDirection, verticalDirection); // remove this tail from tail list, because its a head now _centipedeTailsList.Remove(tail); //change indexing according to new head for (int i = 0; i < _centipedeTailsList.Count; i++) { _centipedeTailsList[i].AssignNewLeader(i == 0 ? (IBodyPart)_centipedeHead : _centipedeTailsList[i - 1], i); _centipedeTailsList[i].GetTailObject().name = "Head_" + CentipedeManager.CentipedeCount + "_tail_" + i; } }
public void DestroyCentipedeBodyPart(IntVector2 position) { if (_centipedeHead != null) { GameObject headGameObject = _centipedeHead.GetHeadGameObject(); IntVector2 headPosition = new IntVector2((int)headGameObject.transform.position.x, (int)headGameObject.transform.position.y); if (headPosition == position) { Object.Destroy(headGameObject); _levelManager.ClearCell(position); AssignNewHead(); } } //Debug.LogWarning("list count = " + _centipedeTailsList.Count); for (int i = _centipedeTailsList.Count - 1; i >= 0; i--) { //Debug.Log("index = " + i); CentipedeTail tail = _centipedeTailsList[i]; GameObject tailGameObject = tail.GetTailObject(); IntVector2 tailPosition = new IntVector2((int)tailGameObject.transform.position.x, (int)tailGameObject.transform.position.y); if (tailPosition == position) { _levelManager.ClearCell(position); if (i == _centipedeTailsList.Count - 1) { Object.Destroy(tailGameObject); _centipedeTailsList.Remove(tail); } else { Object.Destroy(tailGameObject); DivideCentipedeFrom(tail, tailPosition); } break; } } }
private void DivideCentipedeFrom(CentipedeTail destroyedTail, IntVector2 tailPosition) { //spawn mushroom at destroyed tail position GameClient.Get <IMushroomManager>().SpawnMushroom(tailPosition); //create new head CentipedeTail tail = _centipedeTailsList[destroyedTail.TailIndex + 1]; Vector2 lastPosition = tail.LastPosition; GameObject newHeadGameObject = tail.GetTailObject(); // tails List <CentipedeTail> remainingTail = new List <CentipedeTail>(); int index = destroyedTail.TailIndex + 2; for (int i = index; i < _centipedeTailsList.Count; i++) { remainingTail.Add(_centipedeTailsList[i]); } // remove from old tail list for (int i = 0; i < remainingTail.Count; i++) { CentipedeTail oldTail = _centipedeTailsList.Find(x => x == remainingTail[i]); if (oldTail != null) { _centipedeTailsList.Remove(oldTail); } } _centipedeTailsList.Remove(destroyedTail); _centipedeTailsList.Remove(tail); // create new centipede GameClient.Get <ICentipedeManager>().CreateNewCentipede(newHeadGameObject, lastPosition, remainingTail, _centipedeHead.HorizontalDirection, _centipedeHead.VerticalDirection); }