EditorActorPreview Add(string id, ActorReference reference, bool initialSetup = false) { var owner = Players.Players[reference.InitDict.Get <OwnerInit>().PlayerName]; var preview = new EditorActorPreview(worldRenderer, id, reference, owner); previews.Add(id, preview); screenMap.Add(preview, preview.Bounds); foreach (var kv in preview.Footprint) { List <EditorActorPreview> list; if (!cellMap.TryGetValue(kv.Key, out list)) { list = new List <EditorActorPreview>(); cellMap.Add(kv.Key, list); } list.Add(preview); } if (!initialSetup) { UpdateNeighbours(preview.Footprint); if (reference.Type == "mpspawn") { SyncMultiplayerCount(); } } return(preview); }
public void Remove(EditorActorPreview preview) { previews.Remove(preview); screenMap.Remove(preview); foreach (var kv in preview.Footprint) { List <EditorActorPreview> list; if (!cellMap.TryGetValue(kv.Key, out list)) { continue; } list.Remove(preview); if (!list.Any()) { cellMap.Remove(kv.Key); } } UpdateNeighbours(preview.Footprint); if (preview.Info.Name == "mpspawn") { SyncMultiplayerCount(); } }
public void Add(EditorActorPreview preview, bool initialSetup = false) { previews.Add(preview); if (!preview.Bounds.IsEmpty) { screenMap.Add(preview, preview.Bounds); } // Fallback to the actor's CenterPosition for the ActorMap if it has no Footprint var footprint = preview.Footprint.Select(kv => kv.Key).ToArray(); if (!footprint.Any()) { footprint = new[] { worldRenderer.World.Map.CellContaining(preview.CenterPosition) } } ; foreach (var cell in footprint) { AddPreviewLocation(preview, cell); } preview.AddedToEditor(); if (!initialSetup) { UpdateNeighbours(preview.Footprint); if (preview.Type == "mpspawn") { SyncMultiplayerCount(); } } }
public void Add(EditorActorPreview preview, bool initialSetup = false) { previews.Add(preview); if (!preview.Bounds.IsEmpty) { screenMap.Add(preview, preview.Bounds); } foreach (var kv in preview.Footprint) { AddPreviewLocation(preview, kv.Key); } // Fallback to the actor's CenterPosition for the ActorMap if it has no Footprint if (!preview.Footprint.Any()) { AddPreviewLocation(preview, worldRenderer.World.Map.CellContaining(preview.CenterPosition)); } if (!initialSetup) { UpdateNeighbours(preview.Footprint); if (preview.Actor.Type == "mpspawn") { SyncMultiplayerCount(); } } }
public EditorActorPreview Add(string id, ActorReference reference, bool initialSetup = false) { var owner = Players.Players[reference.Get <OwnerInit>().InternalName]; var preview = new EditorActorPreview(worldRenderer, id, reference, owner); Add(preview, initialSetup); return(preview); }
public int SetActor(WorldRenderer wr, ActorInfo actor, PlayerReference owner) { var ios = actor.TraitInfoOrDefault <IOccupySpaceInfo>(); var buildingInfo = ios as BuildingInfo; actorCenterOffset = buildingInfo != null?buildingInfo.CenterOffset(world) : WVec.Zero; actorSharesCell = ios != null && ios.SharesCell; actorSubCell = SubCell.Invalid; // Enforce first entry of ValidOwnerNames as owner if the actor has RequiresSpecificOwners var ownerName = owner.Name; var specificOwnerInfo = actor.TraitInfoOrDefault <RequiresSpecificOwnersInfo>(); if (specificOwnerInfo != null && !specificOwnerInfo.ValidOwnerNames.Contains(ownerName)) { ownerName = specificOwnerInfo.ValidOwnerNames.First(); } var reference = new ActorReference(actor.Name); reference.Add(new OwnerInit(ownerName)); reference.Add(new FactionInit(owner.Faction)); var worldPx = wr.Viewport.ViewToWorldPx(Viewport.LastMousePos) - wr.ScreenPxOffset(actorCenterOffset); var cell = wr.Viewport.ViewToWorld(wr.Viewport.WorldToViewPx(worldPx)); reference.Add(new LocationInit(cell)); if (ios != null && ios.SharesCell) { actorSubCell = editorLayer.FreeSubCellAt(cell); if (actorSubCell != SubCell.Invalid) { reference.Add(new SubCellInit(actorSubCell)); } } if (actor.HasTraitInfo <IFacingInfo>()) { reference.Add(new FacingInit(info.PreviewFacing)); } if (actor.HasTraitInfo <TurretedInfo>()) { reference.Add(new TurretFacingInit(info.PreviewFacing)); } Type = EditorCursorType.Actor; Actor = new EditorActorPreview(wr, null, reference, owner); TerrainTemplate = null; Resource = null; return(++CurrentToken); }
void AddPreviewLocation(EditorActorPreview preview, CPos location) { List <EditorActorPreview> list; if (!cellMap.TryGetValue(location, out list)) { list = new List <EditorActorPreview>(); cellMap.Add(location, list); } list.Add(preview); }
void ITickRender.TickRender(WorldRenderer wr, Actor self) { if (wr.World.Type != WorldType.Editor) { return; } if (Type == EditorCursorType.TerrainTemplate || Type == EditorCursorType.Resource) { var cell = wr.Viewport.ViewToWorld(Viewport.LastMousePos); if (terrainOrResourceCell != cell || terrainOrResourceDirty) { terrainOrResourceCell = cell; terrainOrResourceDirty = false; terrainOrResourcePreview.Clear(); var pos = world.Map.CenterOfCell(cell); if (Type == EditorCursorType.TerrainTemplate) { terrainOrResourcePreview.AddRange(terrainRenderer.RenderPreview(wr, TerrainTemplate, pos)); } else { var variant = Resource.Sequences.FirstOrDefault(); var sequence = wr.World.Map.Rules.Sequences.GetSequence("resources", variant); var sprite = sequence.GetSprite(Resource.MaxDensity - 1); var palette = wr.Palette(Resource.Palette); var alpha = sequence.GetAlpha(Resource.MaxDensity - 1); var tintModifiers = sequence.IgnoreWorldTint ? TintModifiers.IgnoreWorldTint : TintModifiers.None; terrainOrResourcePreview.Add(new SpriteRenderable(sprite, pos, WVec.Zero, 0, palette, 1f, alpha, float3.Ones, tintModifiers, false)); } } } else if (Type == EditorCursorType.Actor) { // Offset mouse position by the center offset (in world pixels) var worldPx = wr.Viewport.ViewToWorldPx(Viewport.LastMousePos) - wr.ScreenPxOffset(actorCenterOffset); var cell = wr.Viewport.ViewToWorld(wr.Viewport.WorldToViewPx(worldPx)); var subCell = actorSharesCell ? editorLayer.FreeSubCellAt(cell) : SubCell.Invalid; var updated = false; if (actorLocation != cell) { actorLocation = cell; Actor.ReplaceInit(new LocationInit(cell)); updated = true; } if (actorSubCell != subCell) { actorSubCell = subCell; if (Actor.RemoveInits <SubCellInit>() > 0) { updated = true; } var subcell = world.Map.Tiles.Contains(cell) ? editorLayer.FreeSubCellAt(cell) : SubCell.Invalid; if (subcell != SubCell.Invalid) { Actor.AddInit(new SubCellInit(subcell)); updated = true; } } if (updated) { Actor = new EditorActorPreview(wr, null, Actor.Export(), Actor.Owner); } } }
void ITickRender.TickRender(WorldRenderer wr, Actor self) { if (wr.World.Type != WorldType.Editor) { return; } if (Type == EditorCursorType.TerrainTemplate || Type == EditorCursorType.Resource) { var cell = wr.Viewport.ViewToWorld(Viewport.LastMousePos); if (terrainOrResourceCell != cell || terrainOrResourceDirty) { terrainOrResourceCell = cell; terrainOrResourceDirty = false; terrainOrResourcePreview.Clear(); var pos = world.Map.CenterOfCell(cell); if (Type == EditorCursorType.TerrainTemplate) { var i = 0; for (var y = 0; y < TerrainTemplate.Size.Y; y++) { for (var x = 0; x < TerrainTemplate.Size.X; x++) { var tile = new TerrainTile(TerrainTemplate.Id, (byte)i++); var tileInfo = world.Map.Rules.TileSet.GetTileInfo(tile); // Empty tile if (tileInfo == null) { continue; } var sprite = wr.Theater.TileSprite(tile, 0); var offset = world.Map.Offset(new CVec(x, y), tileInfo.Height); var palette = wr.Palette(TerrainTemplate.Palette ?? TileSet.TerrainPaletteInternalName); terrainOrResourcePreview.Add(new SpriteRenderable(sprite, pos, offset, 0, palette, 1, false, false)); } } } else { var variant = Resource.Sequences.FirstOrDefault(); var sequence = wr.World.Map.Rules.Sequences.GetSequence("resources", variant); var sprite = sequence.GetSprite(Resource.MaxDensity - 1); var palette = wr.Palette(Resource.Palette); terrainOrResourcePreview.Add(new SpriteRenderable(sprite, pos, WVec.Zero, 0, palette, 1, false, sequence.IgnoreWorldTint)); } } } else if (Type == EditorCursorType.Actor) { // Offset mouse position by the center offset (in world pixels) var worldPx = wr.Viewport.ViewToWorldPx(Viewport.LastMousePos) - wr.ScreenPxOffset(actorCenterOffset); var cell = wr.Viewport.ViewToWorld(wr.Viewport.WorldToViewPx(worldPx)); var subCell = actorSharesCell ? editorLayer.FreeSubCellAt(cell) : SubCell.Invalid; var updated = false; if (actorLocation != cell) { actorLocation = cell; Actor.ReplaceInit(new LocationInit(cell)); updated = true; } if (actorSubCell != subCell) { actorSubCell = subCell; if (Actor.RemoveInits <SubCellInit>() > 0) { updated = true; } var subcell = world.Map.Tiles.Contains(cell) ? editorLayer.FreeSubCellAt(cell) : SubCell.Invalid; if (subcell != SubCell.Invalid) { Actor.AddInit(new SubCellInit(subcell)); updated = true; } } if (updated) { Actor = new EditorActorPreview(wr, null, Actor.Export(), Actor.Owner); } } }
public void Remove(EditorActorPreview preview) { previews.Remove(preview); screenMap.Remove(preview); // Fallback to the actor's CenterPosition for the ActorMap if it has no Footprint var footprint = preview.Footprint.Select(kv => kv.Key).ToArray(); if (!footprint.Any()) { footprint = new[] { worldRenderer.World.Map.CellContaining(preview.CenterPosition) } } ; foreach (var cell in footprint) { if (!cellMap.TryGetValue(cell, out var list)) { continue; } list.Remove(preview); if (!list.Any()) { cellMap.Remove(cell); } } preview.RemovedFromEditor(); UpdateNeighbours(preview.Footprint); if (preview.Info.Name == "mpspawn") { SyncMultiplayerCount(); } } void SyncMultiplayerCount() { var newCount = previews.Count(p => p.Info.Name == "mpspawn"); var mp = Players.Players.Where(p => p.Key.StartsWith("Multi")).ToList(); foreach (var kv in mp) { var name = kv.Key; var index = int.Parse(name.Substring(5)); if (index >= newCount) { Players.Players.Remove(name); OnPlayerRemoved(); } } for (var index = 0; index < newCount; index++) { if (Players.Players.ContainsKey("Multi{0}".F(index))) { continue; } var pr = new PlayerReference { Name = "Multi{0}".F(index), Faction = "Random", Playable = true, Enemies = new[] { "Creeps" } }; Players.Players.Add(pr.Name, pr); worldRenderer.UpdatePalettesForPlayer(pr.Name, pr.Color, true); } var creeps = Players.Players.Keys.FirstOrDefault(p => p == "Creeps"); if (!string.IsNullOrEmpty(creeps)) { Players.Players[creeps].Enemies = Players.Players.Keys.Where(p => !Players.Players[p].NonCombatant).ToArray(); } } void UpdateNeighbours(IReadOnlyDictionary <CPos, SubCell> footprint) { // Include actors inside the footprint too var cells = Util.ExpandFootprint(footprint.Keys, true); foreach (var p in cells.SelectMany(c => PreviewsAt(c))) { p.ReplaceInit(new RuntimeNeighbourInit(NeighbouringPreviews(p.Footprint))); } } void AddPreviewLocation(EditorActorPreview preview, CPos location) { if (!cellMap.TryGetValue(location, out var list)) { list = new List <EditorActorPreview>(); cellMap.Add(location, list); } list.Add(preview); } Dictionary <CPos, string[]> NeighbouringPreviews(IReadOnlyDictionary <CPos, SubCell> footprint) { var cells = Util.ExpandFootprint(footprint.Keys, true).Except(footprint.Keys); return(cells.ToDictionary(c => c, c => PreviewsAt(c).Select(p => p.Info.Name).ToArray())); }
void ITickRender.TickRender(WorldRenderer wr, Actor self) { if (wr.World.Type != WorldType.Editor) { return; } if (Type == EditorCursorType.TerrainTemplate || Type == EditorCursorType.Resource) { var cell = wr.Viewport.ViewToWorld(Viewport.LastMousePos); if (terrainOrResourceCell != cell || terrainOrResourceDirty) { terrainOrResourceCell = cell; terrainOrResourceDirty = false; terrainOrResourcePreview.Clear(); var pos = world.Map.CenterOfCell(cell); if (Type == EditorCursorType.TerrainTemplate) { terrainOrResourcePreview.AddRange(terrainRenderer.RenderPreview(wr, TerrainTemplate, pos)); } else { terrainOrResourcePreview.AddRange(resourceRenderers.SelectMany(r => r.RenderPreview(wr, ResourceType, pos))); } } } else if (Type == EditorCursorType.Actor) { // Offset mouse position by the center offset (in world pixels) var worldPx = wr.Viewport.ViewToWorldPx(Viewport.LastMousePos) - wr.ScreenPxOffset(actorCenterOffset); var cell = wr.Viewport.ViewToWorld(wr.Viewport.WorldToViewPx(worldPx)); var subCell = actorSharesCell ? editorLayer.FreeSubCellAt(cell) : SubCell.Invalid; var updated = false; if (actorLocation != cell) { actorLocation = cell; Actor.ReplaceInit(new LocationInit(cell)); updated = true; } if (actorSubCell != subCell) { actorSubCell = subCell; if (Actor.RemoveInits <SubCellInit>() > 0) { updated = true; } var subcell = world.Map.Tiles.Contains(cell) ? editorLayer.FreeSubCellAt(cell) : SubCell.Invalid; if (subcell != SubCell.Invalid) { Actor.AddInit(new SubCellInit(subcell)); updated = true; } } if (updated) { Actor = new EditorActorPreview(wr, null, Actor.Export(), Actor.Owner); } } }
void INotifyEditorPlacementInfo.RemovedFromEditor(EditorActorPreview preview, World editorWorld, object data) { editorWorld.WorldActor.Trait <TerrainLighting>().RemoveLightSource((int)data); }
object INotifyEditorPlacementInfo.AddedToEditor(EditorActorPreview preview, World editorWorld) { var tint = new float3(RedTint, GreenTint, BlueTint); return(editorWorld.WorldActor.Trait <TerrainLighting>().AddLightSource(preview.CenterPosition, Range, Intensity, tint)); }