File size: 2,121 Bytes
ac55997
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Quantum;
using Photon.Deterministic;

public unsafe class InputHandler : MonoBehaviour
{

  public int InitialIndex;
  public int TargetIndex;
  public bool HasInitial;
  public bool DebugMoves = false;
  public MovesIndicatorManager MovesIndicator;
  public GameObject SelectedPieceIndicator;

  public UnityEngine.LayerMask BoardsRaycastMask;

  private void Update()
  {
    HandleClick();

    if (HasInitial)
    {
      ChessViewUpdater.Instance.SetObjectByIndex(SelectedPieceIndicator, InitialIndex);
    }
    else {
      SelectedPieceIndicator.SetActive(false);
    }
  }

  public void HandleClick()
  {
    if (UnityEngine.Input.GetMouseButtonDown(0))
    {
      // Perform the Unity raycast
      Ray ray = Camera.main.ScreenPointToRay(UnityEngine.Input.mousePosition);
      RaycastHit hit;
      Physics.Raycast(ray, out hit, 100, BoardsRaycastMask);

      // If the raycast hit the Board...
      if (hit.collider != null)
      {
        var position = new FPVector2(FP.FromFloat_UNSAFE(hit.point.x), FP.FromFloat_UNSAFE(hit.point.z));
        if (HasInitial == false || DebugMoves)
        {
          InitialIndex = BoardHelper.GetIndexByPosition(position);
          HasInitial = true;
          MovesIndicator.UpdatePossibleMovements(InitialIndex);
        }
        else
        {
          TargetIndex = BoardHelper.GetIndexByPosition(position);
          Frame f = QuantumRunner.Default.Game.Frames.Verified;
          if (MoveValidatorHelper.IsValidMove(ref f.Global->Board, InitialIndex, TargetIndex, true) == false)
          {
            InitialIndex = TargetIndex;
            MovesIndicator.UpdatePossibleMovements(InitialIndex);
          }
          else
          {
            HasInitial = false;
            //sendcommand
            var c = new MoveCommand();
            c.Data.InitialIndex = InitialIndex;
            c.Data.TargetIndex = TargetIndex;
            QuantumRunner.Default.Game.SendCommand(c);
            MovesIndicator.ResetPrefabs();
          }
        }
      }
    }
  }
}