void OnCollisionEnter2D(Collision2D collision) { // get last collision point with the boat lastCollisionContactPoint = ((ContactPoint2D[])collision.contacts)[0].point; // get last collision's transform Transform lastCollisionTransform = ((ContactPoint2D[])collision.contacts)[0].collider.transform; // get Ship script if collided with a boat, null otherwise lastCollisionShip = lastCollisionTransform.GetComponent <ShipDrillableView>(); // check if collided to a dam or bridge bool isCollisionWithBridgeOrDam = lastCollisionTransform.GetComponent <DamBridgeView>() != null; // change icon to drill and calculate lastRoundedDiverShipAngle if (lastCollisionShip != null) { // if the ship is not sunk yet if (!lastCollisionShip.isSunk && lastDispatchedEvent != InventoryModel.Events.ApproachDrillableShip) { lastDispatchedEvent = InventoryModel.Events.ApproachDrillableShip; _inventoryEventFireSignal.Dispatch(lastDispatchedEvent, TouchScreenPosition.zero); } // if the ship is sunk if (lastCollisionShip.isSunk && lastDispatchedEvent != InventoryModel.Events.ApproachSunkShip) { lastDispatchedEvent = InventoryModel.Events.ApproachSunkShip; _inventoryEventFireSignal.Dispatch(lastDispatchedEvent, TouchScreenPosition.zero); } } if (lastCollisionShip != null || isCollisionWithBridgeOrDam) { // angle of the vector pointing from the diver to the collision point lastDiverShipDamOrBridgeAngle = 180 + Mathf.Rad2Deg * Mathf.Atan2( collision.contacts[0].normal.y, collision.contacts[0].normal.x); } // round the value to 45, 135, etc //lastRoundedDiverShipAngle = (int) diverShipAngle / 90 % 4 * 90 + 45; //Debug.Log (diverShipAngle + " " + lastRoundedDiverShipAngle); }
void OnCollisionEnter2D(Collision2D collision2D) { // Assume there are no bombs mounted nearby InventoryModel.Events e = InventoryModel.Events.ApproachBridgeOrDamWithoutLocalBomb; // check if any bombs mounted nearby foreach (Vector2 bombPos in damBridgeModel.bombsMounted) { if ((lastCollisionPoint(collision2D) - bombPos).magnitude < Const.minimumTimeBombDistance) { e = InventoryModel.Events.ApproachBridgeOrDamWithLocalBomb; } } // check if we have any bombs left to mount if (damBridgeModel.bombsMounted.Count >= Const.numberOfTimeBombsToMount) { e = InventoryModel.Events.ApproachBridgeOrDamWithAllBombs; } // handle the collision handleCollision(e, collision2D); }
void Update() { // if ship is close and space hit start drilling if (isDrillingGameInputEventReceived && isCloseToBoat()) { // do not move during drilling rigidbody2D.isKinematic = true; // look at the ship lookTowards2D(lastDiverShipDamOrBridgeAngle); // drill the ship and request if it's still drillable i.e. not sink _isDrilling = lastCollisionShip.drill(lastCollisionContactPoint); // trigger the animator setAnimationTrigger(getTriggerBasedOnAngle(lastDiverShipDamOrBridgeAngle)); } // if attempting to drill a patrol boat or something else else if (isDrillingGameInputEventReceived && !isCloseToBoat()) { foreach (ShipPatrolProperties s in shipPatrolModel.shipPatrolPropertiesDict.Values) { if ((s.cachedPosition - (Vector2)transform.position).magnitude < .5f) { _shipPatrolDrilledSignal.Dispatch(s); _isDrilling = true; } } } // if mounting a time bomb else if (playerModel.isMountingTimeBomb) { // do not move during drilling rigidbody2D.isKinematic = true; // look at the dam or bridge lookTowards2D(lastDiverShipDamOrBridgeAngle); // trigger the animator if (lastDiverShipDamOrBridgeAngle < 90 || 270 < lastDiverShipDamOrBridgeAngle) { setAnimationTrigger("BombMountingRightDown"); } else { setAnimationTrigger("BombMountingLeftDown"); } } // if finished drilling or in cruising mode else { // start move away from ship rigidbody2D.isKinematic = false; // stop playing drill sound as ship does not know when drilling is finished if (_isDrilling) { _isDrilling = false; } } // if boat became far then icon should change if (!isCloseToBoat() && lastDispatchedEvent != InventoryModel.Events.AbandonShipBridgeOrDam) { lastDispatchedEvent = InventoryModel.Events.AbandonShipBridgeOrDam; _inventoryEventFireSignal.Dispatch(lastDispatchedEvent, TouchScreenPosition.zero); } // Rotate diver into velocity direction if (rigidbody2D.velocity.magnitude > .08) { Vector2 lookDirection = (Vector2)(Quaternion.Euler(0, 0, Const.PlayerRotationTrim) * (Vector3)rigidbody2D.velocity); lookTowards2D(lookDirection); } }
void handleCollision(InventoryModel.Events e, Collision2D collision2D) { damBridgeModel.lastCollisionTouchScreenPosition = new TouchScreenPosition((Vector2)collision2D.contacts[0].point); _inventoryEventFireSignal.Dispatch(e, TouchScreenPosition.zero); }