Skip to content

Commit

Permalink
Victory dialog window
Browse files Browse the repository at this point in the history
  • Loading branch information
gleblebedev committed Dec 4, 2023
1 parent 6d11b08 commit 4f53423
Show file tree
Hide file tree
Showing 10 changed files with 244 additions and 215 deletions.
23 changes: 23 additions & 0 deletions Content/Common/Data/Materials/Red.material
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0"?>
<material>
<technique name="Techniques/UnlitOpaque.xml" quality="0" loddistance="0.000000" />
<parameter name="MatEnvMapColor" value="1 1 1" />
<parameter name="NormalScale" value="1.000000" />
<parameter name="MatDiffColor" value="1 0 0 1" />
<parameter name="VOffset" value="0 1 0 0" />
<parameter name="DielectricReflectance" value="0.500000" />
<parameter name="Metallic" value="1.000000" />
<parameter name="MatSpecColor" value="0 0 0 1" />
<parameter name="MatEmissiveColor" value="0 0 0" />
<parameter name="UOffset" value="1 0 0 0" />
<parameter name="AlphaCutoff" value="0.500000" />
<parameter name="Roughness" value="1.000000" />
<cull value="ccw" />
<shadowcull value="ccw" />
<fill value="solid" />
<depthbias constant="0.000000" slopescaled="0.000000" normaloffset="0.000000" />
<alphatocoverage enable="false" />
<lineantialias enable="false" />
<renderorder value="128" />
<occlusion enable="true" />
</material>
13 changes: 11 additions & 2 deletions Content/Common/Data/UI/GameScreen.rml
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,24 @@
<body data-model="GameRmlUIComponent" style="width: 100%; height: 100%; margin: 0; nav: auto">
<div class="gray-header-panel menu-panel" style="top: 0%; left: 50%; position: absolute; transform: translate(-50%, 0); width: 100%; ">
<div style="float: left">
<!--<input type="button" class="blue-button" style="display: block">-->
<input type="button" class="blue-button" style="display: block" data-event-click="Settings">
<img sprite="icon-settings"></img>
<!--</input>-->
</input>
</div>
<div style="float: right">
<img sprite="icon-diamond"></img> 0
</div>
<div style="text-align: center">{{Level}}</div>
</div>

<div data-visible="Victory" class="" style="background: #000000A0; padding:10px; top: 50%; left: 50%; position: absolute; transform: translate(-50%, -50%); width: 50%; max-height: 90%">
<div style="text-align: center">Level complete!</div>
<img src="../Images/Victory.png" style="margin: 0 auto; max-width: 33%"/>
<input type="button" class="blue-button" style="display: block" data-event-click="Next">
Next
</input>
</div>

<div class="gray-footer-panel menu-panel" style="top: 100%; left: 50%; position: absolute; transform: translate(-50%, -100%); width: 100%; ">
<!--<input type="button" class="blue-button" style="display: block" data-event-click="Exit">
Hint
Expand Down
3 changes: 1 addition & 2 deletions RbfxTemplate/GameRmlUIComponent.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using Urho3DNet;
using Urho3DNet;

namespace RbfxTemplate
{
Expand Down
137 changes: 73 additions & 64 deletions RbfxTemplate/GameState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Collections.Generic;
using RbfxTemplate.GameStates;
using Urho3DNet;
using static System.Net.WebRequestMethods;

namespace RbfxTemplate
{
Expand All @@ -20,13 +19,8 @@ public sealed class GameState : RmlUIStateBase
private readonly PickState _pickState;
private readonly DragState _dragState;
private readonly VictoryState _victoryState;
private StateBase _state;
private readonly Random _random = new Random();
private readonly List<Tile> _currentTiles = new List<Tile>();
private Vector2 _areaSize = new Vector2(1,1);

private int _levelIndex;
private int _hintsLeft = 1;

private readonly Tuple<string, string>[] _pairs =
{
Expand Down Expand Up @@ -54,8 +48,12 @@ public sealed class GameState : RmlUIStateBase

private readonly PhysicsRaycastResult _raycastResult;
private readonly Material _baseMaterial;
private readonly Sprite _victorySprite;
private readonly FiniteTimeAction _victoryAction;
private StateBase _state;
private Vector2 _areaSize = new Vector2(1, 1);

private int _levelIndex;
private int _hintsLeft = 1;
private bool _victory;

public GameState(UrhoPluginApplication app) : base(app, "UI/GameScreen.rml")
{
Expand All @@ -64,13 +62,6 @@ public GameState(UrhoPluginApplication app) : base(app, "UI/GameScreen.rml")

_raycastResult = new PhysicsRaycastResult();

_victorySprite = new Sprite(Context);
var victoryTexture = Context.ResourceCache.GetResource<Texture2D>("Images/Victory.png");
_victorySprite.Texture = victoryTexture;
_victorySprite.IsVisible = false;
_victorySprite.HotSpot = new IntVector2(victoryTexture.Size.X / 2, victoryTexture.Size.Y / 2);
UIRoot.AddChild(_victorySprite);

_app = app;
_scene = Context.CreateObject<Scene>();
_scene.Ptr.LoadXML("Scenes/Scene.scene");
Expand All @@ -95,13 +86,7 @@ public GameState(UrhoPluginApplication app) : base(app, "UI/GameScreen.rml")
_dragState = new DragState(this);
_victoryState = new VictoryState(this);

_victoryAction = new ActionBuilder(Context)
.ScaleBy(1.0f, new Vector2(10.0f, 10.0f)).ElasticInOut()
.DelayTime(0.5f).CallFunc(_=>NextLevel())
.Then(new ActionBuilder(Context).ScaleBy(1.0f, new Vector2(0.1f, 0.1f)).ElasticInOut().Build())
.Hide().Build();

NextLevel();
NextLevel(null);

Deactivate();
}
Expand All @@ -123,6 +108,14 @@ public StateBase State
public override void OnDataModelInitialized(GameRmlUIComponent component)
{
component.BindDataModelProperty("Level", _ => _.Set("Level " + _levelIndex), _ => { });
component.BindDataModelProperty("Victory", _ => _.Set(_victory), _ => { });
component.BindDataModelEvent("Next", NextLevel);
component.BindDataModelEvent("Settings", Settings);
}

private void Settings(VariantList obj)
{
_app.HandleBackKey();
}

public override void Activate(StringVariantMap bundle)
Expand All @@ -148,8 +141,8 @@ public override void Update(float timeStep)
_state.Update(timeStep);

var orthoSizeY = _areaSize.Y;
var orthoSizeX = _areaSize.X * (float)UI.Size.Y / (float)UI.Size.X;
_camera.OrthoSize = Math.Max(orthoSizeX, orthoSizeY);
var orthoSizeX = _areaSize.X * UI.Size.Y / UI.Size.X;
_camera.OrthoSize = Math.Max(orthoSizeX, orthoSizeY);
}

public override void Deactivate()
Expand Down Expand Up @@ -183,13 +176,10 @@ public void AddPairs(int numTiles)
visited.Add(pair.Item1);
visited.Add(pair.Item2);

Tile t1 = CreateTile(pair.Item1);
Tile t2 = CreateTile(pair.Item2);
var t1 = CreateTile(pair.Item1);
var t2 = CreateTile(pair.Item2);

if (_random.Next(2) == 0)
{
(t1, t2) = (t2, t1);
}
if (_random.Next(2) == 0) (t1, t2) = (t2, t1);

leftTiles.Add(t1.Node);
rightTiles.Add(t2.Node);
Expand All @@ -205,20 +195,20 @@ public void AddPairs(int numTiles)

var d = 1.5f;

var pos = new Vector3(0, 0, (-leftTiles.Count * 0.5f+0.5f)* d);
var pos = new Vector3(0, 0, (-leftTiles.Count * 0.5f + 0.5f) * d);
for (var index = 0; index < leftTiles.Count; index++)
{
leftTiles[index].Position = pos + new Vector3(-d, 0, index*d);
rightTiles[index].Position = pos + new Vector3(d, 0, index*d);
leftTiles[index].Position = pos + new Vector3(-d, 0, index * d);
rightTiles[index].Position = pos + new Vector3(d, 0, index * d);
}

_areaSize = new Vector2(5, (leftTiles.Count+1)*d);
_areaSize = new Vector2(5, (leftTiles.Count + 1) * d);
}

public Tile CreateTile(string imageName)
{
var m = _baseMaterial.Clone();
var tex = Context.ResourceCache.GetResource<Texture2D>("Images/Emoji/" + imageName+".png");
var tex = Context.ResourceCache.GetResource<Texture2D>("Images/Emoji/" + imageName + ".png");
m.SetTexture("Albedo", tex);

var tile = _scene.Ptr.InstantiatePrefab(_tilePrefab);
Expand Down Expand Up @@ -252,32 +242,57 @@ public void DragTile(Tile tile, int? touchId)
public void StartPicking()
{
State = _pickState;

var isComplete = true;
var isCorrect = true;
Tile hint = null;

foreach (var tile in _currentTiles)
{
if (tile.LinkedTile == null) isComplete = false;
if (tile.LinkedTile != tile.ValidLink)
{
if (_hintsLeft > 0)
{
_pickState.ShowHint(tile);
--_hintsLeft;
}
isCorrect = false;
hint = tile;
}
}

if (isComplete)
{
if (isCorrect)
{
Victory();
}
else
{
var material = Context.ResourceCache.GetResource<Material>("Materials/Red.material");
foreach (var tile in _currentTiles)
if (tile.LinkedTile != tile.ValidLink)
tile.Link.GetComponent<AnimatedModel>().SetMaterial(material);

return;
}
}

Victory();
if (_hintsLeft > 0 && hint != null)
{
_pickState.ShowHint(hint);
--_hintsLeft;
}
}

private void Victory()
public void NextLevel(VariantList args)
{
State = _victoryState;
var size = UI.Size;
var s = Math.Min(size.X, size.Y) * 0.9f;
var imgSize = new Vector2(s, s);
_victorySprite.Position = size.ToVector2() *0.5f;
_victorySprite.Size = imgSize.ToIntVector2();
_victorySprite.IsVisible = true;
_victorySprite.Scale = new Vector2(0.1f, 0.1f);
ActionManager.AddAction(_victoryAction, _victorySprite);
_victory = false;
RmlUiComponent.UpdateProperties();

++_levelIndex;
RmlUiComponent.UpdateProperties();
foreach (var tile in _currentTiles) tile.Node.Remove();

AddPairs(Math.Min(4, _levelIndex));

StartPicking();
}

protected override void Dispose(bool disposing)
Expand All @@ -287,6 +302,13 @@ protected override void Dispose(bool disposing)
base.Dispose(disposing);
}

private void Victory()
{
State = _victoryState;
_victory = true;
RmlUiComponent.UpdateProperties();
}

private void Randomize(List<Node> rightTiles)
{
for (var index = 0; index < rightTiles.Count; index++)
Expand Down Expand Up @@ -356,18 +378,5 @@ private void HandleKeyUp(VariantMap args)
return;
}
}

public void NextLevel()
{
//_victorySprite.IsVisible = false;

++_levelIndex;
RmlUiComponent.UpdateProperties();
foreach (var tile in _currentTiles) tile.Node.Remove();

AddPairs(Math.Min(4, _levelIndex));

StartPicking();
}
}
}
60 changes: 31 additions & 29 deletions RbfxTemplate/GameStates/DragState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,6 @@ public DragState(GameState game) : base(game)
{
}

public void TrackTile(Tile tile, int? touchId)
{
_tile = tile;
_touchId = touchId;
tile.LinkTo(null);
}

public override void Activate()
{
_tile.Link.IsEnabled = true;
Expand All @@ -31,48 +24,57 @@ public override void Deactivate()

public override void HandleMouseMove(IntVector2 intVector2, int buttons, int qualifiers)
{
if (_touchId.HasValue) { return; }
if (_touchId.HasValue) return;

MoveLink(intVector2);
}

private void MoveLink(IntVector2 intVector2)
{
var ray = Game.GetScreenRay(intVector2);
var normal = Vector3.Up;
var d = normal.DotProduct(ray.Direction);
float t = -(normal.DotProduct(ray.Origin) - _tile.Link.Position.Y) / d;
_tile.LinkTarget.WorldPosition = ray.Origin + ray.Direction * t;
_tile.LinkSource.LookAt(_tile.LinkTarget.WorldPosition);
_tile.LinkTarget.LookAt(_tile.LinkSource.WorldPosition);
}

public override void HandleMouseUp(int button, IntVector2 inputMousePosition, int buttons, int qualifiers)
{
if (_touchId.HasValue) { return; }
if (_touchId.HasValue) return;

CompleteLink(inputMousePosition);
}

private void CompleteLink(IntVector2 inputMousePosition)
{
var link = Game.PickTile(inputMousePosition);
_tile.LinkTo(link);
Game.StartPicking();
}

public override void HandleTouchMove(int touchId, IntVector2 intVector2)
{
if (_touchId != touchId) { return; }
if (_touchId != touchId) return;

MoveLink(intVector2);
}

public override void HandleTouchEnd(int touchId, IntVector2 intVector2)
{
if (_touchId != touchId) { return; }
if (_touchId != touchId) return;

CompleteLink(intVector2);
}

public void TrackTile(Tile tile, int? touchId)
{
_tile = tile;
_touchId = touchId;
tile.LinkTo(null);
var material = Context.ResourceCache.GetResource<Material>("Materials/White.material");
tile.Link.GetComponent<AnimatedModel>(true).SetMaterial(material);
}

private void MoveLink(IntVector2 intVector2)
{
var ray = Game.GetScreenRay(intVector2);
var normal = Vector3.Up;
var d = normal.DotProduct(ray.Direction);
var t = -(normal.DotProduct(ray.Origin) - _tile.Link.Position.Y) / d;
_tile.LinkTarget.WorldPosition = ray.Origin + ray.Direction * t;
_tile.LinkSource.LookAt(_tile.LinkTarget.WorldPosition);
_tile.LinkTarget.LookAt(_tile.LinkSource.WorldPosition);
}

private void CompleteLink(IntVector2 inputMousePosition)
{
var link = Game.PickTile(inputMousePosition);
_tile.LinkTo(link);
Game.StartPicking();
}
}
}
Loading

0 comments on commit 4f53423

Please sign in to comment.