Skip to content

Instantly share code, notes, and snippets.

@phrohdoh
Created May 20, 2014 23:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phrohdoh/928c9dd77c2c3f05c349 to your computer and use it in GitHub Desktop.
Save phrohdoh/928c9dd77c2c3f05c349 to your computer and use it in GitHub Desktop.
Requesting help for stashing Plug/PlugActor in BuildingUtil/Util.cs ~11-12
public static bool CanPlaceBuilding(this World world, string name, BuildingInfo building, CPos topLeft, Actor toIgnore)
{
if (building.AllowInvalidPlacement)
return true;
var existing = world.WorldActor.Trait<BuildingInfluence>().GetBuildingAt(topLeft);
var toPlace = Rules.Info[name.ToLowerInvariant()];
if (CanPlacePlug(existing, toPlace, topLeft))
{
foreach (var pluggable in existing.TraitsImplementing<Pluggable>())
pluggable.Plug = toPlace.Traits.Get<Plug>();
Game.Debug("Placing plug on {0} at {1}.".F(existing.Info.Name, existing.Location.TopLeft.ToCPos() - topLeft));
return true;
}
var res = world.WorldActor.Trait<ResourceLayer>();
return FootprintUtils.Tiles(name, building, topLeft).All(
t => world.Map.IsInMap(t.X, t.Y) && res.GetResource(t) == null &&
world.IsCellBuildable(t, building, toIgnore));
}
public class PlugInfo : ITraitInfo
{
[Desc("This is a plug of type...")]
public readonly string Type = null;
public virtual object Create(ActorInitializer init) { return new Plug(init.self, this); }
}
public class Plug
{
public Actor PlugActor;
public Plug(Actor self, PlugInfo info)
{
PlugActor = self;
}
}
public class PlugInit : IActorInit<Plug>
{
[FieldFromYamlKey] public readonly Plug value = null;
public PlugInit() { }
public PlugInit(Plug init) { value = init; }
public Plug Value(World world) { return value; }
}
public class PluggableInfo : ITraitInfo
{
[Desc("Which cell does this plug belong in? Relative to the top left cell of the pluggable structure.")]
public readonly CVec CellOffset = new CVec();
[Desc("Plug types supported for this cell.")]
public readonly string[] PlugTypes = { };
public virtual object Create(ActorInitializer init) { return new Pluggable(init, this); }
}
public class Pluggable
{
public Plug Plug;
public Pluggable(ActorInitializer init, PluggableInfo info)
{
if (init.Contains<PlugInit>())
Plug = init.Get<PlugInit, Plug>();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment