Exemplo n.º 1
0
    static MapObject BuildNpcObject(
        int tileWidth,
        int tileHeight,
        Dictionary <ushort, string> functionsByEventId,
        GetTileFunc getTileFunc,
        Dictionary <int, int> npcPathIndices,
        int npcIndex,
        MapNpc npc,
        ref int nextId)
    {
        var objProps = new List <TiledProperty>
        {
            new(Prop.Visual, npc.SpriteOrGroup.ToString()),
            new(Prop.Flags, npc.Flags.ToString()),
            new(Prop.Triggers, npc.Triggers.ToString()),
            new(Prop.Movement, npc.Movement.ToString()),
            new(Prop.Type, npc.Type.ToString()),
        };

        if (!npc.Id.IsNone)
        {
            objProps.Add(new TiledProperty(Prop.Id, npc.Id.ToString()));
        }
        if (npc.Node != null)
        {
            objProps.Add(new TiledProperty(Prop.Script, functionsByEventId[npc.Node.Id]));
        }
        if (!npc.Sound.IsNone)
        {
            objProps.Add(new TiledProperty(Prop.Sound, npc.Sound.ToString()));
        }
        if (npcPathIndices.TryGetValue(npcIndex, out var pathObjectId))
        {
            objProps.Add(TiledProperty.Object(Prop.Path, pathObjectId));
        }

        var(tileId, tileW, tileH) = getTileFunc(npc.SpriteOrGroup);
        return(new MapObject
        {
            Id = nextId++,
            Gid = tileId ?? 0,
            Name = $"NPC{npcIndex} {npc.Id}",
            Type = ObjectGroupMapping.TypeName.Npc,
            X = npc.Waypoints[0].X * tileWidth,
            Y = npc.Waypoints[0].Y * tileHeight,
            Width = tileW,
            Height = tileH,
            Properties = objProps
        });
    }
Exemplo n.º 2
0
    public static IEnumerable <ObjectGroup> BuildNpcs(
        BaseMapData map,
        int tileWidth,
        int tileHeight,
        GetTileFunc getTileFunc,
        Dictionary <ushort, string> functionsByEventId,
        ref int nextObjectGroupId,
        ref int nextObjectId)
    {
        int nextId     = nextObjectId;
        int npcGroupId = nextObjectGroupId++;

        var waypointGroups = new List <ObjectGroup>();
        var npcPathIndices = new Dictionary <int, int>();

        for (var index = 0; index < map.Npcs.Count; index++)
        {
            var npc = map.Npcs[index];
            if (!npc.HasWaypoints(map.Flags))
            {
                continue;
            }

            if (npc.SpriteOrGroup == AssetId.None) // Unused 2D slots
            {
                continue;
            }

            if (npc.SpriteOrGroup == new AssetId(AssetType.ObjectGroup, 1) && npc.Id.IsNone) // unused 3D slots
            {
                continue;
            }

            int firstWaypointObjectId = nextId;
            npcPathIndices[index] = firstWaypointObjectId;
            waypointGroups.Add(new ObjectGroup
            {
                Id      = nextObjectGroupId++,
                Name    = $"NPC{index} Path",
                Objects = NpcPathBuilder.Build(index, npc.Waypoints, tileWidth, tileHeight, ref nextId),
                Hidden  = true,
            });
        }

        var group = new ObjectGroup
        {
            Id      = npcGroupId,
            Name    = "NPCs",
            Objects = map.Npcs.Select((x, i) =>
                                      BuildNpcObject(
                                          tileWidth,
                                          tileHeight,
                                          functionsByEventId,
                                          getTileFunc,
                                          npcPathIndices,
                                          i,
                                          x,
                                          ref nextId))
                      .ToList(),
        };

        nextObjectId = nextId;
        return(new[] { group }.Concat(waypointGroups));
    }