Beispiel #1
0
    public GameObject SpawnFromTile(ETile tile_, ChunkInfo info_, int x_, int y_)
    {
        EItem toSpawn = EItem.None;

        if (tile_ == ETile.Mountain)
        {
            toSpawn = EItem.Stone;
        }
        else if (tile_ == ETile.Tree)
        {
            toSpawn = EItem.Wood;
        }

        if (toSpawn == EItem.None)
        {
            return(null);
        }

        ItemInstance ii = SpawnItem(toSpawn);

        if (!ii)
        {
            return(null);
        }

        Vector2 worldPos = WorldMap.Chunk2World(info_, x_, y_);

        ii.gameObject.transform.position = new Vector3(worldPos.x, worldPos.y, -0.06f);

        return(ii.gameObject);
    }
Beispiel #2
0
    public bool GenerateSquare(ETile tile, int width, int height)
    {
        if (width < 0)
        {
            return(false);
        }
        if (height < 0)
        {
            return(false);
        }

        Debug.Assert(width <= Width);
        Debug.Assert(height <= Height);

        width  = Math.Min(Width, width);
        height = Math.Min(Height, height);

        int startX = Width / 2 - width / 2;
        int startY = Height / 2 - height / 2;

        int endX = startX + width - 1;
        int endY = startY + height - 1;

        for (int j = startY; j <= endY; ++j)
        {
            for (int i = startX; i < endX; ++i)
            {
                WriteSlotValue(i, j, tile);
            }
        }

        return(true);
    }
Beispiel #3
0
 public Tile(Texture2D _texture, Vector2 _position, ETile _type)
 {
     hasPowerUp = false;
     texture    = _texture;
     position   = _position;
     type       = _type;
 }
Beispiel #4
0
        private void HandleIndex(string name, string param, string value, ref ETile type)
        {
            if (EnumUtil <ETile> .TryGetEnumValue(name, ref type))
            {
                if (!this.table.Table.ContainsKey(type))
                {
                    this.table.Table.Add(type, new TileParams(type));
                }

                switch (param)
                {
                case ("Cost"): { this.table.Table[type].Cost = int.Parse(value); } break;

                case ("Liquid"): { this.table.Table[type].Liquid = bool.Parse(value); } break;

                case ("Sprites"): { this.HandleSprites(type, value); } break;

                case ("StamCost"): { this.table.Table[type].StaminaCost = int.Parse(value); } break;

                case ("ThreatMod"): { this.table.Table[type].ThreatMod = double.Parse(value); } break;

                case ("VulnMod"): { this.table.Table[type].VulnMod = double.Parse(value); } break;
                }
            }
        }
Beispiel #5
0
 public TileDef(ETile id, string name, TileResourceDef tileResourceDef, TileProperties tileProperties)
 {
     Id         = id;
     Name       = name;
     Resource   = tileResourceDef;
     Properties = tileProperties;
 }
Beispiel #6
0
    string ETileToString(ETile t)
    {
        switch (t)
        {
        case ETile.Wall:
            return("W");

        case ETile.Barrier:
            return("B");

        case ETile.Space:
            return(" ");

        case ETile.Door:
            return("D");

        case ETile.Path:
            return(".");

        case ETile.NullETile:
            return("N");

        default:
            return("?");
        }
    }
Beispiel #7
0
 private void AddPatches(ETile tt, int howManyPatch, int howManyPerPatch)
 {
     for (int patchCount = 0; patchCount < howManyPatch; patchCount++)
     {
         AddOnePatch(tt, howManyPerPatch);
     }
 }
Beispiel #8
0
 public VTile(MTile h)
 {
     this._center = h.Center;
     this._col    = h.GetCol();
     this._height = h.GetHeight();
     this._row    = h.GetRow();
     this._type   = h.Type;
 }
Beispiel #9
0
 /// <summary>
 /// Add a rectangle of a certain terrain type in the world
 /// </summary>
 /// <param name="tt">Terrain type</param>
 /// <param name="x1">Left coordinate of rectangle</param>
 /// <param name="x2">Right coordinate of rectangle</param>
 /// <param name="y1">Up coordinate of rectangle</param>
 /// <param name="y2">Down coordinate of rectangle</param>
 private void AddRectangle(ETile tt, int x1, int x2, int y1, int y2)
 {
     for (var x = x1; x <= x2; x++)
     {
         for (var y = y1; y <= y2; y++)
         {
             WriteSlotValue(x, y, new TileInfo(tt));
         }
     }
 }
Beispiel #10
0
    static public TileProperties GetTileProperties(ETile tile)
    {
        TileDef tileDef;

        if (!m_tilesDef.TryGetValue(tile, out tileDef))
        {
            return(null);
        }
        else
        {
            return(tileDef.Properties);
        }
    }
Beispiel #11
0
    static public TileResourceDef GetTileResourceDef(ETile tile)
    {
        TileDef tileDef;

        if (!m_tilesDef.TryGetValue(tile, out tileDef))
        {
            return(null);
        }
        else
        {
            return(tileDef.Resource);
        }
    }
Beispiel #12
0
    public TileInfo(ETile tile_) : this()
    {
        if (tile_ == ETile.Mountain || tile_ == ETile.Tree)
        {
            HP = 100.0f;
        }
        else
        {
            HP = 0.0f;
        }

        Tile = tile_;
    }
Beispiel #13
0
        private void HandleSprites(ETile type, string value)
        {
            var csv = value.Split(',');

            foreach (var v in csv)
            {
                int result = 0;
                if (int.TryParse(v, out result))
                {
                    this.table.Table[type].Sprites.Add(result);
                }
            }
        }
Beispiel #14
0
 private void FillWorldWith(ETile tt)
 {
     //Stopwatch sw = Stopwatch.StartNew();
     for (int i = 0; i < Width; ++i)
     {
         for (int j = 0; j < Height; ++j)
         {
             WriteSlotValue(i, j, tt);
         }
     }
     //sw.Stop();
     //Debug.Log(string.Format("ChunkInfo::FillWorldWith took {0}ms", sw.ElapsedMilliseconds));
 }
Beispiel #15
0
    public Graph2 <Vector3Int> MakeGraphFromPositionsListForTileType(List <PositionVector> _positions, ETile tileType)
    {
        List <int> nodes = new List <int>();
        Dictionary <int, List <int> > edges = new Dictionary <int, List <int> >();
        int ids = 0;
        Graph2 <Vector3Int> _graph = new Graph2 <Vector3Int>(nodes, edges);

        // This only needs to be made once
        nidsByPos = new int[size.x, size.y];

        // Make nodes
        foreach (PositionVector p in _positions)
        {
            nodes.Add(ids);             // Adds node to graph
            _graph.SetData(ids, p.vec); // Adds vector data to node
            nidsByPos[p.x, p.y] = ids;
            ids += 1;
        }

        // Make edges based on tile type
        foreach (PositionVector p in _positions)
        {
            var        neighbors = GetNeighborPositions(p); // TODO: support neighbor filtering by type
            int        pid       = nidsByPos[p.x, p.y];
            List <int> nids      = new List <int>();
            foreach (var n in neighbors)
            {
                int nid = 0;
                try
                {
                    nid = nidsByPos[n.x, n.y];
                }
                catch (System.Exception ex)
                {
                    // Detects invalid positions
                    Console.WriteLine("INVALID POS: " + n);
                    throw ex;
                }

                ETile tileAtNode = MGet(n.x, n.y, tilemap);
                if (tileAtNode == tileType)
                {
                    nids.Add(nid);
                }
            }
            edges.Add(pid, nids);
        }

        return(_graph);
    }
Beispiel #16
0
    public bool MSet(int x, int y, ETile t, List <List <ETile> > _tilemap)
    {
        bool isValidETile = MGet(x, y, _tilemap) != ETile.OutOfBoundsETile;

        if (isValidETile)
        {
            _tilemap[y][x] = t;
            return(true);
        }
        else
        {
            return(false);
        }
    }
Beispiel #17
0
    List <Vector2Int> GetDoorNeighborSpaces(Vector2Int doorPos)
    {
        var neighbors = GetNeighborPositions(doorPos);
        List <Vector2Int> spaceNeighbors = new List <Vector2Int>();

        foreach (var n in neighbors)
        {
            ETile nt = MGet(n.x, n.y, tilemap);
            if (nt == ETile.Space)
            {
                spaceNeighbors.Add(n);
            }
        }
        return(spaceNeighbors);
    }
Beispiel #18
0
    private static bool BuildFromJSON(JSONNode rootNode)
    {
        m_tilesDef.Clear();

        JSONNode tilesInfo = rootNode["tilesInfo"];

        if (tilesInfo == null)
        {
            return(false);
        }

        foreach (var tileNode in tilesInfo.Childs)
        {
            JSONNode resNode = tileNode["resource"];
            if (resNode == null)
            {
                continue;
            }

            string filename = resNode["file"];
            int    x        = resNode["x"].AsInt;
            int    y        = resNode["y"].AsInt;
            int    w        = resNode["w"].AsInt;
            int    h        = resNode["h"].AsInt;
            int    count    = resNode["count"].AsInt;

            TileResourceDef trd = new TileResourceDef(filename, new Rect(x, y, w, h), count);

            ETile  id   = (ETile)tileNode["id"].AsInt;
            string name = tileNode["name"];

            TileProperties tileProperties = new TileProperties(tileNode["properties"]);

            TileDef td = new TileDef(id, name, trd, tileProperties);

            if (m_tilesDef.ContainsKey(id))
            {
                Debug.Log(string.Format("Ignoring duplicate id {0}", id));
                continue;
            }

            m_tilesDef.Add(id, td);
        }

        return(true);
    }
Beispiel #19
0
    /// <summary>
    /// Add patch of terrain to a world. The parameter are respected in a best-effort way.
    /// </summary>
    /// <param name="tt"></param>
    /// <param name="percent">How much of the world is covered by this terrain</param>
    /// <param name="tightness">How tight that terrain is packed. 0 = as many patches possible, 1 = single patch</param>
    private void AddPatches(ETile tt, float percent, float tightness)
    {
        //Debug.Log(string.Format("Creating {0} patch for {1}% at {2} tightness", tt, percent, tightness));

        Stopwatch sw      = Stopwatch.StartNew();
        int       howMany = (int)(percent * (float)Width * (float)Height);

        if (howMany == 0)
        {
            sw.Stop();
            Debug.Log(string.Format("ChunkInfo::AddPatches took {0}ms", sw.ElapsedMilliseconds));
            return;
        }


        // how much terrain per a given patch
        // some patch may have more than others
        int howManyPerPatch;

        // how many patches
        int howManyPatch;

        if (tightness == 0.0f)
        {
            howManyPerPatch = 1;
            howManyPatch    = howMany;
        }
        else
        {
            // 1.0 -> howMany
            // 0.5 -> howMany / 2
            // 0.2 -> howMany / 5
            howManyPerPatch = (int)(howMany * tightness);
            howManyPatch    = (int)Mathf.Round((float)howMany / howManyPerPatch);
            if (howManyPatch == 0)
            {
                howManyPatch = 1;
            }
        }

        AddPatches(tt, howManyPatch, howManyPerPatch);
        sw.Stop();
        //Debug.Log(string.Format("ChunkInfo::AddPatches took {0}ms", sw.ElapsedMilliseconds));
    }
Beispiel #20
0
        public HealStationTile(ETile type, Vector2 position, ContentManager content, bool isUsed) : base(type, position, content)
        {
            //Save content instance for later changes
            this.content = content;
            this.isUsed  = isUsed;

            //Load correct texture
            if (isUsed)
            {
                texture = content.Load <Texture2D>("Tile/HealStationUsed");
            }
            else
            {
                texture = content.Load <Texture2D>("Tile/HealStation");
            }

            //Load sound
            useSound       = content.Load <SoundEffect>("Sound/HealthStationFinal");
            useDeniedSound = content.Load <SoundEffect>("Sound/HealthStationUsedFinal");
        }
Beispiel #21
0
    // PRIVATE METHODS

    private Vector2[] GetUVs(ETile tile)
    {
        var settings = System.Array.Find(m_TileSettings, obj => obj.Tile == tile);

        if (settings == null)
        {
            throw new System.ArgumentOutOfRangeException("No settings for tile " + tile);
        }

        var position = settings.Position;

        position.y = m_TileSize - position.y - 1;

        return(new Vector2[]
        {
            new Vector2((position.x) / (float)m_TileSize + m_TileUVOffset, (position.y) / (float)m_TileSize + m_TileUVOffset),
            new Vector2((position.x) / (float)m_TileSize + m_TileUVOffset, (position.y + 1) / (float)m_TileSize - m_TileUVOffset),
            new Vector2((position.x + 1) / (float)m_TileSize - m_TileUVOffset, (position.y + 1) / (float)m_TileSize - m_TileUVOffset),
            new Vector2((position.x + 1) / (float)m_TileSize - m_TileUVOffset, (position.y) / (float)m_TileSize + m_TileUVOffset)
        });
    }
Beispiel #22
0
    /// <summary>
    /// Add a circle patch of a certain terrain type in the world
    /// </summary>
    /// <param name="tt">Terrain type</param>
    /// <param name="x">Center of circle X coordinate</param>
    /// <param name="y">Center of circle Y coordinate</param>
    /// <param name="radius">Radius of circle</param>
    private void AddCircle(ETile tt, int x, int y, int radius)
    {
        //Stopwatch sw = Stopwatch.StartNew();

        for (var i = -radius; i <= radius; ++i)
        {
            for (var j = -radius; j <= radius; j++)
            {
                // check if valid region
                Vector2 candidate = new Vector2(x + i, x + j);
                if (IsValid(candidate))
                {
                    // now check if within the circle
                    if (i * i + j * j <= radius * radius)
                    {
                        WriteSlotValue(x + i, y + j, tt);
                    }
                }
            }
        }
        //sw.Stop();
        //Debug.Log(string.Format("ChunkInfo::AddCircle took {0}ms", sw.ElapsedMilliseconds));
    }
Beispiel #23
0
    List <Vector2Int> GetNeighborPositionsOfType(Vector2Int pos, ETile et)
    {
        var neighbors = GetNeighborPositions(pos);

        // No neighbors
        if (neighbors == null)
        {
            return(null);
        }

        // Filter neighbors
        List <Vector2Int> filteredNeighbors = new List <Vector2Int>();

        foreach (var n in neighbors)
        {
            ETile tile = MGet(n.x, n.y, tilemap);
            if (tile == et)
            {
                filteredNeighbors.Add(n);
            }
        }

        return(filteredNeighbors);
    }
Beispiel #24
0
        private static void translateEntries(List<MapEntry> entries, DataManager manager, ContentManager content)
        {
            Dictionary<string, EPoint> points = new Dictionary<string, EPoint>();
            Dictionary<string, Texture2D> textures = new Dictionary<string, Texture2D>();
            Dictionary<string, ETile> tiles = new Dictionary<string, ETile>();
            Dictionary<string, Grid> grids = new Dictionary<string, Grid>();
            Dictionary<string, ECell> cells = new Dictionary<string, ECell>();
            Dictionary<string, double> numbers = new Dictionary<string, double>();
            Dictionary<string, bool> booleans = new Dictionary<string, bool>();
            Dictionary<string, string> strings = new Dictionary<string, string>();

            foreach (MapEntry entry in entries)
            {
                if (entry.TypeName == "Texture")
                {
                    Texture2D texture = content.Load<Texture2D>(entry[0]);
                    textures.Add(entry.EntryName, texture);
                }
                else if (entry.TypeName == "Point")
                {
                    EPoint point = new EPoint(
                        int.Parse(entry[0]),
                        int.Parse(entry[1]),
                        int.Parse(entry[2]));
                    points.Add(entry.EntryName, point);
                }
                else if (entry.TypeName == "Tile")
                {
                    ETile tile = new ETile();
                    tile.Texture = textures[entry[0]];
                    tile.Solid = bool.Parse(entry[1]);

                    tiles.Add(entry.EntryName, tile);
                }
                else if (entry.TypeName == "Grid")
                {
                    EPoint gridSize = points[entry[0]];
                    EPoint gridOrigin = points[entry[1]];
                    EPoint gridTileOffset = points[entry[2]];
                    EPoint gridTileSize = points[entry[3]];
                    EPoint playerStart = points[entry[4]];

                    Grid grid = new Grid(
                        gridSize.X,
                        gridSize.Y,
                        gridSize.Z,
                        null,
                        gridTileOffset.X,
                        gridTileOffset.Y,
                        gridTileSize.X,
                        gridTileSize.Y,
                        gridTileOffset.Z,
                        gridOrigin.X,
                        gridOrigin.Y);

                    grid.ShadowEast = content.Load<Texture2D>("Block Shadow\\Shadow East");
                    grid.ShadowEast = content.Load<Texture2D>("Block Shadow\\Shadow North East");
                    grid.ShadowEast = content.Load<Texture2D>("Block Shadow\\Shadow North West");
                    grid.ShadowEast = content.Load<Texture2D>("Block Shadow\\Shadow North");
                    grid.ShadowEast = content.Load<Texture2D>("Block Shadow\\Shadow West");
                    grid.ShadowEast = content.Load<Texture2D>("Block Shadow\\Shadow South East");
                    grid.ShadowEast = content.Load<Texture2D>("Block Shadow\\Shadow South West");
                    grid.ShadowEast = content.Load<Texture2D>("Block Shadow\\Shadow South");
                    grid.ShadowEast = content.Load<Texture2D>("Block Shadow\\Shadow East");

                    grids.Add(entry.EntryName, grid);
                }
                else if (entry.TypeName == "Cell")
                {
                    ECell cell = new ECell();

                    cell.Point = new EPoint(int.Parse(entry[0]), int.Parse(entry[1]), int.Parse(entry[2]));
                    cell.Grid = grids[entry[3]];
                    cell.Tile = tiles[entry[4]];

                    cell.Grid.setTile(
                        cell.Point.X,
                        cell.Point.Y,
                        cell.Point.Z,
                        cell.Tile.Texture,
                        cell.Tile.Solid);

                    if (!string.IsNullOrEmpty(entry.EntryName))
                        cells.Add(entry.EntryName, cell);
                }
                else if (entry.TypeName == "Number")
                {
                    numbers.Add(entry.EntryName, double.Parse(entry[0]));
                }
                else if (entry.TypeName == "Boolean")
                {
                    booleans.Add(entry.EntryName, bool.Parse(entry[0]));
                }
                else if (entry.TypeName == "String")
                {
                    strings.Add(entry.EntryName, entry[0]);
                }
            }

            foreach (string key in grids.Keys)
            {
                manager.AddItem(key, grids[key]);
            }

            foreach (string key in textures.Keys)
            {
                manager.AddItem(key, textures[key]);
            }
        }
Beispiel #25
0
        public Sprite[] GetTileSprites(ETile tile)
        {
            var path = StringUtil.PathBuilder(PATH, tile.ToString(), TILE_EXTENSION);

            return(GetSprites(path));
        }
Beispiel #26
0
        public Tile(ETile type, Vector2 position, ContentManager content)
        {
            //Create the tile and load its texture and give the correct collision setting
            switch (type)
            {
            case ETile.Stone:
                texture   = content.Load <Texture2D>("Tile/Stone");
                collision = ETileCollision.Solid;
                break;

            case ETile.Background:
                texture   = content.Load <Texture2D>("Tile/Back");
                collision = ETileCollision.Passable;
                break;

            case ETile.Door:
                texture   = content.Load <Texture2D>("Tile/Back");
                collision = ETileCollision.Passable;
                break;

            case ETile.Spike:
                texture   = content.Load <Texture2D>("Tile/Spikes");
                collision = ETileCollision.Passable;
                break;

            case ETile.HealStation:
                texture   = content.Load <Texture2D>("Tile/HealStation");
                collision = ETileCollision.Passable;
                break;

            case ETile.HealStationUsed:
                texture   = content.Load <Texture2D>("Tile/HealStationUsed");
                collision = ETileCollision.Passable;
                break;

            case ETile.DoorOpen:
                texture   = content.Load <Texture2D>("Tile/DoorOpen");
                collision = ETileCollision.Passable;
                break;

            case ETile.DoorLocked:
                texture   = content.Load <Texture2D>("Tile/DoorLocked");
                collision = ETileCollision.Passable;
                break;

            case ETile.AcidTop:
                texture   = content.Load <Texture2D>("Tile/AcidTop");
                collision = ETileCollision.Passable;
                break;

            case ETile.AcidFull:
                texture   = content.Load <Texture2D>("Tile/AcidFull");
                collision = ETileCollision.Passable;
                break;

            case ETile.Trigger:
                texture   = content.Load <Texture2D>("Tile/Trigger");
                collision = ETileCollision.Passable;
                break;

            case ETile.Triggered:
                texture   = content.Load <Texture2D>("Tile/Stone");
                collision = ETileCollision.Solid;
                break;
            }

            //Save the information and set the rectangle
            this.type       = type;
            this.position   = position;
            bounds          = texture.Bounds;
            bounds.Location = new Point((int)position.X, (int)position.Y);
        }
Beispiel #27
0
 public TypeTile(ETile tile, int subTile)
 {
     Tile    = tile;
     SubTile = subTile;
 }
Beispiel #28
0
 public TriggeredTile(ETile type, Vector2 position, ContentManager content) : base(type, position, content)
 {
     //Save content instance
     this.content = content;
 }
Beispiel #29
0
 public void SetType(ETile type)
 {
     this._type = type;
 }
Beispiel #30
0
 public PatchTemplate(ETile _tile, float _percent, float _tightness)
 {
     tile      = _tile;
     percent   = _percent;
     tightness = _tightness;
 }
Beispiel #31
0
    /// <summary>
    /// Add one patch
    /// </summary>
    /// <param name="tt">The type of tile to add</param>
    /// <param name="howMany">How many tiles to add</param>
    private void AddOnePatch(ETile tt, int howMany)
    {
        HashSet <Vector2> current   = new HashSet <Vector2>(new Vector2Comparer());
        HashSet <Vector2> potential = new HashSet <Vector2>();

        // Look if there are exiting neighbors first, and start from them if they have the right tiles
        // Look at all directions
        // TODO: This algorithm can be generalized
        if (NorthNeighbor != null)
        {
            for (int x = 0; x < Width; x++)
            {
                if (NorthNeighbor.m_data[x, 0].Tile == tt)
                {
                    potential.Add(new Vector2(x, Height - 1));
                }
            }
        }
        if (EastNeighbor != null)
        {
            for (int y = 0; y < Height; y++)
            {
                if (EastNeighbor.m_data[0, y].Tile == tt)
                {
                    potential.Add(new Vector2(Width - 1, y));
                }
            }
        }
        if (SouthNeighbor != null)
        {
            for (int x = 0; x < Width; x++)
            {
                if (SouthNeighbor.m_data[x, Height - 1].Tile == tt)
                {
                    potential.Add(new Vector2(x, 0));
                }
            }
        }
        if (WestNeighbor != null)
        {
            for (int y = 0; y < Height; y++)
            {
                if (WestNeighbor.m_data[Width - 1, y].Tile == tt)
                {
                    potential.Add(new Vector2(0, y));
                }
            }
        }

        // If we had no neighbor with potential, just pick one point at random
        if (potential.Count == 0)
        {
            int i = Random.Range(0, Width);
            int j = Random.Range(0, Height);
            potential.Add(new Vector2(i, j));
        }

        Vector2[] dirs = new Vector2[4];
        dirs[0] = new Vector2(1, 0);
        dirs[1] = new Vector2(-1, 0);
        dirs[2] = new Vector2(0, -1);
        dirs[3] = new Vector2(0, 1);
        Vector2[] newPotential = new Vector2[4];

        // while still some to do and still some place to do it
        while (howMany > 0 && potential.Count > 0)
        {
            howMany--;

            // Pick one guy at random in potential
            int     pick   = Random.Range(0, potential.Count);
            Vector2 picked = Enumerable.ElementAt(potential, pick);

            // remove from potential, add to current
            current.Add(picked);
            potential.Remove(picked);

            WriteSlotValue(picked, tt);

            // now check all 4 neighbors to see if we can add to new potential
            int found = 0;

            for (int dir = 0; dir < 4; ++dir)
            {
                Vector2 testNeighbor = dirs[dir] + picked;
                if (IsValid(testNeighbor) && !current.Contains(testNeighbor))
                {
                    newPotential[found++] = testNeighbor;
                }
            }

            for (int dir = 0; dir < found; ++dir)
            {
                potential.Add(newPotential[dir]);
            }
        }
    }