private void OnGemDissapear(GemController gem) { lock (dissapearedGemsLocker) { dissapearedGems.Add(gem); } }
private void PossibleMoveFound(GemController[] participantsNeighbors, GemController key, Line direction) { if (null != OnPossibleMoveFound) { OnPossibleMoveFound(this, participantsNeighbors, key, direction); } }
public void OnGemMove(GemController gem, Direction direction) { GemController neighbor = gem.GetNeighbor(direction); if (neighbor == null) { return; } if (!neighbor.IsActive || !gem.IsActive) { return; } if (gem.SpecialType != GemSpecialType.Regular && neighbor.SpecialType != GemSpecialType.Regular || gem.SpecialType == GemSpecialType.HitType || neighbor.SpecialType == GemSpecialType.HitType) { DoSpecialSwipe(gem, neighbor); return; } int x1 = gem.Position.x; int y1 = gem.Position.y; int x2 = neighbor.Position.x; int y2 = neighbor.Position.y; SwitchGems(x1, y1, x2, y2); gem.SetPosition(x2, y2, true); neighbor.SetPosition(x1, y1, true); SetGemNeighbor(gem); SetGemNeighbor(neighbor); SwipeAction swipeAction = new SwipeAction(gem, neighbor); swipeAction.OnSwipeActionOver += OnSwipeOver; gem.CurrentSwipeAction = swipeAction; neighbor.CurrentSwipeAction = swipeAction; }
private void CheckNeighborPossibleMove(Direction direction) { GemController neighbor = GetNeighbor(direction); if ((needToCheckNeighborPossibleMove & direction) == direction && neighbor != null && neighbor.IsActive) { bool havesamepossiblematch = false; foreach (PossibleMatch pm in neighbor.PossibleMatches) { if (PossibleMatches.Contains(pm)) { havesamepossiblematch = true; } } if (havesamepossiblematch) { neighbor.GetNeighbor(direction)?.CheckForPossibleMove(); } else { neighbor.CheckForPossibleMove(); } } needToCheckNeighborPossibleMove &= (~direction); }
private void OnSwipeOver(SwipeAction sender, GemController gem1, GemController gem2, bool isMatched) { if (gem1.CurrentSwipeAction == sender) { gem1.CurrentSwipeAction = null; } if (gem2.CurrentSwipeAction == sender) { gem2.CurrentSwipeAction = null; } if (isMatched) { return; } int x1 = gem1.Position.x; int y1 = gem1.Position.y; int x2 = gem2.Position.x; int y2 = gem2.Position.y; SwitchGems(x1, y1, x2, y2); gem1.SetPosition(x2, y2, true); gem2.SetPosition(x1, y1, true); SetGemNeighbor(gem1); SetGemNeighbor(gem2); }
public bool AddGem(GemController gem) { if (!IsPossibleToAdd(gem)) { return(false); } if (matchedGems.Count == 1 && !isContainer) { if (matchedGems[0].Position.x == gem.Position.x) { MatchDirection = Line.Vertical; } else if (matchedGems[0].Position.y == gem.Position.y) { MatchDirection = Line.Horizontal; } } Logger.Instance.Message(ToString() + ": " + gem.ToString() + " was added"); matchedGems.Add(gem); if (!IsContainer) { gem.PossibleMatches.Add(this); } return(true); }
private void OnPossibleMoveFound(GemController sender, GemController[] participants, GemController key, Line direction) { foreach (PossibleMove pm in PossibleMoves) { if (pm.GetHashCode() == MatchUtils.CalculateHash(new int[] { key.Position.x, key.Position.y, sender.Position.x, sender.Position.y })) { return; } } List <GemController> participantsList = new List <GemController>(); foreach (GemController participant in participants) { PossibleMatch participantPossibleMatch = participant.GetPossibleMatchByDirection(direction); if (participantPossibleMatch != null && !participantPossibleMatch.IsMatch()) { participantsList.AddRange(participantPossibleMatch.MatchedGems); } else { participantsList.Add(participant); } } PossibleMove possibleMove = new PossibleMove(participantsList, key, sender.Position, direction); possibleMove.OnOver += PossibleMove_OnOver; possibleMoves.Add(possibleMove); if (OnPossibleMoveCreate != null) { OnPossibleMoveCreate(possibleMove); } }
private Direction GetDirectionByNeighbor(GemController neighbor) { if (neighbor == null) { return(Direction.None); } return(DirectionHelper.GetDirectionByPosition(Position, neighbor.Position)); }
private void SwitchGems(int x1, int y1, int x2, int y2) { Logger.Instance.Message("Switching gems " + (gems[x1][y1] != null ? gems[x1][y1].ToString() : "null") + " and " + (gems[x2][y2] != null ? gems[x2][y2].ToString() : "null")); GemController tmpgem = gems[x1][y1]; gems[x1][y1] = gems[x2][y2]; gems[x2][y2] = tmpgem; }
private void OnGemNotReady(GemController sender) { if (gemNotReadyCount == 0) { IsReady = false; Logger.Instance.Warning("Game is not ready"); } gemNotReadyCount++; }
internal void RemoveKey(GemController key) { if (key != Key) { return; } IsValid = false; Over(); }
private void DoSpecialSwipe(GemController gem, GemController neighbor) { if (gem == null || neighbor == null) { Logger.Instance.Error("DoSpecialSwipe(): gem or neighbor is null"); return; } if (neighbor.SpecialType == GemSpecialType.HitType && gem.SpecialType == GemSpecialType.HitType) { BreakByType(GemType.All); } else if (gem.SpecialType == GemSpecialType.HitType) { if (neighbor.SpecialType != GemSpecialType.Regular) { SetSpecialTypeByType(neighbor.SpecialType, neighbor.CurrentGemType); } BreakByType(neighbor.CurrentGemType); MatchGem(gem); } else if (neighbor.SpecialType == GemSpecialType.HitType) { if (gem.SpecialType != GemSpecialType.Regular) { SetSpecialTypeByType(gem.SpecialType, gem.CurrentGemType); } BreakByType(gem.CurrentGemType); MatchGem(neighbor); } else { GemSpecialType newSpecialType = GemSpecialType.Regular; if (neighbor.SpecialType == gem.SpecialType) { switch (neighbor.SpecialType) { case GemSpecialType.Bomb: newSpecialType = GemSpecialType.DoubleBomb; break; case GemSpecialType.Horizontal: case GemSpecialType.Vertical: newSpecialType = GemSpecialType.Horizontal | GemSpecialType.Vertical; break; } } else { newSpecialType = neighbor.SpecialType | gem.SpecialType; } neighbor.SpecialType = newSpecialType; gem.SpecialType = GemSpecialType.Regular; MatchGem(gem); MatchGem(neighbor); } }
public void CheckMatchedMatches() { if (matchedMatches.Count > 0) { foreach (PossibleMatch pm in matchedMatches) { if (pm.IsOver) { continue; } Logger.Instance.Message(pm.ToString() + " was matched"); List <GemController> matchedGems = new List <GemController>(pm.MatchedGems); if (matchedGems.Count > 3 && !pm.IsContainer) { GemSpecialType type = GemSpecialType.Regular; if (matchedGems.Count > 4 && (pm.MatchDirection == Line.Horizontal || pm.MatchDirection == Line.Vertical)) { type = GemSpecialType.HitType; } else if (pm.MatchDirection == Line.Cross) { type = GemSpecialType.Bomb; } else if (pm.MatchDirection == Line.Vertical) { type = GemSpecialType.Horizontal; } else if (pm.MatchDirection == Line.Horizontal) { type = GemSpecialType.Vertical; } GemController gem = pm.MatchInitiator; if (gem == null) { gem = matchedGems[Randomizer.Range(0, matchedGems.Count)]; } gem.SpecialType = type; matchedGems.Remove(gem); } foreach (GemController gem in matchedGems) { MatchGem(gem); } pm.Over(); } matchedMatches.Clear(); } if (dissapearedGems.Count > 0) { RefillColums(); } }
public bool IsMatch(GemController additionalGem) { if (IsPossibleToAdd(additionalGem)) { return(matchedGems.Count >= MATCH_COUNT - 1); } else { return(IsMatch()); } }
private void OnPossibleMatchAdded(GemController sender, PossibleMatch possibleMatch) { if (null == possibleMatch) { Logger.Instance.Error("possibleMatch is null"); return; } Logger.Instance.Message(possibleMatch.ToString() + " was added to possibleMatches"); possibleMatch.OnMatch += OnGemsMatch; possibleMatch.OnOver += OnPossibleMatchOver; possibleMatches.Add(possibleMatch); }
public PossibleMove(List <GemController> participants, GemController key, Position matchablePosition, Line direction) { Key = key; MatchablePosition = matchablePosition; Direction = direction; hash = MatchUtils.CalculateHash(new int[] { Key.Position.x, Key.Position.y, MatchablePosition.x, MatchablePosition.y }); Key.AddPossibleMove(this, Role.Key); foreach (GemController participant in participants) { participant.AddPossibleMove(this, Role.Participant); Participants.Add(participant); } }
private void MatchGem(GemController gem) { if (gem == null || gem.CurrentState == GemController.State.Matched) { return; } RemoveGemNeighbor(gem); gem.OnMatch(); if (null != OnGemMatch) { OnGemMatch(this, gem); } }
private void OnGemReady(GemController sender) { gemNotReadyCount--; if (gemNotReadyCount == 0) { IsReady = true; Logger.Instance.Warning("Game is ready"); } else if (gemNotReadyCount < 0) { Logger.Instance.Error("gem ready counter is less than 0"); } }
internal void Over() { foreach (GemController participant in Participants) { participant.RemovePossibleMove(this, Role.Participant); } Participants.Clear(); key.RemovePossibleMove(this, Role.Key); key = null; if (OnOver != null) { OnOver(this); } }
private List <GemController> GetGemsByColumnIndex(int columnIndex) { List <GemController> result = new List <GemController>(); for (int y = 0; y < rowCount; y++) { GemController gem = gems[columnIndex][y]; if (gem == null) { continue; } result.Add(gem); } return(result); }
public void SetGemSwipeResult(GemController gem, bool isMatched) { Logger.Instance.Message(gem.ToString() + " is " + (isMatched ? "matched" : "not matched")); for (int i = 0; i < swipingGems.Count; i++) { if (swipingGems[i].gem == gem) { swipingGems[i].isMatched = isMatched; swipingGems[i].isOver = true; break; } } CheckGemsSwipeEnded(); }
private List <GemController> GetGemsByRowIndex(int rowIndex) { List <GemController> result = new List <GemController>(); for (int x = 0; x < columnCount; x++) { GemController gem = gems[x][rowIndex]; if (gem == null) { continue; } result.Add(gem); } return(result); }
public void RemoveGem(GemController gem) { Logger.Instance.Message(ToString() + ": " + gem.ToString() + " was removed"); if (MatchInitiator == gem) { MatchInitiator = null; } matchedGems.Remove(gem); if (!IsContainer) { gem.PossibleMatches.Remove(this); } if (matchedGems.Count == 1) { Over(); } }
private GemController CreateGem(int x, int y, bool beggining = true) { GemController gem = gemStackManager.Pop(); if (gem == null) { gem = new GemController(); gem.OnReadyEvent += OnGemReady; gem.OnNotReadyEvent += OnGemNotReady; gem.OnPossibleMatchAddedEvent += OnPossibleMatchAdded; gem.OnMovingToEvent += OnGemMove; gem.OnDissapear += OnGemDissapear; gem.OnSpecialMatch += OnSpecialGemMatch; gem.OnPossibleMoveFound += OnPossibleMoveFound; if (null != OnGemCreated) { OnGemCreated(this, gem); } } gem.SetPosition(x, y); GemType unacceptableType = GemType.None; GemController xm1Neighbor = x - 1 >= 0 ? gems[x - 1][y] : null; GemController xm2Neighbor = x - 2 >= 0 ? gems[x - 2][y] : null; if ((xm1Neighbor != null && xm2Neighbor != null) && (xm1Neighbor.CurrentGemType.HasSameFlags(xm2Neighbor.CurrentGemType))) { unacceptableType.AddFlag(xm1Neighbor.CurrentGemType.GetSameFlags(xm2Neighbor.CurrentGemType), out unacceptableType); } GemController ym1Neighbor = y - 1 >= 0 ? gems[x][y - 1] : null; GemController ym2Neighbor = y - 2 >= 0 ? gems[x][y - 2] : null; if ((ym1Neighbor != null && ym2Neighbor != null) && (ym1Neighbor.CurrentGemType.HasSameFlags(ym2Neighbor.CurrentGemType))) { unacceptableType.AddFlag(ym1Neighbor.CurrentGemType.GetSameFlags(ym2Neighbor.CurrentGemType), out unacceptableType); } GemType type = (~unacceptableType).Random(); gem.SetGemType(type); gem.Init(beggining); return(gem); }
public bool IsPossibleToAdd(GemController addableGem) { if (addableGem.CurrentGemType.HasSameFlags(matchType)) { if (matchedGems.Count == 0) { return(true); } else if (MatchDirection == Line.Horizontal && addableGem.Position.y != matchedGems[0].Position.y || MatchDirection == Line.Vertical && addableGem.Position.x != matchedGems[0].Position.x || matchedGems.Contains(addableGem)) { return(false); } return(true); } else { return(false); } }
private void CheckNeighbor(GemController neighbor, Direction neighborDirection) { if (null == neighbor) { neighborChangedFlag &= (~neighborDirection); return; } if (!neighbor.IsActive) { return; } if (!neighbor.CurrentGemType.HasSameFlags(this.CurrentGemType)) { neighborChangedFlag &= (~neighborDirection); return; } PossibleMatch addedPossibleMatch = null; foreach (PossibleMatch possibleMatch in neighbor.PossibleMatches) { if (possibleMatch.AddGem(this)) { addedPossibleMatch = possibleMatch; } } if (addedPossibleMatch == null) { addedPossibleMatch = new PossibleMatch(this.CurrentGemType.GetSameFlags(neighbor.CurrentGemType)); addedPossibleMatch.AddGem(this); Logger.Instance.Message(this.ToString() + " adding neighbor " + neighbor.ToString() + " to " + addedPossibleMatch.ToString()); addedPossibleMatch.AddGem(neighbor); if (null != OnPossibleMatchAddedEvent) { OnPossibleMatchAddedEvent(this, addedPossibleMatch); } } neighborChangedFlag &= (~neighborDirection); }
internal void RemoveParticipant(GemController participant) { if (!Participants.Contains(participant)) { return; } GemController[] relatedParticipants = GetRelatedParticipant(participant); Participants.Remove(participant); participant.RemovePossibleMove(this, Role.Participant); if (relatedParticipants != null) { foreach (GemController rp in relatedParticipants) { Participants.Remove(rp); rp.RemovePossibleMove(this, Role.Participant); } } IsValid = CheckValidity(); if (!IsValid) { Over(); } }
public void Init() { gems = new GemController[columnCount][]; for (int x = 0; x < columnCount; x++) { gems[x] = new GemController[rowCount]; for (int y = 0; y < rowCount; y++) { if (tiles[x][y] != TileType.None) { gemNotReadyCount++; gems[x][y] = CreateGem(x, y); } } } for (int x = 0; x < columnCount; x++) { for (int y = 0; y < rowCount; y++) { if (null != gems[x][y]) { SetGemNeighbor(gems[x][y]); } } } for (int x = 0; x < columnCount; x++) { for (int y = 0; y < rowCount; y++) { if (null != gems[x][y]) { gems[x][y].CheckNeighbors(); } } } Logger.Instance.Message("GemManager was initialized"); }
private void SetGemNeighbor(GemController gem) { if (gem == null) { return; } int x = gem.Position.x; int y = gem.Position.y; GemController leftNeighbor = x - 1 >= 0 ? gems[x - 1][y] : null; GemController rightNeighbor = x + 1 < columnCount ? gems[x + 1][y] : null; GemController upNeighbor = y + 1 < rowCount ? gems[x][y + 1] : null; GemController downNeighbor = y - 1 >= 0 ? gems[x][y - 1] : null; gem.LeftNeighbor = leftNeighbor; gem.RightNeighbor = rightNeighbor; gem.UpNeighbor = upNeighbor; gem.DownNeighbor = downNeighbor; if (leftNeighbor != null) { leftNeighbor.RightNeighbor = gem; } if (rightNeighbor != null) { rightNeighbor.LeftNeighbor = gem; } if (upNeighbor != null) { upNeighbor.DownNeighbor = gem; } if (downNeighbor != null) { downNeighbor.UpNeighbor = gem; } }
private void RemoveGemNeighbor(GemController gem) { gem.LeftNeighbor = null; gem.RightNeighbor = null; gem.UpNeighbor = null; gem.DownNeighbor = null; if (gem.LeftNeighbor != null) { gem.LeftNeighbor.RightNeighbor = null; } if (gem.RightNeighbor != null) { gem.RightNeighbor.LeftNeighbor = null; } if (gem.UpNeighbor != null) { gem.UpNeighbor.DownNeighbor = null; } if (gem.DownNeighbor != null) { gem.DownNeighbor.UpNeighbor = null; } }