public void InitializePlayers() { //spawn Player GameObject pgo = PlayerPreset; IPlayer p = pgo.GetComponent <IPlayer>(); RoundHandler.PlayerList.Enqueue(p); DotHandler d = GameHandler.gm.StartPoints[0]; playerstart = d; pgo.transform.position = d.transform.position; d.Owner = p; d.UpdateStrength(3); p.StartPlayer = true; p.isTurn = true; foreach (Dialog dial in dialogs) { if (dial != null) { dial.transform.localScale *= UIScale; } } DialogPos = dialogs[0].transform.position; //spawn aiPlayer pgo = AiPreset; p = pgo.GetComponent <IPlayer>(); RoundHandler.PlayerList.Enqueue(p); d = GameHandler.gm.StartPoints[13]; d.Owner = p; d.UpdateStrength(3); NextButtonInteraction(); }
public Connection InitializeAbstractConnection(DotHandler Origin, DotHandler Destination) { Connection ConnectTemp = GetComponent <Connection>(); ConnectTemp.cube = GetComponentInChildren <SpriteRenderer>().transform; Debug.Log("Origin: " + Origin); Debug.Log("Destination: " + Destination); if (Destination.transform.position.y > Origin.transform.position.y) { ConnectTemp.origin = Origin; ConnectTemp.destination = Destination; } else { ConnectTemp.origin = Destination; ConnectTemp.destination = Origin; } ConnectTemp.DrawConnection(); name = Origin.name + "-" + Destination.name; Origin.Connections.Add(ConnectTemp); Destination.Connections.Add(ConnectTemp); cube.gameObject.layer = 8; GetComponentInChildren <SpriteRenderer>().color = Origin.Owner.playercolor; Debug.Log("updated color"); Abs = false; return(ConnectTemp); }
public void AttackDot(DotHandler Destination, int Amount) { if (Amount > 0 && Amount < 20 && Strength > 1) { //if in the right conditions to make an attack if (Destination.Strength <= 1) { //takeover attack Destination.OnSwitchPlayer(Owner); CreateConnection(this, Destination); UpdateList(); Destination.UpdateList(); UpdateStrength(-Amount); Destination.UpdateStrength(Amount - 1); } else { Debug.Log("Amount: " + Amount); //normal attack (both nodes damage eachother) UpdateStrength(-1); Destination.UpdateStrength(-1); AttackDot(Destination, Amount - 1); } } }
void TakeOverDot(DotHandler subjectDot, DotHandler otherDothandler) { otherDothandler.Owner = this; DotHandler.CreateConnection(subjectDot, otherDothandler); DotHandler.EnergyMove(subjectDot, otherDothandler, subjectDot.Strength - 1, 1); RoundHandler.CurrPlayerMove.playerDotHandlers.Add(otherDothandler); }
public static bool EnergyMove(DotHandler from, DotHandler to, int Amount, int evaporation = 0) { if (!(from.Strength - Amount < 1) && !(to.Strength + Amount - evaporation > 6)) { from.UpdateStrength(-Amount); to.UpdateStrength(Amount - evaporation); return(true); } return(false); }
public static DotFragment CreateDotFragment(DotHandler dotHandler, int level) { float size = level * 1.5f; GameObject gameObj = Instantiate(GameHandler.gm.FragmentPrefab, dotHandler.gameObject.transform); DotFragment Dotfragment = gameObj.GetComponent <DotFragment>(); Dotfragment.level = level; Dotfragment.Draw(); dotHandler.UpdateRecognition(); return(Dotfragment); }
public static Connection FindConnectionBetween(DotHandler Origin, DotHandler Destination) { foreach (Connection c in FindObjectsOfType <Connection>()) { if ((c.origin == Origin && c.destination == Destination) || (c.origin == Destination && c.destination == Origin) && (c.destination != null && c.origin != null)) { return(c); } } return(null); }
public DotHandler[] GetNearbyDotHandlers(float Multiplier) { List <DotHandler> dotHandlers = new List <DotHandler>(); Collider2D[] temp = Physics2D.OverlapCircleAll(transform.position, GameHandler.gm.tilling.Radius * Multiplier); for (int i = 0; i < temp.Length; i++) { DotHandler checkD = temp[i].gameObject.GetComponent <DotHandler>(); if (checkD != null) { dotHandlers.Add(checkD); } } return(dotHandlers.ToArray()); }
public void OnRegisterEnter(DotHandler node) { if (!RoundHandler.energyDistributionState && selectedDot != null) { selectedDotBefore = selectedDot; selectedDot = node; Debug.Log(selectedDotBefore + " // " + selectedDot); SecondSelected = true; if (selectedDot.Owner != null && selectedDotBefore.Owner != selectedDot.Owner) { Destroy(AbsConnection.gameObject); AbsConnection = null; } } }
public ClickRegist(DotHandler selectdot, DotFragment d = null) { if (!RoundHandler.energyDistributionState) { Debug.Log("dotfragments: " + (selectdot.DotFragments.Count) + " // " + d.level); for (int i = selectdot.DotFragments.Count - 1; i > d.level - 1; i--) { Debug.Log("dotfragments: " + selectdot.DotFragments[i]); selectdot.DotFragments[i].SelectColor(true); } if (selectdot.Owner != RoundHandler.CurrPlayerMove) { CancelBuild(6); } //checks if the player owns this node selectedDot = selectdot; if (d == null) { energyOnSelect = selectedDot.Strength; } else { energyOnSelect = selectedDot.Strength - d.level + 1; } //initializes energy selected AbsConnection = CreateConnection(selectedDot.transform.position, selectedDot.Owner.cam.ScreenToWorldPoint(Input.mousePosition)); AbsConnection.cube.gameObject.layer = 9; selectedDot.OnDrag = true; //creates AbsConnection and sets drag to true Debug.Log(energyOnSelect + " // " + d.level); foreach (Connection c in selectdot.Connections) { if (c != null) { c.cube.gameObject.layer = 9; } } } }
public static DotHandler[] HexagonalTilling(int rangeX, int rangeY, float radius, Vector2 offset) ///Hexagonal Tilling { List <Vector2> seen = new List <Vector2>(); List <DotHandler> anchors = new List <DotHandler>(); Vector2 center = offset; int posX = 0; int posY = 0; int timesrun = 0; Debug.Log(center.x + " not Looping " + rangeX); while (posY != rangeY && timesrun < rangeX * rangeY && timesrun < 120) { timesrun++; for (int i = 0; i < 6; i++) { float angleRad = (60 * i - 30) * Mathf.Deg2Rad; Vector2 p = new Vector2(center.x + radius * Mathf.Cos(angleRad), center.y + radius * Mathf.Sin(angleRad)); p.x = Mathf.Round(p.x * 10000) / 10000; p.y = Mathf.Round(p.y * 10000) / 10000; if (!seen.Contains(p)) { seen.Add(p); DotHandler anchor = CreateNode(p); anchor.name = "Hex(" + posX + ", " + posY + ")[" + i + "]"; anchors.Add(anchor); } } if (posX == rangeX - 1) { posY++; posX = 0; center.x = 0f; center.y += (radius + 0.5f * radius) * 2; } else { posX++; center.x += Mathf.Cos(30 * Mathf.Deg2Rad) * radius * 2; } } return(anchors.ToArray()); }
public void InitializePlayers() { for (int i = 0; i < players; i++) { GameObject pgo = Instantiate(PlayerPresets[i]); IPlayer p = pgo.GetComponent <IPlayer>(); RoundHandler.PlayerList.Enqueue(p); int RandNum = (int)(Random.value * GameHandler.gm.StartPoints.Count - 1); DotHandler d = GameHandler.gm.StartPoints[RandNum]; pgo.transform.position = d.transform.position; d.Owner = p; d.UpdateStrength(3); if (i == 0) { p.StartPlayer = true; p.isTurn = true; } } }
public static Connection CreateBindConnection(DotHandler ClickDot) //will create a new connection between 2 existing dots { bool alreadyExisting = false; foreach (Connection i in gm.ConnectionFolder.GetComponentsInChildren <Connection>()) { if ((i.origin == CurrDot && i.destination == ClickDot) | (i.destination == CurrDot && i.origin == ClickDot)) { alreadyExisting = true; return(i); } } if (!alreadyExisting) { Debug.Log(CurrDot + " created " + ClickDot); Connection c = CreateConnection(ClickDot, CurrDot); CurrDot.UpdateStrength(-1); return(c); } return(null); }
public static Connection CreateConnection(DotHandler Origin, DotHandler Destination) //static form for ConnectionCreation { GameObject temp = Instantiate(gm.ConnectionPrefab, gm.ConnectionFolder) as GameObject; Connection ConnectTemp = temp.GetComponent <Connection>(); ConnectTemp.cube = temp.GetComponentInChildren <SpriteRenderer>().transform; if (Destination.transform.position.y > Origin.transform.position.y) { ConnectTemp.origin = Origin; ConnectTemp.destination = Destination; } else { ConnectTemp.origin = Destination; ConnectTemp.destination = Origin; } ConnectTemp.DrawConnection(); temp.name = Origin.name + "-" + Destination.name; Origin.Connections.Add(ConnectTemp); Destination.Connections.Add(ConnectTemp); return(ConnectTemp); }
void AttackOtherDot(DotHandler subject, DotHandler otherDot) { subject.AttackDot(otherDot, subject.Strength - 1); RoundHandler.CurrPlayerMove.playerDotHandlers.Add(otherDot); }
public DotHandler CreateNewNode(DotHandler anchor) //will create a new node and connection with initialization when called { anchor.UpdateStrength(1); return(anchor); }
public static DotHandler[] HexagonalTilling(int range, float radius, Vector2 offset) ///Hexagonal Tilling { int currRange = 0; List <Vector2> seen = new List <Vector2>(); List <DotHandler> anchors = new List <DotHandler>(); Vector2 center = offset; Vector2 currCenter = center; float deltaX = Mathf.Sin(60 * Mathf.Deg2Rad) * radius, deltaY = radius * 1.5f; for (int j = 0; j < 6; j++) { currCenter = center; for (int i = 0; i < 6; i++) { float angleRad = (60 * i - 30) * Mathf.Deg2Rad; Vector2 p = new Vector2(currCenter.x + radius * Mathf.Cos(angleRad), currCenter.y + radius * Mathf.Sin(angleRad)); p.x = Mathf.Round(p.x * 1000) / 1000; p.y = Mathf.Round(p.y * 1000) / 1000; if (!seen.Contains(p)) { seen.Add(p); DotHandler anchor = CreateNode(p); anchor.name = "Hex(" + currRange + ")[" + j + "][" + i + "]"; anchor.elevation = range - currRange; if (anchor.elevation == 1) { gm.StartPoints.Add(anchor); } anchor.Gfx.GetComponent <SpriteRenderer>().color = new Color(255, 215, 0); anchor.MaxStrength = 10; gm.GoldNodes.Add(anchor); anchors.Add(anchor); } } } while (currRange != range) { int hexag; Vector2 P_vPos = new Vector2(center.x + deltaX * 2 * (currRange + 1), center.y); Vector2 P_vNeg = new Vector2(center.x - deltaX * 2 * (currRange + 1), center.y); Vector2 N_vPos = P_vPos, N_vNeg = P_vNeg; hexag = 0; while (hexag != range) { Vector2 P_vPos_org = P_vPos; Vector2 P_vNeg_org = P_vNeg; P_vPos = new Vector2(P_vPos.x - deltaX, P_vPos.y + deltaY); P_vNeg = new Vector2(P_vNeg.x + deltaX, P_vNeg.y + deltaY); N_vPos = new Vector2(N_vPos.x - deltaX, N_vPos.y - deltaY); N_vNeg = new Vector2(N_vNeg.x + deltaX, N_vNeg.y - deltaY); Vector2[] t = new Vector2[] { P_vPos_org, P_vNeg_org, P_vPos, P_vNeg, N_vPos, N_vNeg }; for (int j = 0; j < 6; j++) { currCenter = t[j]; for (int i = 0; i < 6; i++) { float angleRad = (60 * i - 30) * Mathf.Deg2Rad; Vector2 p = new Vector2(currCenter.x + radius * Mathf.Cos(angleRad), currCenter.y + radius * Mathf.Sin(angleRad)); p.x = Mathf.Round(p.x * 1000) / 1000; p.y = Mathf.Round(p.y * 1000) / 1000; if (!seen.Contains(p)) { seen.Add(p); DotHandler anchor = CreateNode(p); anchor.name = "Hex(" + currRange + ")[" + j + "][" + i + "]"; anchor.elevation = range - currRange; if (anchor.elevation == 1) { gm.StartPoints.Add(anchor); } if (anchor.elevation == range + 1) { //anchor.Gfx.GetComponent<SpriteRenderer>().color = new Color(255, 215, 0); anchor.MaxStrength = 10; } anchors.Add(anchor); } } } hexag++; } currRange++; } return(null); }
private void LateUpdate() { bool flag_onmouse = false; DotHandler Newhoverdot = null; //if (!Abs) //{ Newhoverdot = null; //searches for the dot a player is hovering over foreach (IPlayer p in RoundHandler.PlayerList) { foreach (DotHandler d in p.playerDotHandlers) { if (d != null) { if (d.OnMouse) { Newhoverdot = d; } } /*Debug.Log("tst"); * if (d != null && hoverdot == null) * { * * if (d.OnMouse) * { * hoverdot = d; * if (hoverdot == d) * { * * foreach (Connection c in hoverdot.Connections) * { * c.cube.gameObject.layer = 8; * } * } * if (!Abs) * { * * foreach (Connection c in origin.Connections) * { * c.cube.gameObject.layer = 8; * } * foreach (Connection c in destination.Connections) * { * c.cube.gameObject.layer = 8; * } * hoverdot = null; * } * flag_onmouse = true; * Debug.Log("onmouse: " + flag_onmouse); * } * } */ } //searches for changes in hoverdot with Newhoverdot. if so, it restores the layers of the old hoverdot if (Newhoverdot != null && hoverdot != null) { foreach (Connection c in hoverdot.Connections) { c.gameObject.layer = 8; } } //changes layer of the connections of the hoverdot if (hoverdot != null) { foreach (Connection c in hoverdot.Connections) { c.gameObject.layer = 9; } } } if (DotHandler.clickRegist != null) { if (DotHandler.clickRegist.selectedDot != null) { foreach (Connection c in DotHandler.clickRegist.selectedDot.Connections) { c.gameObject.layer = 9; } } } if (hoverdot != null) { if (!Abs) { foreach (Connection c in origin.Connections) { c.cube.gameObject.layer = 9; } foreach (Connection c in destination.Connections) { c.cube.gameObject.layer = 9; } hoverdot = null; } } if (DotHandler.clickRegist != null) { if (Abs) { if (collider.GetContacts(t, contacts) > 0) { foreach (Collider2D retrievedcollider in contacts) { if (retrievedcollider != null) { Debug.Log("Retrieved Collider: " + retrievedcollider + " // " + (retrievedcollider.gameObject.transform != transform)); } if (retrievedcollider != null && retrievedcollider.gameObject.transform != transform) { cannotBuild = true; Debug.Log(retrievedcollider.transform.parent.gameObject.name + " // cannotbuild: " + cannotBuild); } } } else { cannotBuild = false; } } //} if (DotHandler.clickRegist != null && DotHandler.clickRegist.AbsConnection != null) { if (cannotBuild) { DotHandler.clickRegist.AbsConnection.cube.GetComponent <SpriteRenderer>().color = Color.red; } else { counter += Time.deltaTime; } if (counter > 0.2f) { Debug.Log("cannotbuild: " + cannotBuild); DotHandler.clickRegist.AbsConnection.cube.GetComponent <SpriteRenderer>().color = RoundHandler.CurrPlayerMove.playercolor; counter = 0; } } } }
//public void decide() public IEnumerator decide() { List <DotHandler> RerouteDotHandlers = new List <DotHandler>(); //cycle through all owned dots foreach (DotHandler d in playerDotHandlers.ToArray()) { //underloaded dot //check if dot can attack if (Random.Range(0.0f, 1.0f) < decisionfloat && d != null) { DotHandler helperDot = null; Debug.Log("decide if do something"); //get nearby dotHandlers and check nearby enemy dothandlers DotHandler[] dothandlers = d.GetNearbyDotHandlers(1.5f); foreach (DotHandler otherDothandler in dothandlers) { //check if this dot can attack other dot if (otherDothandler != null && Random.Range(0.0f, 1.0f) < decisionfloat) { if (otherDothandler.Owner != this && otherDothandler.Strength < d.Strength + 1) { foreach (DotHandler hD in d.GetNearbyDotHandlers(3f)) { if (helperDot == null && hD != null) { helperDot = hD; } //check if this dot meet the requirements to help if (helperDot != null && hD != null) { helperDot = hD.Strength > helperDot.Strength ? hD : helperDot; if (!(helperDot.Strength > 1 && helperDot.Owner == this && Connection.FindConnectionBetween(d, helperDot) != null)) { helperDot = null; } } } if ((d.Strength > 2 || helperDot != null) && Random.Range(0.0f, 1.0f) < decisionfloat) { if (helperDot != null) { if (Connection.FindConnectionBetween(helperDot, d) != null) { DotHandler.EnergyMove(helperDot, d, helperDot.Strength - 1); } if (otherDothandler.Owner == null) { //Take over Dot TakeOverDot(d, otherDothandler); } else { //Attack enemy dot AttackOtherDot(d, otherDothandler); } } if (otherDothandler.Strength < d.Strength - 1) { if (otherDothandler.Owner == null) { //Take over Dot TakeOverDot(d, otherDothandler); } else { //Attack enemy dot AttackOtherDot(d, otherDothandler); } } } } } } } bool HostileNearby = false; foreach (DotHandler dotHandler in d.GetNearbyDotHandlers(3f)) { if (dotHandler.Owner != this) { HostileNearby = true; break; } } if (HostileNearby) { //High Cap needed if (d.Strength < 4) { // ask Re-enforcements foreach (DotHandler dotHandler in d.GetNearbyDotHandlers(2f)) { if (dotHandler.Strength > 4) { DotHandler.EnergyMove(dotHandler, d, dotHandler.Strength - 2); } } } } else { //Okay to Have less energy if (d.Strength > 3) { //Get Some energy away foreach (DotHandler dotHandler in d.GetNearbyDotHandlers(2f)) { if (dotHandler.Strength < 4) { if (Connection.FindConnectionBetween(d, dotHandler) == null) { Connection c = DotHandler.CreateConnection(d, dotHandler); c.Abs = true; if (c.cannotBuild) { c.DestroyConnection(); } else { DotHandler.EnergyMove(d, dotHandler, 1, 1); } } DotHandler.EnergyMove(d, dotHandler, dotHandler.Strength - 2); } } } } } //Recharge Dots List <DotHandler> PriorityDots = new List <DotHandler>(); List <int> AssignedValues = new List <int>(); int combinedValues = 0; try { foreach (DotHandler d in RoundHandler.CurrPlayerMove.playerDotHandlers) { if (d != null) { d.UpdateList(); } //First gets the nearby enemy dothandlers List <DotHandler> nearbyEnemyDothandlers = new List <DotHandler>(); int combinedStrength = 0; foreach (DotHandler otherDotHandler in d.GetNearbyDotHandlers(2f)) { if (otherDotHandler.Owner != this && otherDotHandler.Owner != null) { nearbyEnemyDothandlers.Add(otherDotHandler); //with their combined strength combinedStrength += otherDotHandler.Strength; } } //then calculates how much energy needs to be assigned Debug.Log("to be added: " + d + " // " + (d.Strength < 3) + " // " + (nearbyEnemyDothandlers.Count > 0) + " // " + combinedStrength + " // " + nearbyEnemyDothandlers.Count); if (nearbyEnemyDothandlers.Count > 0 && combinedStrength / nearbyEnemyDothandlers.Count > 0) { PriorityDots.Add(d); AssignedValues.Add(combinedStrength / nearbyEnemyDothandlers.Count); combinedValues += combinedStrength / nearbyEnemyDothandlers.Count; Debug.Log("added: " + d); //TODO } } //if there are more recharges assigned than the amount it can assign or the other way around, //delete some assigned recharges or add some for (int i = 0; i < 100; i++) { if (AssignedValues.Count > 0 && combinedValues > playerDotHandlers.Count) //if too many, delete some { AssignedValues[Random.Range(0, AssignedValues.Count - 1)]--; combinedValues--; } if (AssignedValues.Count > 0 && combinedValues < playerDotHandlers.Count) //if too few, add some { AssignedValues[Random.Range(0, AssignedValues.Count - 1)]++; combinedValues++; } } Debug.Log(AssignedValues.Count); for (int i = 0; i < PriorityDots.Count; i++) { Debug.Log(AssignedValues[i]); PriorityDots[i].UpdateStrength(AssignedValues[i]); } } catch (System.Exception e) { throw new System.Exception(e.StackTrace); } EndTurn(); yield return(null); }
public void OnRegisterRelease() { if (!RoundHandler.energyDistributionState) { if (selectedDot != null) { try { if (OnDrag && !SecondSelected) { Debug.Log("past here"); foreach (DotHandler d in FindObjectsOfType <DotHandler>()) { if (d.OnMouse) { selectedDotBefore = selectedDot; selectedDot = d; break; } } } if (selectedDot.Owner == RoundHandler.CurrPlayerMove) { if (selectedDotBefore != null) { if (RoundHandler.CurrPlayerMove.playerDotHandlers.Contains(selectedDot)) { //establish Connection or Move Energy or wants to cancel if (selectedDot == selectedDotBefore) { CancelBuild(2); } else { if (Connection.FindConnectionBetween(selectedDot, selectedDotBefore) != null) { if (energyOnSelect < selectedDotBefore.Strength) { selectedDot.OnDrag = false; Destroy(AbsConnection.gameObject); AbsConnection = null; OnBuildEnd(); EnergyMove(selectedDotBefore, selectedDot, energyOnSelect); Debug.Log("Move Energy // " + energyOnSelect); //move Energy } else { CancelBuild(3); } } if (!AbsConnection.cannotBuild) { selectedDot.OnDrag = false; Debug.Log("Creating Connection // " + selectedDot + " // " + selectedDotBefore + " // " + this); AbsConnection.InitializeAbstractConnection(selectedDotBefore, selectedDot); //CreateConnection(selectedDot, selectedDotBefore); AbsConnection = null; OnBuildEnd(); EnergyMove(selectedDotBefore, selectedDot, energyOnSelect, 1); //Establish connection } else { //Move Energy over multiple nodes CancelBuild(5); } } } } } else { //Dot without owner or Dot owned by other player if (selectedDot.Owner == null) { //Dot without owner Debug.Log((energyOnSelect) + " // " + (selectedDotBefore) + " // " + (selectedDot) + " // " + (selectedDotBefore.Strength != energyOnSelect)); if (energyOnSelect > 1 && selectedDotBefore.Strength > 2 && selectedDotBefore.Strength != energyOnSelect && !AbsConnection.cannotBuild) { selectedDot.OnDrag = false; selectedDot.Owner = selectedDotBefore.Owner; Debug.Log("Creating Connection // " + selectedDot + " // " + selectedDotBefore); AbsConnection.InitializeAbstractConnection(selectedDotBefore, selectedDot); //CreateConnection(selectedDot, selectedDotBefore); AbsConnection = null; Debug.Log("Taken over dot: " + this); EnergyMove(selectedDotBefore, selectedDot, energyOnSelect, 1); RoundHandler.CurrPlayerMove.playerDotHandlers.Add(selectedDot); OnBuildEnd(); //Taking over empty dot } else { CancelBuild(4); } } else { //player wants to attack other dot Debug.Log(RoundHandler.CurrPlayerMove + " attacks " + selectedDot.Owner + " // " + energyOnSelect); Debug.Log(selectedDotBefore + " // " + selectedDot); if (selectedDotBefore != selectedDot) //small bug might be appearant where selectedDot = selectedDotBefore { selectedDotBefore.AttackDot(selectedDot, energyOnSelect); } OnBuildEnd(); } } } catch (System.Exception e) { CancelBuild(0, "", e); } } } }