/// <summary> /// Screw out the passed screw by one tightness /// </summary> /// <param name="screw">The screw to screw out once</param> /// <param name="useAudio">Should audio be played</param> internal void ScrewOut(ScrewV2 screw, bool useAudio = true) { if (screw.tightness > 0) { if (useAudio) { AudioSource.PlayClipAtPoint(ScrewablePartV2Mod.soundClip, screw.gameObject.transform.position); } screw.gameObject.transform.Rotate(0, 0, -rotationStep); screw.gameObject.transform.Translate(0f, 0f, transformStep); screw.tightness--; } partFixed = false; parentCollider.enabled = true; }
/// <summary> /// This function saves the screws into the config folder of your mod /// </summary> /// <param name="mod">Your mod object (usually "this")</param> /// <param name="screwableParts">An array of all the screwable parts in your mod you want to save</param> /// <param name="saveFile">The save file name</param> public static void SaveScrews(Mod mod, ScrewablePartV2[] screwableParts, string saveFile) { Dictionary <string, int[]> saveDictionary = new Dictionary <string, int[]>(); foreach (ScrewablePartV2 screwablePart in screwableParts) { int[] tightnessArr = new int[screwablePart.screws.Length]; for (int i = 0; i < screwablePart.screws.Length; i++) { ScrewV2 screw = screwablePart.screws[i]; tightnessArr[i] = screw.tightness; } saveDictionary[screwablePart.id] = tightnessArr; } SaveLoad.SerializeSaveFile <Dictionary <string, int[]> >(mod, saveDictionary, saveFile); }
public void SetScrewPosition(ScrewV2 screw, int tightness, bool useAudio = false) { if (screw.tightness > tightness) { while (screw.tightness != tightness) { ScrewOut(screw, useAudio); } } else if (screw.tightness < tightness) { while (screw.tightness != tightness) { ScrewIn(screw, useAudio); } } }
/// <summary> /// Screw in the passed screw by one tightness /// </summary> /// <param name="screw">The screw to screw in once</param> /// <param name="useAudio">Should audio be played</param> internal void ScrewIn(ScrewV2 screw, bool useAudio = true) { if (screw.tightness < maxTightness) { if (useAudio) { AudioSource.PlayClipAtPoint(ScrewablePartV2Mod.soundClip, screw.gameObject.transform.position); } screw.gameObject.transform.Rotate(0, 0, rotationStep); screw.gameObject.transform.Translate(0f, 0f, -transformStep); screw.tightness++; if (screw.tightness == maxTightness) { CheckAllScrewsTight(screws); } } }
/// <summary> /// Initializes the screwable part /// </summary> /// <param name="parent">The parent where the screws are supposed to be placed on</param> /// <param name="screws">The array of screws to initialize</param> /// <param name="showScrewSize">Auto set. This is used by the logic to detect if the screw size can be shown to the user</param> private void InitScrewable(GameObject parent, ScrewV2[] screws, bool showScrewSize) { for (int i = 0; i < screws.Length; i++) { ScrewV2 screw = screws[i]; screw.id = String.Format("{0}_SCREW{1}", parent.name, i + 1); screw.gameObject = CreateScrewModel(screw.id, screw.position, screw.rotation, new Vector3(screw.scale, screw.scale, screw.scale), screw.type); screw.renderer = screw.gameObject.GetComponentsInChildren <MeshRenderer>(true)[0]; int tmpTigness = screw.tightness; screw.tightness = 0; screw.showSize = showScrewSize; for (int j = 0; j < tmpTigness; j++) { ScrewIn(screw, false); } } logic = parent.AddComponent <ScrewablePartLogicV2>(); logic.Init(parent, screws, this); }
/// <summary> /// This detects the screw the user is aiming at /// </summary> /// <returns>Either a ScrewV2 object or null if nothing is found</returns> private ScrewV2 DetectScrew() { if (previousScrew != null) { previousScrew.renderer.material = screwMaterial; previousScrew = null; } if (Camera.main == null) { return(null); } RaycastHit hit; GameObject hitObject; if (!Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 1f, 1 << LayerMask.NameToLayer("DontCollide"))) { return(null); } hitObject = hit.collider?.gameObject; if (!hitObject.name.Contains("SCREW") || !hitObject.name.Contains(parent.name)) { return(null); } for (int i = 0; i < screws.Length; i++) { ScrewV2 screw = screws[i]; if (hitObject.name == screw.id) { if (previousScrew != null) { previousScrew.renderer.material = screwMaterial; } previousScrew = screw; return(screw); } } return(null); }
/// <summary> /// Called every frame and detects the screws /// </summary> void Update() { if (!isToolInHand || (!spanner.activeSelf && !ratchet.activeSelf)) { return; } ScrewV2 screw = DetectScrew(); if (screw != null) { int wrenchSize = Mathf.RoundToInt(_wrenchSize.Value * 10f); if (wrenchSize < 0) { return; } try { if ((bool)ScrewablePartV2Mod.showScrewSize && screw.showSize) { ScrewablePart.GuiInteraction = "Screw size: " + screw.size; } } catch { } if (wrenchSize != screw.size) { return; } screw.renderer.material.shader = Shader.Find("GUI/Text Shader"); screw.renderer.material.SetColor("_Color", Color.green); screwingTimer += Time.deltaTime; if (screwingTimer >= _boltingSpeed.Value) { screwingTimer = 0; if (Input.GetAxis("Mouse ScrollWheel") > 0f) { if (ratchet.activeSelf) { if (ratchetSwitch.Value) { screwablePart.ScrewIn(screw); } else { screwablePart.ScrewOut(screw); } } else { screwablePart.ScrewIn(screw); } } if (Input.GetAxis("Mouse ScrollWheel") < 0f) { if (ratchet.activeSelf) { if (ratchetSwitch.Value) { screwablePart.ScrewIn(screw); return; } else { screwablePart.ScrewOut(screw); return; } } else { screwablePart.ScrewOut(screw); } } } } }