summaryrefslogtreecommitdiff
path: root/Client/Assets/Scripts/XMainClient/XCameraShakeComponent.cs
blob: 82dfed04b1d63e4b91fcd7bc81ca1fe0e4d50e03 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
using System;
using UnityEngine;
using XUtliPoolLib;

namespace XMainClient
{
	internal class XCameraShakeComponent : XComponent
	{
		public override uint ID
		{
			get
			{
				return XCameraShakeComponent.uuID;
			}
		}

		public new static readonly uint uuID = XSingleton<XCommon>.singleton.XHash("Camera_Basic_Shake");

		private XCameraEffectData _args;

		private XCameraEx _camera = null;

		private float _timeEscaped = 0f;

		private float _timeInterval = 0f;

		private float _fov = 0f;

		private float _time_scale = 1f;

		private bool _shake = false;

		private Vector3 x = Vector3.right;

		private Vector3 y = Vector3.up;

		private Vector3 z = Vector3.forward;

		private bool _random = false;

		private int _rfactor = 1;

		protected override void EventSubscribe()
		{
			base.RegisterEvent(XEventDefine.XEvent_CameraShake, new XComponent.XEventHandler(this.OnBasicShake));
		}

		public override void OnAttachToHost(XObject host)
		{
			base.OnAttachToHost(host);
			this._camera = (host as XCameraEx);
			this._fov = this._camera.InitFOV;
		}

		private bool OnBasicShake(XEventArgs e)
		{
			bool flag = this._camera != null;
			if (flag)
			{
				XCameraShakeEventArgs xcameraShakeEventArgs = e as XCameraShakeEventArgs;
				bool flag2 = xcameraShakeEventArgs.Effect == null;
				if (flag2)
				{
					this.StopShake();
					return true;
				}
				this._time_scale = xcameraShakeEventArgs.TimeScale;
				this._args = xcameraShakeEventArgs.Effect;
				this._timeEscaped = 0f;
				this._timeInterval = 0f;
				this._fov = this._camera.InitFOV;
				this._shake = true;
				switch (this._args.Coordinate)
				{
				case CameraMotionSpace.World:
					this.x = Vector3.right;
					this.y = Vector3.up;
					this.z = Vector3.forward;
					break;
				case CameraMotionSpace.Self:
					this.x = XSingleton<XEntityMgr>.singleton.Player.EngineObject.Right;
					this.y = XSingleton<XEntityMgr>.singleton.Player.EngineObject.Up;
					this.z = XSingleton<XEntityMgr>.singleton.Player.EngineObject.Forward;
					break;
				case CameraMotionSpace.Camera:
					this.x = XSingleton<XScene>.singleton.GameCamera.UnityCamera.transform.right;
					this.y = XSingleton<XScene>.singleton.GameCamera.UnityCamera.transform.up;
					this.z = XSingleton<XScene>.singleton.GameCamera.UnityCamera.transform.forward;
					break;
				}
				this._random = this._args.Random;
			}
			this._rfactor = 1;
			return true;
		}

		public override void PostUpdate(float fDeltaT)
		{
			bool flag = this._camera != null && this._shake;
			if (flag)
			{
				this._timeEscaped += fDeltaT;
				this._timeInterval += fDeltaT;
				bool flag2 = XSingleton<XCommon>.singleton.IsGreater(this._timeEscaped, this._args.Time * this._time_scale);
				if (flag2)
				{
					this.StopShake();
				}
				else
				{
					bool flag3 = XSingleton<XCommon>.singleton.IsGreater(this._timeInterval, 1f / this._args.Frequency * this._time_scale);
					if (flag3)
					{
						this._rfactor = -this._rfactor;
						this._camera.CameraTrans.position += this.Shake();
						this._camera.UnityCamera.fieldOfView = this._fov + (this._random ? UnityEngine.Random.Range(-this._args.FovAmp, this._args.FovAmp) : (this._args.FovAmp * (float)this._rfactor));
						this._timeInterval = 0f;
					}
				}
			}
		}

		private void StopShake()
		{
			this._args = null;
			this._timeEscaped = 0f;
			this._shake = false;
			this._camera.FovBack();
		}

		private Vector3 Shake()
		{
			float num = this._random ? UnityEngine.Random.Range(-this._args.AmplitudeX, this._args.AmplitudeX) : (this._args.AmplitudeX * (float)this._rfactor);
			float num2 = this._random ? UnityEngine.Random.Range(-this._args.AmplitudeY, this._args.AmplitudeY) : (this._args.AmplitudeY * (float)this._rfactor);
			float num3 = this._random ? UnityEngine.Random.Range(-this._args.AmplitudeZ, this._args.AmplitudeZ) : (this._args.AmplitudeZ * (float)this._rfactor);
			Vector3 vector = Vector3.zero;
			bool shakeX = this._args.ShakeX;
			if (shakeX)
			{
				vector += this.x * num;
			}
			bool shakeY = this._args.ShakeY;
			if (shakeY)
			{
				vector += this.y * num2;
			}
			bool shakeZ = this._args.ShakeZ;
			if (shakeZ)
			{
				vector += this.z * num3;
			}
			return vector;
		}
	}
}