Exemplo n.º 1
0
    private PuzzleCharacter PushCharacter(string characterId)
    {
        PuzzleCharacter character = new PuzzleCharacter(this.currentCharacterIndex++, characterId);

        this.PuzzleCharacters.Add(character);
        return(character);
    }
    /// <summary>
    ///
    /// </summary>
    /// <param name="character"></param>
    private void RenderPuzzleNode(PuzzleCharacter character)
    {
        anchorInterval = intervalAnchor.transform.localPosition.x - startAnchor.transform.localPosition.x;
        startPosition  = new Vector3(startAnchor.transform.localPosition.x - anchorInterval,
                                     startAnchor.transform.localPosition.y + anchorInterval,
                                     -1);

        GameObject nodeObject = new GameObject(GetCharacterKey(character.Index));

        nodeObject.transform.parent = this.transform;

        nodeObject.transform.localPosition = this.ConvertToPixelPosition(character.Position);
        nodeObject.transform.localScale    = new Vector3(1.0f, 1.0f, 1);

        var sprite = GlobalStorage.GetSpriteFromDictionary(character.CharacterId);

        //// var sprite = Resources.Load<Sprite>("characters/fzlb/c_" + character.CharacterId);
        if (sprite != null)
        {
            var renderer = nodeObject.AddComponent <SpriteRenderer>();
            renderer.sprite = sprite;
            var clr = renderer.color;
            renderer.color = new Color(clr.r, clr.g, clr.b, 0f);
            var fadeIn = nodeObject.AddComponent <FadeIn>();
            fadeIn.SetFadeTime(2.5f);
        }

        var nodeRenderer = nodeObject.AddComponent <PuzzleNodeRenderer>();

        nodeRenderer.Initialize(character, (chara) => { this.OnBoardClickedAt(character); });
    }
    public void Initialize(PuzzleCharacter character, Action <PuzzleCharacter> callback)
    {
        this.Character       = character;
        this.OnClickCallback = callback;

        if (this.gameObject.GetComponent <BoxCollider2D>() == null)
        {
            this.gameObject.AddComponent <BoxCollider2D>();
        }
    }
Exemplo n.º 4
0
    private FormulaDefinition AreCharactersMatching(PuzzleCharacter charA, PuzzleCharacter charB)
    {
        if (charA == null || charB == null)
        {
            return(null);
        }

        var formula = this.stageDefinition.FindFormula(charA.CharacterId, charB.CharacterId);

        return(formula);
    }
    /////////// Callback from Provider /////////


    /// <summary>
    ///
    /// </summary>
    /// <param name="position"></param>
    public void OnChooseCharacter(PuzzleCharacter character)
    {
        Debug.Log("OnChooseCharacter: " + character.Index);

        var characterNode = this.FindCharacterNode(character);

        if (characterNode == null)
        {
            return;
        }

        characterNode.transform.localScale = new Vector3(1.4f, 1.4f, 0);
    }
Exemplo n.º 6
0
 private void ClearCharacter(PuzzleCharacter character)
 {
     if (character != null)
     {
         this.PuzzleCharacters.Remove(character);
         Vector2Int position = character.Position;
         this.CharacterMatrix[position.x, position.y] = null;
     }
     else
     {
         Debug.LogWarning("ClearCharacter: character is null.");
     }
 }
Exemplo n.º 7
0
    private void PlaceCharacterInMatrix(PuzzleCharacter puzzleChar)
    {
        int posX = Utils.RandomInteger(1, this.Width);
        int posY = Utils.RandomInteger(1, this.Height);

        while (this.CharacterMatrix[posX, posY] != null)
        {
            posX = Utils.RandomInteger(1, this.Width);
            posY = Utils.RandomInteger(1, this.Height);
        }

        this.CharacterMatrix[posX, posY] = puzzleChar;
        puzzleChar.Position = new Vector2Int(posX, posY);
    }
    public GameObject FindCharacterNode(PuzzleCharacter character)
    {
        if (character == null)
        {
            return(null);
        }

        var childTransform = this.gameObject.transform.Find("Character_" + character.Index.ToString());

        if (childTransform != null)
        {
            return(childTransform.gameObject);
        }

        return(null);
    }
    /// <summary>
    /// The two characters not match, or use click the firstPosition again. Cancel them.
    /// </summary>
    /// <param name="position"></param>
    /// <param name="firstPosition"></param>
    public void OnUnchooseCharacter(PuzzleCharacter character, PuzzleCharacter firstCharacter)
    {
        Debug.Log("OnUnchooseCharacter: " + character.Position);
        var characterNode = this.FindCharacterNode(character);

        if (characterNode == null)
        {
            return;
        }

        characterNode.transform.localScale = new Vector3(1f, 1f, 0);

        characterNode = this.FindCharacterNode(firstCharacter);
        if (characterNode == null)
        {
            return;
        }

        characterNode.transform.localScale = new Vector3(1f, 1f, 0);
    }
    public void OnConnected(PuzzleCharacter character, PuzzleCharacter firstCharacter, List <Vector2Int> connectionPoints, string targetCharId)
    {
        Debug.Log("OnConnected: " + character.Position);
        var characterNode = this.FindCharacterNode(character);

        if (characterNode == null)
        {
            Debug.LogWarning("characterNode is null.");
        }

        var firstCharacterNode = this.FindCharacterNode(firstCharacter);

        if (firstCharacterNode == null)
        {
            Debug.LogWarning("firstCharacterNode is null.");
        }

        FormulaDefinition formula = this.StageDefinition.FindFormula(character.CharacterId, firstCharacter.CharacterId);

        if (formula == null)
        {
            return;
        }

        var activityManager = gameObject.GetComponentInParent <MainGameScene>().AquireActivityManager();

        PlayAnimationMergeChars(activityManager, characterNode, firstCharacterNode, connectionPoints);

        if (poemInstance.GetCoveredCharIds().Contains(formula.Target))
        {
            int charIndex = poemInstance.GetFirstCoveredIndex(formula.Target);
        }

        if (this.onReceivedCharacter != null)
        {
            activityManager.PushCallback(() =>
                                         { this.onReceivedCharacter(this, new ReceivedCharEventArgs(formula.Target, activityManager)); });
        }

        activityManager.PushCallback(() => { CheckAndMakeShuffle(); });
    }
Exemplo n.º 11
0
    public void TakeAction(PuzzleCharacter character)
    {
        if (this.Status == PuzzleBoardStatus.IDLE)
        {
            this.LastSelectedCharacter = character;
            this.Status = PuzzleBoardStatus.ONE_SELECTED;
            this.handler.OnChooseCharacter(character);

            return;
        }

        var matchingFormula = this.AreCharactersMatching(character, this.LastSelectedCharacter);

        if (character.Index == this.LastSelectedCharacter.Index || matchingFormula == null)
        {
            // Cancel selection
            this.Status = PuzzleBoardStatus.IDLE;
            this.handler.OnUnchooseCharacter(character, this.LastSelectedCharacter);
            return;
        }

        var connetionPoints = this.ConnectCharacters(character, this.LastSelectedCharacter);

        if (connetionPoints != null && connetionPoints.Count > 0)
        {
            // Erase the two characters
            this.ClearCharacter(this.LastSelectedCharacter);
            this.ClearCharacter(character);

            this.Status = PuzzleBoardStatus.IDLE;
            this.handler.OnConnected(character, this.LastSelectedCharacter, connetionPoints, matchingFormula.Target);
        }
        else
        {
            // Matching but cannot connect
            this.Status = PuzzleBoardStatus.IDLE;
            this.handler.OnMatchNotConnected(character, this.LastSelectedCharacter);
        }
    }
 public void OnMatchNotConnected(PuzzleCharacter character, PuzzleCharacter firstCharacter)
 {
     Debug.Log("OnMatchNotConnected: " + character.Position);
     OnUnchooseCharacter(character, firstCharacter);
 }
 public void OnBoardClickedAt(PuzzleCharacter character)
 {
     Debug.Log("OnBoardClickedAt: " + character.CharacterId);
     this.puzzleBoard.TakeAction(character);
 }
Exemplo n.º 14
0
    private List <Vector2Int> ConnectCharacters(PuzzleCharacter charA, PuzzleCharacter charB)
    {
        Debug.Log("Start ConnectCharacters.");

        var posA = charA.Position;
        var posB = charB.Position;

        List <Vector2Int> result = new List <Vector2Int>();

        result.Add(posA);

        // Check One Connection
        if (this.AreDirectConnectedPos(posA, posB))
        {
            result.Add(posB);
            return(result);
        }
        Debug.Log("Check one connection failed.");

        // Check Two Connections
        var candidate = new Vector2Int(posA.x, posB.y);

        if (this.GetCharacterAt(candidate) == null &&
            this.AreDirectConnectedPos(posA, candidate) && this.AreDirectConnectedPos(candidate, posB))
        {
            result.Add(candidate);
            result.Add(posB);
            return(result);
        }
        candidate = new Vector2Int(posB.x, posA.y);
        if (this.GetCharacterAt(candidate) == null &&
            this.AreDirectConnectedPos(posA, candidate) && this.AreDirectConnectedPos(candidate, posB))
        {
            result.Add(candidate);
            result.Add(posB);
            return(result);
        }
        Debug.Log("Check two connections failed.");

        // Check Three Inner Connections
        if (posA.x != posB.x)
        {
            var deltaX = posA.x < posB.x ? 1 : -1;
            var iter   = posA.x + deltaX;
            while (iter != posB.x)
            {
                var candidate1 = new Vector2Int(iter, posA.y);
                var candidate2 = new Vector2Int(iter, posB.y);
                if (this.GetCharacterAt(candidate1) == null && this.GetCharacterAt(candidate2) == null &&
                    this.AreDirectConnectedPos(posA, candidate1) &&
                    this.AreDirectConnectedPos(candidate1, candidate2) &&
                    this.AreDirectConnectedPos(candidate2, posB)
                    )
                {
                    result.Add(candidate1);
                    result.Add(candidate2);
                    result.Add(posB);
                    return(result);
                }

                iter += deltaX;
            }
        }
        if (posA.y != posB.y)
        {
            var deltaY = posA.y < posB.y ? 1 : -1;
            var iter   = posA.y + deltaY;
            while (iter != posB.y)
            {
                var candidate1 = new Vector2Int(posA.x, iter);
                var candidate2 = new Vector2Int(posB.x, iter);
                if (this.GetCharacterAt(candidate1) == null && this.GetCharacterAt(candidate2) == null &&
                    this.AreDirectConnectedPos(posA, candidate1) &&
                    this.AreDirectConnectedPos(candidate1, candidate2) &&
                    this.AreDirectConnectedPos(candidate2, posB)
                    )
                {
                    result.Add(candidate1);
                    result.Add(candidate2);
                    result.Add(posB);
                    return(result);
                }

                iter += deltaY;
            }
        }
        Debug.Log("Check three inner connections failed.");

        // Check Three Outter Connection
        var minX = Math.Min(posA.x, posB.x);
        var maxX = Math.Max(posA.x, posB.x);
        var minY = Math.Min(posA.y, posB.y);
        var maxY = Math.Max(posA.y, posB.y);

        for (int i = maxX + 1; i <= this.Width + 1; i++)
        {
            var candidate1 = new Vector2Int(i, posA.y);
            var candidate2 = new Vector2Int(i, posB.y);
            if (this.GetCharacterAt(candidate1) == null && this.GetCharacterAt(candidate2) == null &&
                this.AreDirectConnectedPos(posA, candidate1) &&
                this.AreDirectConnectedPos(candidate1, candidate2) &&
                this.AreDirectConnectedPos(candidate2, posB)
                )
            {
                result.Add(candidate1);
                result.Add(candidate2);
                result.Add(posB);
                return(result);
            }
        }
        for (int i = minX - 1; i >= 0; i--)
        {
            var candidate1 = new Vector2Int(i, posA.y);
            var candidate2 = new Vector2Int(i, posB.y);
            if (this.GetCharacterAt(candidate1) == null && this.GetCharacterAt(candidate2) == null &&
                this.AreDirectConnectedPos(posA, candidate1) &&
                this.AreDirectConnectedPos(candidate1, candidate2) &&
                this.AreDirectConnectedPos(candidate2, posB)
                )
            {
                result.Add(candidate1);
                result.Add(candidate2);
                result.Add(posB);
                return(result);
            }
        }
        for (int j = maxY + 1; j <= this.Height + 1; j++)
        {
            var candidate1 = new Vector2Int(posA.x, j);
            var candidate2 = new Vector2Int(posB.x, j);
            if (this.GetCharacterAt(candidate1) == null && this.GetCharacterAt(candidate2) == null &&
                this.AreDirectConnectedPos(posA, candidate1) &&
                this.AreDirectConnectedPos(candidate1, candidate2) &&
                this.AreDirectConnectedPos(candidate2, posB)
                )
            {
                result.Add(candidate1);
                result.Add(candidate2);
                result.Add(posB);
                return(result);
            }
        }
        for (int j = minY - 1; j >= 0; j--)
        {
            var candidate1 = new Vector2Int(posA.x, j);
            var candidate2 = new Vector2Int(posB.x, j);
            if (this.GetCharacterAt(candidate1) == null && this.GetCharacterAt(candidate2) == null &&
                this.AreDirectConnectedPos(posA, candidate1) &&
                this.AreDirectConnectedPos(candidate1, candidate2) &&
                this.AreDirectConnectedPos(candidate2, posB)
                )
            {
                result.Add(candidate1);
                result.Add(candidate2);
                result.Add(posB);
                return(result);
            }
        }
        Debug.Log("Check three outer connections failed.");

        return(null);
    }