/// <summary> /// This adds a new hinge joint to the <paramref name="anchor"/> /// and connects it to the <paramref name="connectedBody"/> through the <paramref name="position"/> /// </summary> /// <param name="anchor"></param> /// <param name="connectedBody"></param> /// <param name="position"></param> public void ConnectPart(BridgePart anchor, BridgePart connectedBody, Vector2 position) { AnchoredJoint2D joint; this.anchor = anchor; if (anchor is RopePart) { joint = SetupRope (anchor as RopePart, connectedBody, position); } else if (connectedBody is RopePart) { joint = SetupRope (connectedBody as RopePart, anchor, position); } else { joint = anchor.gameObject.AddComponent<HingeJoint2D>(); joint.connectedBody = connectedBody.rigid; joint.anchor = Grid.ToGrid(anchor.transform.InverseTransformPoint(position)); joint.connectedAnchor = Grid.ToGrid(connectedBody.transform.InverseTransformPoint(position)); } joint.enableCollision = false; connections.Add(joint); }
public static BridgePart Create(BridgePart part, Vector2 position) { BridgePart instance = Instantiate<BridgePart>(part); instance.partOrigin = Grid.ToGrid(position); instance.rigid = instance.GetComponent<Rigidbody2D>(); instance.rigid.isKinematic = true; instance.transform.position = (Vector3)instance.partOrigin; return instance; }
DistanceJoint2D SetupRope(RopePart anchor, BridgePart other, Vector2 position) { DistanceJoint2D joint = anchor.gameObject.AddComponent<DistanceJoint2D>(); joint.connectedBody = other.rigid; joint.distance = anchor.partLength * 0.5f; joint.maxDistanceOnly = true; joint.anchor = Grid.ToGrid(Vector3.zero); joint.connectedAnchor = Grid.ToGrid(other.transform.InverseTransformPoint(position)); return joint; }
void DoCommands() { if(Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift)) { if(Input.GetKeyDown(KeyCode.Z)) { Level.Undo(); buildingPart = Level.undoStack.Peek() as BridgePart; } if(Input.GetKeyDown(KeyCode.R)) { buildingPart = Level.redoStack.Peek() as BridgePart; Level.Redo(); } } }
public void OnPointerDown(PointerEventData eventData) { if(leftMouseDown(eventData)) { currentPart = BridgePart.Create( ResourcesManager.Instance.bridgePartPrefabs[ConstructionControl.partType], Input.GetKey(KeyCode.LeftShift) ? currentPart.partEnd : mousePosition(eventData)); currentPart.Stretch(mousePosition(eventData)); } if(rightMouseDown(eventData)) { if(ConstructionControl.partType < ResourcesManager.Instance.bridgePartPrefabs.Length-1) { ConstructionControl.partType++; } else { ConstructionControl.partType = 0; } } }