// // Create a list of stacks with each animal in its own stack // private void InitialiseStacks() { // Create stacks of each animal levelStacks = new List<AnimalStack> (); for(int i = 0; i < gameAnimals.Count; i++) { // Create a stack and add an animal to it AnimalStack temp = new AnimalStack(); gameAnimals[i].GetComponent<AnimalBehaviour>().SetParentStack(temp,0); temp.Add(gameAnimals[i]); levelStacks.Add(temp); } // Initialise index stackIndex = 0; animalIndex = 0; // Quick access to current stack/animal currentStack = levelStacks[stackIndex]; currentAnimal = levelStacks[stackIndex].Get(animalIndex); // Activate the animal currentAnimal.GetComponent<AnimalBehaviour>().Activate(); // Get height of the animals animalHeight = currentAnimal.transform.localScale.y; }
void Start () { CreateGrid (); // Create animals and assign them to their respective stacks AnimalStack stack1 = new AnimalStack (); GameObject animal1 = CreateAnimal ("Animal1", 2, 3, Color.yellow, stack1); AnimalStack stack2 = new AnimalStack (); GameObject animal2 = CreateAnimal ("Animal2", 9, 8, Color.green, stack2); AnimalStack stack3 = new AnimalStack (); GameObject animal3 = CreateAnimal ("Animal3", 5, 2, Color.red, stack3); AnimalStack stack4 = new AnimalStack (); GameObject animal4 = CreateAnimal ("Animal4", 1, 7, Color.blue, stack4); AnimalStack shepherdStack = new AnimalStack (); GameObject shepherd = CreateShepherd ("Shepherd", 6, 7, new Color(230.0f/255.0f, 160.0f/255.0f, 0.0f), shepherdStack); // Add all stacks to the stacks list stacks = new List<AnimalStack> (); stacks.Add (stack1); stacks.Add (stack2); stacks.Add (stack3); stacks.Add (stack4); stacks.Add (shepherdStack); currentStack = stacks [0]; stackIndex = 0; animalIndex = 0; currentAnimal = stacks [stackIndex].animals [animalIndex]; throwingMode = false; // Set up shaders diffuse = Shader.Find ("Legacy Shaders/Diffuse Fast"); outlined = Shader.Find ("Outlined/Silhouetted Diffuse"); // Highlight first animal UpdateHighlight (); // Set up throwing range lists throwingBoxes = new List<GameObject>[4]; for(int i=0; i<4; i++){ throwingBoxes[i] = new List<GameObject>(); } }
void SwitchAnimal (bool goingUp) { // Switching up if (goingUp) { do { // Check if there are moveable animals remaining in current stack if (animalIndex < currentStack.animals.Count - 1) { animalIndex++; } // Otherwise, choose new stack else { // Start at beginning of next stack animalIndex = 0; // If at end of stacks list, return to beginning of list if (stackIndex == stacks.Count - 1) { stackIndex = 0; } // Otherwise, choose next stack in list else { stackIndex++; } // Update current stack currentStack = stacks [stackIndex]; } } while(stacks[stackIndex].animals[animalIndex].name.Equals("Shepherd")); } // Switching down else { do { // Check if there are moveable animals remaining in current stack if (animalIndex > 0) { animalIndex--; } // Otherwise, choose new stack else { // If at beginning of stacks list, return to end of list if (stackIndex == 0) { stackIndex = stacks.Count - 1; } // Otherwise, choose next stack in list else { stackIndex--; } // Update current stack currentStack = stacks [stackIndex]; // Start at top of stack animalIndex = currentStack.animals.Count - 1; } } while(stacks[stackIndex].animals[animalIndex].name.Equals("Shepherd")); } lastAnimal = currentAnimal; currentAnimal = currentStack.animals [animalIndex]; // Highlight currently selected animal UpdateHighlight (); if(throwingMode){ // Every animal starts in movement mode throwingMode = false; ClearThrowingBoxes(); UpdateHighlight(); } }
// Throw animals towards target block public void ThrowAnimals (){ Vector3 movement = throwTarget.transform.localPosition - currentAnimal.transform.localPosition; movement.y = 0; // Heigh changes calculated in TransferAnimals() AnimalStack targetStack = StackAtTarget (movement); if (targetStack == null) { targetStack = new AnimalStack (); stacks.Add (targetStack); } lastAnimal = currentAnimal; // Move current animal to be up one in the stack, as stack being thrown starts there currentAnimal = currentStack.animals [++animalIndex]; TransferAnimals (targetStack, movement); throwingMode = false; ClearThrowingBoxes (); UpdateHighlight (); }
// Transfers animals from one stack to another // Note: "Target" is a newly created List if animals are moving off a stack back to the ground public void TransferAnimals(AnimalStack target, Vector3 movement){ List<GameObject> sourceStack = currentStack.animals; List<GameObject> targetStack = target.animals; // Get height at which animals should be popped onto float movingHeight; // If moving onto floor, target stack is empty if (targetStack.Count == 0) { movingHeight = -animalHeight/2; // Subtract half of animal height to place animal on grid surface } // Moving onto another stack - get height of topmost animal to place moving animals else { if(targetStack[targetStack.Count-1].name.Equals("Shepherd")) { // If just shepherd in stack, animals go to floor beneath it if(targetStack.Count==1){ movingHeight = -animalHeight/2; } // Otherwise move on top of topmost non-shepherd animal else{ movingHeight = targetStack[targetStack.Count-2].transform.localPosition.y; } } else { movingHeight = targetStack[targetStack.Count-1].transform.localPosition.y; } } // Place moving animals into a temporary list, and remove from previous stack List<GameObject> tempStack = new List<GameObject>(sourceStack.GetRange (animalIndex, sourceStack.Count-animalIndex)); sourceStack.RemoveRange (animalIndex, sourceStack.Count-animalIndex); // Move animals to correct heights Vector3 newHeight = tempStack [0].transform.localPosition; for (int i=0; i<tempStack.Count; i++) { newHeight.y = movingHeight + 1.0f * (i+1); tempStack[i].transform.localPosition = newHeight; } // Move animals to correct x and z positions MoveAnimals(tempStack, movement); GameObject shepherdTemp = null; // If moving onto stack with shepherd on top, remove shepherd before adding animals if((targetStack.Count > 0) && targetStack[targetStack.Count-1].name.Equals("Shepherd")) { shepherdTemp = targetStack[targetStack.Count-1]; targetStack.RemoveAt(targetStack.Count-1); } // Add animals to the target stack targetStack.AddRange (tempStack); // If shepherd was temporarily removed, add it back to the top of the stack if(shepherdTemp != null) { Vector3 shepherdHeight = targetStack[targetStack.Count-1].transform.localPosition; shepherdHeight.y += animalHeight; shepherdTemp.transform.localPosition = shepherdHeight; targetStack.Add(shepherdTemp); } // Remove currentStack from stacks list if all animals from it have moved if (animalIndex == 0) { stacks.Remove(currentStack); } // If only shepherd was thrown, give control back to animal which did the throwing if (currentAnimal.name.Equals ("Shepherd")) { currentAnimal = lastAnimal; lastAnimal = currentAnimal; animalIndex = currentStack.animals.IndexOf(currentAnimal); } else { // Control remains with animal added to stack animalIndex = targetStack.Count - tempStack.Count; // Remove 1 from index if shepherd present if (shepherdTemp != null) { animalIndex--; } stackIndex = stacks.IndexOf (target); // Update current stacks and animals currentStack = target; } tempStack.Clear (); }
// Moves animal stack void MoveStack(Vector3 movement){ AnimalStack targetStack = StackAtTarget (movement); // Moving onto another stack if(targetStack!=null){ //Debug.Log("Moving onto other stack."); TransferAnimals(targetStack, movement); } // Move onto ground else{ // If bottom animal is the one moving, there is no need to make new stack if(animalIndex==0){ // Debug.Log("No need for new stack."); MoveAnimals(currentStack.animals, movement); } // Otherwise, create a new stack from the animals leaving previous stack else { //Debug.Log("Making new stack."); AnimalStack newStack = new AnimalStack(); stacks.Add(newStack); TransferAnimals(newStack, movement); } } stepCount++; }
GameObject CreateShepherd(string shepherdName, int StartingX, int StartingZ, Color col, AnimalStack animalStack) { GameObject shepherd = GameObject.CreatePrimitive (PrimitiveType.Sphere); shepherd.name = shepherdName; shepherd.transform.localScale = new Vector3 (1.0f, 1.0f, 1.0f); shepherd.transform.localPosition = new Vector3 (StartingX * planeSideLength, animalHeight/2, StartingZ * planeSideLength); shepherd.GetComponent<Renderer> ().material = diffuseMat; shepherd.GetComponent<Renderer> ().material.color = col; animalStack.animals.Add (shepherd); return shepherd; }
// Creates an animal at a certain grid location, with a certain colour (animal type), and stack which they belong to GameObject CreateAnimal(string animalName, int StartingX, int StartingZ, Color col, AnimalStack animalStack) { GameObject animal = GameObject.CreatePrimitive (PrimitiveType.Cylinder); animal.name = animalName; animal.transform.localScale = new Vector3 (1.0f, 0.5f, 1.0f); animal.transform.localPosition = new Vector3 (StartingX * planeSideLength, animalHeight/2, StartingZ * planeSideLength); animal.GetComponent<Renderer> ().material = diffuseMat; animal.GetComponent<Renderer> ().material.color = col; animalStack.animals.Add (animal); return animal; }
public void UpdateIndices(GameObject animal) { // Search all stacks and set index's for(int stack = 0; stack < levelStacks.Count; stack++) { // Check if the stack contains the animal if(levelStacks[stack].Contains(animal)) { // Store indices and update stackIndex = stack; currentStack = levelStacks[stackIndex]; animalIndex = currentStack.GetIndex(animal); currentAnimal = levelStacks[stackIndex].Get (animalIndex); break; } } }
// // Merges a stack into the current one // StackA represents the one we want to merge into ours // StackB represents our current stack // pos represents how we want to merge // public void MergeStack(AnimalStack stackA, AnimalStack stackB, ExecutePosition pos) { AnimalStack newStack = new AnimalStack(); Vector3 basePos = stackA.Get(0).transform.position; // Define the order to merge stacks switch(pos) { // Places the current stack on top of the new one (i.e. we should use this for walking into a stack) case ExecutePosition.TOP: newStack.GetList().AddRange(stackB.GetList()); newStack.GetList().AddRange(stackA.GetList()); break; // Places the current stack beneath the new one (i.e. we should use this for falling ontop of the stack) case ExecutePosition.BOTTOM: newStack.GetList().AddRange(stackA.GetList()); newStack.GetList().AddRange(stackB.GetList()); break; } // Refresh stack for(int i = 0; i < newStack.GetSize(); i++) { // Make animals above base animal non-kinematic if(i>0) { newStack.Get(i).GetComponent<Rigidbody>().isKinematic = false; newStack.Get(i).GetComponent<Rigidbody>().useGravity = false; } // Refresh position newStack.Get(i).transform.position = basePos + new Vector3(0, animalHeight * i, 0); newStack.Get(i).GetComponent<AnimalBehaviour>().SetParentStack(newStack, i); newStack.Get(i).GetComponent<AnimalBehaviour>().currentVelocity = new Vector3(0.0f,0.0f,0.0f); // Keep rotation same as base rotation newStack.Get (i).transform.rotation = newStack.Get(0).transform.rotation; } // Remove the old stacks levelStacks.Remove(stackA); levelStacks.Remove(stackB); // Add the new one levelStacks.Add(newStack); // Refresh selections UpdateIndices(currentAnimal); // Stop merging for a period DisableMerge(); }
// // Splits a stack, adding everything from the index upwards into its own stack // pos represents how we want to split // Top represents stepping of a stack // Bottom represents gecko stepping out beneath stack // public void SplitStack(AnimalStack oldStack, int index, ExecutePosition pos, Vector3 moveDirection) { AnimalStack newStack = new AnimalStack(); if(pos.Equals(ExecutePosition.TOP)) { // Add animals to new stack and remove from old newStack.GetList().AddRange( oldStack.GetList().GetRange(index, oldStack.GetSize() - index) ); oldStack.GetList ().RemoveRange(animalIndex, oldStack.GetSize()-animalIndex); // Update stack for(int i = 0; i < newStack.GetSize(); i++) { // Move animals in direction //newStack.Get(i).transform.position = newStack.Get (i).transform.position + moveDirection*1.1f*animalHeight;// + new Vector3(0.0f, -animalHeight*animalIndex,0.0f); // Update parent and index newStack.Get(i).GetComponent<AnimalBehaviour>().SetParentStack(newStack, i); } // Enable base animal newStack.Get(0).GetComponent<Rigidbody>().useGravity = true; // Add new stack to levelStacks before updating the selection so it can be considered levelStacks.Add(newStack); // Refresh selection UpdateSelectedAnimal(newStack.Get(0)); } else if(pos.Equals(ExecutePosition.BOTTOM)) { Vector3 basePos = oldStack.Get(0).transform.position; // Add gecko to the new stack newStack.GetList().AddRange( oldStack.GetList().GetRange(0, index)); // Remove gecko from the old stack oldStack.GetList().RemoveRange(0, index); // Reposition the old stack for(int i = 0; i < oldStack.GetSize(); i++) { // Move animals in direction oldStack.Get(i).transform.position = basePos + new Vector3(0.0f, animalHeight*i,0.0f); // Refresh index oldStack.Get(i).GetComponent<AnimalBehaviour>().SetParentStack(oldStack, i); } // Reposition the new stack for(int i = 0; i < newStack.GetSize(); i++) { // Move animals in direction newStack.Get(i).transform.position = basePos + moveDirection*1.1f*animalHeight;// + new Vector3(0.0f, -animalHeight*animalIndex,0.0f); // Refresh index newStack.Get(i).GetComponent<AnimalBehaviour>().SetParentStack(newStack, i); } // Add new stack to the list levelStacks.Add(newStack); } // Stop merging for a period DisableMerge(); }
public void SetParentStack(AnimalStack a, int i) { parentStack = a; animalIndex = i; // Calculate position in stack stackLocalPosition = new Vector3(0, i * animalHeight, 0); }