Пример #1
0
 private static void SpawnEntity(EntityLayer layer, int entityIndex, int x, int y)
 {
     if (entityIndex < 0 || entityIndex >= classes.Length || classes[entityIndex] == null)
         return;
     Vector2 position = new Vector2((x + 0.5f) * tileSize, (y + 0.5f) * tileSize);
     Entity entity = (Entity)Activator.CreateInstance(classes[entityIndex], position);
     layer.AddComponent(entity);
 }
Пример #2
0
        public static void PopulateEntityLayer(EntityLayer layer, int[] data)
        {
            if (!initialized)
                throw new Exception("EntityMap mus be initialized first!");

            for (int y = 0; y < mapHeight; y++)
            {
                for (int x = 0; x < mapWidth; x++)
                {
                    int i = x + y * mapWidth;
                    if (i < data.Length)
                        SpawnEntity(layer, data[i], x, y);
                }
            }
        }
Пример #3
0
        public static Entity[] ConstructTileMap(EntityLayer entityLayer)
        {
            int width = (int)Math.Ceiling(entityLayer.Bounds.X/TileSize);
            int height = (int)Math.Ceiling(entityLayer.Bounds.Y/TileSize);
            Entity[] result = new Entity[width*height];

            foreach (Component component in entityLayer.Components)
            {
                Entity entity = component as Entity;
                if (entity != null)
                {
                    if (entity.Properties.GetInt("isTile", 0) > 0)
                    {
                        int x = (int)MathHelper.Clamp((int)Math.Floor(entity.Position.X / TileSize), 0, width-1);
                        int y = (int)MathHelper.Clamp((int)Math.Floor(entity.Position.Y / TileSize), 0, height - 1);
                        result[x + y * width] = entity;
                    }
                }
            }

            return result;
        }
Пример #4
0
        private void DrawEntities(RenderInfo info, EntityLayer entities)
        {
            int tiles = layers[currentLayer].Type == LayerType.Entities ? 0: 1;
            Vector2 topLeft = new Vector2(info.Camera.Left, info.Camera.Top);
            for (int i = 0; i < entities.Components.Count; i++)
            {
                Entity entity = entities.Components[i] as Entity;
                if (entity != null && entity.Shape != null && entity.Properties.GetInt("isTile",0)==tiles)
                {
                    info.Canvas.StrokeColor = Color.White;
                    info.Canvas.LineWidth = 4;
                    topLeft = DrawEntity(info, topLeft, entity);

                    info.Canvas.StrokeColor = Color.Black;
                    if (entity == selectedEntity)
                        info.Canvas.StrokeColor = Color.Yellow;
                    else if (entity == hoveringEntity)
                        info.Canvas.StrokeColor = Color.Cyan;
                    info.Canvas.LineWidth = 2;
                    topLeft = DrawEntity(info, topLeft, entity);

                    if (windows.Ghost)
                    {
                        if (entity == selectedEntity)
                        {
                            string name = MapLoader.ShortTypeName(selectedEntity.GetType());
                            Vector2 size = font.MeasureString(name);
                            info.Batch.DrawString(font, name, entity.Position - topLeft - size * 0.5f, Color.Yellow);
                        }
                        else if (entity == hoveringEntity)
                        {
                            string name = MapLoader.ShortTypeName(hoveringEntity.GetType());
                            Vector2 size = font.MeasureString(name);
                            info.Batch.DrawString(font, name, entity.Position - topLeft - size * 0.5f, Color.Cyan);
                        }
                    }
                }
            }

            if (windows.Ghost)
            {
                info.Canvas.StrokeColor = Color.Red;
                info.Canvas.LineWidth = 2;
                if (drawingEntity != null && hoveringEntity == null)
                {
                    DrawEntity(info, topLeft, drawingEntity);
                    string name = MapLoader.ShortTypeName(drawingEntity.GetType());
                    Vector2 size = font.MeasureString(name);
                    info.Batch.DrawString(font, name, drawingEntity.Position - topLeft - size * 0.5f, Color.Red);
                }
                else if (layers[currentLayer].Type == LayerType.Tiles)
                {
                    Vector2 pos = SnapPosition(mousePosition) - topLeft;
                    info.Canvas.Begin();
                    info.Canvas.MoveTo(pos - Vector2.One * MapLoader.TileSize * 0.5f);
                    info.Canvas.LineTo(pos + Vector2.One * MapLoader.TileSize * 0.5f);
                    info.Canvas.MoveTo(pos + new Vector2(MapLoader.TileSize * 0.5f, -MapLoader.TileSize * 0.5f));
                    info.Canvas.LineTo(pos + new Vector2(-MapLoader.TileSize * 0.5f, MapLoader.TileSize * 0.5f));
                    info.Canvas.Stroke();
                }
            }
        }
Пример #5
0
        private static void ParseTiles(string[] lines, ref int index, EntityLayer entities)
        {
            string[] parameters = lines[index].Split(' ');
            index++;

            Dictionary<int, Type> types = new Dictionary<int, Type>();
            for (int i = 1; i < parameters.Length; i++)
            {
                for (int j =0; j<TileList.Length; j++)
                    if (ShortTypeName(TileList[j]) == parameters[i])
                    {
                        types[i-1] = TileList[j];
                        break;
                    }
            }

            int width = (int)Math.Ceiling(entities.Bounds.X / TileSize);
            int height = (int)Math.Ceiling(entities.Bounds.Y / TileSize);

            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    while (lines[index].Length == 0) index++;
                    string tile = lines[index].Substring(x * 4, 3);
                    int t = -1;
                    if (int.TryParse(tile, out t) && types.ContainsKey(t))
                        AddTile(x, y, types[t], entities);
                }
                index++;
            }
        }
Пример #6
0
        private static void ParseEntity(string line, EntityLayer entities)
        {
            string[] parameters = line.Split(' ');
            if (parameters.Length<5) {
                Trace.WriteLine("Entity requires at least 4 parameters (type x y orientation).");
                return;
            }
            Vector2 position = new Vector2();
            if (!float.TryParse(parameters[2], out position.X))
            {
                Trace.WriteLine("Error parsing entity x.");
                return;
            }
            if (!float.TryParse(parameters[3], out position.Y))
            {
                Trace.WriteLine("Error parsing entity y.");
                return;
            }
            float orientation = 0;
            if (!float.TryParse(parameters[4], out orientation))
            {
                Trace.WriteLine("Error parsing entity orientation.");
                return;
            }
            int type = -1;
            for (int i = 0; i < EntityList.Length; i++)
            {
                if (ShortTypeName(EntityList[i]) == parameters[1])
                {
                    type =i;
                    break;
                }
            }

            if (type < 0)
            {
                Trace.WriteLine("Could not find entity type "+parameters[1]+".");
                return;
            }

            Entity entity = (Entity)Activator.CreateInstance(EntityList[type], position);
            entity.Orientation = MathHelper.ToRadians(orientation);
            entities.AddComponent(entity);
            ParseProperties(entity, parameters, 5);
        }
Пример #7
0
        private static string GetEntityData(EntityLayer entityLayer)
        {
            string data = "";
            if (entityLayer.Properties.GetBoolean("tiles", true))
            {
                int width = (int)Math.Ceiling(entityLayer.Bounds.X / TileSize);
                int height = (int)Math.Ceiling(entityLayer.Bounds.Y / TileSize);
                data += "tiles";
                for (int i = 0; i < TileList.Length; i++)
                    data += " " + ShortTypeName(TileList[i]);
                data += "\n";

                Entity[] tileMap = ConstructTileMap(entityLayer);

                for (int y = 0; y < height; y++)
                {
                    for (int x = 0; x < width; x++)
                    {
                        int tile = -1;
                        int index = x + y * width;
                        if (tileMap[index] != null)
                        {
                            for (int i = 0; i < TileList.Length; i++)
                            {
                                if (TileList[i] == tileMap[index].GetType())
                                {
                                    tile = i;
                                    break;
                                }

                            }
                        }
                        string t = tile.ToString();
                        while (t.Length < 3) t = " " + t;
                        data += t + " ";
                    }
                    data += '\n';
                }
            }

            foreach (Component component in entityLayer.Components)
            {
                Entity entity = component as Entity;
                if (entity != null && entity.Properties.GetInt("isTile", 0) == 0)
                        data += EntityString(entity)+ "\n";
            }
            return data;
        }
Пример #8
0
        private static void AddTile(int x, int y, Type tile, EntityLayer entities)
        {
            Vector2 position = new Vector2((x + 0.5f) * TileSize, (y + 0.5f) * TileSize);

            Entity entity = (Entity)Activator.CreateInstance(tile, position);
            entity.Properties.Ints["isTile"] = 1;
            entities.AddComponent(entity);
        }