// Use this for initialization public void Start() { _initialLocalPosition = transform.localPosition; _initialLocalRotation = transform.localRotation; _group = transform.parent.GetComponent<PieceGroup>(); var random = Random.value; foreach (var special in GameManager.Instance.SpecialMaterials.OrderByDescending(x => x.Chance).Where(special => random < special.Chance)) { _specialName = special.Name; GetComponent<Renderer>().material = special.Material; } //if (string.IsNullOrEmpty(_specialName)) // GetComponent<Renderer>().material = // GameManager.Instance.Materials[Random.Range(0, GameManager.Instance.Materials.Length)]; if (string.IsNullOrEmpty(_specialName)) { GetComponent<Renderer>().material = GameManager.Instance.NormalPieceMaterial; GetComponent<Renderer>().material.color = GameManager.Instance.Colours[Random.Range(0, GameManager.Instance.Colours.Length)]; } GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeAll; }
public void DestoryGroup(bool withSound = true) { if (group != null) { if (withSound) { group.CutChain(); } group.RemoveChild(this); group = null; } }
private void DestoryChain(PieceGroup pieceGroup) { if (pieceGroup != null) { if (pieceGroup.chains != null) { foreach (Chain chain in pieceGroup.chains) { if (chain != null) { GameObject.DestroyImmediate(chain.gameObject); } } } pieceGroup = null; } }
public override void OnInspectorGUI() { Piece piece = this.target as Piece; EditorGUILayout.LabelField("ID:", piece.id.ToString()); type = piece.colorType; type = (PieceColor)EditorGUILayout.EnumPopup("Upper Color", type); piece.ChangeColor(type); state = piece.state; state = (PieceState)EditorGUILayout.EnumPopup("Upper State", state); UpdatePieceState(); if (this.targets.Length >= 2 && this.targets.Length <= 3) { if (GUILayout.Button("Make Chain")) { PieceGroup pieceGroup = PieceGroup.CreateInstance <PieceGroup>(); for (int i = 0; i < this.targets.Length; i++) { Piece member = this.targets[i] as Piece; if (member.group != null) { DestoryChain(member.group); } pieceGroup.AddChild(member); } pieceGroup.Sort(); MakeChain(pieceGroup); } if (GUILayout.Button("Destory Chain")) { for (int i = 0; i < this.targets.Length; i++) { Piece member = this.targets[i] as Piece; if (member.group != null) { DestoryChain(member.group); } member.group = null; } } } }
private void MakeChain(PieceGroup pieceGroup) { List <Chain> chains = new List <Chain>(); for (int i = 1; i < pieceGroup.children.Count; i++) { Piece start = pieceGroup.children[i - 1]; Piece end = pieceGroup.children[i]; if (!pieceGroup.HasChained(start, end)) { GameObject chainObj = Instantiate(Resources.Load("Prefabs/Chain")) as GameObject; Chain chain = chainObj.GetComponent <Chain>(); chain.transform.parent = start.transform.parent; chain.SetUp(start, end); chains.Add(chain); } } pieceGroup.chains = chains; }
public override void Reset() { base.Reset(); x = -1; y = -1; isDead = false; isFadeAway = false; isCore = false; state = PieceState.Normal; ResetScale(); group = null; moving = false; twine = null; ice = null; clock = null; coke = false; shaker = null; colorType = type; defaultColor = Wall.GetColor(type); this.GetComponent <SpriteRenderer>().color = defaultColor; passSession = BoardDirection.None; cokeCounter.Reset(); }
public void ParseFromText(ref Board board, ref string text) { board.ResetBoard(); XDocument document = XDocument.Parse(text); XElement level = document.Element("Level"); step = (int)level.Attribute("Step"); objective = (LevelObjective)((int)level.Attribute("Mode")); XElement hexagons = level.Element("Hexagons"); foreach (XElement hexagon in hexagons.Elements("Hexagon")) { HexagonState upperState = (HexagonState)((int)hexagon.Attribute("UpperState")); HexagonState lowerState = (HexagonState)((int)hexagon.Attribute("LowerState")); int blockState = (int)hexagon.Attribute("BlockState"); int x = (int)hexagon.Attribute("X"); int y = (int)hexagon.Attribute("Y"); board.SetHexagonStateAt(x, y, true, upperState); board.SetHexagonStateAt(x, y, false, lowerState); if (blockState > 0) { board.GetHexagonAt(x, y).SetBlock(blockState); } } XElement pieces = level.Element("Pieces"); foreach (XElement piece in pieces.Elements("Piece")) { bool isUpper = ((int)piece.Attribute("Upper")) == 1; bool core = piece.Attribute("Core") == null ? false : (((int)piece.Attribute("Core")) == 1); PieceState state = (PieceState)((int)piece.Attribute("State")); PieceColor color = (PieceColor)((int)piece.Attribute("Type")); Piece p = board.GeneratePieceAt((int)piece.Attribute("X"), (int)piece.Attribute("Y"), isUpper, color, core); p.SetState(state); } XElement walls = level.Element("Walls"); foreach (XElement wall in walls.Elements("Wall")) { int index = (int)wall.Attribute("Index"); board.SetWallState(index, (WallState)((int)wall.Attribute("State"))); if (wall.Attribute("Level") != null) { board.GetWall(index).SetLevel((int)wall.Attribute("Level")); } } XElement groups = level.Element("Groups"); foreach (XElement group in groups.Elements("Group")) { PieceGroup g = PieceGroup.CreateInstance <PieceGroup>(); foreach (XElement piece in group.Elements("Piece")) { bool isUpper = ((int)piece.Attribute("Upper")) == 1; g.AddChild(board.GetPieceAt((int)piece.Attribute("X"), (int)piece.Attribute("Y"), isUpper)); } g.Sort(); g.MakeChain(); } XElement switchers = level.Element("Switchers"); foreach (XElement switcher in switchers.Elements("Switcher")) { int x = (int)switcher.Attribute("X"); int y = (int)switcher.Attribute("Y"); bool isUpper = (int)switcher.Attribute("Upper") == 1; bool isStatic = (int)switcher.Attribute("Static") == 1; PieceColor color = (PieceColor)((int)switcher.Attribute("Color")); //Debug.Log(color); Hexagon hexagon = board.GetHexagonAt(x, y); if (isUpper) { hexagon.switchU.isStatic = isStatic; hexagon.switchU.color = color; hexagon.switchU.UpdateColor(); } else { hexagon.switchD.isStatic = isStatic; hexagon.switchD.color = color; hexagon.switchD.UpdateColor(); } } XElement clocks = level.Element("Clocks"); foreach (XElement clock in clocks.Elements("Clock")) { int x = (int)clock.Attribute("X"); int y = (int)clock.Attribute("Y"); bool isUpper = (int)clock.Attribute("Upper") == 1; HexagonEdget edget = (HexagonEdget)((int)clock.Attribute("Edget")); Piece piece = board.GetPieceAt(x, y, isUpper); piece.clock.triggerEdget = edget; piece.clock.UpdateTrigger(); } XElement twines = level.Element("Twines"); foreach (XElement twine in twines.Elements("Twine")) { int x = (int)twine.Attribute("X"); int y = (int)twine.Attribute("Y"); bool isUpper = (int)twine.Attribute("Upper") == 1; int state = ((int)twine.Attribute("State")); Piece piece = board.GetPieceAt(x, y, isUpper); piece.twine.SetState(state); } XElement ices = level.Element("Ices"); foreach (XElement ice in ices.Elements("Ice")) { int x = (int)ice.Attribute("X"); int y = (int)ice.Attribute("Y"); bool isUpper = (int)ice.Attribute("Upper") == 1; int life = ((int)ice.Attribute("Life")); Piece piece = board.GetPieceAt(x, y, isUpper); piece.ice.SetLife(life); } guides = new List <LevelGuide>(); XElement steps = level.Element("Steps"); foreach (XElement s in steps.Elements("Step")) { int index = (int)s.Attribute("Index"); BoardDirection direction = (BoardDirection)((int)s.Attribute("Direction")); LevelGuide guide = new LevelGuide(); guide.direction = direction; guide.PieceIndex = index; guides.Add(guide); } }
public XDocument CreateDocument(ref Board board, ref Level level, int step) { XDocument document = new XDocument(); XElement root = new XElement("Level"); document.Add(root); List <PieceGroup> groups = new List <PieceGroup>(); List <Switcher> switchers = new List <Switcher>(); List <Clock> clocks = new List <Clock>(); List <Twine> twines = new List <Twine>(); List <Ice> ices = new List <Ice>(); XAttribute attribute; attribute = new XAttribute("Mode", 1); root.Add(attribute); attribute = new XAttribute("Step", (int)step); root.Add(attribute); XElement parent; parent = new XElement("Hexagons"); root.Add(parent); Hexagon[] hexagons = board.GetHexagons(); for (int i = 0; i < hexagons.Length; i++) { Hexagon hexagon = hexagons[i]; if (hexagon.blockState != 0 || hexagon.upperState != HexagonState.Normal || hexagon.lowerState != HexagonState.Normal) { XElement element = new XElement("Hexagon"); attribute = new XAttribute("X", hexagon.x); element.Add(attribute); attribute = new XAttribute("Y", hexagon.y); element.Add(attribute); attribute = new XAttribute("BlockState", hexagon.blockState); element.Add(attribute); attribute = new XAttribute("UpperState", (int)hexagon.upperState); element.Add(attribute); if (hexagon.upperState == HexagonState.SwitchType) { switchers.Add(hexagon.switchU); } attribute = new XAttribute("LowerState", (int)hexagon.lowerState); element.Add(attribute); if (hexagon.lowerState == HexagonState.SwitchType) { switchers.Add(hexagon.switchD); } parent.Add(element); } } parent = new XElement("Pieces"); root.Add(parent); for (int i = 0; i < hexagons.Length; i++) { Hexagon hexagon = hexagons[i]; if (hexagon.upper != null || hexagon.lower != null) { if (hexagon.upper != null) { XElement element = new XElement("Piece"); attribute = new XAttribute("X", hexagon.upper.x); element.Add(attribute); attribute = new XAttribute("Y", hexagon.upper.y); element.Add(attribute); attribute = new XAttribute("Upper", 1); element.Add(attribute); attribute = new XAttribute("State", (int)hexagon.upper.state); element.Add(attribute); attribute = new XAttribute("Type", (int)hexagon.upper.colorType); element.Add(attribute); int core = hexagon.upper.isCore ? 1 : 0; attribute = new XAttribute("Core", core); element.Add(attribute); parent.Add(element); if (hexagon.upper.group != null) { if (!groups.Contains(hexagon.upper.group)) { groups.Add(hexagon.upper.group); } } if (hexagon.upper.clock != null) { clocks.Add(hexagon.upper.clock); } if (hexagon.upper.twine != null) { twines.Add(hexagon.upper.twine); } if (hexagon.upper.ice != null) { ices.Add(hexagon.upper.ice); } } if (hexagon.lower != null) { XElement element = new XElement("Piece"); attribute = new XAttribute("X", hexagon.lower.x); element.Add(attribute); attribute = new XAttribute("Y", hexagon.lower.y); element.Add(attribute); attribute = new XAttribute("Upper", 0); element.Add(attribute); attribute = new XAttribute("State", (int)hexagon.lower.state); element.Add(attribute); attribute = new XAttribute("Type", (int)hexagon.lower.colorType); element.Add(attribute); int core = hexagon.lower.isCore ? 1 : 0; attribute = new XAttribute("Core", core); element.Add(attribute); parent.Add(element); if (hexagon.lower.group != null) { if (!groups.Contains(hexagon.lower.group)) { groups.Add(hexagon.lower.group); } } if (hexagon.lower.clock != null) { clocks.Add(hexagon.lower.clock); } if (hexagon.lower.twine != null) { twines.Add(hexagon.lower.twine); } if (hexagon.lower.ice != null) { ices.Add(hexagon.lower.ice); } } } } parent = new XElement("Walls"); root.Add(parent); Wall[] walls = board.GetWalls(); for (int i = 0; i < walls.Length; i++) { Wall wall = walls[i]; XElement element = new XElement("Wall"); attribute = new XAttribute("Index", i); element.Add(attribute); attribute = new XAttribute("State", (int)wall.state); element.Add(attribute); attribute = new XAttribute("Level", (int)wall.level); element.Add(attribute); parent.Add(element); } parent = new XElement("Groups"); root.Add(parent); if (groups.Count > 0) { for (int i = 0; i < groups.Count; i++) { PieceGroup group = groups[i]; if (group.childrenRefrence != null) { XElement element = new XElement("Group"); parent.Add(element); for (int j = 0; j < group.childrenRefrence.Length; j++) { XElement pieceEle = new XElement("Piece"); element.Add(pieceEle); attribute = new XAttribute("X", group.childrenRefrence[j].x); pieceEle.Add(attribute); attribute = new XAttribute("Y", group.childrenRefrence[j].y); pieceEle.Add(attribute); int upper = group.childrenRefrence[j].isUpper ? 1 : 0; attribute = new XAttribute("Upper", upper); pieceEle.Add(attribute); } } } } parent = new XElement("Switchers"); root.Add(parent); if (switchers.Count > 0) { for (int i = 0; i < switchers.Count; i++) { Switcher switcher = switchers[i]; XElement element = new XElement("Switcher"); parent.Add(element); attribute = new XAttribute("X", switcher.target.x); element.Add(attribute); attribute = new XAttribute("Y", switcher.target.y); element.Add(attribute); int upper = switcher.isUpper ? 1 : 0; attribute = new XAttribute("Upper", upper); element.Add(attribute); int isStatic = switcher.isStatic ? 1 : 0; attribute = new XAttribute("Static", isStatic); element.Add(attribute); attribute = new XAttribute("Color", (int)switcher.color); element.Add(attribute); } } parent = new XElement("Clocks"); root.Add(parent); if (clocks.Count > 0) { for (int i = 0; i < clocks.Count; i++) { Clock clock = clocks[i]; XElement element = new XElement("Clock"); parent.Add(element); attribute = new XAttribute("X", clock.piece.x); element.Add(attribute); attribute = new XAttribute("Y", clock.piece.y); element.Add(attribute); int upper = clock.piece.isUpper ? 1 : 0; attribute = new XAttribute("Upper", upper); element.Add(attribute); attribute = new XAttribute("Edget", (int)clock.triggerEdget); element.Add(attribute); } } parent = new XElement("Twines"); root.Add(parent); if (twines.Count > 0) { for (int i = 0; i < twines.Count; i++) { Twine twine = twines[i]; XElement element = new XElement("Twine"); parent.Add(element); attribute = new XAttribute("X", twine.piece.x); element.Add(attribute); attribute = new XAttribute("Y", twine.piece.y); element.Add(attribute); int upper = twine.piece.isUpper ? 1 : 0; attribute = new XAttribute("Upper", upper); element.Add(attribute); attribute = new XAttribute("State", (int)twine.state); element.Add(attribute); } } parent = new XElement("Ices"); root.Add(parent); if (ices.Count > 0) { for (int i = 0; i < ices.Count; i++) { Ice ice = ices[i]; XElement element = new XElement("Ice"); parent.Add(element); attribute = new XAttribute("X", ice.piece.x); element.Add(attribute); attribute = new XAttribute("Y", ice.piece.y); element.Add(attribute); int upper = ice.piece.isUpper ? 1 : 0; attribute = new XAttribute("Upper", upper); element.Add(attribute); attribute = new XAttribute("Life", (int)ice.life); element.Add(attribute); } } parent = new XElement("Steps"); if (level != null) { for (int i = 0; i < level.pieceIndex.Length; i++) { if (i < step) { XElement element = new XElement("Step"); parent.Add(element); attribute = new XAttribute("Index", level.pieceIndex[i]); element.Add(attribute); attribute = new XAttribute("Direction", (int)level.moveDirection[i]); element.Add(attribute); } } } root.Add(parent); return(document); }