Beispiel #1
0
        public override void RefreshTile(Vec3I gridPos, TM.ITilemap tilemap)
        {
            Vec2I pos     = GridToTile(gridPos);
            Vec2I subTile = GridToSubTile(gridPos);

            Map.BBTile tile = GetTile(pos);

            if (!disableRefresh || HasSprite(tile, pos, subTile))
            {
                tilemap.RefreshTile(gridPos);
            }

            if (disableRefresh)
            {
                return;
            }

            for (int tx = 0; tx < 2; ++tx)
            {
                for (int ty = 0; ty < 2; ++ty)
                {
                    Vec2I t = pos + new Vec2I(tx + subTile.x - 1, ty + subTile.y - 1);
                    if (t != pos)
                    {
                        t *= 2;
                        for (int i = 0; i < 4; ++i)
                        {
                            tilemap.RefreshTile(new Vec3I(t.x + (i >> 1), t.y + (i % 2), 0));
                        }
                    }
                }
            }
        }
Beispiel #2
0
        private void SetBuilding(Vec2I pos, IBuilding building)
        {
            var tileMain = Tile(pos);

            if (building == null)
            {
                building = tileMain.bldgMain;
                tileMain = null;
            }

            foreach (var t in building.bounds.allPositionsWithin)
            {
                var tile = Tile(t);
                tile.bldgMain = null;
                tile.bldgAdj  = tileMain;
            }

            if (tileMain != null)
            {
                tileMain.bldgMain = building;
                tileMain.bldgAdj  = null;
            }

            tiler.UpdateBuilding(pos);
        }
Beispiel #3
0
        // TODO: this needs to handle sprite anchor
        // TODO: this is kinda jank
        // shouldnt be new'ing defs, and we'll end up with duplicate sprites
        public static bool SplitSprite(SpriteDef sprite, int height, out SpriteDef lower, out SpriteDef upper)
        {
            var atlas       = sprite.atlas;
            var heightTiles = height * atlas.tilesPerUnit;
            var rect        = sprite.rect;

            if (sprite.rect.size.y > heightTiles)
            {
                lower = new SpriteDef(null, atlas, new Atlas.Rect(
                                          rect.origin,
                                          new Vec2I(rect.size.x, heightTiles),
                                          rect.anchor));

                var upperOfs = new Vec2I(0, heightTiles);
                upper = new SpriteDef(null, atlas, new Atlas.Rect(
                                          rect.origin + upperOfs,
                                          new Vec2I(rect.size.x, rect.size.y - heightTiles),
                                          rect.anchor - upperOfs));

                return(true);
            }

            lower = upper = null;
            return(false);
        }
Beispiel #4
0
        public static RectInt RectInclusive(this Rect rect)
        {
            Vec2I lower = rect.min.Floor();
            Vec2I upper = rect.max.Ceil();

            return(new RectInt(lower, upper - lower));
        }
Beispiel #5
0
        private Sprite GetSprite(Map map, Vec2I pos, Vec2I subTile, int frame)
        {
            BB.Assert(frame < def.spriteFrames.Length);

            TerrainDef lambdaDef = def; // C# is dumb

            Tiling.TileType ttype = Tiling.GetTileType(pos, subTile, p => IsSame(map, p, lambdaDef));

            // TODO: Kludge for water arrangement in atlas
            if (def == K_waterDef)
            {
                switch (ttype)
                {
                case Tiling.TileType.ConcaveBL: ttype = Tiling.TileType.ConcaveTR; break;

                case Tiling.TileType.ConcaveTR: ttype = Tiling.TileType.ConcaveBL; break;

                case Tiling.TileType.ConcaveBR: ttype = Tiling.TileType.ConcaveTL; break;

                case Tiling.TileType.ConcaveTL: ttype = Tiling.TileType.ConcaveBR; break;
                }
            }

            var spritePos = def.spriteFrames[frame] + Tiling.SpriteOffset(ttype);

            return(atlas.GetSprite(spritePos, Vec2I.one));
        }
Beispiel #6
0
        public MetaAtlas(AssetSrc assets)
        {
            #region Caches
            keys = new CacheNonNullable <AnimKey, Atlas.Rect>(
                animKey =>
            {
                var origin = new Vec2I(
                    OffsetFrame(animKey.frame),
                    20 - (OriginAnim(animKey.anim) + OffsetDir(animKey.dir)));

                return(new Atlas.Rect(
                           origin * 2,
                           new Vec2I(2, 2),
                           new Vec2I(1, 0)));
            });

            atlases = new Cache <string, Atlas>(
                path =>
            {
                var tex = assets.textures.Get(path);
                return(new Atlas(tex, 32, 64));
            });

            paletteMats = new Cache <int, Material>(
                index =>
            {
                var mat = new Material(assets.spriteShader);
                mat.EnableKeyword("_ISPALETTED");
                mat.SetFloat("_PaletteOffset", 1 - (index + .5f) / 64f);
                mat.SetTexture("_PaletteTex", assets.textures.Get("character/palette"));
                return(mat);
            });
            #endregion
        }
Beispiel #7
0
 // TODO: maybe this goes somewhere else
 public SpriteRenderer CreateJobOverlay(Transform parent, Vec2I pos, SpriteDef sprite)
 => CreateSpriteObject(
     parent,
     pos + new Vec2(.5f, .5f),
     "JobOverlay",
     sprite,
     new Color(.6f, .6f, 1, .5f),
     RenderLayer.Highlight);
Beispiel #8
0
        public void Initialize(UnityEngine.Vector2Int coordinate)
        {
            if (!_isInitialized)
            {
                Coordinate = coordinate;
            }

            _isInitialized = true;
        }
Beispiel #9
0
        public void Init(Vec2 worldPos, Vec2 scPos, IEnumerable <Minion> minions)
        {
            this.minions = minions.ToList();
            BB.Assert(this.minions.Count > 0);

            this.targetPos = worldPos.Floor();
            this.menuPos   = scPos;
            this.selection = ctrl.SelectAll(worldPos);
        }
Beispiel #10
0
 public BldgWallDef(
     string defName, string name, ItemInfo[] materials,
     AtlasDef atlas, Vec2I spriteOrigin)
     : base("BB:Wall", defName, name)
 {
     this.materials    = materials;
     this.atlas        = atlas;
     this.spriteOrigin = spriteOrigin;
 }
Beispiel #11
0
 private bool TryTile(Vec2I pos, out Tile tile)
 {
     tile = null;
     if (ValidTile(pos))
     {
         tile = GetTile(pos);
     }
     return(tile != null);
 }
Beispiel #12
0
        /// <summary>
        /// Creates view as voxeled circle.
        /// </summary>
        /// <param name="atlas"></param>
        private void CreateCircle(Texture2D atlas, int spriteOffset)
        {
            var atlasWidth  = pixelsPerUnit * (int)((radius + 0.5) * 2);
            var atlasHeight = pixelsPerUnit * (int)((radius + 0.5) * 2);
            //every sprite is divided by specific 2D voxels(pixelsPerUnit, pixelsPerUnit)
            //       __ __ __
            //      |__|__|__|
            //      |__|__|__|
            //      |__|__|__|
            var segmentsInRange = new bool[atlasHeight / pixelsPerUnit * atlasWidth / pixelsPerUnit];

            var segmentXIndex  = 0;
            var segmentYIndex  = 0;
            var segment1DIndex = 0;

            for (var i = 0; i < atlasWidth; i++)
            {
                for (var j = 0; j < atlasHeight; j++)
                {
                    segmentXIndex  = i / pixelsPerUnit;
                    segmentYIndex  = j / pixelsPerUnit;
                    segment1DIndex = segmentXIndex * atlasHeight / pixelsPerUnit + segmentYIndex;
                    //skip previously visited segment
                    if (segmentsInRange[segment1DIndex])
                    {
                        continue;
                    }

                    //checking if current pixel coordinates are in raw range
                    if (PixelCoor.Distance(new PixelCoor(i, j), new PixelCoor(atlasWidth / 2, atlasHeight / 2)) / pixelsPerUnit <= radius)
                    {
                        //marking segment as "visited"
                        segmentsInRange[segment1DIndex] = true;
                        //painting all pixels in visited segment
                        //       __ __ __
                        //      |__|__|__|
                        //      ||||__|__|
                        //      ||||__|__|
                        for (var x = segmentXIndex * pixelsPerUnit; x < segmentXIndex * pixelsPerUnit + pixelsPerUnit; x++)
                        {
                            for (var y = segmentYIndex * pixelsPerUnit; y < segmentYIndex * pixelsPerUnit + pixelsPerUnit; y++)
                            {
                                atlas.SetPixel(x + spriteOffset / 2, y + spriteOffset / 2, viewColor);
                            }
                        }
                    }
                    //set clear pixel if coordinates are out of range
                    else
                    {
                        atlas.SetPixel(i + spriteOffset / 2, j + spriteOffset / 2, Color.clear);
                    }
                }
            }

            atlas.Apply();
        }
Beispiel #13
0
 void ProcessMouseMoveEvent(short x, short y, byte button)
 {
     UnityEngine.Vector2Int pos   = new UnityEngine.Vector2Int(x, y);
     UnityEngine.Vector2Int delta = pos - m_prevMousePos;
     InputSystem.QueueStateEvent(RemoteMouse, new MouseState {
         position = pos,
         delta    = delta,
         buttons  = button
     });
     m_prevMousePos = pos;
 }
Beispiel #14
0
            public void UpdateTile(Vec2I v)
            {
                Vec3I gridPos   = (v * 2).Vec3();
                var   vtilePrev = tilemap.GetTile(gridPos) as T;
                var   vtile     = vtilePrev == vtileA ? vtileB : vtileA;

                for (int i = 0; i < 4; ++i)
                {
                    tilemap.SetTile(gridPos + new Vec3I(i % 2, i >> 1, 0), vtile);
                }
            }
Beispiel #15
0
        public override void OnClick(Vec2 realPos)
        {
            Vec2I pos = realPos.Floor();

            if (ctrl.game.CanPlaceBuilding(selection.Bounds(curDir).AsRect(pos)))
            {
                var tile = ctrl.game.Tile(pos);
                builds.CreateBuild(selection, tile, curDir);
                //ctrl.game.AddBuilding(selection.CreateBuilding(tile, curDir));
            }
        }
Beispiel #16
0
        protected override TileSprite GetSprite(Map.BBTile tile, Vec2I pos, Vec2I subTile)
        {
            if (grassSprite == null)
            {
                var   atlas     = map.game.assets.atlases.Get(Terrain.K_grassDef.atlas);
                Vec2I spritePos = Terrain.K_grassDef.spriteFrames[0] + Tiling.SpriteOffset(Tiling.TileType.Base);
                grassSprite = atlas.GetSprite(spritePos, Vec2I.one);
            }

            return(grassSprite);
        }
Beispiel #17
0
        public                     Sprite[] GetAnimationSprites(Map map, Vec2I pos, Vec2I subTile)
        {
            BB.Assert(animated);

            var sprites = new Sprite[def.spriteFrames.Length];

            for (int i = 0; i < def.spriteFrames.Length; ++i)
            {
                sprites[i] = GetSprite(map, pos, subTile, i);
            }
            return(sprites);
        }
 static public int ctor_s(IntPtr l)
 {
     try {
         UnityEngine.Vector2Int o;
         o = new UnityEngine.Vector2Int();
         pushValue(l, true);
         pushValue(l, o);
         return(2);
     }
     catch (Exception e) {
         return(error(l, e));
     }
 }
Beispiel #19
0
    public int[] CoordToTriangleIndices(BlockCoord coord)
    {
        //NOTE: Coord is non-nullable
        if (coord.x >= 0 && coord.x <= GridWidth &&
            coord.y >= 0 && coord.y <= GridHeight)
        {
            coord = InvertY(coord);
            var firstTriangleIndex = (int)(coord.y * GridWidth + coord.x) * 2;
            return(new int[] { firstTriangleIndex, firstTriangleIndex + 1 });
        }

        return(null);
    }
Beispiel #20
0
        public static bool[,] GenAdjData(Vec2I pos, Func <Vec2I, bool> IsSame)
        {
            bool[,] adj = new bool[3, 3];
            for (int ax = 0; ax < 3; ++ax)
            {
                for (int ay = 0; ay < 3; ++ay)
                {
                    adj[ax, ay] = IsSame(new Vec2I(pos.x + ax - 1, pos.y + ay - 1));
                }
            }

            return(adj);
        }
Beispiel #21
0
        protected override Status OnBeginTask()
        {
            // TODO: make loading bar
            agent.SetTool(tool);
            agent.SetAnim(anim);
            Vec2I workTarget = faceFn(agent.pos);

            if (agent.pos != workTarget)
            {
                agent.SetFacing(workTarget - agent.pos);
            }

            return(Status.Continue);
        }
Beispiel #22
0
        private void InitTiles(Vec2I size)
        {
            BB.AssertNull(tiles);

            this.size = size;
            tiles     = new BBTile[size.x, size.y];
            for (int x = 0; x < size.x; ++x)
            {
                for (int y = 0; y < size.y; ++y)
                {
                    tiles[x, y] = new BBTile(new Vec2I(x, y));
                }
            }
        }
        public GridPlaneOrientation(
            UnityEngine.Vector2Int size   = default,
            UnityEngine.Vector3Int offset = default,
            float normalOffset            = 0f,
            int subdivisions = 1
            )
        {
            this.enabled = true;

            this.size         = size;
            this.offset       = offset;
            this.normalOffset = normalOffset;
            this.subdivisions = subdivisions;
        }
Beispiel #24
0
        public override void OnUpdate(Vec2 realMouse)
        {
            Vec2I mouse  = realMouse.Floor();
            var   bounds = selection.Bounds(curDir);
            bool  valid  =
                ctrl.game.ValidTile(mouse) &&
                ctrl.game.CanPlaceBuilding(bounds.AsRect(mouse));
            var area = bounds.AsRect(mouse);

            outlineAllow.SetRect(area);
            outlineAllow.enabled = valid;

            outlineDisallow.SetRect(area);
            outlineDisallow.enabled = !valid;
        }
Beispiel #25
0
        private RectTransform CreateCanvas(Transform parent, Vec2I refSize)
        {
            var node = Gui.CreateCanvas(parent, refSize);
            var obj  = node.gameObject;

            obj.AddComponent <GraphicRaycaster>();
            var events = obj.AddComponent <EventSystem>();

            events.sendNavigationEvents = false;
            var module = obj.AddComponent <StandaloneInputModule>();

            module.forceModuleActive = true;

            return(node);
        }
    protected override void Execute(List <GameEntityG> entities)
    {
        var board = GameStateScope.Get <BoardG>().value;

        for (int x = 0; x < board.x; x++)
        {
            var position   = new Vector2Int(x, board.y);
            var nextRowPos = BoardLogic.GetNextEmptyRow(GameScope, position);
            while (nextRowPos != board.y)
            {
                GameScope.CreateRandomPiece(x, nextRowPos);
                nextRowPos = BoardLogic.GetNextEmptyRow(GameScope, position);
            }
        }
    }
Beispiel #27
0
 public BldgWorkbenchDef(
     string defName, string name,
     BuildingBounds bounds, Vec2I workSpot,
     SpriteDef spriteDown, SpriteDef spriteRight,
     ItemInfo[] materials,
     RecipeDef[] recipes)
     : base("BB:Workbench", defName, name)
 {
     BB.Assert(bounds.IsAdjacent(workSpot));
     this.bounds      = bounds;
     this.workSpot    = workSpot;
     this.spriteDown  = spriteDown;
     this.spriteRight = spriteRight;
     this.materials   = materials;
     this.recipes     = recipes;
 }
Beispiel #28
0
        public override bool GetTileAnimationData(Vec3I gridPos, TM.ITilemap tilemap, ref TM.TileAnimationData tileAnimationData)
        {
            Vec2I   pos     = GridToTile(gridPos);
            Terrain terrain = GetTile(pos).terrain;

            if (!terrain.animated)
            {
                return(false);
            }

            tileAnimationData.animatedSprites    = terrain.GetAnimationSprites(map, pos, GridToSubTile(gridPos));
            tileAnimationData.animationSpeed     = 2;
            tileAnimationData.animationStartTime = 0;

            return(true);
        }
Beispiel #29
0
        public static RectTransform CreateCanvas(Transform parent, Vec2I refSize)
        {
            var node = CreateObject(parent, "<canvas>");
            var obj  = node.gameObject;

            var canvas = obj.AddComponent <Canvas>();

            canvas.renderMode = RenderMode.ScreenSpaceOverlay;

            var scaler = obj.AddComponent <CanvasScaler>();

            scaler.uiScaleMode         = CanvasScaler.ScaleMode.ScaleWithScreenSize;
            scaler.referenceResolution = refSize;
            scaler.matchWidthOrHeight  = .5f;

            return(node);
        }
Beispiel #30
0
        public Agent(Game game, AgentDef def, Vec2I pos, string nodeName)
        {
#if DEBUG
            D_uniqueID = D_nextID;
            ++D_nextID;
#endif
            this.game = game;
            this.def  = def;
            transform = new GameObject(nodeName).transform;
            transform.SetParent(game.agentContainer, false);
            realPos = pos;

            /*    var line = game.assets.CreateLine(
             *      transform, "DgbBounds", RenderLayer.Highlight,
             *      Color.blue, 1 / 32f, true, false);
             *  line.SetCircle(def.bounds, 32);*/
        }