Client LuaCsForBarotrauma
ArtifactEvent.cs
1 using Microsoft.Xna.Framework;
2 
3 namespace Barotrauma
4 {
6  {
7  private ItemPrefab itemPrefab;
8 
9  private Item item;
10 
11  private int state;
12 
13  private Vector2 spawnPos;
14 
15  private bool spawnPending;
16 
17  public bool SpawnPending => spawnPending;
18  public int State => state;
19  public Item Item => item;
20  public Vector2 SpawnPos => spawnPos;
21 
22  public override Vector2 DebugDrawPos
23  {
24  get { return spawnPos; }
25  }
26 
27  public override string ToString()
28  {
29  return $"ArtifactEvent ({(itemPrefab == null ? "null" : itemPrefab.Name)})";
30  }
31 
32  public ArtifactEvent(EventPrefab prefab, int seed)
33  : base(prefab, seed)
34  {
35  if (prefab.ConfigElement.GetAttribute("itemname") != null)
36  {
37  DebugConsole.ThrowError("Error in ArtifactEvent - use item identifier instead of the name of the item.",
38  contentPackage: prefab?.ContentPackage);
39  string itemName = prefab.ConfigElement.GetAttributeString("itemname", "");
40  itemPrefab = MapEntityPrefab.Find(itemName) as ItemPrefab;
41  if (itemPrefab == null)
42  {
43  DebugConsole.ThrowError("Error in SalvageMission: couldn't find an item prefab with the name " + itemName);
44  }
45  }
46  else
47  {
48  Identifier itemIdentifier = prefab.ConfigElement.GetAttributeIdentifier("itemidentifier", Identifier.Empty);
49  itemPrefab = MapEntityPrefab.FindByIdentifier(itemIdentifier) as ItemPrefab;
50  if (itemPrefab == null)
51  {
52  DebugConsole.ThrowError("Error in ArtifactEvent - couldn't find an item prefab with the identifier " + itemIdentifier,
53  contentPackage: prefab?.ContentPackage);
54  }
55  }
56  }
57 
58  protected override void InitEventSpecific(EventSet parentSet)
59  {
60  spawnPos = Level.Loaded.GetRandomItemPos(
61  (Rand.Value(Rand.RandSync.ServerAndClient) < 0.5f) ?
62  Level.PositionType.MainPath | Level.PositionType.SidePath :
64  500.0f, 10000.0f, 30.0f, SpawnPosFilter);
65 
66  spawnPending = true;
67  }
68 
69  public override string GetDebugInfo()
70  {
71  return
72  $"Finished: {IsFinished.ColorizeObject()}\n" +
73  $"Item: {Item.ColorizeObject()}\n" +
74  $"Spawn pending: {SpawnPending.ColorizeObject()}\n" +
75  $"Spawn position: {SpawnPos.ColorizeObject()}";
76  }
77 
78  private void SpawnItem()
79  {
80  item = new Item(itemPrefab, spawnPos, null);
81  item.body.FarseerBody.BodyType = FarseerPhysics.BodyType.Kinematic;
82 
83  //try to find an artifact holder and place the artifact inside it
84  foreach (Item it in Item.ItemList)
85  {
86  if (it.Submarine != null || !it.HasTag(Tags.ArtifactHolder)) { continue; }
87 
88  var itemContainer = it.GetComponent<Items.Components.ItemContainer>();
89  if (itemContainer == null) continue;
90  if (itemContainer.Combine(item, user: null)) break; // Placement successful
91  }
92 
93  if (GameSettings.CurrentConfig.VerboseLogging)
94  {
95  DebugConsole.NewMessage("Initialized ArtifactEvent (" + item.Name + ")", Color.White);
96  }
97 
98 #if SERVER
99  if (GameMain.Server != null)
100  {
101  Entity.Spawner.CreateNetworkEvent(new EntitySpawner.SpawnEntity(item));
102  }
103 #endif
104  }
105 
106  public override void Update(float deltaTime)
107  {
108  if (spawnPending)
109  {
110  if (itemPrefab == null)
111  {
112  isFinished = true;
113  return;
114  }
115  SpawnItem();
116  spawnPending = false;
117  }
118 
119  switch (state)
120  {
121  case 0:
122  if (item.ParentInventory != null) { item.body.FarseerBody.BodyType = FarseerPhysics.BodyType.Dynamic; }
123  if (item.CurrentHull == null) return;
124 
125  state = 1;
126  break;
127  case 1:
128  if (!Submarine.MainSub.AtEitherExit) { return; }
129 
130  Finish();
131  state = 2;
132  break;
133  }
134  }
135  }
136 }
ArtifactEvent(EventPrefab prefab, int seed)
override Vector2 DebugDrawPos
override void InitEventSpecific(EventSet parentSet)
override string GetDebugInfo()
override string ToString()
override void Update(float deltaTime)
string? GetAttributeString(string key, string? def)
XAttribute? GetAttribute(string name)
Identifier GetAttributeIdentifier(string key, string def)
Submarine Submarine
Definition: Entity.cs:53
Func< Level.InterestingPosition, bool > SpawnPosFilter
Definition: Event.cs:22
virtual void Finish()
Definition: Event.cs:73
readonly EventPrefab prefab
Definition: Event.cs:14
bool isFinished
Definition: Event.cs:10
readonly ContentXElement ConfigElement
Definition: EventPrefab.cs:13
Event sets are sets of random events that occur within a level (most commonly, monster spawns and scr...
Definition: EventSet.cs:31
override string Name
Note that this is not a LocalizedString instance, just the current name of the item as a string....
static readonly List< Item > ItemList
Vector2 GetRandomItemPos(PositionType spawnPosType, float randomSpread, float minDistFromSubs, float offsetFromWall=10.0f, Func< InterestingPosition, bool > filter=null)
static MapEntityPrefab FindByIdentifier(Identifier identifier)
static MapEntityPrefab Find(string name, string identifier=null, bool showErrorMessages=true)
Find a matching map entity prefab
static Submarine MainSub
Note that this can be null in some situations, e.g. editors and missions that don't load a submarine.