Exemple #1
0
        public void AddSprite(
            SpriteRecord record
            )
        {
            if (spritePrefab == null)
            {
                return;
            }
            // instantiate sprite prefab
            var spriteGO = UnityEngine.Object.Instantiate(spritePrefab, contentTransform);
            // grab controller
            var spriteCtrl = spriteGO.GetComponent <UxAvailSpriteCtrl>();

            if (spriteCtrl != null)
            {
                // create ctx
                var ctx = new AvailSpriteCtx(record, spriteCtrl);
                // assign sprite image
                spriteCtrl.AssignSprite(record.sprite);
                // wire handlers
                // -- select button
                spriteCtrl.selectButton.onClick.AddListener(() => {
                    if (currentCtx != null)
                    {
                        currentCtx.ctrl.Select(false);
                    }
                    spriteCtrl.Select(true);
                    mgrCtx.selectSpriteFcn(record);
                    currentCtx = ctx;
                });
                // add to ctx map
                ctxMap[record.id] = ctx;
            }
        }
Exemple #2
0
 public AvailSpriteCtx(
     SpriteRecord record,
     UxAvailSpriteCtrl ctrl
     )
 {
     this.record = record;
     this.ctrl   = ctrl;
 }
Exemple #3
0
        public SpriteRecord Lookup(
            int id
            )
        {
            SpriteRecord record = null;

            spriteMap.TryGetValue(id, out record);
            return(record);
        }
Exemple #4
0
        void Fill(
            TilePos startPos,
            SpriteRecord sprite
            )
        {
            // get starting tile kind
            var matchID  = layer.Get(startPos.x, startPos.y);
            var visited  = new HashSet <TilePos>();
            var traverse = new List <TilePos>();

            // add starting node to traverse list
            traverse.Add(startPos);
            // iterate through list of nodes to traverse
            while (traverse.Count > 0)
            {
                var pos = traverse[0];
                traverse.RemoveAt(0);
                // update current node
                AssignTile(imageMap[pos], pos, sprite);
                visited.Add(pos);
                // check/add neighbors
                var neighbors = new TilePos[] {
                    new TilePos(pos.x - 1, pos.y),
                    new TilePos(pos.x + 1, pos.y),
                    new TilePos(pos.x, pos.y - 1),
                    new TilePos(pos.x, pos.y + 1),
                };
                foreach (var neighbor in neighbors)
                {
                    // bounds
                    if (neighbor.x < 0 || neighbor.x >= layer.width || neighbor.y < 0 || neighbor.y >= layer.height)
                    {
                        continue;
                    }
                    // visited
                    if (visited.Contains(neighbor))
                    {
                        continue;
                    }
                    // already set to traverse
                    if (traverse.Contains(neighbor))
                    {
                        continue;
                    }
                    // non-matching tile
                    if (layer.Get(neighbor.x, neighbor.y) != matchID)
                    {
                        continue;
                    }
                    // tile matches
                    traverse.Add(neighbor);
                }
            }
        }
Exemple #5
0
        public void Select(
            int id
            )
        {
            SpriteRecord record = null;

            spriteMap.TryGetValue(id, out record);
            Debug.Log("trying to select: " + id + " w/ record: " + record);
            if (record != null)
            {
                selectedSprite = record;
                ctx.ctrl.SelectSprite(record);
            }
        }
Exemple #6
0
 void Clear()
 {
     // clear controller
     if (ctx != null)
     {
         ctx.ctrl.Clear();
     }
     // clear current sprite map and selection
     if (spriteMap != null)
     {
         spriteMap.Clear();
     }
     selectedSprite = null;
 }
Exemple #7
0
 // INSTANCE METHODS ----------------------------------------------------
 // assign context
 public void AssignCtx(
     SpriteMgrCtx ctx
     )
 {
     // save mgr context
     this.ctx = ctx;
     // assign ctrl context
     ctx.ctrl.Assign(new UxSpriteMgrCtrlCtx {
         project         = ctx.project,
         selectSpriteFcn = (record) => {
             selectedSprite = record;
         },
         reloadFcn = Load,
     });
 }
Exemple #8
0
        public void SelectSprite(
            SpriteRecord record
            )
        {
            AvailSpriteCtx ctx;

            if (ctxMap.TryGetValue(record.id, out ctx))
            {
                if (currentCtx != null)
                {
                    currentCtx.ctrl.Select(false);
                }
                ctx.ctrl.Select(true);
                currentCtx = ctx;
            }
        }
Exemple #9
0
 void AssignTile(
     Image img,
     TilePos pos,
     SpriteRecord record
     )
 {
     if (record == null)
     {
         return;
     }
     // assign sprite to image
     if (img != null)
     {
         img.sprite = record.sprite;
         img.color  = Color.white;
     }
     // assign record id to layer
     layer.Set(pos.x, pos.y, record.id);
 }