private void SetSnap()
 {
     if (snap != null)
     {
         return;
     }
     snap           = GetComponent <ZST_SnapToGrid>();
     snap.hideFlags = HideFlags.HideInInspector;
 }
Example #2
0
    void Start()
    {
        snap = GetComponent <ZST_SnapToGrid>();

        // we need to set up our quadrants, if they're not already set up
        if (quadrants == null || quadrants.Length <= 0)
        {
            List <Quadrant> quadrantsMut = new List <Quadrant>();

            int numDirections = spriteDirections.Length;

            for (int i = 0; i < numDirections; ++i)
            {
                GameObject spriteObj = new GameObject();

                SpriteRenderer sr = spriteObj.AddComponent <SpriteRenderer>();

                float rot   = (float)i / (float)numDirections * 360;
                float theta = rot * Mathf.Deg2Rad + Mathf.PI * 0.25f;

                Vector2 offset = (new Vector2(Mathf.Sign(Mathf.Cos(theta)), Mathf.Sign(Mathf.Sin(theta)))) * 0.5f;

                CardinalDirection dir = (CardinalDirection)spriteDirections[i];
                spriteObj.name = "Sprite " + dir.ToString();
                spriteObj.transform.SetParent(transform);

                Quadrant quadrant = new Quadrant();
                quadrant.startRot = rot;
                quadrant.dir      = dir;
                quadrant.offset   = offset;
                quadrant.sr       = sr;

                quadrantsMut.Add(quadrant);
            }

            quadrants = quadrantsMut.ToArray();

            // we want to be sure to save our serialized quadrants
                        #if UNITY_EDITOR
            if (!Application.isPlaying)
            {
                EditorSceneManager.MarkSceneDirty(UnityEngine.SceneManagement.SceneManager.GetActiveScene());
            }
                        #endif
        }

        // Hide the children! Quick! Before they find out!
        HideChildren();

        // we add tiles so that the map manager knows about them
        ZST_MapManager.SharedInstance.AddTile(this);

        // we want to adjust everything when this is added so that it fits in place
        ZST_MapManager.SharedInstance.AddUpdateTag(tileTag);
    }
Example #3
0
    public Coord GetCoord()
    {
        if (!snap)
        {
            snap = GetComponent <ZST_SnapToGrid>();
        }
        Vector2 gridPos = snap.gridSideLength == 0 ? Vector2.zero : (Vector2)snap.GetGridPosition() / snap.gridSideLength;
        Coord   coord   = new Coord((int)gridPos.y, (int)gridPos.x);

        return(coord);
    }