void Awake() { instance = this; if (!PartDatabase.InitialLoadDone) { PartDatabase.ReloadPartsList(); } cam = Camera.main; }
private void Awake() { if (singleton == null) { singleton = this; } else { Debug.LogWarning("PartDatabase already exists!", this); } }
// Spawns the prefab in the world public void SpawnPrefab() { // Attempt to fetch the model prefab PartDatabase partDB = (PartDatabase)GameObject.FindObjectOfType <PartDatabase>(); // there'll only ever be one part database in the scene WorldPrefab = partDB.RetrievePrefab(ID); // Initialise the world prefab if it's assigned if (WorldPrefab != null) { WorldAsset = (GameObject)GameObject.Instantiate(WorldPrefab); WorldAsset.transform.parent = GameObject.Find("Basher").transform; WorldAsset.transform.localPosition = Vector3.zero; } }
public void Load() { try { if (File.Exists(path)) { PartDatabase db = PartDatabase.Instance; BinaryFormatter bf = new BinaryFormatter(); FileStream file = File.Open(path, FileMode.Open); PlayerData data = (PlayerData)bf.Deserialize(file); file.Close(); Points = data.points; hatName = data.hat; headName = data.head; bodyName = data.body; Hat = db.FindHat(hatName); Head = db.FindHead(headName); Body = db.FindBody(bodyName); Hat.owned = true; Head.owned = true; Body.owned = true; } else { InitLevelData(); } } catch (Exception e) { print(e.Message); InitLevelData(); } }
public static List <PlacedPart> FromSave(List <Build.BuildSave.PlacedPartSave> parts, PartDatabase partDatabase) { List <PlacedPart> list = new List <PlacedPart>(); for (int i = 0; i < parts.Count; i++) { PartData partByName = partDatabase.GetPartByName(parts[i].partName); bool flag = partByName != null; if (flag) { try { partByName.GUID = new Guid(parts[i].GUID); } catch { partByName.GUID = Guid.NewGuid(); } //Tags parsing from text back to a Dictionary partByName.tags = new Dictionary <string, object>(); try { foreach (string text in parts[i].tagsString.Split(new char[] { '|' })) { if (!(text == "")) { Type type = Type.GetType(text.Split(new char[] { '#' })[0]); string key = text.Split(new char[] { '#' })[1]; //ModLoader.mainConsole.log(text.Split('#')[2]); object obj = JsonUtility.FromJson(text.Split(new char[] { '#' })[2], type); //ModLoader.mainConsole.log(obj.ToString(), "Tags"); partByName.tags.Add(key, obj); } } /*foreach (string text2 in partByName.tags.Keys) * { * ModLoader.mainConsole.log(text2 + " " + partByName.tags[text2].ToString(), "Tags"); * }*/ } catch (Exception e) { ModLoader.mainConsole.logError(e); } list.Add(new PlacedPart(null, parts[i].position, parts[i].orientation, partByName)); } } return(list); }
public static void CenterToPositon(List <Build.BuildSave.PlacedPartSave> parts, PartDatabase partDatabase, ref Vector2 size, Vector2 targetCenter) { Vector2 zero = Vector2.zero; Vector2 vector = Vector2.one * float.PositiveInfinity; foreach (Build.BuildSave.PlacedPartSave placedPartSave in parts) { PartData partByName = partDatabase.GetPartByName(placedPartSave.partName); if (!(partByName == null)) { foreach (PartData.Area area in partByName.areas) { Vector2 vector2 = placedPartSave.position + area.start * placedPartSave.orientation; Vector2 vector3 = placedPartSave.position + (area.start + area.size) * placedPartSave.orientation; zero = new Vector2(Mathf.Max(new float[] { zero.x, vector2.x, vector3.x }), Mathf.Max(new float[] { zero.y, vector2.y, vector3.y })); vector = new Vector2(Mathf.Min(new float[] { vector.x, vector2.x, vector3.x }), Mathf.Min(new float[] { vector.y, vector2.y, vector3.y })); } } } Vector2 b = (vector + zero) / 2f; Vector2 b2 = targetCenter - b; for (int j = 0; j < parts.Count; j++) { parts[j].position += b2; } size = zero - vector; }
public static void PositionForLaunch(List <Build.BuildSave.PlacedPartSave> parts, PartDatabase partDatabase, int rotation) { Vector2 zero = Vector2.zero; Vector2 a = Vector2.one * float.PositiveInfinity; foreach (Build.BuildSave.PlacedPartSave placedPartSave in parts) { PartData partByName = partDatabase.GetPartByName(placedPartSave.partName); if (!(partByName == null)) { foreach (PartData.Area area in partByName.areas) { Vector2 vector = placedPartSave.position + area.start * placedPartSave.orientation; Vector2 vector2 = placedPartSave.position + (area.start + area.size) * placedPartSave.orientation; zero = new Vector2(Mathf.Max(new float[] { zero.x, vector.x, vector2.x }), Mathf.Max(new float[] { zero.y, vector.y, vector2.y })); a = new Vector2(Mathf.Min(new float[] { a.x, vector.x, vector2.x }), Mathf.Min(new float[] { a.y, vector.y, vector2.y })); } } } for (int j = 0; j < parts.Count; j++) { parts[j].position -= a - new Vector2(Utility.RoundToHalf((a.x - zero.x) / 2f), 0f); } }
public static void PositionForLaunch(List <Build.BuildSave.PlacedPartSave> parts, PartDatabase partDatabase) { Vector2 zero = Vector2.zero; Vector2 a = Vector2.one * float.PositiveInfinity; foreach (Build.BuildSave.PlacedPartSave current in parts) { PartData.Area[] areas = partDatabase.GetPartByName(current.partName).areas; for (int i = 0; i < areas.Length; i++) { PartData.Area area = areas[i]; Vector2 vector = current.position + area.start * current.orientation; Vector2 vector2 = current.position + (area.start + area.size) * current.orientation; zero = new Vector2(Mathf.Max(new float[] { zero.x, vector.x, vector2.x }), Mathf.Max(new float[] { zero.y, vector.y, vector2.y })); a = new Vector2(Mathf.Min(new float[] { a.x, vector.x, vector2.x }), Mathf.Min(new float[] { a.y, vector.y, vector2.y })); } } for (int j = 0; j < parts.Count; j++) { parts[j].position -= a - new Vector2(Utility.RoundToHalf((a.x - zero.x) / 2f), 0f); } }
public static List <PlacedPart> FromSave(List <Build.BuildSave.PlacedPartSave> parts, PartDatabase partDatabase) { List <PlacedPart> list = new List <PlacedPart>(); for (int i = 0; i < parts.Count; i++) { list.Add(new PlacedPart(null, parts[i].position, parts[i].orientation, partDatabase.GetPartByName(parts[i].partName))); } return(list); }
public static List <Part> CreateBuildParts(Vector3 createPos, Build.BuildSave toLaunch, PartDatabase partDatabase) { List <Part> list = new List <Part>(); List <PlacedPart> list2 = Build.BuildSave.PlacedPartSave.FromSave(toLaunch.parts, partDatabase); for (int i = 0; i < list2.Count; i++) { if (list2[i].partData == null || list2[i].partData.prefab == null || (!Ref.hasPartsExpansion && list2[i].partData.inFull)) { list2.RemoveAt(i); i--; } else { for (int j = i + 1; j < list2.Count; j++) { if (PlacedPart.IsOverplaing(list2[i], list2[j])) { list2.RemoveAt(i); i--; } } } } foreach (PlacedPart placedPart in list2) { Part component = UnityEngine.Object.Instantiate <Transform>(placedPart.partData.prefab, createPos + (Vector3)placedPart.position, Quaternion.identity).GetComponent <Part>(); component.orientation = placedPart.orientation.DeepCopy(); Orientation.ApplyOrientation(component.transform, placedPart.orientation); list.Add(component); } for (int k = 0; k < list2.Count; k++) { for (int l = 0; l < list2[k].partData.attachmentSurfaces.Length; l++) { for (int m = k + 1; m < list2.Count; m++) { for (int n = 0; n < list2[m].partData.attachmentSurfaces.Length; n++) { if (PlacedPart.SurfacesConnect(list2[k], list2[k].partData.attachmentSurfaces[l], list2[m], list2[m].partData.attachmentSurfaces[n]) > 0f) { bool fuelFlow = list2[k].partData.attachmentSurfaces[l].fuelFlow && list2[m].partData.attachmentSurfaces[n].fuelFlow; new Part.Joint(list2[m].position - list2[k].position, list[k], list[m], l, n, fuelFlow); } } } } } return(list); }
public static List <Part> CreateBuildParts(Vector3 createPos, Build.BuildSave toLaunch, PartDatabase partDatabase) { List <Part> list = new List <Part>(); List <PlacedPart> list2 = Build.BuildSave.PlacedPartSave.FromSave(toLaunch.parts, partDatabase); foreach (PlacedPart current in list2) { Part component = UnityEngine.Object.Instantiate <Transform>(current.partData.prefab, createPos + (Vector3)current.position, Quaternion.identity).GetComponent <Part>(); component.orientation = current.orientation.DeepCopy(); Orientation.ApplyOrientation(component.transform, current.orientation); list.Add(component); } for (int i = 0; i < list2.Count; i++) { for (int j = 0; j < list2[i].partData.attachmentSurfaces.Length; j++) { for (int k = i + 1; k < list2.Count; k++) { for (int l = 0; l < list2[k].partData.attachmentSurfaces.Length; l++) { if (PlacedPart.SurfacesConnect(list2[i], list2[i].partData.attachmentSurfaces[j], list2[k], list2[k].partData.attachmentSurfaces[l]) > 0f) { bool fuelFlow = list2[i].partData.attachmentSurfaces[j].fuelFlow && list2[k].partData.attachmentSurfaces[l].fuelFlow; new Part.Joint(list2[k].position - list2[i].position, list[i], list[k], j, l, fuelFlow); } } } } } for (int m = 0; m < list.Count; m++) { list[m].UpdateConnected(); } return(list); }
// GUI Code (Yes the legacy GUI system is slower and can be CPU intense, // but I've never taken the time to learn the 'new' one...) void OnGUI() { // Scale for the buttons float ButtonScale = (Screen.width * 0.276f) / ButtonImg.width; ButtonStyle.fontSize = (int)(Screen.height * 0.04f); // Just in case... TextStyle.wordWrap = false; // If we haven't yet seen the tutorial for the shop, let's show it DialogueManager dialogueManager = GameObject.FindObjectOfType <DialogueManager>(); if (MenuID != 2) { dialogueManager.NTStopShowing(); } // Draw menu elements switch (MenuID) { case 0: // Main Menu ShowGold = false; // Same code as the shop item panel pretty much float ItemDisplayScale = Screen.width / ItemInfoImg.width; // Draw the dialogue panel GUI.DrawTexture(new Rect(0, 0, Screen.width, ItemInfoImg.height * ItemDisplayScale), ItemInfoImg); // Display dialogue float DescriptionYP = 8 * ItemDisplayScale + Screen.height * 0.04f; TitleStyle.fontSize = (int)(Screen.height * 0.08f); // correct the font size TitleStyle.wordWrap = true; GUI.Label(new Rect(19 * ItemDisplayScale, DescriptionYP, ItemInfoImg.width * ItemDisplayScale - 19 * ItemDisplayScale, Screen.height * 0.04f), "Basher Builder", TitleStyle); // Fixed positions float leftX = 41 * ButtonScale; float rightX = 101 * ButtonScale; float topY = 43 * ButtonScale; float bottomY = 70 * ButtonScale; // Play button if (GUI.Button(new Rect(leftX, topY, ButtonImg.width * ButtonScale, ButtonImg.height * ButtonScale), "Play", ButtonStyle)) { MenuID = 1; } // Parts button if (GUI.Button(new Rect(rightX, topY, ButtonImg.width * ButtonScale, ButtonImg.height * ButtonScale), "Parts", ButtonStyle)) { // If we haven't yet seen the tutorial for the shop, let's show it if (!GameObject.FindObjectOfType <PlayerData>().playerData.TutorialsViewed[0]) { dialogueManager.StartDialogue("Shop"); } MenuID = 2; } // Options button if (GUI.Button(new Rect(leftX, bottomY, ButtonImg.width * ButtonScale, ButtonImg.height * ButtonScale), "Settings", ButtonStyle)) { MenuID = 3; } // Quit button if (GUI.Button(new Rect(rightX, bottomY, ButtonImg.width * ButtonScale, ButtonImg.height * ButtonScale), "Quit", ButtonStyle)) { GameObject.FindObjectOfType <PlayerData>().SaveProgress(); Application.Quit(); } break; case 1: // Level Select ShowGold = true; // 6 levels per screen? Obviously only 3 levels atm but... // 3x2 level display. float X1 = 11 * ButtonScale; float X2 = 71 * ButtonScale; float X3 = 131 * ButtonScale; topY = 43 * ButtonScale; bottomY = 70 * ButtonScale; // Fetch the level completion list from the player data bool[] LevelsComplete = GameObject.FindObjectOfType <PlayerData>().playerData.LevelsWon; for (int i = 0; i < 6; i++) { if (i < LevelsComplete.Length) { // The index of the X position to use; 1, 2 or 3 int xi = (i > 2) ? i - 3 : i; float ThisX = (xi == 0) ? X1 : ((xi == 1) ? X2 : X3); // if xi is 0, ThisX = X1, otherwise if xi = 1, ThisX = X2, otherwise ThisX = X3 // Draw the button if (GUI.Button(new Rect(ThisX, i > 2 ? bottomY : topY, ButtonImg.width * ButtonScale, ButtonImg.height * ButtonScale), "Level " + i + " (" + (LevelsComplete[i] ? "Done" : "Not done") + ")", ButtonStyle)) { // You can only start the level if you have completed the previous one if (i == 0) { SceneManager.LoadScene("Level " + (i + 1).ToString()); } if (i > 0) { if (LevelsComplete[i - 1]) { SceneManager.LoadScene("Level " + (i + 1).ToString()); } } } } } break; case 2: // Parts ShowGold = true; // for sure // Same style as the level selection // 3x2 display of parts. Will also need a forward/backward button... X1 = 11 * ButtonScale; X2 = 71 * ButtonScale; X3 = 131 * ButtonScale; topY = 43 * ButtonScale; bottomY = 70 * ButtonScale; PartDatabase partDB = GameObject.FindObjectOfType <PartDatabase>(); ItemInfo[] partList = partDB.ItemInfoList; // Get a local copy of player data PlayerData.SPData playerData = GameObject.FindObjectOfType <PlayerData>().playerData; EquipButtonStyle.fontSize = (int)(Screen.height * 0.04f); bool[] partsOwned = playerData.PartsOwned; // Draw the 3x2 layout for (int i = 0; i < 6; i++) { if (i + (PartPage * 6) < partList.Length) { // The index of the X position to use; 1, 2 or 3 int xi = (i > 2) ? i - 3 : i; float ThisX = (xi == 0) ? X1 : ((xi == 1) ? X2 : X3); // if xi is 0, ThisX = X1, otherwise if xi = 1, ThisX = X2, otherwise ThisX = X3 // Draw the button if (GUI.Button(new Rect(ThisX, i > 2 ? bottomY : topY, ButtonImg.width * ButtonScale, ButtonImg.height * ButtonScale), partList[i + (PartPage * 6)].ItemName + " " + (IsPartEquipped(i, playerData) ? "(*)" : ""), ButtonStyle)) { SelectedPart = i + (PartPage * 6); } } } // Only show the item panel once the shop tutorial has been shown // Draw the item info ItemDisplayScale = Screen.width / ItemInfoImg.width; if (GameObject.FindObjectOfType <PlayerData>().playerData.TutorialsViewed[0]) { // Draw the item info panel GUI.DrawTexture(new Rect(0, 0, Screen.width, ItemInfoImg.height * ItemDisplayScale), ItemInfoImg); // Display the item info string typeString = partList[SelectedPart].IsEnchantment ? " (Enchantment)" : " (Normal Part)"; GUI.Label(new Rect(19 * ItemDisplayScale, 8 * ItemDisplayScale, ItemInfoImg.width * ItemDisplayScale - 19 * ItemDisplayScale, Screen.height * 0.04f), partList[SelectedPart].ItemName + typeString, TextStyle); // Display item description DescriptionYP = 8 * ItemDisplayScale + Screen.height * 0.04f; TextStyle.wordWrap = true; GUI.Label(new Rect(19 * ItemDisplayScale, DescriptionYP, ItemInfoImg.width * ItemDisplayScale - 19 * ItemDisplayScale, Screen.height * 0.04f), partList[SelectedPart].ItemDescription, TextStyle); } // Equip/buy buttons if (partsOwned[SelectedPart]) { // Check if the part is equipped yet if (IsPartEquipped(SelectedPart, playerData)) { if (GUI.Button(new Rect(138 * ItemDisplayScale, 27 * ItemDisplayScale, 18 * ItemDisplayScale, 8 * ItemDisplayScale), "Unequip", EquipButtonStyle)) { // Unequip it UnequipItem(SelectedPart, playerData); } } else { if (GUI.Button(new Rect(138 * ItemDisplayScale, 27 * ItemDisplayScale, 18 * ItemDisplayScale, 8 * ItemDisplayScale), "Equip", EquipButtonStyle)) { // Check if there's space, and equip it if so if (IsSpaceInParts(SelectedPart, playerData)) { EquipItem(SelectedPart, playerData); } } } } else if (GUI.Button(new Rect(138 * ItemDisplayScale, 27 * ItemDisplayScale, 18 * ItemDisplayScale, 8 * ItemDisplayScale), "Buy (" + partList[SelectedPart].GoldCost + "g)", EquipButtonStyle)) { // Check if the player can afford it if (playerData.Gold >= partList[SelectedPart].GoldCost) { // Subtract the cost playerData.Gold -= partList[SelectedPart].GoldCost; // Set the parts owned status to true partsOwned[SelectedPart] = true; GameObject.FindObjectOfType <PlayerData>().playerData.PartsOwned[SelectedPart] = true; // Remember to save progress after each equip/unequip/purchase GameObject.FindObjectOfType <PlayerData>().SaveProgress(); } } // Backward button for page if (GUI.Button(new Rect(81 * ButtonScale, 100 * ButtonScale, 13 * ButtonScale, 7 * ButtonScale), "", BackButtonStyle)) { if (PartPage > 0) { PartPage--; } } // Forward button for page if (GUI.Button(new Rect(95 * ButtonScale, 100 * ButtonScale, 13 * ButtonScale, 7 * ButtonScale), "", ForwardButtonStyle)) { PartPage++; } break; case 3: // Settings // The usual 3x2 display position stuff X1 = 11 * ButtonScale; X2 = 71 * ButtonScale; X3 = 131 * ButtonScale; topY = 43 * ButtonScale; bottomY = 70 * ButtonScale; // Another 3x2 display. Quite liking how it looks /*for (int i = 0; i < 6; i++) * { * // The index of the X position to use; 1, 2 or 3 * int xi = (i > 2) ? i - 3 : i; * float ThisX = (xi == 0) ? X1 : ((xi == 1) ? X2 : X3); // if xi is 0, ThisX = X1, otherwise if xi = 1, ThisX = X2, otherwise ThisX = X3 * * // Draw the button * GUI.Button(new Rect(ThisX, i > 2 ? bottomY : topY, ButtonImg.width * ButtonScale, ButtonImg.height * ButtonScale), "A Setting!", ButtonStyle); * }*/ // Draw the button GUI.Box(new Rect(X1, topY, ButtonImg.width * ButtonScale, ButtonImg.height * ButtonScale), "Music Volume", SettingBoxStyle); SettingBoxStyle.fontSize = ButtonStyle.fontSize; float PlusButtonX = Screen.width * 0.078f; float MinusButtonX = Screen.width * 0.276f; float PlusButtonY = Screen.height * 0.448f; // Minus button if (GUI.Button(new Rect(MinusButtonX, PlusButtonY, 5 * ButtonScale, 5 * ButtonScale), "", PlusButtonStyle)) { // Modify volume value in the player data GameObject.FindObjectOfType <PlayerData>().playerData.MusicVolume += 0.05f; GameObject.FindObjectOfType <PlayerData>().SaveProgress(); // Then assign the volume Camera.main.GetComponent <AudioSource>().volume = GameObject.FindObjectOfType <PlayerData>().playerData.MusicVolume; } // Plus button if (GUI.Button(new Rect(PlusButtonX, PlusButtonY, 5 * ButtonScale, 5 * ButtonScale), "", MinusButtonStyle)) { // Modify volume value in the player data GameObject.FindObjectOfType <PlayerData>().playerData.MusicVolume -= 0.05f; GameObject.FindObjectOfType <PlayerData>().SaveProgress(); // Then assign the volume Camera.main.GetComponent <AudioSource>().volume = GameObject.FindObjectOfType <PlayerData>().playerData.MusicVolume; } break; } // Draw a back button if we're not on the main (main) menu if (MenuID != 0) { if (GUI.Button(new Rect(ButtonScale, Screen.height - BackButtonImg.height * ButtonScale - ButtonScale, BackButtonImg.width * ButtonScale, BackButtonImg.height * ButtonScale), "", BackButtonStyle)) { MenuID = 0; } } // Draw the amount of gold the player has if ShowGold == true if (ShowGold) { float goldScale = (Screen.width * 0.04f) / GoldIcon.width; TextStyle.fontSize = (int)(Screen.height * 0.04f); TextStyle.normal.textColor = Color.white; int goldAmount = GameObject.FindObjectOfType <PlayerData>().GetGoldAmount(); GUI.DrawTexture(new Rect(0, 0, GoldIcon.width * goldScale, GoldIcon.height * goldScale), GoldIcon); GUI.Label(new Rect(10 + 16 * goldScale, 4 * goldScale, 150, 50), goldAmount.ToString(), TextStyle); } }
// Use this for initialization void Start() { Application.targetFrameRate = 0; _this = this.gameObject.GetComponent <Basher>(); this.GetComponent <Damagable>().IsEnemy = false; if (parts == null) { parts = new BasherPart[MaxParts]; } if (enchantments == null) { enchantments = new BasherPart[MaxEnchantments]; } ResistMults = new float[MaxParts + MaxEnchantments]; // Set default Damagable values for the basher Damagable dmgb = this.GetComponent <Damagable>(); dmgb.Health = 100; dmgb.Resistance = 1; SpeedMultiplier = 1; // Fetch a copy of the player data to shorten the code a bit // and reduce CPU strain (slightly) PlayerData.SPData playerData = GameObject.FindObjectOfType <PlayerData>().playerData; // Add each part to the parts list with the help of the part database PartDatabase partDB = GameObject.FindObjectOfType <PartDatabase>(); // Create the parts for (int i = 0; i < MaxParts; i++) { parts[i] = CreatePart(playerData.Parts[i]); } // Create the enchantments for (int i = 0; i < MaxEnchantments; i++) { enchantments[i] = CreatePart(playerData.Enchantments[i]); } // Go through parts and call the Init function for (int i = 0; i < parts.Length; i++) { if (parts[i] != null) { parts[i].Initialise(ref _this); parts[i].SpawnPrefab(); } // Just set to 1 so we don't automatically become invincible ResistMults[i] = 1; } // Go through enchantments and call the Init function for (int i = 0; i < enchantments.Length; i++) { if (enchantments[i] != null) { enchantments[i].Initialise(ref _this); enchantments[i].SpawnPrefab(); } // Just set to 1 so we don't automatically become invincible ResistMults[MaxParts + i] = 1; } }