Skip to content

Commit

Permalink
Loading screen when starting new game
Browse files Browse the repository at this point in the history
  • Loading branch information
gleblebedev committed Sep 9, 2024
1 parent df522f0 commit 5f2424c
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 32 deletions.
62 changes: 38 additions & 24 deletions RbfxTemplate/GameState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,21 @@ namespace RbfxTemplate
[ObjectFactory]
public partial class GameState : RmlUIStateBase
{
protected readonly SharedPtr<Scene> _scene;
protected readonly SharedPtr<Sprite> _cross;
private readonly Node _cameraNode;
private readonly Viewport _viewport;
private readonly Node _character;
private readonly Node _cameraRoot;
private readonly Player _player;
private readonly string _sceneResourceName = "Scenes/Sample.xml";

protected SharedPtr<Scene> _scene;
protected SharedPtr<Sprite> _cross;
private Node _cameraNode;
private Viewport _viewport;
private Node _character;
private Node _cameraRoot;
private Player _player;

private string _interactableTooltip = string.Empty;
private bool _interactionEnabled = false;
private bool _hasInteractable = false;
private float _interactionProgress;
private readonly InputMap _inputMap;
private InputMap _inputMap;
private bool _inTransition = false;

public Player Player => _player;
Expand Down Expand Up @@ -51,10 +53,37 @@ public GameState(UrhoPluginApplication app) : base(app, "UI/GameUI.rml")
MouseMode = MouseMode.MmRelative;
IsMouseVisible = false;

app.QueueSceneResourcesAsync(_sceneResourceName);
}

public override void OnDataModelInitialized(GameRmlUIComponent menuComponent)
{
menuComponent.BindDataModelProperty(nameof(InteractableTooltip), _ => _.Set(_interactableTooltip), _ => { });
menuComponent.BindDataModelProperty(nameof(InteractionEnabled), _ => _.Set(_interactionEnabled), _ => { });
menuComponent.BindDataModelProperty(nameof(HasInteractable), _ => _.Set(_hasInteractable), _ => { });
menuComponent.BindDataModelProperty(nameof(InteractionProgress), _ => _.Set(_interactionProgress), _ => { });
}

public override void Activate(StringVariantMap bundle)
{
EnsureScene();

Application.Settings.Apply(_scene.Ptr.GetComponent<RenderPipeline>());
Application.Settings.Apply(Context);
base.Activate(bundle);
}

private void EnsureScene()
{
if (_scene)
{
return;
}

_inputMap = Context.ResourceCache.GetResource<InputMap>("Input/MoveAndOrbit.inputmap");

_scene = Context.CreateObject<Scene>();
_scene.Ptr.LoadXML("Scenes/Sample.xml");
_scene.Ptr.LoadXML(_sceneResourceName);

var nodeList = _scene.Ptr.GetChildrenWithComponent(nameof(KinematicCharacterController), true);
foreach (var node in nodeList)
Expand Down Expand Up @@ -106,21 +135,6 @@ public GameState(UrhoPluginApplication app) : base(app, "UI/GameUI.rml")
UIRoot.AddChild(_cross);
}

public override void OnDataModelInitialized(GameRmlUIComponent menuComponent)
{
menuComponent.BindDataModelProperty(nameof(InteractableTooltip), _ => _.Set(_interactableTooltip), _ => { });
menuComponent.BindDataModelProperty(nameof(InteractionEnabled), _ => _.Set(_interactionEnabled), _ => { });
menuComponent.BindDataModelProperty(nameof(HasInteractable), _ => _.Set(_hasInteractable), _ => { });
menuComponent.BindDataModelProperty(nameof(InteractionProgress), _ => _.Set(_interactionProgress), _ => { });
}

public override void Activate(StringVariantMap bundle)
{
Application.Settings.Apply(_scene.Ptr.GetComponent<RenderPipeline>());
Application.Settings.Apply(Context);
base.Activate(bundle);
}

public override void TransitionStarted()
{
_inTransition = true;
Expand Down
29 changes: 21 additions & 8 deletions RbfxTemplate/UrhoPluginApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ namespace RbfxTemplate
[LoadablePlugin]
public partial class UrhoPluginApplication : PluginApplication
{
/// <summary>
/// Safe pointer to splash screen.
/// </summary>
private SharedPtr<SplashScreen> _splashScreen;

/// <summary>
/// Safe pointer to game screen.
/// </summary>
Expand Down Expand Up @@ -96,13 +101,11 @@ protected override void Start(bool isMain)
stateManager.FadeOutDuration = 0.1f;

// Setup end enqueue splash screen.
using (SharedPtr<SplashScreen> splash = new SplashScreen(Context))
{
splash.Ptr.Duration = 1.0f;
splash.Ptr.BackgroundImage = Context.ResourceCache.GetResource<Texture2D>("Images/Background.png");
splash.Ptr.ForegroundImage = Context.ResourceCache.GetResource<Texture2D>("Images/Splash.png");
stateManager.EnqueueState(splash);
}
_splashScreen = new SplashScreen(Context);
_splashScreen.Ptr.Duration = 1.0f;
_splashScreen.Ptr.BackgroundImage = Context.ResourceCache.GetResource<Texture2D>("Images/Background.png");
_splashScreen.Ptr.ForegroundImage = Context.ResourceCache.GetResource<Texture2D>("Images/Splash.png");
stateManager.EnqueueState(_splashScreen);

// Crate end enqueue main menu screen.
_stateStack.Push(_mainMenuState);
Expand All @@ -128,14 +131,24 @@ public void ToSettings()
_stateStack.Push(_settingsMenuState);
}

/// <summary>
/// Queue all resources from the given scene to be loaded in background.
/// </summary>
/// <param name="sceneName"></param>
public void QueueSceneResourcesAsync(string sceneName)
{
_splashScreen.Ptr.QueueSceneResourcesAsync(sceneName);
}

/// <summary>
/// Transition to game
/// </summary>
public void ToNewGame()
{
_gameState?.Dispose();
_gameState = new GameState(this);
_stateStack.Push(_gameState);
_stateStack.Push(_splashScreen);
_stateStack.Switch(_gameState);
}

/// <summary>
Expand Down

0 comments on commit 5f2424c

Please sign in to comment.