1 using System;
2 using OpenBveApi.Math;
3 
4 namespace OpenBveApi.Objects
5 {
6 	/// <summary>A single internal state of an Object</summary>
7 	public class ObjectState : ICloneable
8 	{
9 		/// <summary>A reference to the prototype static object</summary>
10 		public StaticObject Prototype;
11 		/// <summary>The translation matrix to be applied</summary>
12 		private Matrix4D _translation;
13 		/// <summary>The scale matrix to be applied</summary>
14 		private Matrix4D _scale;
15 		/// <summary>The rotation matrix to be applied</summary>
16 		private Matrix4D _rotate;
17 		/// <summary>The world position vector</summary>
18 		public Vector3 WorldPosition;
19 		/// <summary>A value between 0 (daytime) and 255 (nighttime).</summary>
20 		public byte DaytimeNighttimeBlend = 0;
21 
22 		/// <summary>The translation matrix to be applied</summary>
23 		public Matrix4D Translation
24 		{
25 			get
26 			{
27 				return _translation;
28 			}
29 			set
30 			{
31 				_translation = value;
32 				updateModelMatrix = true;
33 			}
34 		}
35 		/// <summary>The scale matrix to be applied</summary>
36 		public Matrix4D Scale
37 		{
38 			get
39 			{
40 				return _scale;
41 			}
42 			set
43 			{
44 				_scale = value;
45 				updateModelMatrix = true;
46 			}
47 		}
48 		/// <summary>The rotation matrix to be applied</summary>
49 		public Matrix4D Rotate
50 		{
51 			get
52 			{
53 				return _rotate;
54 			}
55 			set
56 			{
57 				_rotate = value;
58 				updateModelMatrix = true;
59 			}
60 		}
61 
62 		/// <summary>Backing property holding the cached model matrix</summary>
63 		private Matrix4D modelMatrix;
64 
65 		/// <summary>Returns the final model matrix</summary>
66 		public Matrix4D ModelMatrix
67 		{
68 			get
69 			{
70 				if(updateModelMatrix == false)
71 				{
72 					return modelMatrix;
73 				}
74 				else
75 				{
76 					updateModelMatrix = false;
77 					modelMatrix = _scale * _rotate * _translation;
78 					return modelMatrix;
79 				}
80 			}
81 		}
82 
83 		private bool updateModelMatrix;
84 		/// <summary>The texture translation matrix to be applied</summary>
85 		public Matrix4D TextureTranslation;
86 		/// <summary>The brightness value at this object's track position</summary>
87 		public double Brightness;
88 
89 		/// <summary>The starting track position, for static objects only.</summary>
90 		public float StartingDistance;
91 		/// <summary>The ending track position, for static objects only.</summary>
92 		public float EndingDistance;
93 
94 		/// <summary>Creates a new ObjectState</summary>
ObjectState()95 		public ObjectState()
96 		{
97 			Prototype = null;
98 			_translation = Matrix4D.Identity;
99 			_scale = Matrix4D.Identity;
100 			_rotate = Matrix4D.Identity;
101 			TextureTranslation = Matrix4D.Identity;
102 			Brightness = 0.0;
103 			StartingDistance = 0.0f;
104 			EndingDistance = 0.0f;
105 			updateModelMatrix = false;
106 		}
107 
108 		/// <summary>Creates a new ObjectState</summary>
ObjectState(StaticObject prototype)109 		public ObjectState(StaticObject prototype) : this()
110 		{
111 			Prototype = prototype;
112 		}
113 
114 		/// <summary>Clones this ObjectState</summary>
Clone()115 		public object Clone()
116 		{
117 			return MemberwiseClone();
118 		}
119 	}
120 }
121