Exemplo n.º 1
0
    public void size(Utilities.Vec2Int size)
    {
        didBackgroundUpdate = true;

        _background.resize(size);
        _size = _background.size();
    }
Exemplo n.º 2
0
        public void clear()
        {
            clearTexture();

            _object = null;
            _pos = null;
        }
        public override void moveTo(Vector3 v)
        {
            if (!started())
                return;

            if (placed())
                remove();

            Vector3 offset = Utilities.Math.toVector3(Map.tileSize / 2);
            if (v.x > 0)
                v.x += offset.x;
            else
                v.x -= offset.x;

            if (v.y > 0)
                v.y += offset.y;
            else
                v.y -= offset.y;

            Utilities.Vec2Int tileCounter = new Utilities.Vec2Int(
                (int)(v.x / Map.tileSize.x),
                (int)(v.y / Map.tileSize.y));
            if (tileCounter.x % 2 == 0)
                tileCounter.x++;
            if (tileCounter.y % 2 == 0)
                tileCounter.y++;

            Vector3 snappedPosition = new Vector3(
                tileCounter.x * Map.tileSize.x,
                tileCounter.y * Map.tileSize.y);

            base.moveTo(snappedPosition);
        }
Exemplo n.º 4
0
 public Tile(QuadNode<Tile> node, PlaceableObject placeableObject, Vec2Int v)
 {
     _node = node;
     _node.data(this);
     _object = placeableObject;
     _pos = v;
 }
Exemplo n.º 5
0
        public void deleteTile(Vec2Int globalPosition)
        {
            QuadNode<Tile> node = getQuadNode(globalPosition, true);
            if (node == null)
                return;

            node.data().clear();
            node.data(null);
        }
Exemplo n.º 6
0
    protected void Awake()
    {
        _size = new Utilities.Vec2Int(100, 100);
        _background = new DynamicMonochromeSprite(_size);

        didBackgroundUpdate = false;

        //rectTransform.sizeDelta = Utilities.toVector2(_size);
        /*rectTransform.position = Vector3.zero;
        rectTransform.pivot = new Vector2(0.5f, 0.5f);*/
    }
Exemplo n.º 7
0
    public DynamicSprite(Utilities.Vec2Int size)
    {
        _size = new Utilities.Vec2Int(1, 1);
        if (size.x > 1)
            _size.x = size.x;
        if (size.y > 1)
            _size.y = size.y;

        _sprite = null;
        _data = new List<Color>(size.x * size.y);
        for (int i = 0; i < _data.Capacity; i++)
            _data.Add(default(Color));
    }
Exemplo n.º 8
0
        // Creates a static wall if and only if at least one side of the tile set (2x2)
        public static void createEdgeWall(Tile tile, Vec2Int pos)
        {
            if (tile == null)
                return;

            Vec2Int basePos = pos;
            if (basePos.x % 2 != 0)
            {
                tile = tile.getSide(SIDE_4.LEFT);

                if (tile == null)
                    return;
            }

            if (basePos.y % 2 != 0)
            {
                tile = tile.getSide(SIDE_4.BOTTOM);

                if (tile == null)
                    return;
            }

            Map map = Map.Get();

            // If any of the sides are null, consider the tile to be a boundary.
            if (tile.getSide(SIDE_4.LEFT) == null)
            {
                PlaceableStaticWall.createStaticWall(map.toTilePos(tile.position()));
                return;
            }
            else if (tile.getSide(SIDE_4.BOTTOM) == null)
            {
                PlaceableStaticWall.createStaticWall(map.toTilePos(tile.position()));
                return;
            }

            Tile it = tile.getSide(SIDE_4.TOP);
            if (it != null && it.getSide(SIDE_4.TOP) == null)
            {
                PlaceableStaticWall.createStaticWall(map.toTilePos(tile.position()));
                return;
            }

            it = tile.getSide(SIDE_4.RIGHT);
            if (it != null && it.getSide(SIDE_4.RIGHT) == null)
            {
                PlaceableStaticWall.createStaticWall(map.toTilePos(tile.position()));
                return;
            }
        }
Exemplo n.º 9
0
        public override void moveTo(Vector3 v)
        {
            if (!started())
                return;

            if (placed())
                remove();

            _sr.enabled = true;

            Vector3 offset = Utilities.Math.toVector3(Map.tileSize / 2);
            if (v.x > 0)
                v.x += offset.x;
            else
                v.x -= offset.x;

            if (v.y > 0)
                v.y += offset.y;
            else
                v.y -= offset.y;

            Utilities.Vec2Int tileCounter = new Utilities.Vec2Int(
                (int)(v.x / Map.tileSize.x),
                (int)(v.y / Map.tileSize.y));
            switch (_orientation)
            {
                case Utilities.SIDE_4.RIGHT:
                case Utilities.SIDE_4.LEFT:
                    if (tileCounter.x % 2 != 0)
                        tileCounter.x++;
                    if (tileCounter.y % 2 == 0)
                        tileCounter.y++;

                    break;
                case Utilities.SIDE_4.TOP:
                case Utilities.SIDE_4.BOTTOM:
                    if (tileCounter.x % 2 == 0)
                        tileCounter.x++;
                    if (tileCounter.y % 2 != 0)
                        tileCounter.y++;

                    break;
            }

            Vector3 snappedPosition = new Vector3(
                tileCounter.x * Map.tileSize.x,
                tileCounter.y * Map.tileSize.y);

            base.moveTo(snappedPosition);
        }
Exemplo n.º 10
0
        private IEnumerator loadLevelHelper(string levelPath)
        {
            Debug.Log("Loading...");

            MapEditor.Get().destroy();

            yield return null; // Wait for managers to be destroyed

            Map map = MapEditor.Get().map(); // Creates the new managers

            yield return null; // Wait for all managers Start() methods to be called

            if (!Directory.Exists(levelPath))
                yield break;

            XmlDocument doc = new XmlDocument();
            doc.Load(levelPath + "/gamedata.xml");

            XmlElement root = doc.FirstChild as XmlElement;

            XmlElement tilesElement = null;
            XmlElement staticWallsElement = null;
            XmlElement doorsElement = null;
            XmlElement activatorsElement = null;

            foreach (XmlNode child in root)
            {
                XmlElement iterator = child as XmlElement;
                if (iterator == null)
                    continue;

                switch (iterator.Name)
                {
                    case "tiles":
                        tilesElement = iterator;
                        break;
                    case "staticwalls":
                        staticWallsElement = iterator;
                        break;
                    case "doors":
                        doorsElement = iterator;
                        break;
                    case "activators":
                        activatorsElement = iterator;
                        break;
                }
            }

            // Create Tiles
            foreach (XmlNode _tile in tilesElement.ChildNodes)
            {
                XmlElement tileElement = _tile as XmlElement;
                if (tileElement == null)
                    continue;

                string stringPosition = tileElement.GetAttribute("position");
                Vec2Int tilePos = new Vec2Int(stringPosition);

                Tile tile = map.getTile(tilePos);
                QuadNodeProcessors.createTile(tile.node(), tile.position());
            }

            // Create Static Walls
            StaticWallFactory staticWallFactory = new StaticWallFactory();
            if (staticWallsElement != null)
            {
                foreach (XmlNode _staticWallElem in staticWallsElement.ChildNodes)
                {
                    XmlElement staticWallElement = _staticWallElem as XmlElement;
                    if (staticWallElement == null)
                        continue;

                    staticWallFactory.create(staticWallElement);
                }
            }

            // Create Static Walls to Place Doors Over
            if (doorsElement != null)
            {
                foreach (XmlNode _doorElem in doorsElement.ChildNodes)
                {
                    XmlElement doorElement = _doorElem as XmlElement;
                    if (doorElement == null)
                        continue;

                    staticWallFactory.create(doorElement);
                }
            }

            // Create Doors
            DoorFactory doorFactory = new DoorFactory();
            if (doorsElement != null)
            {
                foreach (XmlNode _doorElem in doorsElement.ChildNodes)
                {
                    XmlElement doorElement = _doorElem as XmlElement;
                    if (doorElement == null)
                        continue;

                    doorFactory.create(doorElement);
                }
            }

            // Create Activators
            ActivatorFactory activatorFactory = new ActivatorFactory(doorFactory);
            if (activatorsElement != null)
            {
                foreach (XmlNode _activatorElem in activatorsElement.ChildNodes)
                {
                    XmlElement activatorElement = _activatorElem as XmlElement;
                    if (activatorElement == null)
                        continue;

                    activatorFactory.create(activatorElement);
                }
            }

            ObjectManipulator.Get().activate();
        }
Exemplo n.º 11
0
        protected void processTiles(Action<int, int, Utilities.Vec2Int> processor)
        {
            Utilities.Vec2Int tilePos = new Utilities.Vec2Int(0, 0);

            for (int i = 0; i < _area.GetLength(0); i++)
            {
                tilePos.x = i - _area.GetLength(0) / 2;

                for (int j = 0; j < _area.GetLength(1); j++)
                {
                    tilePos.y = j - _area.GetLength(1) / 2;

                    processor(i, j, tilePos);
                }
            }
        }
Exemplo n.º 12
0
 public static Vector2 toVector2(Vec2Int p)
 {
     return new Vector2(p.x, p.y);
 }
Exemplo n.º 13
0
        public Tile getTile(Vec2Int globalPosition)
        {
            QuadNode<Tile> node = getQuadNode(globalPosition);
            if (node.data() == null)
                node.data(new Tile(node, null, globalPosition));

            return node.data();
        }
Exemplo n.º 14
0
        public Tile[,] getArea(Tile baseTile, Vec2Int size)
        {
            if (size.x < 0 || size.y < 0)
                return null;

            Tile[,] area = new Tile[size.x, size.y];

            Vec2Int basePos = baseTile.position();
            Vec2Int offset = new Vec2Int(0, 0);
            for (int i = 0; i < size.x; i++)
            {
                offset.x = i;
                for (int j = 0; j < size.y; j++)
                {
                    offset.y = j;
                    area[i, j] = getExistingTile(basePos + offset);
                }
            }

            return area;
        }
Exemplo n.º 15
0
 public static void floorTo(Vec2Int v, int multiple)
 {
     v.x = v.x / multiple * multiple;
     v.y = v.y / multiple * multiple;
 }
Exemplo n.º 16
0
        public Tile getExistingTile(Vec2Int globalPosition)
        {
            QuadNode<Tile> node = getQuadNode(globalPosition, true);

            if (node == null)
                return null;

            return node.data();
        }
Exemplo n.º 17
0
        private QuadNode<Tile> getQuadNode(Vec2Int globalPosition, bool existingOnly = false)
        {
            Vec2Int chunkPos = new Vec2Int(0, 0);
            Vec2Int localPos = new Vec2Int(0, 0);

            if (globalPosition.x >= 0)
            {
                chunkPos.x = globalPosition.x / Chunk.size.x;
                localPos.x = globalPosition.x % Chunk.size.x;
            }
            else
            {
                chunkPos.x = (globalPosition.x + 1) / Chunk.size.x - 1;
                localPos.x = Chunk.size.x - 1 + (globalPosition.x + 1) % Chunk.size.x;
            }

            if (globalPosition.y >= 0)
            {
                chunkPos.y = globalPosition.y / Chunk.size.y;
                localPos.y = globalPosition.y % Chunk.size.y;
            }
            else
            {
                chunkPos.y = (globalPosition.y + 1) / Chunk.size.y - 1;
                localPos.y = Chunk.size.y - 1 + (globalPosition.y + 1) % Chunk.size.y;
            }

            Chunk searcher = getChunk(chunkPos, !existingOnly);

            if (searcher == null)
                return null;

            return searcher.get(localPos.x, localPos.y);
        }
Exemplo n.º 18
0
        // Can be used for creating chunks
        private Chunk getChunk(Vec2Int pos, bool create = false)
        {
            Chunk searcher = null;
            if (!_chunks.TryGetValue(pos, out searcher) && create)
            {
                searcher = new Chunk();
                _chunks.Add(pos, searcher);

                pos = new Vec2Int(pos);

                Chunk connector;
                if (_chunks.TryGetValue(pos + new Vec2Int(-1, 0), out connector))
                    connector.connectTo(searcher, SIDE_4.RIGHT);
                if (_chunks.TryGetValue(pos + new Vec2Int(1, 0), out connector))
                    connector.connectTo(searcher, SIDE_4.LEFT);
                if (_chunks.TryGetValue(pos + new Vec2Int(0, -1), out connector))
                    connector.connectTo(searcher, SIDE_4.TOP);
                if (_chunks.TryGetValue(pos + new Vec2Int(0, 1), out connector))
                    connector.connectTo(searcher, SIDE_4.BOTTOM);
            }

            return searcher;
        }
Exemplo n.º 19
0
        public Vector3 toTilePos(Vec2Int pos)
        {
            Vector3 retVal = Utilities.Math.toVector3(pos);
            retVal.x *= tileSize.x;
            retVal.y *= tileSize.y;

            return retVal;
        }
Exemplo n.º 20
0
 public void setTile(Vec2Int globalPosition, Tile tile)
 {
     getQuadNode(globalPosition).data(tile);
     tile.position(globalPosition);
 }
Exemplo n.º 21
0
 public static Vector3 toVector3(Vec2Int v)
 {
     return new Vector3(v.x, v.y, 0);
 }
Exemplo n.º 22
0
        private IEnumerator _processAllNodes(Action<QuadNode<Tile>, Vec2Int> processor)
        {
            Vec2Int pos = new Vec2Int(0, 0);
            Vec2Int chunkOffset = new Vec2Int(0, 0);

            int processCounter = 0;
            foreach (KeyValuePair<Vec2Int, Chunk> pair in _chunks)
            {
                chunkOffset = new Vec2Int(pair.Key);
                chunkOffset.x *= Chunk.size.x;
                chunkOffset.y *= Chunk.size.y;

                for (int i = 0; i < Chunk.size.x; i++)
                {
                    pos.x = i;
                    for (int j = 0; j < Chunk.size.y; j++)
                    {
                        pos.y = j;

                        processor(pair.Value.get(i, j), pos + chunkOffset);

                        if (processCounter++ >= CoroutineManager.yieldCount)
                        {
                            processCounter = 0;
                            yield return null;
                        }
                    }
                }
            }

            _processing = false;

            yield break;
        }
Exemplo n.º 23
0
        public Tile getTile(Vector3 worldPosition)
        {
            Vector3 snapDistance = Utilities.Math.toVector3(tileSize);

            Vec2Int globalPosition = new Vec2Int(
                Mathf.RoundToInt(worldPosition.x / snapDistance.x),
                Mathf.RoundToInt(worldPosition.y / snapDistance.y));

            return getTile(globalPosition);
        }
Exemplo n.º 24
0
 public void position(Vec2Int v)
 {
     _pos = v;
 }
Exemplo n.º 25
0
 public Vec2Int(Vec2Int v)
 {
     this.x = v.x;
     this.y = v.y;
 }