Client LuaCsForBarotrauma
BarotraumaClient/ClientSource/Events/Missions/MissionPrefab.cs
1 using Microsoft.Xna.Framework;
2 using System;
3 using System.Collections.Generic;
4 using System.Collections.Immutable;
5 
6 namespace Barotrauma
7 {
8  partial class MissionPrefab : PrefabWithUintIdentifier
9  {
10  private ImmutableArray<Sprite> portraits = new ImmutableArray<Sprite>();
11 
12  public bool HasPortraits => portraits.Length > 0;
13 
14  public Sprite Icon
15  {
16  get;
17  private set;
18  }
19 
20  public Color IconColor
21  {
22  get;
23  private set;
24  }
25 
27  {
28  get;
29  private set;
30  }
31 
32  public float HudIconMaxDistance
33  {
34  get;
35  private set;
36  }
37 
38  public Sprite HudIcon
39  {
40  get
41  {
42  return hudIcon ?? Icon;
43  }
44  }
45 
46  public Color HudIconColor
47  {
48  get
49  {
50  return hudIconColor ?? IconColor;
51  }
52  }
53 
54  private Sprite hudIcon;
55  private Color? hudIconColor;
56 
57  private ImmutableDictionary<int, Identifier> overrideMusicOnState;
58 
59  partial void InitProjSpecific(ContentXElement element)
60  {
61  DisplayTargetHudIcons = element.GetAttributeBool("displaytargethudicons", false);
62  HudIconMaxDistance = element.GetAttributeFloat("hudiconmaxdistance", 1000.0f);
63  Dictionary<int, Identifier> overrideMusic = new Dictionary<int, Identifier>();
64  List<Sprite> portraits = new List<Sprite>();
65  foreach (var subElement in element.Elements())
66  {
67  switch (subElement.Name.ToString().ToLowerInvariant())
68  {
69  case "icon":
70  Icon = new Sprite(subElement);
71  IconColor = subElement.GetAttributeColor("color", Color.White);
72  break;
73  case "hudicon":
74  hudIcon = new Sprite(subElement);
75  hudIconColor = subElement.GetAttributeColor("color");
76  break;
77  case "overridemusic":
78  overrideMusic.Add(
79  subElement.GetAttributeInt("state", 0),
80  subElement.GetAttributeIdentifier("type", Identifier.Empty));
81  break;
82  case "portrait":
83  var portrait = new Sprite(subElement, lazyLoad: true);
84  if (portrait != null)
85  {
86  portraits.Add(portrait);
87  }
88  break;
89  }
90  }
91  this.portraits = portraits.ToImmutableArray();
92  overrideMusicOnState = overrideMusic.ToImmutableDictionary();
93  }
94 
95  public Identifier GetOverrideMusicType(int state)
96  {
97  if (overrideMusicOnState.TryGetValue(state, out Identifier id))
98  {
99  return id;
100  }
101  return Identifier.Empty;
102  }
103 
104  public Sprite GetPortrait(int randomSeed)
105  {
106  if (portraits.Length == 0) { return null; }
107  return portraits[Math.Abs(randomSeed) % portraits.Length];
108  }
109 
110  partial void DisposeProjectSpecific()
111  {
112  Icon?.Remove();
113  }
114  }
115 }
float GetAttributeFloat(string key, float def)
IEnumerable< ContentXElement > Elements()
bool GetAttributeBool(string key, bool def)
readonly Identifier Identifier
Definition: Prefab.cs:34