Implement debug switching
This commit is contained in:
@@ -156,6 +156,52 @@ namespace Services {
|
||||
return true;
|
||||
}
|
||||
|
||||
public async UniTask<bool> TrySwitch(Vector2Int position) {
|
||||
Gem gem = this.gameBoard.GetGemAt(position);
|
||||
if(gem == null)
|
||||
return false;
|
||||
|
||||
GemType[] normalTypes = Enum.GetValues(typeof(GemType))
|
||||
.Cast<GemType>()
|
||||
.Where(t => t != GemType.Bomb)
|
||||
.ToArray();
|
||||
|
||||
if (normalTypes.Length == 0)
|
||||
return false;
|
||||
|
||||
bool nextIsBomb;
|
||||
GemType nextTypeOrMatchColor;
|
||||
|
||||
if (gem.Type != GemType.Bomb) {
|
||||
int index = Array.IndexOf(normalTypes, gem.Type);
|
||||
if (index < 0) index = 0;
|
||||
|
||||
if (index < normalTypes.Length - 1) {
|
||||
nextIsBomb = false;
|
||||
nextTypeOrMatchColor = normalTypes[index + 1];
|
||||
} else {
|
||||
nextIsBomb = true;
|
||||
nextTypeOrMatchColor = normalTypes[0];
|
||||
}
|
||||
} else {
|
||||
int idx = Array.IndexOf(normalTypes, gem.MatchColor);
|
||||
if (idx < 0) idx = 0;
|
||||
|
||||
if (idx < normalTypes.Length - 1) {
|
||||
nextIsBomb = true;
|
||||
nextTypeOrMatchColor = normalTypes[idx + 1];
|
||||
} else {
|
||||
nextIsBomb = false;
|
||||
nextTypeOrMatchColor = normalTypes[0];
|
||||
}
|
||||
}
|
||||
|
||||
// Replace both model+view by releasing current and respawning at same position.
|
||||
ReleaseMatchedGems(position);
|
||||
SpawnGemGameObject(SetGemAt(position, nextTypeOrMatchColor, nextIsBomb), nextIsBomb);
|
||||
return true;
|
||||
}
|
||||
|
||||
private void ApplySwap(Vector2Int from, Vector2Int to) {
|
||||
Gem fromGem = this.gameBoard.GetGemAt(from);
|
||||
Gem toGem = this.gameBoard.GetGemAt(to);
|
||||
|
||||
@@ -6,6 +6,7 @@ namespace Services {
|
||||
public class InputService : MonoBehaviour, IInputService
|
||||
{
|
||||
public event Action<Vector2Int, Vector2Int> OnSwapRequested;
|
||||
public event Action<Vector2Int> OnSwitchRequested;
|
||||
|
||||
private Camera inputCamera;
|
||||
private Vector2 pointerDownScreenPos;
|
||||
@@ -21,14 +22,14 @@ namespace Services {
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (TryGetPrimaryPointerState(out var isDown, out var screenPos))
|
||||
if (TryGetPrimaryPointerState(out bool isDown, out Vector2 screenPos))
|
||||
{
|
||||
if (!this.isPointerDown && isDown) {
|
||||
pointerDownScreenPos = screenPos;
|
||||
this.pointerDownScreenPos = screenPos;
|
||||
}
|
||||
|
||||
if (this.isPointerDown && !isDown) {
|
||||
TryEmitSwap(pointerDownScreenPos, screenPos);
|
||||
TryEmitSwap(this.pointerDownScreenPos, screenPos);
|
||||
}
|
||||
|
||||
this.isPointerDown = isDown;
|
||||
@@ -38,6 +39,12 @@ namespace Services {
|
||||
// No pointer available this frame (rare). Ensure we don't get stuck.
|
||||
this.isPointerDown = false;
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
if (TryGetSecondaryPointerClick(out screenPos)) {
|
||||
OnSwitchRequested?.Invoke(WorldToCell(this.inputCamera.ScreenToWorldPoint(screenPos)));
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
private void TryEmitSwap(Vector2 downScreen, Vector2 upScreen)
|
||||
@@ -105,5 +112,15 @@ namespace Services {
|
||||
isDown = Input.GetMouseButton(0);
|
||||
return true;
|
||||
}
|
||||
|
||||
private static bool TryGetSecondaryPointerClick(out Vector2 screenPosition) {
|
||||
if (Input.GetMouseButtonDown(1)) {
|
||||
screenPosition = Input.mousePosition;
|
||||
return true;
|
||||
}
|
||||
|
||||
screenPosition = Input.mousePosition;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,5 +6,6 @@ namespace Services.Interfaces {
|
||||
void Setup();
|
||||
|
||||
UniTask<bool> TrySwap(Vector2Int from, Vector2Int to);
|
||||
UniTask<bool> TrySwitch(Vector2Int position);
|
||||
}
|
||||
}
|
||||
@@ -4,5 +4,6 @@ using UnityEngine;
|
||||
namespace Services.Interfaces {
|
||||
public interface IInputService {
|
||||
event Action<Vector2Int, Vector2Int> OnSwapRequested;
|
||||
event Action<Vector2Int> OnSwitchRequested;
|
||||
}
|
||||
}
|
||||
@@ -25,10 +25,15 @@ namespace Services
|
||||
{
|
||||
this.gameBoardService.Setup();
|
||||
this.inputService.OnSwapRequested += HandleSwapRequest;
|
||||
this.inputService.OnSwitchRequested += HandleSwitchRequest;
|
||||
}
|
||||
|
||||
private void HandleSwapRequest(Vector2Int from, Vector2Int to) {
|
||||
this.gameBoardService.TrySwap(from, to);
|
||||
}
|
||||
|
||||
private void HandleSwitchRequest(Vector2Int position) {
|
||||
this.gameBoardService.TrySwitch(position);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user