// Use this for initialization void Start() { //setting up the pointer to parent's script parentsScript = GetComponentInParent <PancakeBehaviourScript> (); //ignoring collisions with sibling rigid bodies for the trigger circleCollider CircleCollider2D circle = gameObject.GetComponent <CircleCollider2D> (); //the trigger signals start working before the start function is called. //If I call is on collision enter before Start() is finalised, I get an error such that parentScript is not set. //I enable the trigger collider ONLY after I set the script pointer. circle.enabled = true; GameObject parentGameObject = this.transform.parent.gameObject; string otherSideName = "rigidBody1"; if (gameObject.name == "rigidBody1") { otherSideName = "rigidBody2"; } for (int i = 0; i < parentGameObject.transform.childCount - 1; i++) { string name = parentGameObject.transform.GetChild(i).transform.name; if (name == "rigidBody0" || name == otherSideName) { BoxCollider2D siblingBox = parentGameObject.transform.GetChild(i).GetComponent <BoxCollider2D> (); CircleCollider2D siblingCircle = parentGameObject.transform.GetChild(i).GetComponent <CircleCollider2D> (); Physics2D.IgnoreCollision(circle, siblingBox); Physics2D.IgnoreCollision(circle, siblingCircle); } } //setting the initial colour of the material: }
void resortRigidBodiesInStack() { if (selectedPancake < stackHeight - 1) { //Debug.Log ("selectedPancake " + selectedPancake); //Debug.Log ("stackHeight " + stackHeight); PancakeBehaviourScript pancakeScript = RigidBodiesInStack [selectedPancake + 1].gameObject.GetComponentInParent <PancakeBehaviourScript> (); pancakeScript.reConnectSpringsBasedOnThisPancake(RigidBodiesInStack [selectedPancake]); //the pancake is not the top pancake, spring sorting wil be necessarry: } else { //the pancake to be thrown is the top pancake, I need to make the pancake below the top pancake, //if there is a pancake below this one: if (selectedPancake > 0) { PancakeBehaviourScript pancakeScript = RigidBodiesInStack [selectedPancake - 1].gameObject.GetComponentInParent <PancakeBehaviourScript> (); pancakeScript.toggleTopPancake(); } } for (int i = selectedPancake; i < stackHeight - 1; ++i) { RigidBodiesInStack [i] = RigidBodiesInStack [i + 1]; } RigidBodiesInStack [stackHeight - 1] = null; incrementStackSizeDown(); }
bool hitByattacker(Collider2D other) { //Debug.Log ("collidedwith:"); //Debug.Log (other.tag); GameObject collidedGameObject = other.gameObject; if (collidedGameObject.CompareTag("SinglePancakeConnectRigidBody") || collidedGameObject.CompareTag("SinglePancakeSideRigidBody")) { PancakeBehaviourScript script = collidedGameObject.GetComponentInParent <PancakeBehaviourScript> (); //Debug.Log ("is pancake stacked?"); //Debug.Log (script.isStacked ()); if (script.isThrown()) { //the pancake I collided with was thrown //is it my own thrown pancake? int attackerId = script.getOwnerID(); if (playerId != attackerId) { //I am not the owner of this pancake, I am hit by an attacker! hitByPlayer = attackerId; return(true); } } } return(false); }
void throwPancake(int side) { PancakeBehaviourScript pancakeScript = RigidBodiesInStack [selectedPancake].gameObject.GetComponentInParent <PancakeBehaviourScript> (); //turn highlight off: RigidBodiesInStack [selectedPancake].gameObject.GetComponent <SpriteRenderer> ().enabled = false; //reordering and re-attaching rigid bodies in stack: resortRigidBodiesInStack(); //resetting selector: selectedPancake = -1; //disabling collisions: //foreach (Rigidbody2D rb in RigidBodiesInStack) { for (int i = 0; i < stackHeight; ++i) { pancakeScript.disableCollisionsWith(RigidBodiesInStack[i]); } pancakeScript.disableCollisionsWith(GetComponent <Rigidbody2D> ()); //throwing the pancake: if (side == 1) { //throw pancake to right: if (attackSpeed1 < 0) { pancakeScript.throwThisPancake(-1.0f * attackSpeed1, side); } else { pancakeScript.throwThisPancake(attackSpeed1, side); } } if (side == 2) { pancakeScript.throwThisPancake(attackSpeed2, side); } }
void adjustParametersUponStack(Collider2D other) { if (stackingToPlate) { //Debug.Log ("stackingToPlate"); //increment the stack size //CharacterBehaviourScript script = other.GetComponent<CharacterBehaviourScript>(); //script.incrementStackSizeUp(); } else { //toggle top pancake options PancakeBehaviourScript script = other.GetComponentInParent <PancakeBehaviourScript> (); script.toggleTopPancake(); } ownerScript.incrementStackSizeUp(); toggleTopPancake(); stacked = true; gameObject.layer = LayerMask.NameToLayer("StackedPancake"); foreach (Transform child in gameObject.transform) { child.gameObject.layer = LayerMask.NameToLayer("StackedPancake"); } rigidBodyOfInterest.gravityScale = 1.0f; sideRigidBody1.gravityScale = 1.0f; sideRigidBody2.gravityScale = 1.0f; //Debug.Log ("isTopPancakeAfterStack?"); //Debug.Log (topPancake); }
void activateSideSprings(Rigidbody2D collidedRigidbody) { SpringJoint2D springJoint1 = sideRigidBody1.gameObject.GetComponent <SpringJoint2D> (); SpringJoint2D springJoint2 = sideRigidBody2.gameObject.GetComponent <SpringJoint2D> (); if (stackingToPlate) { //the pancake is stacking to plate, need to adjust spring attachment parameters (dafaults are for other pancakes). springJoint1.connectedAnchor = new Vector2(-1.0f * sideSpringPlatePlacement, 0); springJoint1.dampingRatio = springPancakeToPlateDamping; springJoint1.frequency = springPancakeToPlateFrequency; springJoint2.connectedAnchor = new Vector2(sideSpringPlatePlacement, 0); springJoint2.dampingRatio = springPancakeToPlateDamping; springJoint2.frequency = springPancakeToPlateFrequency; } if (flippedAttached) { Vector2 v1 = springJoint1.connectedAnchor; springJoint1.connectedAnchor = new Vector2(-1.0f * v1[0], 0); Vector2 v2 = springJoint2.connectedAnchor; springJoint2.connectedAnchor = new Vector2(-1.0f * v2[0], 0); } if (stackingToPlate) { springJoint1.connectedBody = collidedRigidbody; springJoint1.enabled = true; springJoint2.connectedBody = collidedRigidbody; springJoint2.enabled = true; } else { PancakeBehaviourScript script = collidedRigidbody.gameObject.GetComponentInParent <PancakeBehaviourScript> (); bool pancakeBelowIsFlipped = script.isFlippedAttached(); if (flippedAttached && pancakeBelowIsFlipped) { springJoint1.connectedBody = script.getSideRigidBody1(); springJoint2.connectedBody = script.getSideRigidBody2(); } else if (flippedAttached || pancakeBelowIsFlipped) { //one of the pancakes is flipped, I need to attach side 1 to side 2 and vice versa springJoint1.connectedBody = script.getSideRigidBody2(); springJoint2.connectedBody = script.getSideRigidBody1(); } else { //nothing is flipped: springJoint1.connectedBody = script.getSideRigidBody1(); springJoint2.connectedBody = script.getSideRigidBody2(); } springJoint1.enabled = true; springJoint2.enabled = true; } }
void activateSprings(Rigidbody2D collidedRigidbody) { SpringJoint2D[] springJoints = rigidBodyOfInterest.gameObject.GetComponents <SpringJoint2D> (); bool foundFirst = false; foreach (SpringJoint2D springJoint in springJoints) { if (stackingToPlate) { //the pancake is stacking to plate, need to adjust spring attachment parameters (dafaults are for other pancakes). Vector2 v = springJoint.connectedAnchor; springJoint.connectedAnchor = new Vector2(v[0] / springPancakePlacement * springPlatePlacement, 0); foundFirst = true; springJoint.dampingRatio = springPancakeToPlateDamping; springJoint.frequency = springPancakeToPlateFrequency; springJoint.distance = springPancakeToPlateDistance; } else { //attaching to another pancake, if the pancake below is flipper during attachemnt, I should rotate the // attachment (same as if this pancake is flipped). //if this pancake is also flipped, the rotation will be repeated, //briging the connectin to norma n=once again in the below if clause PancakeBehaviourScript script = collidedRigidbody.gameObject.GetComponentInParent <PancakeBehaviourScript> (); if (script.isFlippedAttached()) { Vector2 v = springJoint.connectedAnchor; springJoint.connectedAnchor = new Vector2(-1.0f * v[0], 0); } } float zRot = (rigidBodyOfInterest.transform.rotation.eulerAngles.z % 360); //Debug.Log ("zRot:"); //Debug.Log (zRot); if ((zRot > 90 && zRot < 270.0) || (zRot < -90 && zRot > -270)) { //the colliding PancakeBehaviourScript is upside down, //I will flip the springPlatePlacement connections, such that the springs will still be cross: Vector2 v = springJoint.connectedAnchor; springJoint.connectedAnchor = new Vector2(-1.0f * v[0], 0); flippedAttached = true; } springJoint.connectedBody = collidedRigidbody; springJoint.enabled = true; } activateSideSprings(collidedRigidbody); }
public void reConnectSpringsBasedOnThisPancake(Rigidbody2D reAttachmentBase) { SpringJoint2D[] ownSpringJoints = rigidBodyOfInterest.gameObject.GetComponents <SpringJoint2D> (); SpringJoint2D[] baseSpringJoints = reAttachmentBase.gameObject.GetComponents <SpringJoint2D> (); for (int i = 0; i < 2; ++i) { Vector2 v = baseSpringJoints [i].connectedAnchor; ownSpringJoints [i].connectedAnchor = new Vector2(v [0], v [1]); ownSpringJoints [i].dampingRatio = baseSpringJoints [i].dampingRatio; ownSpringJoints [i].frequency = baseSpringJoints [i].frequency; ownSpringJoints [i].distance = baseSpringJoints [i].distance; ownSpringJoints[i].connectedBody = baseSpringJoints[i].connectedBody; } PancakeBehaviourScript scriptOfBase = reAttachmentBase.gameObject.GetComponentInParent <PancakeBehaviourScript> (); if (scriptOfBase.isFlippedAttached()) { flippedAttached = true; } if (scriptOfBase.isStackingToPlate()) { stackingToPlate = true; } SpringJoint2D ownSideJoint1 = sideRigidBody1.gameObject.GetComponent <SpringJoint2D> (); SpringJoint2D ownSideJoint2 = sideRigidBody2.gameObject.GetComponent <SpringJoint2D> (); SpringJoint2D baseSideJoint1 = scriptOfBase.getSideRigidBody1().gameObject.GetComponent <SpringJoint2D> (); SpringJoint2D baseSideJoint2 = scriptOfBase.getSideRigidBody2().gameObject.GetComponent <SpringJoint2D> (); Vector2 v1 = baseSideJoint1.connectedAnchor; ownSideJoint1.connectedAnchor = new Vector2(v1 [0], v1 [1]); Vector2 v2 = baseSideJoint2.connectedAnchor; ownSideJoint2.connectedAnchor = new Vector2(v2 [0], v2 [1]); ownSideJoint1.dampingRatio = baseSideJoint1.dampingRatio; ownSideJoint2.dampingRatio = baseSideJoint2.dampingRatio; ownSideJoint1.frequency = baseSideJoint1.frequency; ownSideJoint2.frequency = baseSideJoint2.frequency; ownSideJoint1.distance = baseSideJoint1.distance; ownSideJoint2.distance = baseSideJoint2.distance; ownSideJoint1.connectedBody = baseSideJoint1.connectedBody; ownSideJoint2.connectedBody = baseSideJoint2.connectedBody; }
void breakStack(int breakPoint) { //if there is selected pancake, it will not be selected anymore: if (selectedPancake > 0) { RigidBodiesInStack [selectedPancake].gameObject.GetComponent <SpriteRenderer> ().enabled = false; //resetting selector: selectedPancake = -1; } //disabling and removing pancakes from the rigid body list while (stackHeight > breakPoint) { PancakeBehaviourScript pancakeScript = RigidBodiesInStack [stackHeight - 1].gameObject.GetComponentInParent <PancakeBehaviourScript> (); pancakeScript.dropPancakeFromStack(); RigidBodiesInStack [stackHeight - 1] = null; stackHeight--; } //setting the pancake below the break point to be the top pancake: PancakeBehaviourScript pancakeScriptNewTop = RigidBodiesInStack [breakPoint - 1].gameObject.GetComponentInParent <PancakeBehaviourScript> (); pancakeScriptNewTop.toggleTopPancake(); }
bool collidedToStackAble(Collider2D other) { //Debug.Log ("collidedwith:"); //Debug.Log (other.tag); GameObject collidedGameObject = other.gameObject; if (collidedGameObject.CompareTag("Player")) { //collided with plate: CharacterBehaviourScript script = collidedGameObject.GetComponentInParent <CharacterBehaviourScript> (); if (!script.collidedToPlateFromTop(other, rigidBodyOfInterest.transform.position.y)) { //the collision must be from above, and to the plate, not to the feet! return(false); } if (script.getSizeOfStack() > 0) { //the pile has other pancakes, I do not sack on this pancake return(false); } else { if (script.checkIfHasSpaceForStacking()) { stackingToPlate = true; ownerScript = script; ownerID = ownerScript.getPlayerId(); return(true); } else { //the stack is full, there can be no more pancakes return(false); } } } else if (collidedGameObject.CompareTag("SinglePancakeConnectRigidBody")) { //Debug.Log ("collided with singePancake"); //the collision is not with the base, it is with another pancake: //did I hit the stackable pancake rigid body? PancakeBehaviourScript script = collidedGameObject.GetComponentInParent <PancakeBehaviourScript> (); //Debug.Log ("is pancake stacked?"); //Debug.Log (script.isStacked ()); if (script.isStacked()) { //Debug.Log ("pancake is stacked"); //pancake is stacked: if (script.isTopPancake()) { //Debug.Log("Pancake is top pancake"); //pancake is top pancake, if (collidedGameObject.transform.position.y < rigidBodyOfInterest.transform.position.y) { //the collision is from above if (script.getOwnerScript().checkIfHasSpaceForStacking()) { //the owner plate of the pancake it collided with has space: //can stack now: ownerScript = script.getOwnerScript(); ownerID = ownerScript.getPlayerId(); return(true); } } } } } return(false); }