1 using OpenBveApi.Hosts;
2 using OpenBveApi.Objects;
3 
4 namespace LibRender2.Trains
5 {
6 	/// <summary>An animated object attached to a car (Exterior, cab etc.)</summary>
7 	public class CarSection
8 	{
9 		/// <summary>Holds a reference to the current host</summary>
10 		private readonly HostInterface currentHost;
11 		/// <summary>The groups of animated objects</summary>
12 		public ElementsGroup[] Groups;
13 		/// <summary>The current additional group (touch etc.)</summary>
14 		public int CurrentAdditionalGroup;
15 		/// <summary>Whether this is visible from internal views</summary>
16 		public readonly bool VisibleFromInterior;
17 
CarSection(HostInterface Host, ObjectType Type, bool visibleFromInterior, UnifiedObject Object = null)18 		public CarSection(HostInterface Host, ObjectType Type, bool visibleFromInterior, UnifiedObject Object = null)
19 		{
20 			currentHost = Host;
21 			Groups = new ElementsGroup[1];
22 			Groups[0] = new ElementsGroup(Type);
23 			VisibleFromInterior = visibleFromInterior;
24 			if (Object is StaticObject)
25 			{
26 				StaticObject s = (StaticObject) Object;
27 				Groups[0].Elements = new AnimatedObject[1];
28 				Groups[0].Elements[0] = new AnimatedObject(Host)
29 				{
30 					States = new[] {new ObjectState(s)},
31 					CurrentState = 0
32 				};
33 				currentHost.CreateDynamicObject(ref Groups[0].Elements[0].internalObject);
34 			}
35 			else if (Object is AnimatedObjectCollection)
36 			{
37 				AnimatedObjectCollection a = (AnimatedObjectCollection)Object;
38 				Groups[0].Elements = new AnimatedObject[a.Objects.Length];
39 				for (int h = 0; h < a.Objects.Length; h++)
40 				{
41 					Groups[0].Elements[h] = a.Objects[h].Clone();
42 					currentHost.CreateDynamicObject(ref Groups[0].Elements[h].internalObject);
43 				}
44 			}
45 		}
46 
47 		/// <summary>Initalizes the CarSection</summary>
48 		/// <param name="CurrentlyVisible">Whether visible at the time of this call</param>
Initialize(bool CurrentlyVisible)49 		public void Initialize(bool CurrentlyVisible)
50 		{
51 			for (int i = 0; i < Groups.Length; i++)
52 			{
53 				Groups[i].Initialize(CurrentlyVisible);
54 			}
55 		}
56 
57 		/// <summary>Shows all objects associated with the car section</summary>
Show()58 		public void Show()
59 		{
60 			if (Groups.Length > 0)
61 			{
62 				for (int i = 0; i < Groups[0].Elements.Length; i++)
63 				{
64 					currentHost.ShowObject(Groups[0].Elements[i].internalObject, Groups[0].Type);
65 				}
66 			}
67 
68 			int add = CurrentAdditionalGroup + 1;
69 			if (add < Groups.Length)
70 			{
71 				for (int i = 0; i < Groups[add].Elements.Length; i++)
72 				{
73 					currentHost.ShowObject(Groups[add].Elements[i].internalObject, Groups[add].Type);
74 
75 				}
76 			}
77 		}
78 	}
79 }
80