-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChessPiece.s3d
163 lines (145 loc) · 2.99 KB
/
ChessPiece.s3d
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#ifndef _CHESSPIECE
#define _CHESSPIECE
#define CP_PAWN 0
#define CP_TOWER 1
#define CP_HORSE 2
#define CP_BISHOP 3
#define CP_QUEEN 4
#define CP_KING 5
#define CP_W 0
#define CP_B 1
#include "ChessSet.s3d"
function AHto64(c, i)
{
var v;
switch (upper(c))
{
case "A":
v = 0;
break;
case "B":
v = 1;
break;
case "C":
v = 2;
break;
case "D":
v = 3;
break;
case "E":
v = 4;
break;
case "F":
v = 5;
break;
case "G":
v = 6;
break;
case "H":
v =7;
break;
}
return (i-1)* 8 + v;
}
class ChessPiece
{
var type;
var position;
var color;
var obj;
var material;
var myDim;
var isDropping;
var moved; //for castling;
Draw(flag);
SetXYPos(c, d);
CalcXYPosition(newPos, c, d);
GetOffSet(d);
Promote();
IsMoved();
};
function ChessPiece::ChessPiece(t, pos, c, mat, mirror)
{
type = t;
position = pos;
color = c;
material = mat;
isDropping = false;
moved = 0;
switch (t) {
case CP_PAWN:
obj = createPawn(mat);
myDim = 2;
break;
case CP_TOWER:
obj = createTower(mat);
myDim = 3.0;
break;
case CP_HORSE:
obj = createHorse(mirror, mat);
myDim = 3.0;
break;
case CP_BISHOP:
obj = createBishop(mat);
myDim = 3.0;
break;
case CP_QUEEN:
obj = createQueen(mat);
myDim = 2;
break;
case CP_KING:
obj = createKing(mat);
myDim = 2;
break;
}
}
function ChessPiece::Draw(flag)
{
if(flag == NULL)
obj.Draw();
else
obj.Draw(flag);
}
function ChessPiece::SetXYPos(c, d)
{
var step = d/8;
var offset = (step - myDim) / 2;
var posX = Int(position / 8) * step + offset + c[0];
var posY = c[1];
var posZ = (position % 8) * step + offset + c[2];
obj.SetPosition(posX, posY, posZ);
}
function ChessPiece::CalcXYPosition(newPos, c, d)
{
var step = d/8;
var offset = (step - myDim) / 2;
var posX = Int(newPos / 8) * step + offset + c[0];
var posY = c[1];
var posZ = (newPos % 8) * step + offset + c[2];
return [posX, posY, posZ];
}
function ChessPiece::GetOffSet(d)
{
var step = d/8;
var offset = (step - myDim) / 2;
return offset;
}
//TODO: update in time the info for StockFish
function ChessPiece::Promote()
{
if(type != CP_PAWN || position == -1)
return;
if((color == CP_W && position > 55) || (color == CP_B && position < 8))
{
var pos = obj.GetPosition();
obj = createQueen(material);
obj.SetPosition(pos);
myDim = 2;
type = CP_QUEEN;
}
}
function ChessPiece::IsMoved()
{
return moved > 0;
}
#endif