Ejemplo n.º 1
0
        public EditorActorPreview(WorldRenderer worldRenderer, string id, ActorReference reference, PlayerReference owner)
        {
            ID                 = id;
            this.reference     = reference;
            Owner              = owner;
            this.worldRenderer = worldRenderer;

            if (!reference.Contains <FactionInit>())
            {
                reference.Add(new FactionInit(owner.Faction));
            }

            if (!reference.Contains <OwnerInit>())
            {
                reference.Add(new OwnerInit(owner.Name));
            }

            var world = worldRenderer.World;

            if (!world.Map.Rules.Actors.TryGetValue(reference.Type.ToLowerInvariant(), out Info))
            {
                throw new InvalidDataException($"Actor {id} of unknown type {reference.Type.ToLowerInvariant()}");
            }

            CenterPosition = PreviewPosition(world, reference);

            var location = reference.Get <LocationInit>().Value;
            var ios      = Info.TraitInfoOrDefault <IOccupySpaceInfo>();

            var subCellInit = reference.GetOrDefault <SubCellInit>();
            var subCell     = subCellInit != null ? subCellInit.Value : SubCell.Any;

            var radarColorInfo = Info.TraitInfoOrDefault <RadarColorFromTerrainInfo>();

            RadarColor = radarColorInfo == null ? owner.Color : radarColorInfo.GetColorFromTerrain(world);

            Footprint = ios?.OccupiedCells(Info, location, subCell) ?? new Dictionary <CPos, SubCell>()
            {
                { location, SubCell.FullCell }
            };

            tooltip = Info.TraitInfos <EditorOnlyTooltipInfo>().FirstOrDefault(info => info.EnabledByDefault) as TooltipInfoBase
                      ?? Info.TraitInfos <TooltipInfo>().FirstOrDefault(info => info.EnabledByDefault);

            DescriptiveName = tooltip != null ? tooltip.Name : Info.Name;

            GeneratePreviews();

            // Bounds are fixed from the initial render.
            // If this is a problem, then we may need to fetch the area from somewhere else
            var r = previews.SelectMany(p => p.ScreenBounds(worldRenderer, CenterPosition));

            Bounds = r.Union();

            SelectionBox = new SelectionBoxAnnotationRenderable(new WPos(CenterPosition.X, CenterPosition.Y, 8192),
                                                                new Rectangle(Bounds.X, Bounds.Y, Bounds.Width, Bounds.Height), Color.White);
        }
Ejemplo n.º 2
0
        public EditorActorPreview(WorldRenderer worldRenderer, string id, ActorReference actor, PlayerReference owner)
        {
            ID                 = id;
            this.actor         = actor;
            Owner              = owner;
            this.worldRenderer = worldRenderer;

            if (!actor.InitDict.Contains <FactionInit>())
            {
                actor.InitDict.Add(new FactionInit(owner.Faction));
            }

            if (!actor.InitDict.Contains <OwnerInit>())
            {
                actor.InitDict.Add(new OwnerInit(owner.Name));
            }

            var world = worldRenderer.World;

            if (!world.Map.Rules.Actors.TryGetValue(actor.Type.ToLowerInvariant(), out Info))
            {
                throw new InvalidDataException("Actor {0} of unknown type {1}".F(id, actor.Type.ToLowerInvariant()));
            }

            CenterPosition = PreviewPosition(world, actor.InitDict);

            var location = actor.InitDict.Get <LocationInit>().Value(worldRenderer.World);
            var ios      = Info.TraitInfoOrDefault <IOccupySpaceInfo>();

            var subCellInit = actor.InitDict.GetOrDefault <SubCellInit>();
            var subCell     = subCellInit != null?subCellInit.Value(worldRenderer.World) : SubCell.Any;

            if (ios != null)
            {
                Footprint = ios.OccupiedCells(Info, location, subCell);
            }
            else
            {
                var footprint = new Dictionary <CPos, SubCell>()
                {
                    { location, SubCell.FullCell }
                };
                Footprint = new ReadOnlyDictionary <CPos, SubCell>(footprint);
            }

            tooltip = Info.TraitInfos <EditorOnlyTooltipInfo>().FirstOrDefault(info => info.EnabledByDefault) as TooltipInfoBase
                      ?? Info.TraitInfos <TooltipInfo>().FirstOrDefault(info => info.EnabledByDefault);

            DescriptiveName = tooltip != null ? tooltip.Name : Info.Name;

            GeneratePreviews();

            // Bounds are fixed from the initial render.
            // If this is a problem, then we may need to fetch the area from somewhere else
            var r = previews.SelectMany(p => p.ScreenBounds(worldRenderer, CenterPosition));

            if (r.Any())
            {
                Bounds = r.First();
                foreach (var rr in r.Skip(1))
                {
                    Bounds = Rectangle.Union(Bounds, rr);
                }
            }

            SelectionBox = new SelectionBoxRenderable(new WPos(CenterPosition.X, CenterPosition.Y, 8192),
                                                      new Rectangle(Bounds.X, Bounds.Y, Bounds.Width, Bounds.Height), Color.White);
        }