Files
PacManUdon/Assets/Scripts/BonusFruit.cs
2025-12-26 18:14:48 +01:00

118 lines
3.3 KiB
C#

namespace Marro.PacManUdon
{
using UdonSharp;
using UnityEngine;
using VRC.SDK3.Data;
using VRC.SDKBase;
using VRC.Udon;
public class BonusFruit : UdonSharpBehaviour
{
[SerializeField, UdonSynced, FieldChangeCallback(nameof(FruitType))] PacManFruitType fruitType;
Animator animator;
new Renderer renderer;
new Collider collider;
ScoreBonusDisplay scoreBonusDisplay;
[UdonSynced] bool active;
private int value;
private float activeCountdown;
private bool frozen;
public void Initialize()
{
animator = GetComponent<Animator>();
renderer = GetComponent<Renderer>();
collider = GetComponent<Collider>();
scoreBonusDisplay = transform.Find("ScoreBonusDisplay").gameObject.GetComponent<ScoreBonusDisplay>();
scoreBonusDisplay.Initialize();
SetActive(false);
}
void Update()
{
if (active && !frozen)
{
activeCountdown -= Time.deltaTime;
if (activeCountdown <= 0)
{
SetActive(false);
}
}
}
public void Spawn()
{
// Debug.Log($"{gameObject} Spawned");
SetActive(true);
activeCountdown = Random.Range(9, 10);
}
public void Despawn()
{
// Debug.Log($"{gameObject} Despawned");
SetActive(false);
}
public int Collected()
{
SetActive(false);
scoreBonusDisplay.DisplayTemporarily(value, 1);
return value;
}
public void SetFruitType(PacManFruitType fruitType)
{
this.fruitType = fruitType;
value = (int)fruitScoreValue[PacManConstants.FruitTypeToValue(fruitType)];
animator.SetFloat("FruitType", PacManConstants.FruitTypeToValue(fruitType));
RequestSerialization();
}
public void SetFrozen(bool frozen)
{
this.frozen = frozen;
}
void SetActive(bool active)
{ // This replaces GameObject.active, as attempting to update an animator while a gameobject is inactive seems to result in a silent failure
renderer.enabled = active;
collider.enabled = active;
this.active = active;
RequestSerialization();
}
public PacManFruitType FruitType
{
set
{
SetFruitType(value);
}
get => fruitType;
}
public bool Active
{
set
{
SetActive(value);
}
get => active;
}
private DataDictionary fruitScoreValue = new DataDictionary()
{
{(int)PacManFruitType.None , 0},
{(int)PacManFruitType.Cherries , 100},
{(int)PacManFruitType.Strawberry, 300},
{(int)PacManFruitType.Peach , 500},
{(int)PacManFruitType.Apple , 700},
{(int)PacManFruitType.Grapes , 1000},
{(int)PacManFruitType.Galaxian , 2000},
{(int)PacManFruitType.Bell , 3000},
{(int)PacManFruitType.Key , 5000}
};
}
}