Example #1
0
        public void HandleMoveClick(Direction direction)
        {
            Hero hero = SokobanProgram.GetGameManager().GetHero();

            if (hero == null)
            {
                return;
            }
            MapLocation location = hero.GetLocation();

            hero.SetDirection(direction);
            MapLocation moveSpot = GetNextMoveSpot(location, direction);

            if (moveSpot == null)
            {
                return;
            }
            bool checkWinning = false;

            if (moveSpot.HasEntitys())
            {
                Entity entity;
                for (int i = 0; i < moveSpot.GetEntitys().Count; i++)
                {
                    entity = moveSpot.GetEntitys()[i];
                    if (entity.GetEntityType() == EntityType.BOX)
                    {
                        MapLocation boxMoveSpot = GetNextMoveSpot(entity.GetLocation(), direction);
                        if (boxMoveSpot == null)
                        {
                            return;
                        }
                        //Box attempting to move to a spot with entites.
                        if (boxMoveSpot.HasEntitys())
                        {
                            Entity e;
                            for (int k = 0; k < boxMoveSpot.GetEntitys().Count; k++)
                            {
                                e = boxMoveSpot.GetEntitys()[k];
                                if (e.GetEntityType() != EntityType.DESTINATION)
                                {
                                    return;
                                }
                                if (e.GetEntityType() == EntityType.DESTINATION)
                                {
                                    ((Box)entity).SetColor(BoxColor.BLUE);
                                    checkWinning = true;
                                    break;
                                }
                            }
                        }
                        else
                        {
                            //Box moving off destination
                            if (entity.GetEntityType() == EntityType.BOX)
                            {
                                ((Box)entity).SetColor(BoxColor.RED);
                            }
                        }
                        entity.GetLocation().RemoveEntity(entity);
                        boxMoveSpot.AddEntity(entity);
                        hero.IncrementBoxesPushed();
                        break;
                    }
                    if (entity.GetEntityType() != EntityType.DESTINATION)
                    {
                        return;
                    }
                }
            }
            location.RemoveEntity(hero);
            moveSpot.AddEntity(hero);
            hero.IncrementMoves();
            if (checkWinning)
            {
                CheckGameWinning();
            }
        }