-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
212 lines (195 loc) · 7.39 KB
/
index.html
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<!doctype html>
<html>
<head>
<link rel='stylesheet' href='./css/overlay.css'>
<style>
body{
background-color:black;
}
#container{
margin: auto;
display:flex;
width:1000px;
height:600px;
justify-content: center;
}
#overlay{
/* display: none; */
}
.top_left{
position:absolute;
top:0;
left:0;
padding:20px;
color:white;
font-size: 25px;
display:none;
}
.top_right{
position:absolute;
top:0;
right:0;
padding:20px;
color:white;
font-size: 25px;
display:none;
}
.bottom_center{
text-align: center;
font-size: 25px;
color: rgb(207, 207, 207);
position:absolute;
bottom: 0px;
padding:10px 0 20px 0;
width:99%;
background-color: black;
}
a{
color:blueviolet;
}
</style>
</head>
<body>
<div id='container'>
<canvas width = '800' height = '600' id = 'my_Canvas'></canvas>
<div id='overlay'>
<div class='menu' id='menu_play' style='left:400px;'>
<button id='btn_play'>PLAY</button>
</div>
<div class='menu' id='menu_decide_game'>
<button id='btn_new'>NEW GAME</button>
<button id='btn_connect'>CONNECT TO GAME</button>
<button id='btn_demo'>DEMO</button>
</div>
<div class='menu' id='menu_new_game'>
<label class='switch'>
<input type='checkbox' id='checkbox_color'>
<div class='in'><span>BLACK</span></div>
<div class='out'><span>WHITE</span></div>
</label>
<button id='btn_gen_room'>CREATE ROOM</button>
</div>
<div class='menu' id='menu_join_game'>
<input type='text' place2holder='ROOM ID' id='inp_room_id'>
<button id='btn_join_room'>JOIN</button>
</div>
</div>
<div class='top_left' id='room_id'>
Room ID:
</div>
<div class='top_right' id='exit'>
<button id='btn_exit' class='btn_back'>exit</button>
</div>
<div class='top_middle' id='error_alert'></div>
</div>
<div class='bottom_center'>
Michael Tchistopolskii, 2022 - <a href='https://github.com/realJavabot/webgl_chess'>https://github.com/realJavabot/webgl_chess</a>
</div>
<script type='module'>
import * as vecMath from '/webgl_chess/js/math.mjs';
import { loadGeometry, geo, geoParams } from '/webgl_chess/js/geometry.mjs';
import { mesh } from '/webgl_chess/js/mesh.mjs';
import { setup, clickMeshes, mouse, singleplayer } from '/webgl_chess/js/gameengine.mjs';
import { main_shader_program } from '/webgl_chess/js/shaders.mjs';
import { pieces, square, squares, spawnPieces, taken, game_input, resetBoard } from '/webgl_chess/js/gameObjects.mjs';
import { setupOverlay } from '/webgl_chess/js/overlay.mjs';
import { setupNetworking, movePiece } from '/webgl_chess/js/network.mjs';
window.onload = () =>{
setup(async () => {
const loadCalls = [
loadGeometry('models/knight.obj', 'knight'),
loadGeometry('models/rook.obj', 'rook'),
loadGeometry('models/pawn.obj', 'pawn'),
loadGeometry('models/king.obj', 'king'),
loadGeometry('models/bishop.obj', 'bishop'),
loadGeometry('models/queen.obj', 'queen'),
main_shader_program.loadImageIntoSampler('chesstex.jpg', 'uChessTex')
];
return Promise.all(loadCalls).then(()=>{
resetBoard();
setupNetworking();
setupOverlay();
});
}, () => {
pieces.forEach(piece=>{piece.update();});
taken.forEach(piece=>{piece.update();});
});
};
let canvas = document.getElementById('my_Canvas');
let availableMoves = [];
let colorMoves = ()=>{
availableMoves.forEach(mov=>{
squares[mov.pos[0]][mov.pos[1]].mesh.setTexIndex(3);
if(typeof mov.attacking !== 'undefined'){
mov.attacking.mesh.setTexIndex(3);
}
})
};
let selected;
canvas.addEventListener('mouseup', (event)=>{
if(!game_input){return;}
if(!mouse.dragging){
const clickedMesh = clickMeshes(event);
if(clickedMesh){
if(!selected){
if(clickedMesh.ob.type){
availableMoves = clickedMesh.ob.showMoves();
selected = clickedMesh.ob;
}
}else{
if(clickedMesh.ob == selected){
selected = 0;
availableMoves = [];
}else if(clickedMesh.texindex == 4){
pieces.forEach(p=>{p.enpassant = false;})
const performed_move = availableMoves.find(move =>
vecMath.same2D(...clickedMesh.ob.pos, ...move.pos)
|| move.attacking == clickedMesh.ob
);
if(singleplayer){
performed_move.perform();
}else{
movePiece(performed_move.piece.pos, performed_move.pos);
}
selected = 0;
availableMoves = [];
}else if(clickedMesh.ob.type){
availableMoves = [];
availableMoves = clickedMesh.ob.showMoves();
selected = clickedMesh.ob;
}
}
}
pieces.forEach(piece=>piece.resetTexIndex());
squares.forEach(row=>
row.forEach(square=>
square.mesh.setTexIndex(0)
)
);
colorMoves();
}
});
let mouseOver;
canvas.addEventListener('mousemove', (event) =>{
if(!game_input){return;}
if(!selected){return;}
if(mouse.state == mouse.states.UP){
const meshUnderMouse = clickMeshes(event);
if(!meshUnderMouse || mouseOver == meshUnderMouse){return;}
mouseOver = meshUnderMouse;
const move = availableMoves.find(mov =>
vecMath.same2D(...meshUnderMouse.ob.pos, ...mov.pos)
|| mov.attacking == meshUnderMouse.ob
);
colorMoves();
if(move){
squares[move.pos[0]][move.pos[1]].mesh.setTexIndex(4);
if(move.attacking){
move.attacking.mesh.setTexIndex(4);
}
}
}
});
</script>
</body>
</html>