Skip to main content
2 of 2
syntax
DanoThom
  • 530
  • 2
  • 9

Using a 2D tile array for your world/level generation and representation will definitely simplify things. For example you could internally represent your world in a grid of tiles and take it from there :

using System;
using System.Collections.Generic;
using System.Text;

namespace Tiles {
    public class Vector2 {
        public int x; public int y;
        public Vector2(int x, int y) {
            this.x = x; this.y = y;
        }
    }

    public enum TileType {
        None,
        Grass,
        Dirt,
        Stone,
        Water,
        Air,
    }

    public class Tile {
        public Vector2 position;
        public TileType tileType;

        public Tile(int x, int y, TileType tileType) {
            this.position = new Vector2(x, y);
            this.tileType = tileType;
        }
    }

    public class World {
        public Tile[,] tiles;
        public int sizeX; public int sizeY;

        public World(int sizeX, int sizeY) {
            this.sizeX = sizeX;
            this.sizeY = sizeY;
            int tileCount;

            // build the world
            tiles = new Tile[sizeX, sizeY];

            Random rnd = new Random();
            tileCount = Enum.GetValues(typeof(TileType)).Length;
            // construct grid of tiles
            for (int x = 0; x < sizeX; x++) {
                for (int y = 0; y < sizeX; y++) {
                    tiles[x, y] = new Tile(x, y, (TileType)rnd.Next(tileCount));
                }
            }
        }
    }

    class Program {
        static void Main(string[] args) {
            World world = new World(20, 20);
            for (int y = 0; y < world.sizeY; y++) {
                for (int x = 0; x < world.sizeX; x++) {
                    Console.Write((int)world.tiles[x, y].tileType + " ");
                }
                Console.WriteLine();
            }
        }
    }
}

Once thats set up you can then interrogate a tile in your move path to see if its walkable based on the terrain type, or if it's blocked by an child obstacle etc.

DanoThom
  • 530
  • 2
  • 9