/// <summary>
    /// Generate a hexagon element with match type from allowable color range.
    /// If bomb has to be dropped it add bomb to the hexagon element.
    /// </summary>
    public void Generate(bool generateBomb = false)
    {
        Array     array     = Enum.GetValues(typeof(MatchType));
        MatchType matchType = (MatchType)array.GetValue(UnityEngine.Random.Range(1, colorNo + 1)); // No empty

        this.hexagonElement = new HexagonElement(matchType, new BombInfo(generateBomb, generateBomb ? this.bombLifeTime : 0));
    }
    /// <summary>
    /// Check grid to decrease every bomb timer with every user action.
    /// If any bomb timer reaches to 0, then send this information to end the game.
    /// </summary>
    public BombEvent BombCheck(ref HexagonGrid grid)
    {
        BombEvent bombEvent = new BombEvent(false, new List <BombElement>());

        for (int x = 0; x < grid.width; x++)
        {
            for (int y = 0; y < grid.height; y++)
            {
                if (!(bool)grid.GetElement(x, y)?.bombInfo.hasBomb) // If hexagon element does not have bomb then continue
                {
                    continue;
                }
                HexagonElement hexagonElement = (HexagonElement)grid.GetElement(x, y);
                int            bombTimer      = hexagonElement.bombInfo.bombLeftMove;
                bombTimer--;
                if (bombTimer <= 0) // Bomb time reaches to zero end the game
                {
                    bombEvent.isBombeExploded = true;
                    Debug.Log("Bomb exploded at: (" + x + ", " + y + ")");
                }
                grid.SetElement(x, y, new HexagonElement(hexagonElement.matchType, new BombInfo(hexagonElement.bombInfo.hasBomb, bombTimer)));
                bombEvent.bombElements.Add(new BombElement(new Vector2(x, y), new BombInfo(true, bombTimer))); // Send list of bomb info with their timer to renew UI and such
            }
        }
        return(bombEvent);
    }
    /// <summary>
    /// Move grid element to new destination specified with offset values.
    /// The left grid will become empty for the vacancy.
    /// </summary>
    public Tuple <int, int> Move(HexagonGrid grid, int x, int y, HexagonElement?hexagonElement = null)
    {
        HexagonElement element = hexagonElement == null ? new HexagonElement(MatchType.empty) : (HexagonElement)hexagonElement;
        bool           valid   = grid.IsValid(x, y);

        if (valid)
        {
            element = (HexagonElement)grid.GetElement(x, y);
        }
        if (element.matchType == MatchType.empty)
        {
            return(null);
        }
        foreach (Tuple <int, int> offset in offsets)
        {
            var dx = offset.Item1;
            var dy = offset.Item2;
            if (grid.GetMatchType(x + dx, y + dy) == MatchType.empty)
            {
                grid.SetElement(x + dx, y + dy, element);

                if (valid)
                {
                    grid.SetElement(x, y, new HexagonElement(MatchType.empty));
                }
                return(new Tuple <int, int>(x + dx, y + dy));
            }
        }
        return(null);
    }
    /// <summary>
    /// With match element, clone mock grids to simulate the situation if the match is valid or not.
    /// Search mock grids to find a match with specified rules. If there is match, add matches to the explode events, send turn numbe rof the mock grid and bomb events.
    /// </summary>
    /// <param name="matchElement">Threee coord values for the match.</param>
    /// <param name="simulate">If match is valid, make it a valid simulation by changing original grid with the mock one.</param>
    /// <returns>Explode event</returns>
    public ExplodeEvent Explode(MatchElement?matchElement, bool isTurnClockwise = false, bool simulate = true)
    {
        // Create mock grid to simulate request
        List <HexagonGrid> mockGrids = new List <HexagonGrid>();

        for (int i = 0; i < 2; i++)
        {
            mockGrids.Add((HexagonGrid)this.grid.Clone());
        }

        // Turn mock grids. First one with 120 degree, secone one with 240 degree.
        if (matchElement != null && matchElement?.coords != null)
        {
            int firstIndex = isTurnClockwise ? 0 : 2;
            int lastIndex  = isTurnClockwise ? 2 : 0;
            for (int mockIndex = 0; mockIndex < 2; mockIndex++)
            {
                for (int turn = 0; turn < mockIndex + 1; turn++)
                {
                    HexagonElement element = (HexagonElement)mockGrids[mockIndex].GetElement((int)matchElement?.coords[firstIndex].x, (int)matchElement?.coords[firstIndex].y);
                    mockGrids[mockIndex].grid[(int)matchElement?.coords[firstIndex].x, (int)matchElement?.coords[firstIndex].y] = mockGrids[mockIndex].grid[(int)matchElement?.coords[1].x, (int)matchElement?.coords[1].y];
                    mockGrids[mockIndex].grid[(int)matchElement?.coords[1].x, (int)matchElement?.coords[1].y] = mockGrids[mockIndex].grid[(int)matchElement?.coords[lastIndex].x, (int)matchElement?.coords[lastIndex].y];
                    mockGrids[mockIndex].grid[(int)matchElement?.coords[lastIndex].x, (int)matchElement?.coords[lastIndex].y] = element;
                }
            }
        }

        // Check explode events on the mock grids to see if the action is valid or not.
        // Every rule has to be tried to be sure the match.
        List <ExplodeElement> explodeElements = new List <ExplodeElement>();
        int selectedMockGridNo = 0;

        CheckGridWithrulesToFindMatch(mockGrids, explodeElements, ref selectedMockGridNo);

        // If action is valid and not for game over check
        if (explodeElements.Count > 0 && simulate)
        {
            this.grid = mockGrids[selectedMockGridNo];
        }

        ExplodeEvent explodeEvent = new ExplodeEvent(explodeElements.Count > 0, selectedMockGridNo + 1, explodeElements);

        // If action is valid and not for game over check
        if (simulate)
        {
            scoring.SetScore(explodeEvent);
        }

        // If action is valid and done by user then check for bomb timers and explosion.
        if (matchElement != null && matchElement?.coords != null && matchElement?.coords.Count > 0 && simulate)
        {
            BombEvent bombEvent = scoring.BombCheck(ref this.grid);
            explodeEvent.bombEvent = bombEvent;
        }


        return(explodeEvent);
    }
Exemplo n.º 5
0
    // 新建一个调试Hexagon对象aa
    public static GameObject NewElement(Hexagon.Coord v, ushort height, float fWeight, int type)
    {
        if (vertex == null)
        {
            vertex = new Vector3[7];

            float fSize    = Hexagon.Manager.GetSingleton().GetHexagonSize() * 0.95f;
            float halfSize = fSize * 0.5f;

            float fSqrt12 = halfSize * 2.0f * Mathf.Sqrt(1.0f / 12.0f);
            float fHeight = 0.1f;

            vertex[0] = Vector3.zero;
            vertex[1] = new Vector3(halfSize, fHeight, -fSqrt12);
            vertex[2] = new Vector3(halfSize, fHeight, fSqrt12);
            vertex[3] = new Vector3(0, fHeight, 2.0f * fSqrt12);

            vertex[4] = new Vector3(-halfSize, fHeight, fSqrt12);
            vertex[5] = new Vector3(-halfSize, fHeight, -fSqrt12);
            vertex[6] = new Vector3(0, fHeight, -2.0f * fSqrt12);

            face = new int[6 * 3]
            {
                0, 6, 5,
                0, 5, 4,
                0, 4, 3,
                0, 3, 2,
                0, 2, 1,
                0, 1, 6
            };
        }

        if (parent == null)
        {
            parent = new GameObject();
            parent.transform.position = Vector3.zero;
            parent.transform.rotation = Quaternion.identity;
        }

        Vector3    pos = Hexagon.Manager.GetSingleton().Coord_Position(v, height);
        GameObject obj = new GameObject();

        obj.name               = v.x + "_" + v.z + "_" + v.y;
        obj.transform.parent   = parent.transform;
        obj.transform.position = pos;
        obj.transform.rotation = Quaternion.identity;;

        HexagonElement ele = obj.AddComponent <HexagonElement>();

        ele.Build(v, height, fWeight, type);
        return(obj);
    }
Exemplo n.º 6
0
 public int this[HexagonElement index]
 {
     get
     {
         if (ElementsAmount != null)
         {
             return(index != HexagonElement.None ? ElementsAmount[(int)index] : -1);
         }
         else
         {
             return(-1);
         }
     }
 }
Exemplo n.º 7
0
    // 虚函数(继承自MonoBehaviour)
    void Start()
    {
        mActor = gameObject.GetComponent <sdGameActor>();
        if (mActor == null)
        {
            mActor = gameObject.GetComponent <sdGameMonster>();
        }

        Hexagon.Coord kCoord = new Hexagon.Coord();
        for (int i = 0; i < 37; ++i)
        {
            mElementObjectList.Add(HexagonElement.NewElement(kCoord, 0, 0.0f, 3));
        }
    }
Exemplo n.º 8
0
 public LevelConditionInfo(HexagonElement element, int requiredAmount)
 {
     Element        = element;
     RequiredAmount = requiredAmount;
 }
 /// <summary>
 /// Set hexagon element to the coords.
 /// </summary>
 public void SetElement(int x, int y, HexagonElement element)
 {
     this.grid[x, y] = element;
 }
Exemplo n.º 10
0
 public MoveElement(HexagonElement hexagonElement, Vector2 from, Vector2 to)
 {
     this.hexagonElement = hexagonElement;
     this.from           = from;
     this.to             = to;
 }
Exemplo n.º 11
0
    // Update is called once per frame
    void Update()
    {
        if (SearchTest)
        {
            SearchTest = false;

            //if(actor.actorType == ActorType.AT_Monster)
            {
                Hexagon.PathFinder.StaticWeightScale    = StaticWeightScale;
                Hexagon.PathFinder.DynamicWeightScale   = DynamicWeightScale;
                Hexagon.PathFinder.StraightWeightScale  = StraightWeightScale;
                Hexagon.PathFinder.HeuristicWeightScale = HeuristicWeightScale;
                Hexagon.PathFinder.MiddlePointWeight    = UseMiddlePointWeight;

                foreach (GameObject o in lstObject)
                {
                    GameObject.Destroy(o);
                }
                lstObject.Clear();

                sdMainChar  mc    = sdGameLevel.instance.mainChar;
                sdGameActor actor = GetComponent <sdGameActor>();
                if (actor != null)
                {
                    actor.UnInject(false);
                }
                mc.UnInject(false);
                List <Hexagon.SearchNode> lstSearch   = new List <Hexagon.SearchNode>();
                BT.BinaryTree             lstUnSearch = new BT.BinaryTree();
                Hexagon.Manager.GetSingleton().DebugFindPath(transform.position, mc.transform.position, ref lstSearch, ref lstUnSearch);
                mc.Inject(false);
                if (actor != null)
                {
                    actor.Inject(false);
                }
                Hexagon.SearchNode end = lstSearch[lstSearch.Count - 1];
                for (int i = lstSearch.Count - 1; i >= 0; i--)
                {
                    Hexagon.SearchNode n = lstSearch[i];
                    int type             = 0;
                    if (n == end)
                    {
                        type = 2;
                        end  = n.parent;
                    }
                    ushort tempHeight = Hexagon.Manager.GetSingleton().GetHeight(n.v);
                    Add(HexagonElement.NewElement(n.v, tempHeight, n.weight(), type));
                }
                Hexagon.SearchNode head = (Hexagon.SearchNode)lstUnSearch.Pop();
                while (true)
                {
                    if (head == null)
                    {
                        break;
                    }
                    ushort tempHeight = Hexagon.Manager.GetSingleton().GetHeight(head.v);
                    Add(HexagonElement.NewElement(head.v, tempHeight, head.weight(), 1));
                    head = (Hexagon.SearchNode)lstUnSearch.Pop();
                }
            }
        }
    }
Exemplo n.º 12
0
        public static Rectangle ElementSource(HexagonElement index)
        {
            int size = Elements.Height;

            return(new Rectangle(size * (int)index, 0, size, size));
        }
Exemplo n.º 13
0
 public HexagonContent(HexagonElement element)
 {
     type         = HexagonType.Element;
     this.element = element;
     frozen       = false;
 }
Exemplo n.º 14
0
 public HexagonContent(HexagonType type = HexagonType.Empty, HexagonElement element = HexagonElement.None, bool frozen = false)
 {
     this.type    = type;
     this.element = element;
     this.frozen  = frozen;
 }