Client LuaCsForBarotrauma
MalfunctionEvent.cs
1 using System;
2 using System.Linq;
3 using System.Collections.Generic;
4 using System.Text;
5 using Microsoft.Xna.Framework;
6 
7 namespace Barotrauma
8 {
10  {
11  private Identifier[] targetItemIdentifiers;
12 
13  private List<Item> targetItems;
14 
15  private int minItemAmount, maxItemAmount;
16 
17  private float decreaseConditionAmount;
18 
19  private float duration;
20 
21  private float timer;
22 
23  public override string ToString()
24  {
25  return "MalfunctionEvent (" + string.Join(", ", targetItemIdentifiers) + ")";
26  }
27 
29  : base(prefab, seed)
30  {
31  targetItems = new List<Item>();
32 
33  minItemAmount = prefab.ConfigElement.GetAttributeInt("minitemamount", 1);
34  maxItemAmount = prefab.ConfigElement.GetAttributeInt("maxitemamount", minItemAmount);
35 
36  decreaseConditionAmount = prefab.ConfigElement.GetAttributeFloat("decreaseconditionamount", 0.0f);
37  duration = prefab.ConfigElement.GetAttributeFloat("duration", 0.0f);
38 
39  targetItemIdentifiers = prefab.ConfigElement.GetAttributeIdentifierArray("itemidentifiers", Array.Empty<Identifier>());
40  }
41 
42  protected override void InitEventSpecific(EventSet parentSet)
43  {
44  var matchingItems = Item.ItemList.FindAll(i => i.Condition > 0.0f && targetItemIdentifiers.Contains(i.Prefab.Identifier));
45  int itemAmount = Rand.Range(minItemAmount, maxItemAmount, Rand.RandSync.ServerAndClient);
46  for (int i = 0; i < itemAmount; i++)
47  {
48  if (matchingItems.Count == 0) break;
49  targetItems.Add(matchingItems[Rand.Int(matchingItems.Count, Rand.RandSync.ServerAndClient)]);
50  }
51  }
52 
53  public override void Update(float deltaTime)
54  {
55  if (isFinished) return;
56  if (targetItems.Count == 0 || timer >= duration)
57  {
58  Finish();
59  return;
60  }
61 
62  targetItems.RemoveAll(i => i.Removed || i.Condition <= 0.0f);
63  foreach (Item item in targetItems)
64  {
65  if (duration <= 0.0f)
66  {
67  item.Condition = 0.0f;
68  }
69  else
70  {
71  item.Condition -= decreaseConditionAmount / duration * deltaTime;
72  }
73  }
74 
75  timer += deltaTime;
76  }
77  }
78 }
Identifier[] GetAttributeIdentifierArray(Identifier[] def, params string[] keys)
float GetAttributeFloat(string key, float def)
int GetAttributeInt(string key, int def)
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:11
Event sets are sets of random events that occur within a level (most commonly, monster spawns and scr...
Definition: EventSet.cs:31
static readonly List< Item > ItemList
override void Update(float deltaTime)
MalfunctionEvent(EventPrefab prefab, int seed)
override void InitEventSpecific(EventSet parentSet)