Example #1
0
    public void SetValue(Vector2int pos, int val, bool update = true)
    {
        collisionTiles.SetValue(pos, (byte)val);

        if (update) {
            SetTile(pos, val);
        }
    }
 public void DeleteAt(Vector2int pos)
 {
     if (resources.ContainsKey(pos)) {
         var obj = resources[pos].GetComponent<Object2DComponent>().Object;
         Destroy(resources[pos]);
         resources.Remove(pos);
         Object2DLoader.Delete(_stage, obj);
     }
 }
Example #3
0
 //public Vector3int[] Neighbours { get { return VoxelMap.GetPathableNeighbors(this); } }
 static Vector2int()
 {
     zero = new Vector2int(0, 0);
     up = new Vector2int(0, 1);
     down = new Vector2int(0, -1);
     left = new Vector2int(-1, 0);
     right = new Vector2int(1, 0);
     one = new Vector2int(1, 1);
 }
Example #4
0
 //public Vector3int[] Neighbours { get { return VoxelMap.GetPathableNeighbors(this); } }
 static Vector2int()
 {
     zero  = new Vector2int(0, 0);
     up    = new Vector2int(0, 1);
     down  = new Vector2int(0, -1);
     left  = new Vector2int(-1, 0);
     right = new Vector2int(1, 0);
     one   = new Vector2int(1, 1);
 }
 public Object2D GetAt(Vector2int pos)
 {
     if (resources.ContainsKey(pos)) {
         var obj = resources[pos].GetComponent<Object2DComponent>().Object;
         return obj;
     } else {
         return null;
     }
 }
Example #6
0
        public Vector2int[] SurroundingPlanar4Diagonal()
        {
            var surr = new Vector2int[4];

            surr[0] = this + left + up;
            surr[1] = this + left + down;
            surr[2] = this + right + up;
            surr[3] = this + right + down;
            return(surr);
        }
Example #7
0
 public static byte[] Load(Vector2int mapLocation, SpriteLayer layer, string levelname)
 {
     if (File.Exists(TileDirectoryPath(layer, levelname) + mapLocation)) {
         string s;
         using (var reader = new StreamReader(TileDirectoryPath(layer, levelname) + mapLocation)) {
             s = reader.ReadLine();
         }
         return s.ConvertHexToByteArray();
     }
     else {
         return null;
     }
 }
Example #8
0
 public IEnumerable<Vector2int> GetPositions(Vector2int areaPosition)
 {
     var positions = new List<Vector2int>();
     var m = GetMapFromReducedPoint(areaPosition);
     for (int i = 0; i < m.Length; i++) {
         if (m[i] > 0) {
             var x = i / MapSize;
             var y = i - (x * MapSize);
             positions.Add(MapSize * areaPosition + new Vector2int(x, y));
         }
     }
     return positions;
 }
    public void AddTile(Vector2int mapPos)
    {
        var reducedPos = TileMap2D.GetReducedPoint(mapPos);
        if (!resources.ContainsKey(reducedPos)) {
            resources[reducedPos] = new Dictionary<Vector2int, GameObject>();
        }

        if (!resources[reducedPos].ContainsKey(mapPos)) {
            var obj = GameObject.Instantiate<GameObject>(tilePrefab);
            obj.transform.parent = tileParent;
            obj.transform.position = GetWorldPositionFromMapPosition(mapPos);
            resources[reducedPos][mapPos] = obj;
        }
    }
Example #10
0
        public Vector2int[] SurroundingPlanar8()
        {
            var surr = new Vector2int[8];

            surr[0] = this + left;
            surr[1] = this + right;
            surr[2] = this + up;
            surr[3] = this + down;
            surr[4] = this + left + up;
            surr[5] = this + left + down;
            surr[6] = this + right + up;
            surr[7] = this + right + down;
            return(surr);
        }
Example #11
0
    public static Dictionary<Vector2int, byte[]> LoadAll(SpriteLayer l, string levelname)
    {
        //create dictionary for layer l
        var dict = new Dictionary<Vector2int, byte[]>();

        //load each tile in layer l and place in dictionary
        foreach (var file in Directory.GetFiles(TileDirectoryPath(l, levelname))) {
            var name = Path.GetFileName(file);
            var stringSplit = name.Replace("(", "").Replace(")", "").Split(',');
            var vec = new Vector2int(int.Parse(stringSplit[0]), int.Parse(stringSplit[1]));
            dict[vec] = Load(vec, l, levelname);
        }
        return dict;
    }
Example #12
0
        public override bool Equals(object obj)
        {
            if (obj == null)
            {
                return(false);
            }

            Vector2int p = obj as Vector2int;

            if ((System.Object)p == null)
            {
                return(false);
            }

            if (x != p.x)
            {
                return(false);
            }
            if (y != p.y)
            {
                return(false);
            }
            return(true);
        }
 public static Vector2 GetWorldPositionFromMapPosition(Vector2int mapPosition)
 {
     return new Vector2(mapPosition.x, mapPosition.y) * GridSize;
 }
Example #14
0
 public static Vector2int Cross(Vector2int v1, Vector2int v2)
 {
     return(new Vector2int(
                v1.y * v2.x - v1.x * v2.y,
                v1.x * v2.y - v1.y * v2.x));
 }
Example #15
0
 public static void Save(Vector2int mapLocation, byte[] bytes, SpriteLayer layer, string levelname)
 {
     using (var writer = new StreamWriter(TileDirectoryPath(layer, levelname) + mapLocation.ToString())) {
         writer.WriteLine(bytes.ConvertToHexString());
     }
 }
Example #16
0
 public void SetValue(Vector2int position, byte value)
 {
     var map = GetMap(position);
     map[GetLocalPosition(position)] = value;
     if (AutoSave) {
         MapLoader2D.Save(GetReducedPoint(position), map, layer, levelname);
     }
 }
Example #17
0
 byte[] GetMapFromReducedPoint(Vector2int reducedPoint)
 {
     if (!maps.ContainsKey(reducedPoint)) {
         byte[] m = null;
         if (AutoSave) {
             m = MapLoader2D.Load(reducedPoint, layer, levelname);
         }
         if (m == null) {
             maps[reducedPoint] = new byte[MapSize * MapSize];
             //var newMap = maps[reducedPoint];
             //for (int i = 0; i < newMap.Length; i++) { newMap[i] = 0; }
         } else {
             maps[reducedPoint] = m;
         }
     }
     return maps[reducedPoint];
 }
Example #18
0
 byte[] GetMap(Vector2int point)
 {
     return GetMapFromReducedPoint(GetReducedPoint(point));
 }
Example #19
0
 public static int BlockDistance(Vector2int a, Vector2int b)
 {
     return(Math.Abs(a.x - b.x) + Math.Abs(a.y - b.y));
 }
Example #20
0
 public static int SquareDistance(Vector2int a, Vector2int b)
 {
     return (a.x - b.x).Squared() + (a.y - b.y).Squared();
 }
Example #21
0
 public Vector2int[] SurroundingPlanar8()
 {
     var surr = new Vector2int[8];
     surr[0] = this + left;
     surr[1] = this + right;
     surr[2] = this + up;
     surr[3] = this + down;
     surr[4] = this + left + up;
     surr[5] = this + left + down;
     surr[6] = this + right + up;
     surr[7] = this + right + down;
     return surr;
 }
Example #22
0
 public static int BlockDistance(Vector2int a, Vector2int b)
 {
     return Math.Abs(a.x - b.x) + Math.Abs(a.y - b.y);
 }
Example #23
0
 void SetTile(Vector2int pos, int val)
 {
     if (resources.ContainsKey(pos)) {
         if(val == 0) {
             GameObject.Destroy(resources[pos]);
             resources.Remove(pos);
         }
     } else {
         if(val != 0) {
             var r = GameObject.Instantiate(GetPrefab());
             resources[pos] = r;
             r.transform.position = pos.ToVector2() * TileSize + Offset;
             r.transform.parent = transform;
         }
     }
 }
Example #24
0
 public TileObject(Vector2int pos, byte t)
 {
     position = pos;
     type = t;
 }
Example #25
0
 public byte GetValue(Vector2int position)
 {
     if (!maps.ContainsKey(GetReducedPoint(position))) {
         return 0;
     } else {
         return maps[GetReducedPoint(position)][GetLocalPosition(position)];
     }
 }
Example #26
0
 public static Vector2int GetReducedPoint(Vector2int point)
 {
     return new Vector2int(Mathf.FloorToInt((float)point.x / MapSize), Mathf.FloorToInt((float)point.y / MapSize));
 }
Example #27
0
 public static Vector2int Cross(Vector2int v1, Vector2int v2)
 {
     return new Vector2int(
         v1.y * v2.x - v1.x * v2.y,
         v1.x * v2.y - v1.y * v2.x);
 }
 public Vector2 GetWorldPosition(Vector2int pos)
 {
     return pos.ToVector2() * gridSize;
 }
Example #29
0
 public Vector2int[] SurroundingPlanar4Diagonal()
 {
     var surr = new Vector2int[4];
     surr[0] = this + left + up;
     surr[1] = this + left + down;
     surr[2] = this + right + up;
     surr[3] = this + right + down;
     return surr;
 }
Example #30
0
 public static int SquareDistance(Vector2int a, Vector2int b)
 {
     return((a.x - b.x).Squared() + (a.y - b.y).Squared());
 }
 public void RemoveTile(Vector2int mapPos)
 {
     var reducedPos = TileMap2D.GetReducedPoint(mapPos);
     if (resources.ContainsKey(reducedPos)) {
         if (resources[reducedPos].ContainsKey(mapPos)) {
             GameObject.Destroy(resources[reducedPos][mapPos]);
             resources.Remove(mapPos);
         }
     }
 }
Example #32
0
 int GetLocalPosition(Vector2int point)
 {
     var p = point - MapSize * GetReducedPoint(point);
     return p.x * MapSize + p.y;
 }