Skip to content

Commit

Permalink
Bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
gleblebedev committed Dec 4, 2023
1 parent cd4e3d8 commit 6d11b08
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 52 deletions.
Binary file added Content/Common/Data/Images/Victory.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
115 changes: 75 additions & 40 deletions RbfxTemplate/GameState.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System;
using System.Collections.Generic;
using System.IO;
using RbfxTemplate.GameStates;
using Urho3DNet;
using static System.Net.WebRequestMethods;

namespace RbfxTemplate
{
Expand All @@ -13,16 +13,17 @@ public sealed class GameState : RmlUIStateBase
private readonly SharedPtr<Scene> _scene;
private readonly UrhoPluginApplication _app;
private readonly Node _cameraNode;
private readonly Camera _camera;
private readonly Viewport _viewport;
private readonly PrefabResource _tilePrefab;

private readonly Dictionary<string, Material> _tileMaterials = new Dictionary<string, Material>();

private readonly PickSate _pickSate;
private readonly DragSate _dragSate;
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;
Expand Down Expand Up @@ -52,6 +53,9 @@ public sealed class GameState : RmlUIStateBase
};

private readonly PhysicsRaycastResult _raycastResult;
private readonly Material _baseMaterial;
private readonly Sprite _victorySprite;
private readonly FiniteTimeAction _victoryAction;

public GameState(UrhoPluginApplication app) : base(app, "UI/GameScreen.rml")
{
Expand All @@ -60,39 +64,42 @@ 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");

_cameraNode = _scene.Ptr.GetChild("Main Camera");
_viewport = Context.CreateObject<Viewport>();
_viewport.Camera = _cameraNode?.GetComponent<Camera>();
_camera = _cameraNode?.GetComponent<Camera>();
_viewport.Camera = _camera;
_viewport.Scene = _scene;
SetViewport(0, _viewport);
_scene.Ptr.IsUpdateEnabled = false;

var stringList = new StringList();
Context.VirtualFileSystem.Scan(stringList, new FileIdentifier("", "Images/Emoji"), "*.png",
ScanFlag.ScanFiles);
var baseMaterial = Context.ResourceCache.GetResource<Material>("Materials/TileMaterial.material");
foreach (var imageName in stringList)
{
var m = baseMaterial.Clone();
var tex = Context.ResourceCache.GetResource<Texture2D>("Images/Emoji/" + imageName);
if (tex == null)
throw new ExecutionEngineException();
m.SetTexture("Albedo", tex);
_tileMaterials[Path.GetFileNameWithoutExtension(imageName)] = m;
}
_baseMaterial = Context.ResourceCache.GetResource<Material>("Materials/TileMaterial.material");

_tilePrefab = Context.ResourceCache.GetResource<PrefabResource>("Objects/Tile.prefab");

var pointer =
_scene.Ptr.InstantiatePrefab(
Context.ResourceCache.GetResource<PrefabResource>("Objects/Pointer.prefab"));

_pickSate = new PickSate(this, pointer);
_dragSate = new DragSate(this);
_pickState = new PickState(this, pointer);
_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();

Expand Down Expand Up @@ -139,6 +146,10 @@ public override void Activate(StringVariantMap bundle)
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);
}

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

Tile t1, t2;
{
var tile = _scene.Ptr.InstantiatePrefab(_tilePrefab);
var model = tile.GetComponent<StaticModel>(true);
model.SetMaterial(_tileMaterials[pair.Item1]);
leftTiles.Add(tile);
t1 = tile.GetComponent<Tile>(true);
}
Tile t1 = CreateTile(pair.Item1);
Tile t2 = CreateTile(pair.Item2);

if (_random.Next(2) == 0)
{
var tile = _scene.Ptr.InstantiatePrefab(_tilePrefab);
var model = tile.GetComponent<StaticModel>(true);
model.SetMaterial(_tileMaterials[pair.Item2]);
rightTiles.Add(tile);
t2 = tile.GetComponent<Tile>(true);
(t1, t2) = (t2, t1);
}

leftTiles.Add(t1.Node);
rightTiles.Add(t2.Node);
t1.ValidLink = t2;
t2.ValidLink = t1;

Expand All @@ -205,6 +211,20 @@ public void AddPairs(int numTiles)
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);
}

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

var tile = _scene.Ptr.InstantiatePrefab(_tilePrefab);
var model = tile.GetComponent<StaticModel>(true);
model.SetMaterial(m);
return tile.GetComponent<Tile>(true);
}

public Ray GetScreenRay(IntVector2 inputMousePosition)
Expand All @@ -225,26 +245,39 @@ public Tile PickTile(IntVector2 inputMousePosition)

public void DragTile(Tile tile, int? touchId)
{
_dragSate.TrackTile(tile, touchId);
State = _dragSate;
_dragState.TrackTile(tile, touchId);
State = _dragState;
}

public void StartPicking()
{
State = _pickSate;
State = _pickState;
foreach (var tile in _currentTiles)
if (tile.LinkedTile != tile.ValidLink)
{
if (_hintsLeft > 0)
{
_pickSate.ShowHint(tile);
_pickState.ShowHint(tile);
--_hintsLeft;
}

return;
}

NextLevel();
Victory();
}

private void Victory()
{
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);
}

protected override void Dispose(bool disposing)
Expand Down Expand Up @@ -324,8 +357,10 @@ private void HandleKeyUp(VariantMap args)
}
}

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

++_levelIndex;
RmlUiComponent.UpdateProperties();
foreach (var tile in _currentTiles) tile.Node.Remove();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

namespace RbfxTemplate.GameStates
{
public class DragSate : StateBase
public class DragState : StateBase
{
private Tile _tile;
private int? _touchId;

public DragSate(GameState game) : base(game)
public DragState(GameState game) : base(game)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

namespace RbfxTemplate.GameStates
{
public class PickSate: StateBase
public class PickState: StateBase
{
private readonly Node _pointer;
private Tile _hintTile = null;
private float _hintTime;

public PickSate(GameState game, Node pointer) : base(game)
public PickState(GameState game, Node pointer) : base(game)
{
_pointer = pointer;
_pointer.SetDeepEnabled(false);
Expand Down
23 changes: 23 additions & 0 deletions RbfxTemplate/GameStates/VictoryState.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Urho3DNet;

namespace RbfxTemplate.GameStates
{
public class VictoryState : StateBase
{
public VictoryState(GameState game) : base(game)
{
}

public override void HandleMouseUp(int button, IntVector2 inputMousePosition, int buttons, int qualifiers)
{
base.HandleMouseUp(button, inputMousePosition, buttons, qualifiers);
//Game.NextLevel();
}

public override void HandleTouchEnd(int touchId, IntVector2 intVector2)
{
base.HandleTouchEnd(touchId, intVector2);
//Game.NextLevel();
}
}
}
14 changes: 6 additions & 8 deletions RbfxTemplate/UrhoPluginApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public UrhoPluginApplication(Context context) : base(context)
{
}


/// <summary>
/// Gets a value indicating whether the game is running.
/// </summary>
Expand Down Expand Up @@ -67,13 +66,12 @@ protected override void Start(bool isMain)
stateManager.FadeInDuration = 0.1f;
stateManager.FadeOutDuration = 0.1f;

StringList stringList = new StringList();
Context.VirtualFileSystem.Scan(stringList, new FileIdentifier("", "Images/Emoji"), "*.png", ScanFlag.ScanFiles);
foreach (var imageName in stringList)
{
Context.ResourceCache.BackgroundLoadResource(nameof(Texture2D), "Images/Emoji/" + imageName);
}

//StringList stringList = new StringList();
//Context.VirtualFileSystem.Scan(stringList, new FileIdentifier("", "Images/Emoji"), "*.png", ScanFlag.ScanFiles);
//foreach (var imageName in stringList)
//{
// Context.ResourceCache.BackgroundLoadResource(nameof(Texture2D), "Images/Emoji/" + imageName);
//}

// Setup end enqueue splash screen.
using (SharedPtr<SplashScreen> splash = new SplashScreen(Context))
Expand Down

0 comments on commit 6d11b08

Please sign in to comment.