summaryrefslogtreecommitdiff
path: root/Valheim_r202102_v0.141.2/Valheim/assembly_valheim/ShipEffects.cs
blob: d7bf567acf3d809b918de004d6b849d9013a0ca1 (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
using System.Collections.Generic;
using UnityEngine;

public class ShipEffects : MonoBehaviour
{
	public Transform m_shadow;

	public float m_offset = 0.01f;

	public float m_minimumWakeVel = 5f;

	public GameObject m_speedWakeRoot;

	public GameObject m_wakeSoundRoot;

	public GameObject m_inWaterSoundRoot;

	public float m_audioFadeDuration = 2f;

	public AudioSource m_sailSound;

	public float m_sailFadeDuration = 1f;

	public GameObject m_splashEffects;

	private float m_sailBaseVol = 1f;

	private ParticleSystem[] m_wakeParticles;

	private List<KeyValuePair<AudioSource, float>> m_wakeSounds = new List<KeyValuePair<AudioSource, float>>();

	private List<KeyValuePair<AudioSource, float>> m_inWaterSounds = new List<KeyValuePair<AudioSource, float>>();

	private Rigidbody m_body;

	private Ship m_ship;

	private void Awake()
	{
		ZNetView componentInParent = GetComponentInParent<ZNetView>();
		if ((bool)componentInParent && componentInParent.GetZDO() == null)
		{
			base.enabled = false;
			return;
		}
		m_body = GetComponentInParent<Rigidbody>();
		m_ship = GetComponentInParent<Ship>();
		if ((bool)m_speedWakeRoot)
		{
			m_wakeParticles = m_speedWakeRoot.GetComponentsInChildren<ParticleSystem>();
		}
		if ((bool)m_wakeSoundRoot)
		{
			AudioSource[] componentsInChildren = m_wakeSoundRoot.GetComponentsInChildren<AudioSource>();
			foreach (AudioSource audioSource in componentsInChildren)
			{
				audioSource.pitch = Random.Range(0.9f, 1.1f);
				m_wakeSounds.Add(new KeyValuePair<AudioSource, float>(audioSource, audioSource.volume));
			}
		}
		if ((bool)m_inWaterSoundRoot)
		{
			AudioSource[] componentsInChildren = m_inWaterSoundRoot.GetComponentsInChildren<AudioSource>();
			foreach (AudioSource audioSource2 in componentsInChildren)
			{
				audioSource2.pitch = Random.Range(0.9f, 1.1f);
				m_inWaterSounds.Add(new KeyValuePair<AudioSource, float>(audioSource2, audioSource2.volume));
			}
		}
		if ((bool)m_sailSound)
		{
			m_sailBaseVol = m_sailSound.volume;
			m_sailSound.pitch = Random.Range(0.9f, 1.1f);
		}
	}

	private void LateUpdate()
	{
		float waterLevel = WaterVolume.GetWaterLevel(base.transform.position);
		Vector3 position = base.transform.position;
		float deltaTime = Time.deltaTime;
		if (position.y > waterLevel)
		{
			m_shadow.gameObject.SetActive(value: false);
			SetWake(enabled: false, deltaTime);
			FadeSounds(m_inWaterSounds, enabled: false, deltaTime);
			return;
		}
		m_shadow.gameObject.SetActive(value: true);
		bool flag = m_body.velocity.magnitude > m_minimumWakeVel;
		FadeSounds(m_inWaterSounds, enabled: true, deltaTime);
		SetWake(flag, deltaTime);
		if ((bool)m_sailSound)
		{
			float target = (m_ship.IsSailUp() ? m_sailBaseVol : 0f);
			FadeSound(m_sailSound, target, m_sailFadeDuration, deltaTime);
		}
		if (m_splashEffects != null)
		{
			m_splashEffects.SetActive(m_ship.HasPlayerOnboard());
		}
	}

	private void SetWake(bool enabled, float dt)
	{
		ParticleSystem[] wakeParticles = m_wakeParticles;
		for (int i = 0; i < wakeParticles.Length; i++)
		{
			ParticleSystem.EmissionModule emission = wakeParticles[i].emission;
			emission.enabled = enabled;
		}
		FadeSounds(m_wakeSounds, enabled, dt);
	}

	private void FadeSounds(List<KeyValuePair<AudioSource, float>> sources, bool enabled, float dt)
	{
		foreach (KeyValuePair<AudioSource, float> source in sources)
		{
			if (enabled)
			{
				FadeSound(source.Key, source.Value, m_audioFadeDuration, dt);
			}
			else
			{
				FadeSound(source.Key, 0f, m_audioFadeDuration, dt);
			}
		}
	}

	private void FadeSound(AudioSource source, float target, float fadeDuration, float dt)
	{
		float maxDelta = dt / fadeDuration;
		if (target > 0f)
		{
			if (!source.isPlaying)
			{
				source.Play();
			}
			source.volume = Mathf.MoveTowards(source.volume, target, maxDelta);
		}
		else if (source.isPlaying)
		{
			source.volume = Mathf.MoveTowards(source.volume, 0f, maxDelta);
			if (source.volume <= 0f)
			{
				source.Stop();
			}
		}
	}
}