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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
using UnityEngine;
public class Floating : MonoBehaviour, IWaterInteractable
{
public float m_waterLevelOffset;
public float m_forceDistance = 1f;
public float m_force = 0.5f;
public float m_balanceForceFraction = 0.02f;
public float m_damping = 0.05f;
private static float m_minImpactEffectVelocity = 0.5f;
public EffectList m_impactEffects = new EffectList();
public GameObject m_surfaceEffects;
private float m_inWater = -10000f;
private bool m_beenInWater;
private bool m_wasInWater = true;
private Rigidbody m_body;
private Collider m_collider;
private ZNetView m_nview;
private void Awake()
{
m_nview = GetComponent<ZNetView>();
m_body = GetComponent<Rigidbody>();
m_collider = GetComponentInChildren<Collider>();
SetSurfaceEffect(enabled: false);
InvokeRepeating("TerrainCheck", Random.Range(10f, 30f), 30f);
}
public Transform GetTransform()
{
if (this == null)
{
return null;
}
return base.transform;
}
public bool IsOwner()
{
if (m_nview.IsValid())
{
return m_nview.IsOwner();
}
return false;
}
private void TerrainCheck()
{
if (!m_nview.IsValid() || !m_nview.IsOwner())
{
return;
}
float groundHeight = ZoneSystem.instance.GetGroundHeight(base.transform.position);
if (base.transform.position.y - groundHeight < -1f)
{
Vector3 position = base.transform.position;
position.y = groundHeight + 1f;
base.transform.position = position;
Rigidbody component = GetComponent<Rigidbody>();
if ((bool)component)
{
component.velocity = Vector3.zero;
}
ZLog.Log("Moved up item " + base.gameObject.name);
}
}
private void FixedUpdate()
{
if (!m_nview.IsValid() || !m_nview.IsOwner())
{
return;
}
if (!IsInWater())
{
SetSurfaceEffect(enabled: false);
return;
}
UpdateImpactEffect();
float floatDepth = GetFloatDepth();
if (floatDepth > 0f)
{
SetSurfaceEffect(enabled: false);
return;
}
SetSurfaceEffect(enabled: true);
Vector3 position = m_collider.ClosestPoint(base.transform.position + Vector3.down * 1000f);
Vector3 worldCenterOfMass = m_body.worldCenterOfMass;
float num = Mathf.Clamp01(Mathf.Abs(floatDepth) / m_forceDistance);
Vector3 vector = Vector3.up * m_force * num * (Time.fixedDeltaTime * 50f);
m_body.WakeUp();
m_body.AddForceAtPosition(vector * m_balanceForceFraction, position, ForceMode.VelocityChange);
m_body.AddForceAtPosition(vector, worldCenterOfMass, ForceMode.VelocityChange);
m_body.velocity -= m_body.velocity * m_damping * num;
m_body.angularVelocity -= m_body.angularVelocity * m_damping * num;
}
public bool IsInWater()
{
return m_inWater > -10000f;
}
private void SetSurfaceEffect(bool enabled)
{
if (m_surfaceEffects != null)
{
m_surfaceEffects.SetActive(enabled);
}
}
private void UpdateImpactEffect()
{
if (m_body.IsSleeping() || !m_impactEffects.HasEffects())
{
return;
}
Vector3 vector = m_collider.ClosestPoint(base.transform.position + Vector3.down * 1000f);
if (vector.y < m_inWater)
{
if (!m_wasInWater)
{
m_wasInWater = true;
Vector3 pos = vector;
pos.y = m_inWater;
if (m_body.GetPointVelocity(vector).magnitude > m_minImpactEffectVelocity)
{
m_impactEffects.Create(pos, Quaternion.identity);
}
}
}
else
{
m_wasInWater = false;
}
}
private float GetFloatDepth()
{
return m_body.worldCenterOfMass.y - m_inWater - m_waterLevelOffset;
}
public void SetInWater(float waterLevel)
{
m_inWater = waterLevel;
if (!m_beenInWater && waterLevel > -10000f && GetFloatDepth() < 0f)
{
m_beenInWater = true;
}
}
public bool BeenInWater()
{
return m_beenInWater;
}
private void OnDrawGizmosSelected()
{
Gizmos.color = Color.blue;
Gizmos.DrawWireCube(base.transform.position + Vector3.down * m_waterLevelOffset, new Vector3(1f, 0.05f, 1f));
}
}
|