Spaces:
Runtime error
Runtime error
File size: 4,253 Bytes
ce81a16 |
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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Quantum;
public unsafe class ChessViewUpdater : QuantumCallbacks
{
public static ChessViewUpdater Instance;
public PieceView[] Pieces = new PieceView[32];
public Vector3 PieceOffset = new Vector3(.5f, 0, .5f);
public GameObject LastMoveIdicatorInitial;
public GameObject LastMoveIdicatorTarget;
public Sprite WhiteQueenSprite;
public Sprite WhiteRookSprite;
public Sprite WhiteKnightSprite;
public Sprite WhiteBishopSprite;
//
public Sprite BlackQueenSprite;
public Sprite BlackRookSprite;
public Sprite BlackKnightSprite;
public Sprite BlackBishopSprite;
public DeadPiecesManager DeadPieces;
private bool _initialized = false;
public void Start()
{
if (Instance == null)
{
Instance = this;
}
QuantumEvent.Subscribe<EventChangePiecePosition>(this, UpdatePiece);
QuantumEvent.Subscribe<EventRemovePiece>(this, RemovePiece);
QuantumEvent.Subscribe<EventPiecePromotion>(this, PiecePromotion);
}
public override void OnUpdateView(QuantumGame game)
{
if (!_initialized)
{
_initialized = true;
var f = game.Frames.Verified;
for (int i = 0; i < f.Global->Board.Cells.Length; i++)
{
int index = GetPieceIndex(i);
if (index != -1)
{
var p = Pieces[index];
var position = BoardHelper.GetCordinatesByIndex(p.IndexOnBoard);
p.SetTargetPosition(new Vector3((float)position.X + PieceOffset.x, PieceOffset.x, (float)position.Y + PieceOffset.z));
}
}
}
}
public void PiecePromotion(EventPiecePromotion e)
{
var index = GetPieceIndex(e.Index);
Pieces[index].Type = e.NewType;
switch (e.NewType)
{
case PieceType.Bishop:
if (Pieces[index].Color == PieceColor.White)
Pieces[index].GetComponent<SpriteRenderer>().sprite = WhiteBishopSprite;
else
Pieces[index].GetComponent<SpriteRenderer>().sprite = BlackBishopSprite;
break;
case PieceType.Knight:
if (Pieces[index].Color == PieceColor.White)
Pieces[index].GetComponent<SpriteRenderer>().sprite = WhiteKnightSprite;
else
Pieces[index].GetComponent<SpriteRenderer>().sprite = BlackKnightSprite;
break;
case PieceType.Queen:
if (Pieces[index].Color == PieceColor.White)
Pieces[index].GetComponent<SpriteRenderer>().sprite = WhiteQueenSprite;
else
Pieces[index].GetComponent<SpriteRenderer>().sprite = BlackQueenSprite;
break;
case PieceType.Rook:
if (Pieces[index].Color == PieceColor.White)
Pieces[index].GetComponent<SpriteRenderer>().sprite = WhiteRookSprite;
else
Pieces[index].GetComponent<SpriteRenderer>().sprite = BlackRookSprite;
break;
}
}
public void UpdatePiece(EventChangePiecePosition e)
{
var origin = (int)e.Index.X;
var target = (int)e.Index.Y;
SetObjectByIndex(LastMoveIdicatorInitial, origin);
SetObjectByIndex(LastMoveIdicatorTarget, target);
int index = GetPieceIndex(origin);
if (index != -1)
{
var p = Pieces[index];
p.IndexOnBoard = target;
var position = BoardHelper.GetCordinatesByIndex(p.IndexOnBoard);
p.SetTargetPosition(new Vector3((float)position.X + PieceOffset.x, PieceOffset.x, (float)position.Y + PieceOffset.z));
}
}
public void SetObjectByIndex(GameObject go, int index)
{
var position = BoardHelper.GetCordinatesByIndex(index);
go.transform.position = new Vector3((float)position.X + PieceOffset.x, PieceOffset.y, (float)position.Y + PieceOffset.z);
go.SetActive(true);
}
public void RemovePiece(EventRemovePiece e)
{
var index = GetPieceIndex(e.Index);
DeadPieces.StoreDeadPiece(Pieces[index], e.Color);
Pieces[index].IndexOnBoard = -1;
}
public int GetPieceIndex(int index)
{
for (int i = 0; i < Pieces.Length; i++)
{
if (Pieces[i] == null)
{
continue;
}
if (Pieces[i].IndexOnBoard == index)
{
return i;
}
}
return -1;
}
public void OnDisable()
{
QuantumEvent.UnsubscribeListener(this);
}
}
|