public int calc(int x_1, int y_1, int x_2, int y_2) { CountryNode startCountry = root.findCountry(x_1, y_1); CountryNode endCountry = root.findCountry(x_2, y_2); int steps = 0; CountryNode currentCountry = startCountry; while (currentCountry != endCountry) { if (currentCountry.isInner(endCountry)) { foreach (var country in currentCountry.innerCountries) { if (country.isInner(endCountry) || country == endCountry) { currentCountry = country; } } } else { currentCountry = currentCountry.outerCountry; } steps++; } return(steps); }
public string ExportNode() { string contents = "{\n"; contents += "name:" + gameObject.name + "\n"; contents += "position:" + MeshMaker.ExportVector(gameObject.transform.position) + "\n"; contents += "unit:" + System.Enum.GetName(System.Type.GetType("UnitType"), unitNode.unitType) + "\n"; contents += "unit-displace:" + MeshMaker.ExportVector(unitNode.gameObject.transform.position - gameObject.transform.position) + "\n"; string slinks = "link-to:"; foreach (CLink link in links) { CNode otherNode = link.nodes.Find(x => x != this); if (otherNode.GetType() == System.Type.GetType("CountryNode")) { CountryNode country = otherNode as CountryNode; slinks += country.country.countryName + "[" + country.country.nodes.IndexOf(country).ToString() + "]"; if (links.IndexOf(link) != links.Count - 1) { slinks += ","; } //e.g. Moscow[0] would be Moscow's base node - the number really only matters for coasts, which have multiple nodes } } slinks += "\n"; contents += slinks; contents += "}\n"; return(contents); }
public void MergeInto(Country other) { if (other == this) { return; } while (territories.Count > 0) { AddTo(territories[0], other); } while (nodes.Count > 0) { CountryNode node = nodes[0]; foreach (CLink link in node.links) //Merge the other links made into this one { CNode otherNode = link.nodes.Find(x => x != node && x.GetType() != System.Type.GetType("UnitNode")); if (otherNode != null) { other.nodes[0].EstablishLink(otherNode); } } nodes.Remove(node); GameObject.Destroy(node.gameObject); } faction = null; territories = null; Camera.main.GetComponent <CanvasCreator> ().countries.Remove(this); }
public CountryTree(int[] x, int[] y, int[] r) { var circles = new List <Circle>(); for (int i = 0; i < x.Length; ++i) { circles.Add(new Circle(x[i], y[i], r[i])); } circles.Sort((a, b) => a.R - b.R); var allCountries = new List <CountryNode> (); foreach (var circle in circles) { allCountries.Add(new CountryNode(circle)); } foreach (var newCountry in allCountries) { foreach (var country in allCountries.Where(country => country.outerCountry == null).Where(country => newCountry.isInner(country))) { setRelation(newCountry, country); } } root = new CountryNode(new Circle(0, 0, int.MaxValue)); foreach (var country in allCountries.Where(country => country.outerCountry == null)) { setRelation(root, country); } }
public CountryTree(int[] x, int[] y, int[] r) { var circles = new List<Circle>(); for(int i = 0; i < x.Length; ++i) circles.Add(new Circle(x[i], y[i], r[i])); circles.Sort ((a, b) => a.R - b.R); var allCountries = new List<CountryNode> (); foreach (var circle in circles) allCountries.Add (new CountryNode (circle)); foreach (var newCountry in allCountries) foreach (var country in allCountries.Where(country => country.outerCountry == null).Where (country => newCountry.isInner (country))) setRelation (newCountry, country); root = new CountryNode (new Circle (0, 0, int.MaxValue)); foreach (var country in allCountries.Where(country => country.outerCountry == null)) setRelation (root, country); }
public void ColorNodes() { for (int i = 0; i < nodes.Count; i++) { CountryNode node = nodes [i]; if (i == 0 && centre == true) { node.GetComponent <MeshRenderer> ().material.color = Color.red; } else { node.GetComponent <MeshRenderer> ().material.color = Color.blue; } } }
private void setRelation(CountryNode outerCountry, CountryNode innerCountry) { outerCountry.innerCountries.Add(innerCountry); innerCountry.outerCountry = outerCountry; }
public bool isInner(CountryNode node) { return(node.R < R && (node.X - X) * (node.X - X) + (node.Y - Y) * (node.Y - Y) < (Int64)R * (Int64)R); }
public override void ClickUpdate() { if (Input.GetMouseButtonUp(0)) { Ray castRay = Camera.main.ScreenPointToRay(Input.mousePosition); //Debug.Log ("Raycasting from " + castRay.origin.ToString()); RaycastHit hit; if (Physics.Raycast(castRay, out hit) && hit.collider != null) { GameObject hitObject = hit.collider.gameObject; if (hitObject.GetComponent <CountryNode>() != null) { CountryNode node = hitObject.GetComponent <CountryNode>(); if (lastNode != null && node != lastNode) { CLink activeLink = lastNode.links.Find(x => x.nodes.Contains(node)); if (activeLink != null) { GameObject.Destroy(activeLink.gameObject); } else { lastNode.EstablishLink(node); } } else if (lastNode) { lastNode.menuOpen = !lastNode.menuOpen; } lastNode = null; } } } if ((Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1)) && GUIUtility.hotControl == 0) //We check that we haven't clicked on an active GUI element { Ray castRay = Camera.main.ScreenPointToRay(Input.mousePosition); //Debug.Log ("Raycasting from " + castRay.origin.ToString()); RaycastHit hit; if (Physics.Raycast(castRay, out hit) && hit.collider != null) { GameObject hitObject = hit.collider.gameObject; if (hitObject.GetComponent <CountryObject>() != null) { CountryObject country = hitObject.GetComponent <CountryObject>(); if (Input.GetMouseButtonDown(0)) { country.parentMenuOpen = !country.parentMenuOpen; country.menuOpen = false; } else if (Input.GetMouseButtonDown(1)) { country.menuOpen = !country.menuOpen; country.parentMenuOpen = false; } } if (hitObject.GetComponent <CountryNode>() != null) { CountryNode node = hitObject.GetComponent <CountryNode>(); if (Input.GetMouseButtonDown(0)) { lastNode = node; node.StartCoroutine("LinkDraw"); } if (Input.GetMouseButton(1)) { node.StartCoroutine("NodeMove"); } } if (hitObject.GetComponent <UnitNode>() != null) { UnitNode unitNode = hitObject.GetComponent <UnitNode>(); if (Input.GetMouseButtonDown(1)) { unitNode.StartCoroutine("NodeMove"); } else if (Input.GetMouseButtonDown(0)) { unitNode.menuOpen = !unitNode.menuOpen; } } } } }
void OnGUI() { if (menuOpen == false && parentMenuOpen == false) { return; } Vector3 boxPos = Camera.main.WorldToScreenPoint(gameObject.transform.position - new Vector3(0f, 0f, 10f)); float screenX = boxPos.x; float screenY = (Screen.height - boxPos.y); float guiWidth = 150; float guiHeight = 80; if (parentMenuOpen == true) { guiHeight = 250; } if (screenX > Screen.width - guiWidth) { screenX -= guiWidth; } if (screenY > Screen.height - guiHeight) { screenY -= guiHeight; } GUI.BeginGroup(new Rect(screenX, screenY, guiWidth, guiHeight)); GUI.Box(new Rect(0, 0, guiWidth, guiHeight), "Options"); if (menuOpen == true) { string buttonName = "Make Sea"; if (land == false) { buttonName = "Make Land"; } if (GUI.Button(new Rect(10, 20, guiWidth - 20, 20), buttonName)) { land = !land; ColorToFaction(); } if (parentCountry.territories.Count > 1) { if (GUI.Button(new Rect(10, 50, guiWidth - 20, 20), "Split Off")) { parentCountry.RemoveTerritory(this); } } } if (parentMenuOpen == true) { parentCountry.countryName = GUI.TextField(new Rect(10, 30, guiWidth - 20, 20), parentCountry.countryName); bool lastLocked = parentCountry.locked; parentCountry.locked = GUI.Toggle(new Rect(10, 60, guiWidth - 20, 20), parentCountry.locked, "Locked"); if (parentCountry.locked != lastLocked) { parentCountry.ColorTerritories(); } string[] toolButtons = new string[parentCC.factions.Count]; for (int i = 0; i < toolButtons.Length; i++) { if (parentCC.factions[i].factionName == "") { toolButtons[i] = "N/A"; } else { toolButtons[i] = parentCC.factions[i].factionName.Substring(0, 1); } } int factionCount = -1; factionCount = GUI.Toolbar(new Rect(10, 90, guiWidth - 20, 30), factionCount, toolButtons); if (factionCount != -1 && factionCount != parentCC.factions.IndexOf(parentCountry.faction)) { parentCC.factions[factionCount].AddCountry(parentCountry); } parentCountry.land = GUI.Toggle(new Rect(10, 130, guiWidth - 20, 20), parentCountry.land, "Land"); if (parentCountry.land == true) { bool lastCentre = parentCountry.centre; parentCountry.centre = GUI.Toggle(new Rect(10, 160, guiWidth - 20, 20), parentCountry.centre, "Centre"); if (lastCentre != parentCountry.centre) { parentCountry.ColorNodes(); } } else if (parentCountry.centre == true) //can't have a centre in a sea territory { parentCountry.centre = false; parentCountry.ColorNodes(); } if (parentCountry.nodes.Count > 1) { if (GUI.Button(new Rect(10, 210, 30, 30), "-")) { CountryNode toDelete = parentCountry.nodes[parentCountry.nodes.Count - 1]; parentCountry.nodes.Remove(toDelete); Destroy(toDelete.gameObject); } } GUI.Label(new Rect(60, 210, 30, 30), parentCountry.nodes.Count.ToString()); if (GUI.Button(new Rect(100, 210, 30, 30), "+")) { parentCountry.CreateCountryNode(); } } GUI.EndGroup(); }
public override void ClickUpdate() { if (Input.GetMouseButtonUp (0)) { Ray castRay = Camera.main.ScreenPointToRay (Input.mousePosition); //Debug.Log ("Raycasting from " + castRay.origin.ToString()); RaycastHit hit; if (Physics.Raycast (castRay, out hit) && hit.collider != null) { GameObject hitObject = hit.collider.gameObject; if(hitObject.GetComponent<CountryNode>() != null) { CountryNode node = hitObject.GetComponent<CountryNode>(); if(lastNode != null && node != lastNode) { CLink activeLink = lastNode.links.Find (x => x.nodes.Contains(node)); if(activeLink != null) { GameObject.Destroy(activeLink.gameObject); } else { lastNode.EstablishLink(node); } } else if(lastNode) { lastNode.menuOpen = !lastNode.menuOpen; } lastNode = null; } } } if ((Input.GetMouseButtonDown (0) || Input.GetMouseButtonDown(1)) && GUIUtility.hotControl==0) { //We check that we haven't clicked on an active GUI element Ray castRay = Camera.main.ScreenPointToRay (Input.mousePosition); //Debug.Log ("Raycasting from " + castRay.origin.ToString()); RaycastHit hit; if (Physics.Raycast (castRay, out hit) && hit.collider != null) { GameObject hitObject = hit.collider.gameObject; if(hitObject.GetComponent<CountryObject>() != null) { CountryObject country = hitObject.GetComponent<CountryObject>(); if(Input.GetMouseButtonDown (0)) { country.parentMenuOpen = !country.parentMenuOpen; country.menuOpen = false; } else if(Input.GetMouseButtonDown(1)) { country.menuOpen = !country.menuOpen; country.parentMenuOpen = false; } } if(hitObject.GetComponent<CountryNode>() != null) { CountryNode node = hitObject.GetComponent<CountryNode>(); if(Input.GetMouseButtonDown (0)) { lastNode = node; node.StartCoroutine("LinkDraw"); } if(Input.GetMouseButton(1)) { node.StartCoroutine ("NodeMove"); } } if(hitObject.GetComponent<UnitNode>() != null) { UnitNode unitNode = hitObject.GetComponent<UnitNode>(); if(Input.GetMouseButtonDown(1)) { unitNode.StartCoroutine("NodeMove"); } else if(Input.GetMouseButtonDown(0)) { unitNode.menuOpen = !unitNode.menuOpen; } } } } }
private void setRelation(CountryNode outerCountry, CountryNode innerCountry) { outerCountry.innerCountries.Add (innerCountry); innerCountry.outerCountry = outerCountry; }
public bool isInner(CountryNode node) { return node.R < R && (node.X - X) * (node.X - X) + (node.Y - Y) * (node.Y - Y) < (Int64)R * (Int64)R; }