Exemple #1
0
        public void Move(Entity entity, Point location)
        {
            Point         actualLocation = entity.MapLocation;
            AnimationType animationType  = AnimationType.StaticRight;
            Int32         topY           = actualLocation.Y;

            if (currentMap.Objects[actualLocation.X, actualLocation.Y] != entity)
            {
                throw new Exception("Perda de referência da entidade com o mapa.");
            }
            if (currentMap.Objects[location.X, location.Y] != null)
            {
                Entity obstacle = currentMap.Objects[location.X, location.Y];
                if (obstacle is Character && ((Character)obstacle).Health.IsDead)
                {
                    hiddenEntities.Add(obstacle);
                    currentMap.Objects[location.X, location.Y] = null;
                }
                else
                {
                    throw new Exception("Destino ocupado por outra entidade.");
                }
            }

            List <Point> route        = pathFinder.GetRoute(entity.MapLocation, location);
            Point        lastPosition = entity.MapLocation;

            foreach (Point point in route)
            {
                if (point.Y > topY)
                {
                    topY = point.Y;
                }

                if (point.X > lastPosition.X)
                {
                    animationType = AnimationType.Right;
                }
                else if (point.X < lastPosition.X)
                {
                    animationType = AnimationType.Left;
                }
                else if (point.Y > lastPosition.Y)
                {
                    animationType = AnimationType.Down;
                }
                else if (point.Y < lastPosition.Y)
                {
                    animationType = AnimationType.Up;
                }

                if (animationType != AnimationType.StaticRight)
                {
                    entity.AddAnimatedMovement(animationType, lastPosition, point, entity.DefaultSpeed);
                }
                lastPosition = point;
            }

            entity.LayerDepth = Camera.GetInstance().GetCharacterLayerDepth(new Point(5, topY));

            Watch(entity, location);
        }
Exemple #2
0
        private static Map ReadMap(XmlDocument document)
        {
            Map        map;
            XmlElement mapElement = document.DocumentElement;
            string     terrainMap = mapElement.GetElementsByTagName("terrain")[0].InnerText;
            string     objectMap  = mapElement.GetElementsByTagName("entities")[0].InnerText;
            int        width      = int.Parse(mapElement.Attributes["width"].InnerText);
            int        height     = int.Parse(mapElement.Attributes["height"].InnerText);

            map = new Map(width, height);

            XmlNodeList imports = mapElement.GetElementsByTagName("external");
            Dictionary <String, Terrain> importedTerrains = new Dictionary <String, Terrain>();
            Dictionary <String, Entity>  importedObjects  = new Dictionary <string, Entity>();

            foreach (XmlNode import in imports)
            {
                string importClass = import.Attributes["class"].InnerText;
                string importName  = import.Attributes["rootName"].InnerText;
                string alias       = null;
                if (import.Attributes["alias"] != null)
                {
                    alias = import.Attributes["alias"].InnerText;
                }

                switch (importClass)
                {
                case "Terrains":
                    Terrain terrain = Bundle.Terrains[importName];
                    importedTerrains.Add(importName, terrain);
                    if (!string.IsNullOrEmpty(alias))
                    {
                        importedTerrains.Add(alias, terrain);
                    }
                    break;

                case "Environment":
                    Entity environment = Bundle.Environment[importName];
                    importedObjects.Add(importName, environment);
                    if (!string.IsNullOrEmpty(alias))
                    {
                        importedObjects.Add(alias, environment);
                    }
                    break;
                }
            }

            terrainMap = terrainMap.Replace(" ", "").Replace("\n", "");
            String[] cells = terrainMap.Split(new String[] { "[", "]" }, StringSplitOptions.RemoveEmptyEntries);
            if (cells.Length != width * height)
            {
                throw new Exception("Corrupted File");
            }

            for (int i = 0; i < width * height; i++)
            {
                int     x       = i % width;
                int     y       = i / width;
                Terrain terrain = importedTerrains[cells[i]].Clone();
                terrain.MapLocation = new Point(x, y);
                map.Terrain[x, y]   = terrain;
            }

            objectMap = objectMap.Replace(" ", "").Replace("\n", "");
            cells     = objectMap.Split(new String[] { "[", "]" }, StringSplitOptions.RemoveEmptyEntries);
            if (cells.Length != width * height)
            {
                throw new Exception("Corrupted File");
            }
            for (int i = 0; i < width * height; i++)
            {
                if (string.IsNullOrEmpty(cells[i]) || cells[i] == ".")
                {
                    continue;
                }
                int    x      = i % width;
                int    y      = i / width;
                Entity entity = importedObjects[cells[i]].Clone();
                entity.LayerDepth  = Constants.LD_ENVIRONMENT;
                entity.MapLocation = new Point(x, y);
                entity.AddAnimatedMovement(AnimationType.StaticRight);
                map.SetObject(entity);
            }

            return(map);
        }