A component that listens for trigger enters to mark level completion. When something enters the trigger it records the player's score, enables win particles, plays a win sound, applies a force and increased gravity to a rigidbody, sets an animation parameter, then initiates loading the next scene after a delay.
using Sandbox;
using static Sandbox.Resources.ResourceGenerator;
public sealed class TriggerWin : Component, Component.ITriggerListener
{
[Property] public SkinnedModelRenderer model { get; set; }
[Property] public Rigidbody rb { get; set; }
[Property] public SoundEvent win { get; set; }
[Property] public static int CurrentLevelIndex = 1;
[Property] public ParticleSphereEmitter winparticles { get; set; }
[Property] static Scene ActiveScene { get; set; }
[Property] public SceneFile nextscene { get; set; }
[Property] int nextLevelDelay = 3;
[Property] public ChickenControls chicken { get; set; }
async void ITriggerListener.OnTriggerEnter( Collider other )
{
ScoreManager.CurrentScore = chicken.cornpoints;
winparticles.Enabled = true;
model.Set( "AnimPar", 6 );
rb.ApplyForce( Vector3.Backward * 50000 );
GameObject.PlaySound( win );
await Task.Delay( 500 );
rb.GravityScale = 125;
LoadNextLevel();
}
void ITriggerListener.OnTriggerExit( Collider other )
{
}
async void DelayedMethod()
{
// Wait for 2 seconds
await Task.Delay( nextLevelDelay );
// Code to run after delay
var options = new SceneLoadOptions();
options.SetScene( nextscene);
Scene.Load( options );
}
void LoadNextLevel()
{
DelayedMethod();
}
}