//select passive powerup when clicking on an icon in the panel public void SelectPassivePowerUp(int index) { //if the exit menu is active, do nothing if (panels.main.activeInHierarchy) { return; } //try to set the current passive selection gui.SelectPassivePowerUp(index); //display selected powerup properties PassivePowerUp powerup = gui.GetPassivePowerUp(); labels.passivePUName.text = powerup.name; labels.passivePUDescription.text = powerup.description; for (int i = 0; i < powerup.cost.Length; i++) { labels.passivePUPrice[i].text = "$" + powerup.cost[i]; } //show or hide the buy button based on its state if (powerup.enabled || powerup.locked) { buttons.button_buyPassive.SetActive(false); } else { buttons.button_buyPassive.SetActive(true); } }
//forward call to the PowerUpManager //activate passive powerup selection public bool ActivatePassivePowerUp() { if (!IsPassivePowerUpSelected()) { return(false); } PassivePowerUp powerup = GetPassivePowerUp(); bool affordable = true; //loop through resources for (int i = 0; i < GameHandler.resources.Length; i++) { //check if we can afford buying this powerup if (GameHandler.resources[i] < powerup.cost[i]) { affordable = false; break; } } if (!affordable) { return(false); } //affordable is true, substract resources for (int i = 0; i < powerup.cost.Length; i++) { GameHandler.resources[i] -= powerup.cost[i]; } //activate powerup powerUpScript.ActivatePassive(); return(true); }
//select passive powerup based on list indices public void SelectPassivePowerUp(int index) { if (passivePowerUps.Count - 1 < index) { Debug.LogWarning("Button index " + index + " does not match PowerUps list count."); } passivePowerUp = passivePowerUps[index]; }
void OnUndoRedoPerformed() { Debug.Log("Undo Redo called"); battlePowerUpToEdit = null; passivePowerUpToEdit = null; selectedBattle = null; selectedPassive = null; }
//loops through passive powerups and tries to find new unlocks public void UnlockPassive() { for (int i = 0; i < passivePowerUps.Count; i++) { PassivePowerUp powerup = passivePowerUps[i]; bool state = true; for (int j = 0; j < powerup.req.Count; j++) { //found an inactive powerup in the requirements, //which means this powerup can't be unlocked yet if (!passivePowerUps[powerup.req[j]].enabled) { state = false; break; } } //if no inactive powerups were found, unlock if (state) { powerup.Unlock(); } } }
//helper method to remove a passive powerup and cleaning up requirement references void RemovePassiveReq(int index) { //loop through powerups for (int i = 0; i < powerUpScript.passivePowerUps.Count; i++) { PassivePowerUp powerup = powerUpScript.passivePowerUps[i]; //backward loop over requirements for (int j = powerup.req.Count - 1; j >= 0; j--) { //this powerup has the old powerup as a requirement, //remove it if (powerup.req[j] == index) { powerup.req.RemoveAt(j); continue; } //lower all requirements above the removed powerup index if (powerup.req[j] > index) { powerup.req[j] -= 1; } } } }
//draws the passive power up requirement window void DrawPassivePowerUpsReqEditor() { //do not draw the following editor window //if no editable selection was set if (passivePowerUpToEdit == null) { GUILayout.BeginArea(new Rect((Screen.width / 2) + 50, (Screen.height / 2), 150, 100)); GUILayout.Label("No Power Up selected.", EditorStyles.boldLabel); GUILayout.EndArea(); return; } //fetch serialized values script.Update(); //yellow lines, symbolizing dependencies GUI.color = Color.yellow; GUI.Box(new Rect(527, 238, 5, 40), ""); GUI.Box(new Rect(327, 238, 5, 100), ""); GUI.Box(new Rect(728, 238, 5, 100), ""); GUI.Box(new Rect(327, 334, 149, 5), ""); GUI.Box(new Rect(584, 334, 149, 5), ""); GUI.color = Color.white; //display sprite boxes surrounding the sprite Rect myPos = new Rect(490, 290, 80, 80); GUI.Box(new Rect(myPos.x - 5, myPos.y - 5, myPos.width + 10, myPos.height + 25), ""); GUI.Box(myPos, ""); //draw sprite texture with calculated uv offset, //along with the powerup name if (selectedPassive.icon && selectedPassive.icon.sprite) { GUI.color = selectedPassive.icon.color; GUI.DrawTexture(myPos, selectedPassive.icon.mainTexture); GUI.color = Color.white; } GUI.Label(new Rect(myPos.x, myPos.y + myPos.height, myPos.width, 20), selectedPassive.name); //begin a scrolling view inside GUI, pass in current Vector2 scroll position scrollPosPassiveReqEditor = EditorGUILayout.BeginScrollView(scrollPosPassiveReqEditor, GUILayout.Width(530), GUILayout.Height(175)); //invisible label for dynamically resizing the window width based on requirement count int windowWidth = 10 + selectedPassive.req.Count * 100; GUILayout.Label("", GUILayout.Width(windowWidth), GUILayout.Height(0)); //tint the following controls in yellow if we have a dragged item if (currentlyDragged >= 0) { GUI.color = Color.yellow; } //draw window for dropping the currently dragged powerup into and cache dimensions GUILayout.Box("", GUILayout.ExpandWidth(true), GUILayout.Height(150)); Rect dropBox = GUILayoutUtility.GetLastRect(); GUI.color = Color.white; //we released our dragged item if (Event.current.type == EventType.MouseUp && currentlyDragged >= 0) { //if the mouse was inside the drop box, //add the dragged powerup as a new requirement to the current one if (dropBox.Contains(Event.current.mousePosition) && selectedPassive != powerUpScript.passivePowerUps[currentlyDragged] && !selectedPassive.req.Contains(currentlyDragged)) { selectedPassive.req.Add(currentlyDragged); guiChange = true; this.Repaint(); } else { Debug.Log("Can't add Requirement. The selected Powerup already contains it."); } //reset drag index currentlyDragged = -1; } //if the powerup does not contain a requirement yet, show a info label if (selectedPassive.req.Count == 0) { GUI.Label(new Rect(160 + dropBox.x, 60 + dropBox.y, 200, 20), "Drag & Drop Powerups HERE", EditorStyles.boldLabel); EditorGUILayout.EndScrollView(); return; } //loop through requirements and draw them on screen, //just as we did with the powerup icon sprite for (int i = 0; i < selectedPassive.req.Count; i++) { PassivePowerUp req = powerUpScript.passivePowerUps[selectedPassive.req[i]]; Rect pos = new Rect(20 + i * 100, 20, 80, 80); GUI.Box(new Rect(pos.x - 5, pos.y - 5, pos.width + 10, pos.height + 50), ""); GUI.Box(pos, ""); if (req.icon && req.icon.sprite) { GUI.color = req.icon.color; GUI.DrawTexture(pos, req.icon.mainTexture); GUI.color = Color.white; } GUI.Label(new Rect(pos.x, 20 + pos.height, pos.width, 20), req.name); //additionally show a button for removing the requirement if (GUI.Button(new Rect(pos.x, 40 + pos.height, pos.width, 20), "X")) { selectedPassive.req.RemoveAt(i); return; } } //ends the scrollview defined above EditorGUILayout.EndScrollView(); //push modified values back to the manager script script.ApplyModifiedProperties(); }
//draws a list for all battle powerups void DrawPassivePowerUpsSelector() { EditorGUILayout.BeginVertical(GUILayout.Width(270)); //begin a scrolling view inside GUI, pass in current Vector2 scroll position scrollPosPassiveSelector = EditorGUILayout.BeginScrollView(scrollPosPassiveSelector, false, true, GUILayout.Height(325), GUILayout.Width(270)); GUI.backgroundColor = Color.white; //iterate over all passive powerups for (int i = 0; i < powerUpScript.passivePowerUps.Count; i++) { GUI.Box(new Rect(5, i * 28, 25, 25), i + " "); //compare powerup in the list with the currently selected one //if it's the selected one, tint the background yellow if (powerUpScript.passivePowerUps[i] == selectedPassive) { GUI.backgroundColor = Color.yellow; } Rect box = new Rect(25, i * 28, 225, 25); GUI.Box(box, ""); GUI.backgroundColor = Color.white; box.width = 150; //re-use background box with smaller width for determining currently dragged index if (Event.current.type == EventType.MouseDown && box.Contains(Event.current.mousePosition)) { currentlyDragged = i; } else if (Event.current.type == EventType.MouseUp) { currentlyDragged = -1; } //repaint window after mouse events this.Repaint(); } //iterate over the powerup list for (int i = 0; i < powerUpScript.passivePowerUps.Count; i++) { //get the current powerup PassivePowerUp powerup = powerUpScript.passivePowerUps[i]; EditorGUILayout.BeginHorizontal(); GUILayout.Space(30); //draw the powerup name GUILayout.Label(powerup.name, GUILayout.Width(145)); //draw edit button that selects this powerup if (GUILayout.Button("Edit", GUILayout.Width(35))) { //select powerup as editable reference selectedPassive = powerup; SerializedProperty prop = script.FindProperty("passivePowerUps"); if (prop.arraySize > i) { passivePowerUpToEdit = prop.GetArrayElementAtIndex(i); } else { script.Update(); return; } //deselect other gui fields, //otherwise user input is carried over GUIUtility.hotControl = 0; GUIUtility.keyboardControl = 0; } //draw delete button for this powerup if (GUILayout.Button("X", GUILayout.Width(25))) { Undo.RecordObject(powerUpScript, "DeletePowerup"); Undo.RecordObject(this, "DeletePowerup"); //unset editable selection selectedPassive = null; passivePowerUpToEdit = null; //remove powerup in the main list //remove references to other requirements RemovePassiveReq(i); powerUpScript.passivePowerUps.RemoveAt(i); //mark that the gui changed and update the scipt values guiChange = true; script.Update(); return; } EditorGUILayout.EndHorizontal(); EditorGUILayout.Space(); } //ends the scrollview defined above EditorGUILayout.EndScrollView(); //add a new offensive powerup to the list if (GUILayout.Button("Add Passive Power Up", GUILayout.Height(40))) { //UNITY CRASHES IF THESE ARE ADDED //Undo.RecordObject(powerUpScript, "AddPassive"); //Undo.RecordObject(this, "AddPassive"); //create new instance PassivePowerUp newPass = new PassivePowerUp(); //insert new powerup at the end of the list powerUpScript.passivePowerUps.Add(newPass); //mark that the gui changed and update the script values guiChange = true; script.Update(); //select the newly created powerup, //also select the powerup as active selection for editing selectedPassive = newPass; passivePowerUpToEdit = script.FindProperty("passivePowerUps").GetArrayElementAtIndex(powerUpScript.passivePowerUps.Count - 1); } EditorGUILayout.EndVertical(); }