示例#1
0
    private void Awake()
    {
        previousTileType                        = tileType;
        SceneManager.sceneLoaded               += OnSceneLoaded;
        SceneManager.sceneUnloaded             += OnSceneUnloaded;
        LevelManager.OnCurrentGameSceneChanged += OnLevelStart;
        LevelManager.OnCorruptedLoadDetected   += OnLevelStart;
        GameMenus.OnRestartPressed             += OnRestartLevel;
        LevelManager.OnMainMenuLoading         += OnBackToMainMenu;
        GridManager.OnResetTiles               += ResetTile;

        if (attribute == Attributes.Immobile)
        {
            attributeTile = new TileAttributeImmobile(null);
        }

        meshFilter              = GetComponent <MeshFilter>();
        meshRenderer            = GetComponent <MeshRenderer>();
        originalSharedMaterials = meshRenderer.sharedMaterials;


        if (meshRenderer.sharedMaterial == null)
        {
            return;
        }

        meshRenderer.GetPropertyBlock(PropertyBlock);

        if (meshRenderer.sharedMaterial.HasProperty(tileColor1ShaderName))
        {
            PropertyBlock.SetColor(tileColor1ShaderName, meshRenderer.sharedMaterial.GetColor(tileColor1ShaderName));
        }
        if (meshRenderer.sharedMaterial.HasProperty(tileColor2ShaderName))
        {
            PropertyBlock.SetColor(tileColor2ShaderName, meshRenderer.sharedMaterial.GetColor(tileColor2ShaderName));
        }
        if (meshRenderer.sharedMaterial.HasProperty(tileColor3ShaderName))
        {
            PropertyBlock.SetColor(tileColor3ShaderName, meshRenderer.sharedMaterial.GetColor(tileColor3ShaderName));
        }
        if (meshRenderer.sharedMaterial.HasProperty(tileColor4ShaderName))
        {
            PropertyBlock.SetColor(tileColor4ShaderName, meshRenderer.sharedMaterial.GetColor(tileColor4ShaderName));
        }

        meshRenderer.SetPropertyBlock(PropertyBlock);

        FixTileColor();

        for (int i = 0, length = TileOwner.TileData.materialColorNames.Length; i < length; i++)
        {
            startColors.Add(PropertyBlock.GetColor(TileOwner.TileData.materialColorNames[i]));
        }
    }
示例#2
0
    private void FixTileColor()
    {
        if (PropertyBlock == null)
        {
            return;
        }

        Vector3 localRot = transform.localRotation.eulerAngles;

        MeshRenderer.GetPropertyBlock(PropertyBlock);

        Color color1 = PropertyBlock.GetColor(tileColor1ShaderName);
        Color color3 = PropertyBlock.GetColor(tileColor3ShaderName);

        switch (tileType)
        {
        case TileType.Curve:
        case TileType.Pathless:
            if (localRot.y != 0 && localRot.y != 180)
            {
                return;
            }

            PropertyBlock.SetColor(tileColor3ShaderName, color1);
            PropertyBlock.SetColor(tileColor1ShaderName, color3);
            break;

        case TileType.Straight:
        case TileType.TCrossing:
            if (localRot.y != 90 && localRot.y != 270)
            {
                return;
            }

            PropertyBlock.SetColor(tileColor3ShaderName, color1);
            PropertyBlock.SetColor(tileColor1ShaderName, color3);
            break;
        }

        MeshRenderer.SetPropertyBlock(PropertyBlock);
    }
示例#3
0
    private IEnumerator AnimateTileWrap(Vector2Int moveTargetIndex, Vector3 movementStartPosition)
    {
        // Calculate needed values
        Vector3 moveTarget = TileOwner.GetTileLocalPosition(moveTargetIndex);
        Vector3 diff       = moveTarget - TileOwner.GetTileLocalPosition(tileIndex);

        // Keep in mind the proper grid spacing
        float offset = (moveTargetIndex - tileIndex).x != 0 ? TileOwner.GridSpacing.x : TileOwner.GridSpacing.y;

        // Calculate movement direction
        Vector3 moveDirection = offset * 2.0f * new Vector3(-Mathf.Clamp(diff.x, -1, 1), 0, -Mathf.Clamp(diff.z, -1, 1));

        // Create temporary tile for tile fading
        if (TileOwner.TempTile == null)
        {
            TileOwner.InitializeTempTile();
        }
        else if (TileOwner.TempTile.activeSelf == false)
        {
            TileOwner.TempTile.SetActive(true);
        }

        MeshRenderer tempTileRenderer = TileOwner.TempTile.GetComponent <MeshRenderer>();

        tempTileRenderer.enabled         = true;
        tempTileRenderer.sharedMaterials = MeshRenderer.sharedMaterials;

        TileOwner.TempTile.GetComponent <MeshFilter>().sharedMesh = MeshFilter.sharedMesh;
        TileOwner.TempTile.transform.parent     = transform.parent;
        TileOwner.TempTile.transform.position   = transform.position;
        TileOwner.TempTile.transform.localScale = transform.localScale;
        TileOwner.TempTile.transform.rotation   = transform.rotation;

        MaterialPropertyBlock tempPropertyBlock = new MaterialPropertyBlock();

        tempTileRenderer.GetPropertyBlock(tempPropertyBlock);

        for (int i = 0, length = meshRenderer.sharedMaterials.Length; i < length; i++)
        {
            PropertyBlock.SetFloat(TileOwner.TileData.fragmentationName, 1.0f);
        }

        if (TileOwner.PlayerObject.CurrentTileIndex == moveTargetIndex)
        {
            TileOwner.StartCoroutine(TileOwner.PlayerObject.AnimatePlayerWrapping());
        }

        // Setup loop timer
        float time    = 0.0f;
        float endTime = TileOwner.GridData.TileVerticalMovement[TileOwner.GridData.TileVerticalMovementKeyCount - 1].time;

        // Loops for fading
        while (time < endTime)
        {
            time += Time.deltaTime;

            //Resharper might be complaining, but trust me. It can be null
            if (tempTileRenderer != null)
            {
                if (time < TileOwner.GridData.tileWrapFadeOutDuration)
                {
                    // Fading out effect
                    for (int i = 0, length = tempTileRenderer.sharedMaterials.Length; i < length; i++)
                    {
                        tempPropertyBlock.SetFloat(TileOwner.TileData.fragmentationName, time / TileOwner.GridData.tileWrapFadeOutDuration);

                        for (int j = 0, colorNamesCount = TileOwner.TileData.materialColorNames.Length; j < colorNamesCount; j++)
                        {
                            string materialColorName = TileOwner.TileData.materialColorNames[j];
                            tempPropertyBlock.SetColor(materialColorName, PropertyBlock.GetColor(materialColorName));
                        }

                        tempTileRenderer.SetPropertyBlock(tempPropertyBlock);
                    }
                }
                else
                {
                    tempTileRenderer.enabled = false;
                }
            }

            if (time < TileOwner.GridData.tileWrapFadeInDuration)
            {
                // Fading in effect
                for (int i = 0, length = meshRenderer.sharedMaterials.Length; i < length; i++)
                {
                    PropertyBlock.SetFloat(TileOwner.TileData.fragmentationName, 1.0f - (time / TileOwner.GridData.tileWrapFadeInDuration));

                    for (int j = 0, colorNamesCount = TileOwner.TileData.materialColorNames.Length; j < colorNamesCount; j++)
                    {
                        string materialColorName = TileOwner.TileData.materialColorNames[j];
                        tempPropertyBlock.SetColor(materialColorName, PropertyBlock.GetColor(materialColorName));
                    }

                    meshRenderer.SetPropertyBlock(PropertyBlock);
                }
            }
            else
            {
                //meshRenderer.sharedMaterials = originalSharedMaterials;
                PropertyBlock.SetFloat(TileOwner.TileData.fragmentationName, 0);

                for (int i = 0; i < TileOwner.TileData.MaterialColorNamesLength; i++)
                {
                    tempPropertyBlock.SetColor(TileOwner.TileData.materialColorNames[i], PropertyBlock.GetColor(TileOwner.TileData.materialColorNames[i]));
                }

                meshRenderer.SetPropertyBlock(PropertyBlock);
            }

            // Move tiles (and player if present)
            if (time < endTime)
            {
                if (TileOwner.TempTile != null)
                {
                    TileOwner.TempTile.transform.localPosition = Vector3.Lerp(movementStartPosition, movementStartPosition + moveDirection, TileOwner.GridData.TileVerticalMovement.Evaluate(time));
                }

                transform.localPosition = Vector3.Lerp(moveTarget - moveDirection, moveTarget, TileOwner.GridData.TileVerticalMovement.Evaluate(time));
            }

            yield return(null);
        }

        if (TileOwner.TempTile != null)
        {
            TileOwner.TempTile.transform.localPosition = movementStartPosition + moveDirection;
        }
        transform.localPosition = moveTarget;

        // Destroy temporary objects
        if (TileOwner.TempTile != null)
        {
            TileOwner.TempTile.SetActive(false);
        }

        // Mark as movement ended
        moving = false;
    }