private void setAvailableTowersOfPlayer() { Type root = null; switch (PlayerMe.self.board.scienceType) { case ScienceType.PHYS: root = typeof(PhysicsTower); break; case ScienceType.CHEM: root = typeof(ChemistryTower); break; case ScienceType.BIO: root = typeof(BiologyTower); break; } PlayerMe.availableTowerTypes = new List <Type>(); for (int i = 0; i < UserMe.unlockedTowers.Length; i++) { if (UserMe.unlockedTowers[i]) { Type towerType = TypeIdGenerator.getTowerType(i); TowerNode towerNode = TypeIdGenerator.getTowerNodeInsts(towerType); if (UserMe.unlockedTowers[i] && towerNode.parent != null && towerNode.parent.towerType == root) { PlayerMe.availableTowerTypes.Add(towerType); } } } }
public void Deselect() { selectedNode = null; GetComponent <MeshRenderer>().enabled = false; rangeSphere.GetComponent <MeshRenderer>().enabled = false; UpdateButtonText(); }
static TowerNode FindAttachPoint(TowerNode rootNode, string nameToFind) { if (rootNode == null) { return(null); } foreach (var childName in rootNode.subTowerNames) { if (string.Equals(childName, nameToFind)) { return(rootNode); } } foreach (var tower in rootNode.subTowers) { var foundNode = FindAttachPoint(tower, nameToFind); if (foundNode != null) { return(foundNode); } } return(null); }
static TowerNode FindLastUnbalancedNode(TowerNode rootNode) { var valCount = new Dictionary <int, int>(); var valIndices = new Dictionary <int, int>(); for (int i = 0; i < rootNode.subTowers.Count; i++) { var st = rootNode.subTowers[i]; if (!valCount.ContainsKey(st.totalChildWeight)) { valCount[st.totalChildWeight] = 0; } valCount[st.totalChildWeight]++; valIndices[st.totalChildWeight] = i; } int unbalancedIndex = -1; foreach (var keypair in valCount) { if (keypair.Value == 1) { unbalancedIndex = valIndices[keypair.Key]; } } if (unbalancedIndex == -1) { // This is a balanced subtower return(rootNode); } else { return(FindLastUnbalancedNode(rootNode.subTowers[unbalancedIndex])); } }
public void SelectNode(TowerNode node) { selectedNode = node; UpdateButtonText(); //show selection GetComponent <MeshRenderer>().enabled = true; transform.position = node.transform.position; transform.position += new Vector3(0, 5, 0); //Radius circle, maybe add in once towers have models /* * if (selectedNode.tower != null) * { * rangeSphere.GetComponent<MeshRenderer>().enabled = true; * rangeSphere.transform.position = node.tower.transform.position; * rangeSphere.transform.parent = node.tower.transform; * * //rangeSphere.transform.localScale = Vector3.one * * //rangeSphere.transform.localScale *= node.tower.range; * } */ }
private bool checkScienceTypeOfTower(int typeId, ScienceType userScienceType) { Type root = null; switch (userScienceType) { case ScienceType.PHYS: root = typeof(PhysicsTower); break; case ScienceType.CHEM: root = typeof(ChemistryTower); break; case ScienceType.BIO: root = typeof(BiologyTower); break; } Type type = TypeIdGenerator.getTowerType(typeId); TowerNode node = TypeIdGenerator.getTowerNodeInst(type); if (node.towerType != root && node.getRoot().towerType == root) { return(true); } return(false); }
static void CalcTowerWeights(TowerNode rootNode) { rootNode.totalChildWeight = rootNode.weight; foreach (var subtower in rootNode.subTowers) { CalcTowerWeights(subtower); rootNode.totalChildWeight += subtower.totalChildWeight; } }
private bool checkIfTowerUpgradableTo(int currentTowerTypeId, Type upgradingTowerType) { TowerNode tn = TypeIdGenerator.getTowerNodeInst(upgradingTowerType); if (tn == null) { return(false); } return(tn.checkParent(currentTowerTypeId) != null); }
public void SetNode(TowerNode node) { if (currentNode == node) { DeselectNode(); return; } currentNode = node; turretBuilding = null; towerUI.SetTarget(node); }
private void HandleTowerSelection(GameObject hitObject) { var node = hitObject.GetComponent <TowerNode>(); if (node != null) { if (currentNode != null) { currentNode.OnCloseRequested(); } currentNode = node; node.OnNodeSelected(); } }
public void BuildTurret(TowerNode node) { if (PlayerStatus.iGold < turretBuilding.iCost) { Debug.Log("insufficient gold"); //add ui return; } PlayerStatus.iGold -= turretBuilding.iCost; GameObject turret = (GameObject)Instantiate(turretBuilding.pObject, node.transform.position + node.vOffset, Quaternion.identity); node.turretActive = turret; Debug.Log("turret built, gold left: " + PlayerStatus.iGold); }
//build turret on the tower node public void BuildTurretOn(TowerNode TowerNode) { if (PlayerStats.Money < turretToBuild.cost) { Debug.Log("Not Enough Money To Build That Turret"); return; } else { PlayerStats.Money -= turretToBuild.cost; GameObject turret = Instantiate(turretToBuild.prefab, TowerNode.GetBuildPosition(), Quaternion.identity); TowerNode.turret = turret; } }
public void BuyNode() { // do node buying PlayerMoneyManager.Instance.UseMoney(GetNextNodePrice()); TowerNode node = availableNodesToBuy[UnityEngine.Random.Range(0, availableNodesToBuy.Count)]; OnNodeBought(node); availableNodesToBuy.Remove(node); UpdateBuyText(); UpdateNodeBuyButtonState(); ShowUI(false); }
public void BuildTower(TowerNode node, TowerType towerType) { Tower tower = Instantiate(towerPrefabs[(int)towerType]); tower.transform.position = node.transform.position; tower.transform.SetParent(towerParent); tower.SetStatsGetter(towerStatGetter); tower.SetStats(0, towerStatGetter.GetTowerDamage(towerType, 0), towerStatGetter.GetTowerFireRate(towerType, 0), towerStatGetter.GetTowerRange(towerType, 0)); // hide node mesh node.TowerBuiltOnNode(); PlayerMoneyManager.Instance.UseMoney(PlayerMoneyManager.Instance.GetPrice(towerType, 0)); }
public void Init() { availableNodesToBuy = new List <TowerNode>(); foreach (TowerNode node in nodes) { if (!node.IsBought) { availableNodesToBuy.Add(node); } } for (int i = 0; i < startNodeCount; i++) { TowerNode nodeToActivate = availableNodesToBuy[UnityEngine.Random.Range(0, availableNodesToBuy.Count)]; OnNodeBought(nodeToActivate); availableNodesToBuy.Remove(nodeToActivate); } UpdateNodeBuyButtonState(); }
public void SetTarget(TowerNode n) { Vector3 vOffset = new Vector3(2f, -2f, -1.25f); this.targetNode = n; transform.position = targetNode.transform.position + vOffset; menu.SetActive(true); if (this.targetNode.turretActive.tag == "Mage") { button1Desc.text = "Speed"; button2Desc.text = "Burst"; button3Desc.text = "Range"; button1Cost.text = "400"; button2Cost.text = "600"; button3Cost.text = "350"; iChoice = 0; } if (this.targetNode.turretActive.tag == "Druid") { button1Desc.text = "Speed"; button2Desc.text = "Alchemy"; button3Desc.text = "Range"; button1Cost.text = "650"; button2Cost.text = "800"; button3Cost.text = "400"; iChoice = 1; } if (this.targetNode.turretActive.tag == "Rogue") { button1Desc.text = "Speed"; button2Desc.text = "2x Hit"; button3Desc.text = "Bleed"; button1Cost.text = "450"; button2Cost.text = "600"; button3Cost.text = "550"; iChoice = 2; } }
public void DeselectNode() { currentNode = null; towerUI.HideUI(); }
static public void Puzzle1() { string filename = "input/d7p1_b.txt"; var lines = File.ReadAllLines(filename); var rootNode = new TowerNode("", -1); // Just used to keep everything organized var allNodes = new List <TowerNode>(); // First create all the nodes and then organize them into a tree after foreach (var line in lines) { // Parse the line - aaaa (32) -> bbbb, cccc, dddd, ... var chunks = line.Split(' '); var name = chunks[0]; var weight = int.Parse(chunks[1].Split('(')[1].Split(')')[0]); string[] children = new string[0]; if (chunks.Length > 3) { children = new string[chunks.Length - 3]; for (int c = 3; c < chunks.Length; c++) { children[c - 3] = chunks[c].Split(',')[0]; } } var tower = new TowerNode(name, weight); tower.subTowerNames = children; allNodes.Add(tower); } // Now we can build the tree foreach (var node in allNodes) { // Find any nodes in the tree that need this node as a child and connect them TowerNode attachPoint = null; foreach (var tower in rootNode.subTowers) { attachPoint = FindAttachPoint(tower, node.name); if (attachPoint != null) { attachPoint.subTowers.Add(node); node.parentNode = attachPoint; break; } } if (attachPoint == null) { rootNode.subTowers.Add(node); } // Find any child nodes that are already in the tree and connect them for (int t = rootNode.subTowers.Count - 1; t >= 0; t--) { var tower = rootNode.subTowers[t]; foreach (var childName in node.subTowerNames) { if (string.Equals(childName, tower.name)) { // Remove the found root node and add it as a child to the new node node.subTowers.Add(tower); tower.parentNode = node; rootNode.subTowers.RemoveAt(t); } } } } Console.WriteLine("Number of root nodes: " + rootNode.subTowers.Count); Console.WriteLine("Root tower name: " + rootNode.subTowers[0].name); // Part 2 - Uses all the existing code plus more stuff CalcTowerWeights(rootNode.subTowers[0]); var unbalancedNode = FindLastUnbalancedNode(rootNode.subTowers[0]); var parent = unbalancedNode.parentNode; Console.Write("Tower weights: "); var valCount = new Dictionary <int, int>(); var valIndices = new Dictionary <int, int>(); for (int i = 0; i < parent.subTowers.Count; i++) { var st = parent.subTowers[i]; Console.Write(st.totalChildWeight + " "); if (!valCount.ContainsKey(st.totalChildWeight)) { valCount[st.totalChildWeight] = 0; } valCount[st.totalChildWeight]++; valIndices[st.totalChildWeight] = i; } Console.WriteLine(); int unbalancedIndex = -1; int balancedValue = -1; foreach (var keypair in valCount) { if (keypair.Value == 1) { unbalancedIndex = valIndices[keypair.Key]; } else { balancedValue = keypair.Key; } } Console.WriteLine("Index of unbalanced tower: " + unbalancedIndex); int weightDelta = parent.subTowers[unbalancedIndex].totalChildWeight - balancedValue; int newWeight = parent.subTowers[unbalancedIndex].weight - weightDelta; Console.WriteLine("Old weight: " + parent.subTowers[unbalancedIndex].weight + ", new weight: " + newWeight); }
private void saveNodes() { if (nodes.Count > 0) { // We fill with as many skills as nodes we have TowerBlueprint[] blueprints = new TowerBlueprint[nodes.Count]; int[] In_dependencies; int[] Out_dependencies; List <int> In_dependenciesList = new List <int>(); List <int> Out_dependenciesList = new List <int>(); // Iterate over all of the nodes. Populating the skills with the node info for (int i = 0; i < nodes.Count; ++i) { if (connections != null) { List <Connection> In_connectionsToRemove = new List <Connection>(); List <Connection> Out_connectionsToRemove = new List <Connection>(); for (int j = 0; j < connections.Count; j++) { if (connections[j].inPoint == nodes[i].inPoint) { for (int k = 0; k < nodes.Count; ++k) { if (connections[j].outPoint == nodes[k].outPoint) { //Debug.Log("node k = " + k); In_dependenciesList.Add(k); } } In_connectionsToRemove.Add(connections[j]); } if (connections[j].outPoint == nodes[i].outPoint) { for (int k = 0; k < nodes.Count; ++k) { if (connections[j].inPoint == nodes[k].inPoint) { Out_dependenciesList.Add(k); } } Out_connectionsToRemove.Add(connections[j]); } } } In_dependencies = In_dependenciesList.ToArray(); In_dependenciesList.Clear(); Out_dependencies = Out_dependenciesList.ToArray(); Out_dependenciesList.Clear(); TowerNode newTowerNode = (TowerNode)nodes[i].myInfo; blueprints[i] = newTowerNode.GetTower(); //Debug.Log(blueprints[i].name +" In dependecies length = " + In_dependencies.Length.ToString()); //Debug.Log(blueprints[i].name + "Out dependecies length = " + Out_dependencies.Length.ToString()); for (int k = 0; k < In_dependencies.Length; k++) { TowerNode towerNode = (TowerNode)nodes[In_dependencies[k]].myInfo; TowerBlueprint towerToAdd = ScriptableObject.CreateInstance <TowerBlueprint>(); towerToAdd = towerNode.GetTower(); string _path = workingFolders[0] + "/" + towerToAdd.name + ".asset"; TowerBlueprint _towerToAdd = AssetDatabase.LoadAssetAtPath(_path, typeof(TowerBlueprint)) as TowerBlueprint; //Debug.LogWarning("Tower to add type: " + towerToAdd.GetType()); blueprints[i].upgradesFrom.Add(_towerToAdd); string tempPath = workingFolders[0] + "/" + blueprints[i].name + ".asset"; TowerBlueprint tempBlueprintsI = AssetDatabase.LoadAssetAtPath(tempPath, typeof(TowerBlueprint)) as TowerBlueprint; TowerBlueprint tempTowerToAdd = _towerToAdd; //move this functionallity out of bluprints[i] loop } blueprints[i].setEditorPosition(new Vector2(nodes[i].nodeRect.x, nodes[i].nodeRect.y) + drag); //Debug.Log(blueprints[i].name + " Editor Position = " + blueprints[i].getEditorPosition()); string path = workingFolders[0] + "/" + blueprints[i].name + ".asset"; TowerBlueprint asset = AssetDatabase.LoadAssetAtPath(path, typeof(TowerBlueprint)) as TowerBlueprint; if (asset == null) { AssetDatabase.CreateAsset(blueprints[i], workingFolders[0] + "/" + blueprints[i].name + ".asset"); } else { EditorUtility.CopySerialized(blueprints[i], asset); } } for (int i = 0; i < blueprints.Length; i++) { for (int j = 0; j < blueprints[i].upgradesFrom.Count; j++) { for (int k = 0; k < blueprints.Length; k++) { if (blueprints[i].upgradesFrom[j].name == blueprints[k].name) { //Debug.Log(blueprints[k].name + " || " + blueprints[i].name); string tempOutPath = workingFolders[0] + "/" + blueprints[k].name + ".asset"; string tempInPath = workingFolders[0] + "/" + blueprints[i].name + ".asset"; TowerBlueprint tempOutTower = AssetDatabase.LoadAssetAtPath(tempOutPath, typeof(TowerBlueprint)) as TowerBlueprint; TowerBlueprint tempInTower = AssetDatabase.LoadAssetAtPath(tempInPath, typeof(TowerBlueprint)) as TowerBlueprint; blueprints[k].upgradesTo.Add(tempInTower); //tempTowerToAdd.upgradesTo.Add(tempBlueprintsI); EditorUtility.CopySerialized(blueprints[k], tempOutTower); } } } } Debug.Log("Towers have been saved/updated"); } }
private void LoadTowers() { Rect windoPosition = position; Vector2 newNodePosition = new Vector2(windoPosition.x, windoPosition.y); string[] result = AssetDatabase.FindAssets("t:TowerBlueprint", workingFolders); if (result.Length != 0) { if (nodes == null) { nodes = new List <Node>(); } if (result.Length <= nodes.Count) { //we have already loaded once Debug.LogWarning("We have already loaded the Towers once... no need to have duplicate nodes"); return; } foreach (String asset in result) { string path = AssetDatabase.GUIDToAssetPath(asset); tower = (TowerBlueprint)AssetDatabase.LoadAssetAtPath(path, typeof(TowerBlueprint)); Debug.Log("Towers Found = " + tower.name); TowerNode towerNode = new TowerNode(tower); if (tower.getEditorPosition() == null) { //Debug.Log("editor position is null"); tower.setEditorPosition(newNodePosition); } //Debug.Log(tower.name + " Editor position to load in = " + tower.getEditorPosition()); nodes.Add(new Node(tower.getEditorPosition(), 200, 50, nodeStyle, selectedNodeStyle, inPointStyle, outPointStyle, OnClickInPoint, OnClickOutPoint, OnClickRemoveNode, towerNode)); } /*for (int i = 0; i < nodes.Count; ++i) * { * for (int j = 0; j < nodes[i].skill.skill_Dependencies.Length; ++j) * { * if (skillDictionary.TryGetValue(nodes[i].skill.skill_Dependencies[j], out outSkill)) * { * for (int k = 0; k < nodes.Count; ++k) * { * if (nodes[k].skill.id_Skill == outSkill.id_Skill) * { * outNode = nodes[k]; * OnClickOutPoint(outNode.outPoint); * break; * } * } * OnClickInPoint(nodes[i].inPoint); * } * } * }*/ for (int i = 0; i < nodes.Count; i++) { //string curTowerName = nodes[i].myInfo.title; string path = AssetDatabase.GUIDToAssetPath(result[i]); tower = (TowerBlueprint)AssetDatabase.LoadAssetAtPath(path, typeof(TowerBlueprint)); for (int j = 0; j < tower.upgradesFrom.Count; j++) { string nameToMatch = tower.upgradesFrom[j].name; for (int k = 0; k < nodes.Count; ++k) { Node nodeToMatch; if (nodes[k].myInfo.title == nameToMatch) { nodeToMatch = nodes[k]; OnClickOutPoint(nodeToMatch.outPoint); OnClickInPoint(nodes[i].inPoint); break; } } } for (int j = 0; j < tower.upgradesTo.Count; j++) { } } } else { Debug.Log("No TowerBlueprints seems to exist yet"); } }
private void OnNodeBought(TowerNode node) { node.OnNodeBuy(); }
private static Dictionary <Type, TowerNode> generateInheritanceTowerTreeHashMap <T>() { Dictionary <Type, TowerNode> resultDictionary = new Dictionary <Type, TowerNode>(); Type type = typeof(T); var types = AppDomain.CurrentDomain.GetAssemblies() .SelectMany(s => s.GetTypes()) .Where(p => type.IsAssignableFrom(p) && p.IsClass && !p.IsAbstract); var abstypes = AppDomain.CurrentDomain.GetAssemblies() .SelectMany(s => s.GetTypes()) .Where(p => type.IsAssignableFrom(p) && p.IsClass && p.IsAbstract); List <Type> abstTypes = new List <Type>(); foreach (Type t in abstypes) { abstTypes.Add(t); } List <Type> typeList = new List <Type>(); List <Type> typeNodes = new List <Type>(); List <Type> nextTypes = new List <Type>(); List <Type> removeList = new List <Type>(); List <TowerNode> currentNodes = new List <TowerNode>(); List <TowerNode> nextNodes = new List <TowerNode>(); foreach (Type t in abstTypes) { TowerNode newNode = new TowerNode(); newNode.towerType = t; newNode.parent = null; currentNodes.Add(newNode); typeNodes.Add(t); } foreach (Type t in types) { typeList.Add(t); } for (int i = 0; i < abstTypes.Count; i++) { if (abstTypes[i] != type) { resultDictionary.Add(abstTypes[i], currentNodes[i]); } } while (typeList.Count > 0) { for (int j = 0; j < typeNodes.Count; j++) { for (int i = 0; i < typeList.Count; i++) { if (typeList[i].BaseType == typeNodes[j]) { TowerNode newNode = new TowerNode(); newNode.towerType = typeList[i]; newNode.parent = currentNodes[j]; currentNodes[j].children.Add(newNode); nextNodes.Add(newNode); nextTypes.Add(typeList[i]); resultDictionary.Add(typeList[i], newNode); removeList.Add(typeList[i]); } } } foreach (Type t in removeList) { typeList.Remove(t); } removeList.Clear(); currentNodes.Clear(); currentNodes.AddRange(nextNodes); nextNodes.Clear(); typeNodes.Clear(); typeNodes.AddRange(nextTypes); nextTypes.Clear(); } return(resultDictionary); }