public static Entity CreateGem( GemView gem, int x, int y ) { var gemEntity = new Entity(Registrator.GenerateName(BaseNames.Gem)); var gDisplayComponent = new DisplayComponent(); gem.GemShape.Origin = new SFML.System.Vector2f( gem.GemShape.GetLocalBounds().Width / 2.0f, gem.GemShape.GetLocalBounds().Height / 2.0f ); gDisplayComponent.DisplayObject = gem.GemShape; var gPositionComponent = new PositionComponent(); gPositionComponent.X = x; gPositionComponent.Y = y; var gCollisionComponent = new CollisionComponent(); gCollisionComponent.BoundingBox = new FloatRect(x, y, Layout.GemSize, Layout.GemSize); var gAnimationComponent = new AnimationComponent(); gAnimationComponent.X = x; gAnimationComponent.Y = y; gAnimationComponent.Speed = Engine.GameSpeed; gAnimationComponent.Scale = gDisplayComponent.DisplayObject.Scale; var gGemComponent = new GemComponent(); gGemComponent.GemType = gem.GemType; gGemComponent.GemSubType = gem.GemSubType; gemEntity.AddComponent(gDisplayComponent); gemEntity.AddComponent(gPositionComponent); gemEntity.AddComponent(gCollisionComponent); gemEntity.AddComponent(gAnimationComponent); gemEntity.AddComponent(gGemComponent); return(gemEntity); }
void PlayMatch(List <GemNode> clickedGems, List <Name> clickedGemsNames) { if (engine.AnimationEnded) { if (!gemsDestroying) { if (clickedGems.Count == 2) { GemNode first; GemNode second; int firstClicked = 0; if (clickedGems[0].Entity.Name != clickedGemsNames[0]) { firstClicked = 1; } first = clickedGems[firstClicked]; second = clickedGems[1 - firstClicked]; if (AreNeighbours(first.Entity.Name.Id - 1, second.Entity.Name.Id - 1)) { SwapGems(first, second, Engine.GameSpeed); prevClicked.Add(new Pair(first, second)); lastMoved = first.Entity.Name.Id - 1; } return; } FindLines(); if (needDelete.Count == 0) { if (prevClicked.Count != 0) { foreach (var clickedPair in prevClicked) { SwapGems((GemNode)clickedPair.First, (GemNode)clickedPair.Second, Engine.GameSpeed ); } } } else { if (engine.Playlist.Count == 0) { engine.Playlist.Add(Assets.GemSound); } engine.Score += 10 * needDelete.Count; DestroyGems(); } prevClicked.Clear(); return; } var rnd = new Random(); foreach (GemNode target in gemNodes) { if (target.Entity.Name.BaseName == BaseNames.Gem && needDelete.Contains(target.Entity.Name.Id - 1)) { var gem = new GemView( (Gem)Enum.GetValues(typeof(Gem)).GetValue(rnd.Next(0, 5)) ); var gemShape = gem.GemShape; gemShape.Origin = new Vector2f( gemShape.GetLocalBounds().Width / 2.0f, gemShape.GetLocalBounds().Height / 2.0f ); target.Display.DisplayObject = gemShape; target.Gem.GemType = gem.GemType; target.Gem.GemSubType = GemSub.Gem; Vector2i gridPlace = GetGridPlace(target.Entity.Name.Id - 1); gemGrid[gridPlace.X, gridPlace.Y] = target.Gem.GemType; target.Animation.Scale = new Vector2f(1.0f, 1.0f); } } StabilizeField(); needDelete.Clear(); gemsDestroying = false; } }
void FindLines() { var gottenGems = new List <Gotten>(); for (int i = 0; i < Settings.GameSize; i++) { for (int j = 0; j < Settings.GameSize; j++) { gottenGems.Add(new Gotten()); } } for (int i = 0; i < Settings.GameSize; i++) { for (int j = 0; j < Settings.GameSize; j++) { var currentId = i * Settings.GameSize + j; var horDelete = new List <int> { idGrid[currentId] }; var vertDelete = new List <int> { idGrid[currentId] }; int horLineSize = 1; int vertLineSize = 1; for (int z = 1; j + z < Settings.GameSize; z++) { if (gemGrid[i, j] == gemGrid[i, j + z]) { var id = idGrid[i * Settings.GameSize + j + z]; if (!gottenGems[id].Horizontal) { gottenGems[id].Horizontal = true; horDelete.Add(id); horLineSize++; } } else { break; } } for (int z = 1; i + z < Settings.GameSize; z++) { if (gemGrid[i, j] == gemGrid[i + z, j]) { var id = idGrid[(i + z) * Settings.GameSize + j]; if (!gottenGems[id].Vertical) { gottenGems[id].Vertical = true; vertDelete.Add(id); vertLineSize++; } } else { break; } } if (horLineSize < minLineSize) { horDelete.Clear(); } if (vertLineSize < minLineSize) { vertDelete.Clear(); } CheckBonuses(horDelete, vertDelete); if (horLineSize >= 4 || vertLineSize >= 4 || horLineSize >= 3 && vertLineSize >= 3 ) { int id; if (lastMoved != -1) { id = lastMoved; lastMoved = -1; } else { id = currentId; } GemSub subType = GemSub.Line; if (horLineSize >= 3 && vertLineSize >= 3) { horDelete.Remove(id); vertDelete.Remove(id); } else if (vertLineSize >= 4) { vertDelete.Remove(id); } else if (horLineSize >= 4) { horDelete.Remove(id); } if (horLineSize >= 5 || vertLineSize >= 5 || horLineSize >= 3 && vertLineSize >= 3 ) { subType = GemSub.Bomb; } var target = (GemNode)gemNodes.Find( n => n.Entity.Name.BaseName == BaseNames.Gem && n.Entity.Name.Id - 1 == id ); var gem = new GemView(gemGrid[GetGridPlace(id).X, GetGridPlace(id).Y], subType); var gemShape = gem.GemShape; gemShape.Origin = new Vector2f( gemShape.GetLocalBounds().Width / 2.0f, gemShape.GetLocalBounds().Height / 2.0f ); target.Display.DisplayObject = gemShape; target.Gem.GemSubType = gem.GemSubType; } needDelete.UnionWith(horDelete); needDelete.UnionWith(vertDelete); } } }