PoisonZone fizy

This commit is contained in:
szczuras4
2025-06-04 18:03:43 +02:00
parent 002cca32bb
commit 9e5e321d34
3 changed files with 86 additions and 38 deletions

View File

@@ -18,8 +18,8 @@ public class PoisonZone : MonoBehaviour
[Tooltip("Animation curve for ONE CYCLE of the looping Volume weight. X-axis (Time) from 0 to 1. Y-axis (Value) is the weight intensity (e.g., 0 to 1, where 1 is full effect defined by the curve values).")] [Tooltip("Animation curve for ONE CYCLE of the looping Volume weight. X-axis (Time) from 0 to 1. Y-axis (Value) is the weight intensity (e.g., 0 to 1, where 1 is full effect defined by the curve values).")]
public AnimationCurve loopingVolumeWeightCurve = new AnimationCurve( public AnimationCurve loopingVolumeWeightCurve = new AnimationCurve(
new Keyframe(0, 0), new Keyframe(0, 0),
new Keyframe(0.5f, 0.8f), // Example: peaks at 0.8 halfway through new Keyframe(0.5f, 0.8f),
new Keyframe(1, 0) // Example: returns to 0 at the end of the cycle new Keyframe(1, 0)
); );
[Tooltip("Duration of one full cycle of the looping animation, in seconds. Must be greater than 0.")] [Tooltip("Duration of one full cycle of the looping animation, in seconds. Must be greater than 0.")]
@@ -38,10 +38,10 @@ public class PoisonZone : MonoBehaviour
private Player currentPlayerInZone; private Player currentPlayerInZone;
private float timeSinceLastDamage = 0f; private float timeSinceLastDamage = 0f;
private float timeSinceLastCough = 0f; private float timeSinceLastCough = 0f;
private bool playerCurrentlyInZone = false; private bool playerCurrentlyInZone = false; // Ta flaga jest kluczowa dla logiki ponownego wejœcia
private float currentLoopProgress = 0f; // Postêp w bie¿¹cym cyklu pêtli (0 to 1) private float currentLoopProgress = 0f;
private float currentFadeProgress = 0f; // Postêp fade-in/fade-out (0 to 1), kontroluje ogóln¹ intensywnoœæ pêtli private float currentFadeProgress = 0f;
void Start() void Start()
{ {
@@ -57,63 +57,59 @@ public class PoisonZone : MonoBehaviour
} }
else else
{ {
// Ustaw pocz¹tkow¹ wagê na 0 (poniewa¿ currentFadeProgress jest 0)
poisonVolume.weight = 0f; poisonVolume.weight = 0f;
poisonVolume.enabled = false; poisonVolume.enabled = false;
} }
if (loopCycleDuration <= 0.001f) // Sprawdzenie, czy jest sensownie dodatni if (loopCycleDuration <= 0.001f)
{ {
Debug.LogWarning("PoisonZone: Loop Cycle Duration should be greater than 0 for looping animation. Setting to 1s.", this); Debug.LogWarning("PoisonZone: Loop Cycle Duration should be greater than 0 for looping animation. Setting to 1s.", this);
loopCycleDuration = 1f; // Domyœlna wartoœæ, aby unikn¹æ dzielenia przez zero lub zbyt szybkich pêtli loopCycleDuration = 1f;
} }
} }
void Update() void Update()
{ {
// --- Volume Animation Logic ---
if (poisonVolume != null) if (poisonVolume != null)
{ {
// 1. Update Fade Progress (kontroluje ogóln¹ widocznoœæ/intensywnoœæ pêtli)
float targetFadeProgress = playerCurrentlyInZone ? 1.0f : 0.0f; float targetFadeProgress = playerCurrentlyInZone ? 1.0f : 0.0f;
if (volumeFadeDuration > 0.001f) if (volumeFadeDuration > 0.001f)
{ {
float fadeStep = (1.0f / volumeFadeDuration) * Time.deltaTime; float fadeStep = (1.0f / volumeFadeDuration) * Time.deltaTime;
currentFadeProgress = Mathf.MoveTowards(currentFadeProgress, targetFadeProgress, fadeStep); currentFadeProgress = Mathf.MoveTowards(currentFadeProgress, targetFadeProgress, fadeStep);
} }
else // Natychmiastowe w³¹czenie/wy³¹czenie pêtli (jeœli fade duration jest bliski 0) else
{ {
currentFadeProgress = targetFadeProgress; currentFadeProgress = targetFadeProgress;
} }
// 2. Update Loop Progress (tylko jeœli efekt jest przynajmniej czêœciowo "widoczny" lub gracz jest w strefie)
// To zapewnia, ¿e pêtla rusza, gdy tylko zaczyna siê fade-in.
if (playerCurrentlyInZone || currentFadeProgress > 0.001f) if (playerCurrentlyInZone || currentFadeProgress > 0.001f)
{ {
currentLoopProgress += Time.deltaTime / loopCycleDuration; currentLoopProgress += Time.deltaTime / loopCycleDuration;
// Zapêtlanie postêpu: if (currentLoopProgress >= 1.0f) currentLoopProgress -= 1.0f;
// Lub u¿ycie modulo dla p³ynniejszego przejœcia, gdy Time.deltaTime jest du¿e:
currentLoopProgress = currentLoopProgress % 1.0f; currentLoopProgress = currentLoopProgress % 1.0f;
} }
// 3. Calculate and Apply Volume Weight
// Wartoœæ z krzywej pêtli jest mno¿ona przez postêp fade'owania.
float loopedWeightValue = loopingVolumeWeightCurve.Evaluate(currentLoopProgress); float loopedWeightValue = loopingVolumeWeightCurve.Evaluate(currentLoopProgress);
poisonVolume.weight = loopedWeightValue * currentFadeProgress; poisonVolume.weight = loopedWeightValue * currentFadeProgress;
// 4. Manage Volume Enabled State bool shouldBeEnabled;
// W³¹czamy Volume, jeœli jego waga jest wystarczaj¹co du¿a, aby by³ widoczny. if (currentFadeProgress > 0.001f)
bool shouldBeEnabled = poisonVolume.weight > 0.001f; {
shouldBeEnabled = true;
}
else
{
shouldBeEnabled = false;
}
if (poisonVolume.enabled != shouldBeEnabled) if (poisonVolume.enabled != shouldBeEnabled)
{ {
poisonVolume.enabled = shouldBeEnabled; poisonVolume.enabled = shouldBeEnabled;
} }
} }
// --- Damage and Sound Logic --- if (playerCurrentlyInZone && currentPlayerInZone != null) // Upewnij siê, ¿e currentPlayerInZone nie jest null
if (playerCurrentlyInZone && currentPlayerInZone != null)
{ {
// Zadawanie obra¿eñ
timeSinceLastDamage += Time.deltaTime; timeSinceLastDamage += Time.deltaTime;
if (timeSinceLastDamage >= damageInterval) if (timeSinceLastDamage >= damageInterval)
{ {
@@ -121,7 +117,6 @@ public class PoisonZone : MonoBehaviour
timeSinceLastDamage = 0f; timeSinceLastDamage = 0f;
} }
// Odtwarzanie dŸwiêku kaszlu
if (coughSound != null && coughInterval > 0) if (coughSound != null && coughInterval > 0)
{ {
timeSinceLastCough += Time.deltaTime; timeSinceLastCough += Time.deltaTime;
@@ -139,22 +134,26 @@ public class PoisonZone : MonoBehaviour
Player enteredPlayer = other.GetComponent<Player>(); Player enteredPlayer = other.GetComponent<Player>();
if (enteredPlayer != null) if (enteredPlayer != null)
{ {
if (currentPlayerInZone == null || currentPlayerInZone != enteredPlayer) // Ta logika powinna poprawnie obs³ugiwaæ ponowne wejœcie tego samego gracza
// oraz wejœcie nowego gracza.
if (currentPlayerInZone != enteredPlayer || !playerCurrentlyInZone)
{ {
currentPlayerInZone = enteredPlayer; // Jeœli to faktycznie nowy gracz LUB ten sam gracz, ale nie by³ 'aktywny' (playerCurrentlyInZone by³o false)
playerCurrentlyInZone = true; if (currentPlayerInZone != enteredPlayer)
timeSinceLastDamage = 0f; {
timeSinceLastCough = 0f; // Reset timera kaszlu currentPlayerInZone = enteredPlayer; // Zaktualizuj referencjê tylko jeœli to inny gracz
}
// Opcjonalnie: zresetuj postêp pêtli, aby zaczyna³a siê od pocz¹tku. playerCurrentlyInZone = true; // Zawsze ustawiaj na true przy "aktywnym" wejœciu
// currentLoopProgress = 0f; timeSinceLastDamage = 0f;
// Jeœli chcesz, aby zaczyna³a siê p³ynnie od pocz¹tku przy ka¿dym wejœciu. timeSinceLastCough = 0f;
currentLoopProgress = 0f; // Resetuj pêtlê animacji Volume
if (coughSound != null) if (coughSound != null)
{ {
PlayCoughSound(); // Kaszel od razu przy wejœciu PlayCoughSound();
} }
//Debug.Log(enteredPlayer.name + " entered poison zone: " + gameObject.name); // Debug.Log($"{enteredPlayer.name} ENTERED/RE-ENTERED zone. playerCurrentlyInZone: {playerCurrentlyInZone}");
} }
} }
} }
@@ -162,11 +161,12 @@ public class PoisonZone : MonoBehaviour
void OnTriggerExit(Collider other) void OnTriggerExit(Collider other)
{ {
Player exitedPlayer = other.GetComponent<Player>(); Player exitedPlayer = other.GetComponent<Player>();
if (exitedPlayer != null && exitedPlayer == currentPlayerInZone) // Sprawdzamy, czy to TEN gracz, który by³ aktywnie œledzony w strefie i w³aœnie opuszcza.
if (exitedPlayer != null && exitedPlayer == currentPlayerInZone && playerCurrentlyInZone)
{ {
playerCurrentlyInZone = false; playerCurrentlyInZone = false; // Oznacz, ¿e gracz (ten konkretny) ju¿ nie jest aktywnie w strefie
// Nie zerujemy currentPlayerInZone tutaj, currentFadeProgress zajmie siê wygaszeniem efektu. // Debug.Log($"{exitedPlayer.name} EXITED zone. playerCurrentlyInZone: {playerCurrentlyInZone}");
//Debug.Log(exitedPlayer.name + " exited poison zone: " + gameObject.name); // Nie zerujemy currentPlayerInZone, aby OnTriggerEnter mog³o poprawnie wykryæ powrót tego samego gracza.
} }
} }
@@ -177,7 +177,14 @@ public class PoisonZone : MonoBehaviour
var healthController = currentPlayerInZone.ThirdPersonController; var healthController = currentPlayerInZone.ThirdPersonController;
if (healthController != null && !healthController.isDead) if (healthController != null && !healthController.isDead)
{ {
// Ta linia jest kluczowa dla pomijania animacji hita.
// Zak³ada, ¿e masz zmodyfikowany vDamage.cs (z polem ignoreHitEffects)
// oraz odpowiednie warunki w Player.cs i vHealthController.cs.
vDamage damageInstance = new vDamage(damageAmount, true); vDamage damageInstance = new vDamage(damageAmount, true);
// Jeœli vDamage(int, bool) nie istnieje, a vDamage.cs jest zmodyfikowane:
// vDamage damageInstance = new vDamage(damageAmount);
// damageInstance.ignoreHitEffects = true;
healthController.TakeDamage(damageInstance); healthController.TakeDamage(damageInstance);
} }
} }

View File

@@ -0,0 +1,41 @@
fileFormatVersion: 2
guid: 08f8ddcc9fadca44b88de3155aa75c5e
AudioImporter:
externalObjects: {}
serializedVersion: 8
defaultSettings:
serializedVersion: 2
loadType: 0
sampleRateSetting: 0
sampleRateOverride: 44100
compressionFormat: 0
quality: 1
conversionMode: 0
preloadAudioData: 0
platformSettingOverrides:
4:
serializedVersion: 2
loadType: 0
sampleRateSetting: 0
sampleRateOverride: 44100
compressionFormat: 0
quality: 1
conversionMode: 0
preloadAudioData: 0
7:
serializedVersion: 2
loadType: 0
sampleRateSetting: 0
sampleRateOverride: 44100
compressionFormat: 1
quality: 1
conversionMode: 0
preloadAudioData: 0
forceToMono: 0
normalize: 1
loadInBackground: 0
ambisonic: 0
3D: 1
userData:
assetBundleName:
assetBundleVariant: