private Sprite getBoxSprite(Box.Type type) { switch (type) { case Box.Type.Color1: return(_box_1); case Box.Type.Color2: return(_box_2); case Box.Type.Color3: return(_box_3); case Box.Type.Color4: return(_box_4); case Box.Type.Color5: return(_box_5); case Box.Type.Color6: return(_box_6); case Box.Type.Color7: return(_box_7); case Box.Type.Color8: return(_box_8); } return(_box_1); }
private Box GetOneBoxAtPosition(Vector3 position, char hitsChar) { var go = Instantiate(box, position, Quaternion.identity) as GameObject; int hits = (int)System.Char.GetNumericValue(hitsChar) + 1; //TODO: change '0' in Levels file //Box.Type type = Box.GetRandomColorFromList(_grid.GetAllUniqueTypes()); Box.Type type = Box.GetColorByHits(hits); SpriteRenderer render = go.GetComponent <SpriteRenderer>(); render.sprite = getBoxSprite(type); Box _box = go.GetComponent <Box>(); _box.type = type; _box.amountOfHits = hits; _box.transform.GetChild(0).transform.GetChild(0).gameObject.GetComponent <UnityEngine.UI.Text>().text = "" + _box.amountOfHits; return(_box); }
public List <Box.Type> GetAllUniqueTypes() { List <Box.Type> all = new List <Box.Type>(); for (int i = 0; i < G.rows; i++) { for (int j = 0; j < G.cols; j++) { var one = _grids[i, j]; if (one != null) { Box.Type type = one.type; if (!all.Contains(type)) { all.Add(type); } } } } return(all); }
/// <summary> /// This function organize and calculate all groups, /// these groups will used for /// </summary> public void GroupBoxes() { for (int y = 0; y < 5; y++) // bottom to top scan { for (int x = 0; x < 5; x++) // left to right scan { Box.Type chosen = boxList[y * 5 + x].GetBoxType(); Group group = boxList[y * 5 + x].GetGroup(); if (group == null)//Chosen box doesnt have any group { // Group is null , Check Right and setGroup or create new group if neighbour has a group with same box Type if (x != 4) // this is used for to get neighbour box, when we have 5 box, chosen should be max for to get 5th element as neigh. { if (boxList[y * 5 + x + 1].GetGroup() != null) //rightNeighbour is full { Box.Type neighborRight = boxList[y * 5 + x + 1].GetBoxType(); if (chosen == neighborRight) // neigh's box type is same with chosen, than get neigh's group to chosen { group = boxList[y * 5 + x + 1].GetGroup(); boxList[y * 5 + x].SetGroup(group); // set group to box group.AddNewBox(boxList[y * 5 + x]); // add the box to group } else // neigh's box type is diffreent than chosen so create a new group { group = new Group(chosen); groups.Add(group);// add the created group to list<Group> boxList[y * 5 + x].SetGroup(group); group.AddNewBox(boxList[y * 5 + x]); } } else // right neighbour is empty , create new group and if neigh's type is same, set its group here like chosen { group = new Group(chosen); groups.Add(group); // add the created group to list<Group> boxList[y * 5 + x].SetGroup(group); group.AddNewBox(boxList[y * 5 + x]); Box.Type neighborRight = boxList[y * 5 + x + 1].GetBoxType(); if (chosen == neighborRight) { boxList[y * 5 + x + 1].SetGroup(group); group.AddNewBox(boxList[y * 5 + x + 1]); } } } else // if chosen is 5th element, create new group { group = new Group(chosen); groups.Add(group); // add the created group to list<Group> boxList[y * 5 + x].SetGroup(group); group.AddNewBox(boxList[y * 5 + x]); } if (y != 4) //Set top neighbour's group like the chosen's group if it has same box type with chosen { Box.Type neighborTop = boxList[(y + 1) * 5 + x].GetBoxType(); if (chosen == neighborTop) { boxList[(y + 1) * 5 + x].SetGroup(group); group.AddNewBox(boxList[(y + 1) * 5 + x]); } } } else //Chosen box has a group, because we might have set the group in previous process and we can face these boxes. { if (x != 4) { Group RightGroup = boxList[y * 5 + x + 1].GetGroup(); Box.Type neighborRight = boxList[y * 5 + x + 1].GetBoxType(); if (RightGroup == null) // if right neigh's group is null and its box type same with chosen set its group like chosen's { if (chosen == neighborRight) { boxList[y * 5 + x + 1].SetGroup(group); group.AddNewBox(boxList[y * 5 + x + 1]); } } else if (RightGroup != null) // if right neigh has a group, { if (RightGroup != group) // and its not like chosen, { if (chosen == neighborRight) // but it has same type with chosen { foreach (Box b in RightGroup.GetBoxList()) { b.SetGroup(group); } group.MergeWith(ref RightGroup); // merge the group boxes and set it on chosen's group; // g1=a,b,c , g2 = d,f,g,h g1.MergeWith(g2)==> g1 = a,b,c,d,f,g,h , g2 = null groups.Remove(RightGroup); //Remove from the list RightGroup = null; } } } } if (y != 4)// look at top if it's like chosen, set its group identy as chosen's group { Box.Type neighborTop = boxList[(y + 1) * 5 + x].GetBoxType(); if (chosen == neighborTop) { boxList[(y + 1) * 5 + x].SetGroup(group); group.AddNewBox(boxList[(y + 1) * 5 + x]); } } } } } }
public Group(Box.Type groupBoxType) { this.groupBoxType = groupBoxType; boxList = new List <Box>(); }
//Constructors public Group() { this.groupBoxType = Box.Type.Empty; boxList = new List <Box>(); }