Map LoadMap(MapEditor editor, GameControl gc, string map)
        {
            if (maps[map].map != null)
            {
                maps[map].map.GameControl = gc;
                return maps[map].map;
            }

            string mapdir = maps[map].fulldir;

            Map m;
            BinaryFormatter bf = new BinaryFormatter();

            // Load the floor
            using (StreamReader floortiles = new StreamReader(Path.Combine(mapdir, "floor.tiles")))
            {
                using (FileStream floormap = new FileStream(Path.Combine(mapdir, "floor.map"), FileMode.Open))
                {
                    int[,] floor = (int[,])bf.Deserialize(floormap);
                    int w = floor.GetUpperBound(0) + 1;
                    int h = floor.GetUpperBound(1) + 1;
                    m = new Map(gc, w, h);

                    Dictionary<int, string> tilenumToName = new Dictionary<int, string>();
                    while (!floortiles.EndOfStream)
                    {
                        string line = floortiles.ReadLine();
                        string[] tokens = line.Split(':');
                        tilenumToName[int.Parse(tokens[0])] = tokens[1];
                    }
                    for (int xx = 0; xx < w; xx++)
                    {
                        for (int yy = 0; yy < h; yy++)
                        {
                            m.SetFloor((MapFloor)Activator.CreateInstance(floortypes[tilenumToName[floor[xx, yy]]]), xx, yy);
                        }
                    }
                }
            }

            Regex objectnamelabel = new Regex(@"^\[(?<objectname>[a-z][a-z|0-9]*)\]", RegexOptions.Compiled);
            // Load the objects that are on this map
            using (StreamReader objectlist = new StreamReader(Path.Combine(mapdir, "objects.list")))
            {
                string line = objectlist.ReadLine();
                Match match = objectnamelabel.Match(line);

                while (!objectlist.EndOfStream)
                {
                    string instancename = match.Groups["objectname"].ToString();
                    ICharacter mo = null;
                    // For each instance block
                    while (!objectlist.EndOfStream)
                    {
                        line = objectlist.ReadLine();

                        if (line.StartsWith("type:"))
                        {
                            string type = line.Substring(5);
                            mo = (ICharacter)Activator.CreateInstance(objecttypes[type]);
                        }
                        else if (line.StartsWith("location:"))
                        {
                            string[] tokens = line.Substring(9).Split(',');
                            m.SetCharacter(mo, int.Parse(tokens[0]), int.Parse(tokens[1]));
                        }
                        else if (line.StartsWith("use:"))
                        {
                        }

                        match = objectnamelabel.Match(line);
                        if (match.Success) break;
                    }
                }

            }

            return m;
        }
Exemple #2
0
        Map LoadMap(GameControl g, string mapdir)
        {
            Map m;
            BinaryFormatter bf = new BinaryFormatter();

            // Load the floor
            using (StreamReader floortiles = new StreamReader(Path.Combine(mapdir, "floor.tiles")))
            {
                using (FileStream floormap = new FileStream(Path.Combine(mapdir, "floor.map"), FileMode.Open))
                {
                    int[,] floor = (int[,])bf.Deserialize(floormap);
                    int w = floor.GetUpperBound(0) + 1;
                    int h = floor.GetUpperBound(1) + 1;
                    m = new Map(g, w, h);

                    Dictionary<int, string> tilenumToName = new Dictionary<int, string>();
                    while (!floortiles.EndOfStream)
                    {
                        string line = floortiles.ReadLine();
                        string[] tokens = line.Split(':');
                        tilenumToName[int.Parse(tokens[0])] = tokens[1];
                    }
                    for (int xx = 0; xx < w; xx++)
                    {
                        for (int yy = 0; yy < h; yy++)
                        {
                            m.SetFloor((MapFloor)Activator.CreateInstance(floortypes[tilenumToName[floor[xx, yy]]]), xx, yy);
                        }
                    }
                }
            }

            Regex objectnamelabel = new Regex(@"^\[(?<objectname>[a-z][a-z|0-9]*)\]", RegexOptions.Compiled);
            // Load the objects that are on this map
            using (StreamReader objectlist = new StreamReader(Path.Combine(mapdir, "objects.list")))
            {
                string line = objectlist.ReadLine();
                Match match = objectnamelabel.Match(line);

                while (!objectlist.EndOfStream)
                {
                    string instancename = match.Groups["objectname"].ToString();
                    ICharacter mo = null;
                    // For each instance block
                    while (!objectlist.EndOfStream)
                    {
                        line = objectlist.ReadLine();

                        if (line.StartsWith("type:"))
                        {
                            string type = line.Substring(5);
                            mo = (ICharacter)Activator.CreateInstance(objecttypes[type]);
                        }
                        else if (line.StartsWith("location:"))
                        {
                            string[] tokens = line.Substring(9).Split(',');
                            m.SetCharacter(mo, int.Parse(tokens[0]), int.Parse(tokens[1]));
                        }
                        else if (line.StartsWith("use:"))
                        {
                            Script s = new Script("inst_" + instancename, objectlist, true);

                            mo.Use = (st) => { s.Execute(st); };
                        }

                        match = objectnamelabel.Match(line);
                        if (match.Success) break;
                    }
                }

                //var ob = (ICharacter)Activator.CreateInstance(objecttypes["box"]);
                //m.SetCharacter(ob, 10, 5);

                //Thread t = new Thread(x =>
                //{
                //    int xx = 10;
                //    int yy = 5;
                //    bool right = true;
                //    while (true)
                //    {
                //        Thread.Sleep(1000);

                //        if (right)
                //        {
                //            if (m.MoveCharacter(xx, yy, Direction.Right))
                //            {
                //                xx++;
                //            }
                //            else
                //            {
                //                right = false;
                //            }
                //        }
                //        else
                //        {
                //            if (m.MoveCharacter(xx, yy, Direction.Left))
                //            {
                //                xx--;
                //            }
                //            else
                //            {
                //                right = true;
                //            }
                //        }
                //    }
                //});
                //t.IsBackground = true;
                //t.Start();

            }

            return m;
        }