-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain-scene.js
2471 lines (2191 loc) · 133 KB
/
main-scene.js
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//--------------------------------------------------------------------------------------------------------------------------------
//CHESS PROJECT
//Aditya Sriram
//Adam Cole
//Eli Katz
//Jake Wallin
//-------------------------------------------------------------------------------------------------------------
//BOARD POSITION INDICIES in 3D points
// RIGHT = +X
// LEFT = -X
// UP = -Z
// DOWN = +Z
// WHITE SIDE
/*
[-10,3,-10] [-8,3,-10] [-6,3,-10] [-4,3,-10] [-2,3,-10] [0,3,-10] [2,3,-10] [4,3,-10]
[-10,3,-8] [-8,3,-8] [-6,3,-8] [-4,3,-8] [-2,3,-8] [0,3,-8] [2,3,-8] [4,3,-8]
[-10,3,-6] [-8,3,-6] [-6,3,-6] [-4,3,-6] [-2,3,-6] [0,3,-6] [2,3,-6] [4,3,-6]
[-10,3,-4] [-8,3,-4] [-6,3,-4] [-4,3,-4] [-2,3,-4] [0,3,-4] [2,3,-4] [4,3,-4]
[-10,3,-2] [-8,3,-2] [-6,3,-2] [-4,3,-2] [-2,3,-2] [0,3,-2] [2,3,-2] [4,3,-2]
[-10,3,0] [-8,3,0] [-6,3,0] [-4,3,0] [-2,3,0] [0,3,0] [2,3,0] [4,3,0]
[-10,3,2] [-8,3,2] [-6,3,2] [-4,3,2] [-2,3,2] [0,3,2] [2,3,2] [4,3,2]
[-10,3,4] [-8,3,4] [-6,3,4] [-4,3,4] [-2,3,4] [0,3,4] [2,3,4] [4,3,4]
*/
// BLACK SIDE & ORIGINAL CAMERA PLACEMENT
//-------------------------------------------------------------------------------------------------------------
//CUBE CLASS
window.Cube = window.classes.Cube =
class Cube extends Shape {
// Here's a complete, working example of a Shape subclass. It is a blueprint for a cube.
constructor() {
super("positions", "normals"); // Name the values we'll define per each vertex. They'll have positions and normals.
// First, specify the vertex positions -- just a bunch of points that exist at the corners of an imaginary cube.
this.positions.push(...Vec.cast(
[-1, -1, -1], [1, -1, -1],
[-1, -1, 1], [1, -1, 1],
[1, 1, -1], [-1, 1, -1],
[1, 1, 1], [-1, 1, 1],
[-1, -1, -1], [-1, -1, 1],
[-1, 1, -1], [-1, 1, 1],
[1, -1, 1], [1, -1, -1],
[1, 1, 1], [1, 1, -1],
[-1, -1, 1], [1, -1, 1],
[-1, 1, 1], [1, 1, 1],
[1, -1, -1], [-1, -1, -1],
[1, 1, -1], [-1, 1, -1]));
// Supply vectors that point away from eace face of the cube. They should match up with the points in the above list
// Normal vectors are needed so the graphics engine can know if the shape is pointed at light or not, and color it accordingly.
this.normals.push(...Vec.cast(
[0, -1, 0], [0, -1, 0],
[0, -1, 0], [0, -1, 0],
[0, 1, 0], [0, 1, 0],
[0, 1, 0], [0, 1, 0],
[-1, 0, 0], [-1, 0, 0],
[-1, 0, 0], [-1, 0, 0],
[1, 0, 0], [1, 0, 0],
[1, 0, 0], [1, 0, 0],
[0, 0, 1], [0, 0, 1],
[0, 0, 1], [0, 0, 1],
[0, 0, -1], [0, 0, -1],
[0, 0, -1], [0, 0, -1]));
// Those two lists, positions and normals, fully describe the "vertices". What's the "i"th vertex? Simply the combined
// data you get if you look up index "i" of both lists above -- a position and a normal vector, together. Now let's
// tell it how to connect vertex entries into triangles. Every three indices in this list makes one triangle:
this.indices.push(0, 1, 2, 1, 3, 2, 4, 5, 6, 5, 7, 6, 8, 9, 10, 9, 11, 10, 12, 13,
14, 13, 15, 14, 16, 17, 18, 17, 19, 18, 20, 21, 22, 21, 23, 22);
// It stinks to manage arrays this big. Later we'll show code that generates these same cube vertices more automatically.
}
};
//PRISM CLASS
window.TPrism = window.classes.TPrism =
class TPrism extends Shape {
// Here's a complete, working example of a Shape subclass. It is a blueprint for a cube.
constructor() {
super("positions", "normals"); // Name the values we'll define per each vertex. They'll have positions and normals.
// First, specify the vertex positions -- just a bunch of points that exist at the corners of an imaginary cube.
this.positions.push(...Vec.cast(
[-1, -1, -1], [1, -1, -1], //good
[-1, -1, 1], [1, -1, 1], //good
[1, 1, -1], [-1, 1, -1], //good
[-1, -1, -1], [-1, -1, 1], //good
[1, -1, 1], [1, -1, -1], //good
[-1, -1, 1], [-1, 1, -1], //slant 1
[1, -1, 1], [1, 1, -1], //slant 2
[-1, -1, -1], [-1, 1, -1], //up down 1
[1, -1, -1], [1, 1, -1] //up down 2
));
// Supply vectors that point away from eace face of the cube. They should match up with the points in the above list
// Normal vectors are needed so the graphics engine can know if the shape is pointed at light or not, and color it accordingly.
this.normals.push(...Vec.cast(
[0, -1, 0], [0, -1, 0], [0, -1, 0], [0, -1, 0], [0, 1, 0], [0, 1, 0], [0, 1, 0], [0, 1, 0],
[-1, 0, 0], [-1, 0, 0], [-1, 0, 0], [-1, 0, 0], [1, 0, 0], [1, 0, 0], [1, 0, 0], [1, 0, 0],
[0, 0, 1], [0, 0, 1], [0, 0, 1], [0, 0, 1], [0, 0, -1], [0, 0, -1], [0, 0, -1], [0, 0, -1]));
// Those two lists, positions and normals, fully describe the "vertices". What's the "i"th vertex? Simply the combined
// data you get if you look up index "i" of both lists above -- a position and a normal vector, together. Now let's
// tell it how to connect vertex entries into triangles. Every three indices in this list makes one triangle:
this.indices.push(0, 1, 2, 1, 3, 2, 4, 5, 6, 5, 7, 6, 8, 9, 10, 9, 11, 10, 12, 13,
14, 13, 15, 14, 16, 17, 18, 17, 19, 18, 20, 21, 22, 21, 23, 22);
// It stinks to manage arrays this big. Later we'll show code that generates these same cube vertices more automatically.
}
};
//SQUARE OUTLINE CLASS
window.Outline = window.classes.Outline =
class Outline extends Shape {
constructor() {
super("positions", "colors"); // Name the values we'll define per each vertex.
//make wireframe square
this.positions.push(...Vec.cast(
[1, 1, -1], [-1, 1, -1], [1, 1, 1], [-1, 1, 1], [1, 1, -1], [1, 1, 1], [-1, 1, 1], [-1, 1, -1]));
//color it bright purple or bright blue
const blue = Color.of(0, 0, 255, 1);
this.colors.push(...Vec.cast(blue, blue, blue, blue, blue, blue, blue, blue));
this.indexed = false;
// Do this so we won't need to define "this.indices".
}
};
window.Next_Outline = window.classes.Next_Outline =
class Next_Outline extends Shape {
constructor() {
super("positions", "colors"); // Name the values we'll define per each vertex.
//make wireframe square
this.positions.push(...Vec.cast(
[1, 1, -1], [-1, 1, -1], [1, 1, 1], [-1, 1, 1], [1, 1, -1], [1, 1, 1], [-1, 1, 1], [-1, 1, -1]));
//color it bright purple or bright blue
const red = Color.of(111, 0, 0, 1);
this.colors.push(...Vec.cast(red, red, red, red, red, red, red, red));
this.indexed = false;
// Do this so we won't need to define "this.indices".
}
};
//cube outline
window.Cube_Outline = window.classes.Cube_Outline =
class Cube_Outline extends Shape {
constructor() {
super("positions", "colors"); // Name the values we'll define per each vertex.
// TODO (Requirement 5).
// When a set of lines is used in graphics, you should think of the list entries as
// broken down into pairs; each pair of vertices will be drawn as a line segment.
this.positions.push(...Vec.cast(
[-1, -1, -1], [1, -1, -1], [-1, -1, 1], [1, -1, 1], [1, 1, -1], [-1, 1, -1], [1, 1, 1], [-1, 1, 1],
[-1, -1, -1], [-1, -1, 1], [-1, 1, -1], [-1, 1, 1], [1, -1, 1], [1, -1, -1], [1, 1, 1], [1, 1, -1],
[-1, -1, 1], [1, -1, 1], [-1, 1, 1], [1, 1, 1], [1, -1, -1], [-1, -1, -1],[1, 1, -1], [-1, 1, -1],
//add vertical lines to box
[-1, -1, -1], [-1, 1, -1], [-1, -1, 1], [-1, 1, 1], [1, -1, 1], [1, 1, 1], [1, -1, -1], [1, 1, -1],));
const white = Color.of(100, 100, 100, 1);
this.colors.push(...Vec.cast(
white, white, white, white, white, white, white, white,
white, white, white, white, white, white, white, white,
white, white, white, white, white, white, white, white,
//and their white colors
white, white, white, white, white, white, white, white));
this.indexed = false;
// Do this so we won't need to define "this.indices".
}
};
//-------------------------------------------------------------------------------------------------------------
//MAIN SCENE CLASS
window.Chess_Scene = window.classes.Chess_Scene =
class Chess_Scene extends Scene_Component {
constructor(context, control_box) {
// The scene begins by requesting the camera, shapes, and materials it will need.
super(context, control_box);
// First, include a secondary Scene that provides movement controls:
if (!context.globals.has_controls)
context.register_scene_component(new Movement_Controls(context, control_box.parentElement.insertCell()));
const r = context.width / context.height;
context.globals.graphics_state.camera_transform = Mat4.translation([3, -10, -30]); // Locate the camera here (inverted matrix).
context.globals.graphics_state.projection_transform = Mat4.perspective(Math.PI / 4, r, .1, 1000);
const shapes = {
'box': new Cube(),
'ball': new Subdivision_Sphere(4),
'nextwire': new Next_Outline(),
'wire': new Outline()
};
this.submit_shapes(context, shapes);
// Make some Material objects available to you:
this.board = context.get_instance(Phong_Shader).material(Color.of(.9, .5, .9, 1), {
ambient: .4,
diffusivity: .4,
specularity: 0.2
});
//make wood Material to try
this.wood_mat = context.get_instance(Phong_Shader).material(Color.of(0,0,0,1), {
ambient: 0.4, texture: context.get_instance( "./assets/lightwood.png", true )});
this.out_mat = context.get_instance(Basic_Shader).material();
this.plastic = this.board.override({ambient: 1, specularity: .6});
this.walls = context.get_instance(Phong_Shader).material(Color.of(0,0,0,1), {
ambient: 0.4, texture: context.get_instance( "./assets/stone_wall.png", true )});
//added more lights
this.lights = [new Light(Vec.of(0, 5, 5, 1), Color.of(1, 1, 1, 1), 100000),
new Light(Vec.of(-2, 72, -2, 1), Color.of(1, 1, 1, 1), 100000),
new Light(Vec.of(-5, 5, 0, 1), Color.of(1, 1, 1, 1), 100000)];
//define colors
//board colors
this.woodcolor = Color.of(0.85,0.556,0.35,1);
this.whitecolor = Color.of(1,1,1,1);
this.blackcolor = Color.of(0,0,0,1);
this.purple = Color.of(240, 0, 255, 1);
//piece colors
this.piece_color_black = Color.of(0.5,0.5,0.5,1);
this.piece_color_white = Color.of(234/255,246/255,249/255,1);
//add boolean for which colors the board is
this.wood = true;
//variables for camera view
this.topview = Vec.of(-2,36,-2);
this.beginview = Vec.of(-2, 24.42, 22.69);
//mirrored vectors must change since origin is offset
this.top_mirrored = Vec.of(-4,36,-4);
this.begin_mirrored = Vec.of(-3, 24.42, -26.69);
//buttons for camera view
this.top = false;
this.lookaround = false;
this.rotate = true;
//add toggle bits for buttons
this.selected = false;
//chess game variables
//z "rows" of teams
this.black_pawn_z = 2;
this.black_z = 4;
this.white_pawn_z = -8;
this.white_z = -10;
//x positions of all the pieces
this.rook_x = -10; this.rook2_x = 4;
this.knight_x = -8; this.knight2_x = 2;
this.bish_x = -6; this.bish2_x = 0;
this.queen_x = -4;
this.king_x = -2;
this.max = 4;
this.min = -10;
//current position
this.curr_x = 0;
this.curr_z = 0;
//move booleans
this.whites_move = true;
this.blacks_move = false;
this.white_taken_list = [];
this.black_taken_list = [];
//chess game - initialize to empty board
this.gameboard = [['r-w', 'k-w', 'B-w', 'q-w', 'a-w', 'B-w', 'k-w', 'r-w'],
['p-w', 'p-w', 'p-w', 'p-w', 'p-w', 'p-w', 'p-w', 'p-w'],
[ '_', '_', '_', '_', '_', '_', '_', '_'],
[ '_', '_', '_', '_', '_', '_', '_', '_'],
[ '_', '_', '_', '_', '_', '_', '_', '_'],
[ '_', '_', '_', '_', '_', '_', '_', '_'],
['p-b', 'p-b', 'p-b', 'p-b', 'p-b', 'p-b', 'p-b', 'p-b'],
['r-b', 'k-b', 'B-b', 'q-b', 'a-b', 'B-b', 'k-b', 'r-b']];
// animating state; false means not animating, true means animating
this.animating = false;
this.animating_from = [0,0];
this.animating_to = [0,0];
this.animating_cur = [0,0];
this.animating_start_time = 0;
this.animating_piece = '';
// VARIABLES TO HELP WITH MOUSE PICKING
this.mousedown = false;
// States of clicking; either nothing selected (false), or a piece is selected (true)
this.clickstate = false;
this.clickable_moves = []; // This is populated by the next_moves_XXX functions to move pieces
// Each "clickable area" is defined as (x, y) of bottom left corner of square,
// then width and height of the square for easy checking
this.clickable_areas = [
// Bottom row, left to right
[307, 414, 52, 36], // x,y, width, height
[359, 414, 52, 36],
[411, 414, 52, 36],
[464, 414, 52, 36],
[515, 414, 52, 36],
[567, 414, 52, 36],
[619, 414, 52, 36],
[671, 414, 52, 36],
// row 2, left to right
[319, 374, 47, 33], // (x,y), width, height
[369, 374, 47, 33],
[418, 374, 47, 33],
[467, 374, 47, 33],
[516, 374, 47, 33],
[566, 374, 47, 33],
[616, 374, 47, 33],
[665, 374, 47, 33],
// row 3, left to right
[334, 337, 45, 28], // (x,y), width, height
[382, 337, 45, 28],
[427, 337, 45, 28],
[472, 337, 45, 28],
[518, 337, 45, 28],
[565, 337, 45, 28],
[612, 337, 45, 28],
[659, 337, 45, 28],
// row 4, left to right
[343, 308, 44, 27], // (x,y), width, height
[386, 308, 44, 27],
[432, 308, 44, 27],
[476, 308, 44, 27],
[520, 308, 44, 27],
[564, 308, 44, 27],
[609, 308, 44, 27],
[654, 308, 44, 27],
// row 5, left to right
[350, 279, 43, 26], // (x,y), width, height
[393, 279, 43, 26],
[435, 279, 43, 26],
[478, 279, 43, 26],
[520, 279, 43, 26],
[563, 279, 43, 26],
[606, 279, 43, 26],
[649, 279, 43, 26],
// row 6, left to right
[359, 252, 40, 24], // (x,y), width, height
[400, 252, 40, 24],
[441, 252, 40, 24],
[481, 252, 40, 24],
[522, 252, 40, 24],
[562, 252, 40, 24],
[603, 252, 40, 24],
[645, 252, 40, 24],
// row 7, left to right
[367, 227, 38, 21], // (x,y), width, height
[406, 227, 38, 21],
[444, 227, 38, 21],
[483, 227, 38, 21],
[522, 227, 38, 21],
[561, 227, 38, 21],
[600, 227, 38, 21],
[640, 227, 38, 21],
// row 8, left to right
[372, 205, 37, 20], // (x,y), width, height
[410, 205, 37, 20],
[448, 205, 37, 20],
[485, 205, 37, 20],
[523, 205, 37, 20],
[561, 205, 37, 20],
[599, 205, 37, 20],
[636, 205, 37, 20],
];
}
//-------------------------------------------------------------------------------------------------------------
//BUTTONS / CONTROL PANEL
make_control_panel()
// Draw the scene's buttons, setup their actions and keyboard shortcuts, and monitor live measurements.
{
this.key_triggered_button("Change Board", ["c"], () => {
this.wood = !this.wood;
});
this.key_triggered_button("Top View", ["t"], () => {
this.top = !this.top;
});
this.key_triggered_button("Lookaround", ["l"], () => {
this.lookaround = !this.lookaround;
});
this.new_line();
this.key_triggered_button("Reset Game", ["r"], this.reset_game);
}
//reset game function
reset_game()
{
this.gameboard =
[['r-w', 'k-w', 'B-w', 'q-w', 'a-w', 'B-w', 'k-w', 'r-w'],
['p-w', 'p-w', 'p-w', 'p-w', 'p-w', 'p-w', 'p-w', 'p-w'],
[ '_', '_', '_', '_', '_', '_', '_', '_'],
[ '_', '_', '_', '_', '_', '_', '_', '_'],
[ '_', '_', '_', '_', '_', '_', '_', '_'],
[ '_', '_', '_', '_', '_', '_', '_', '_'],
['p-b', 'p-b', 'p-b', 'p-b', 'p-b', 'p-b', 'p-b', 'p-b'],
['r-b', 'k-b', 'B-b', 'q-b', 'a-b', 'B-b', 'k-b', 'r-b']];
this.white_taken_list = [];
this.black_taken_list = [];
if (this.blacks_move){
this.whites_move = true;
this.blacks_move = false;
this.rotate = !this.rotate;
}
}
//-------------------------------------------------------------------------------------------------------------
//DRAWING SHAPES FUNCTIONS
draw_box(graphics_state, model_transform, i) {
// TODO: Helper function for requirement 3 (see hint).
// This should make changes to the model_transform matrix, draw the next box, and return the newest model_transform.
let color = Color.of(0,1,0,1);
if (this.wood) {
if (i % 2 == 0) {
color = this.blackcolor;
//this.shapes.box.draw(graphics_state, model_transform, this.board.override({color: color}));
} else {
color = this.woodcolor;
//this.shapes.box.draw(graphics_state, model_transform, this.wood_mat);
}
//this.shapes.box.draw(graphics_state, model_transform, this.wood_mat);
} else {
if (i % 2 == 0) {
color = this.blackcolor;
} else {
color = this.whitecolor;
}
//this.shapes.box.draw(graphics_state, model_transform, this.board.override({color: color}));
}
this.shapes.box.draw(graphics_state, model_transform, this.board.override({color: color}));
return model_transform;
}
draw_edge(graphics_state, model_transform) {
// TODO: Helper function for requirement 3 (see hint).
// This should make changes to the model_transform matrix, draw the next box, and return the newest model_transform.
let color = Color.of(0,1,0,1);
if (this.wood) {
color = this.woodcolor;
} else {
color = this.whitecolor;
}
this.shapes.box.draw(graphics_state, model_transform, this.board.override({color: color}));
return model_transform;
}
draw_board(graphics_state, model_transform){
//draw the playing board
// Start offset by 12 so board is kinda centered
model_transform = model_transform.times(Mat4.translation([-10, 2, -10]));
// BOARD
for(let i = 0; i < 8; i++) {
for (let j=0; j < 8; j++) {
// Scale down by half
model_transform = model_transform.times(Mat4.scale(Vec.of( 1, 0.5, 1 )))
// Draw
// console.log(graphics_state.projection_transform.times(graphics_state.camera_transform).times(model_transform));
this.draw_box(graphics_state, model_transform, i+j);
// Scale back by 2 so next square isn't messed up
model_transform = model_transform.times(Mat4.scale(Vec.of( 1, 2, 1 )))
model_transform = model_transform.times(Mat4.translation([2, 0, 0]));
}
model_transform = model_transform.times(Mat4.translation([-16, 0, 2]));
}
//draw first edge of board and its corner
//get to the outside edge
model_transform = Mat4.identity();
model_transform = model_transform.times(Mat4.translation([-12, 2, -10]));
//draw edges of board
for(let i = 0; i < 8; i++) {
//get to the correct edge
let temp = model_transform;
//scale it to the correct border size
//scale down from 2x2x2 to 0.2x1x2
model_transform = model_transform.times(Mat4.scale(Vec.of( 0.1, 0.5, 1 )));
//put it up against wall
model_transform = model_transform.times(Mat4.translation([9, 0, 0]));
//draw box
this.draw_edge(graphics_state, model_transform);
// Scale back by 2 so next square isn't messed up
model_transform = temp.times(Mat4.translation([0, 0, 2]));
}
//draw corner
model_transform = Mat4.identity();
model_transform = model_transform.times(Mat4.translation([-12, 2, -12]));
//scale it to the correct border size
//scale down from 2x2x2 to 0.2x1x2
model_transform = model_transform.times(Mat4.scale(Vec.of( 0.1, 0.5, 0.1 )));
//put it up against wall
model_transform = model_transform.times(Mat4.translation([9, 0, 9]));
//draw box
this.draw_edge(graphics_state, model_transform);
//draw second edge of board and its corner
//get to the outside edge
model_transform = Mat4.identity();
model_transform = model_transform.times(Mat4.translation([-10, 2, -12]));
//draw edges
for(let i = 0; i < 8; i++) {
//get to the correct edge
let temp = model_transform;
//scale it to the correct border size
//scale down from 2x2x2 to 2x1x0.2
model_transform = model_transform.times(Mat4.scale(Vec.of( 1, 0.5, 0.1 )));
//put it up against wall
model_transform = model_transform.times(Mat4.translation([0, 0, 9]));
//draw box
this.draw_edge(graphics_state, model_transform);
// Scale back by 2 so next square isn't messed up
model_transform = temp.times(Mat4.translation([2, 0, 0]));
}
//draw corner
model_transform = Mat4.identity();
model_transform = model_transform.times(Mat4.translation([5, 2, -12]));
//scale it to the correct border size
//scale down from 2x2x2 to 0.2x1x2
model_transform = model_transform.times(Mat4.scale(Vec.of( 0.1, 0.5, 0.1 )));
//put it up against wall
model_transform = model_transform.times(Mat4.translation([1, 0, 9]));
//draw box
this.draw_edge(graphics_state, model_transform);
//draw third edge of board and its corner
//get to the outside edge
model_transform = Mat4.identity();
model_transform = model_transform.times(Mat4.translation([-10, 2, 6]));
//draw edges
for(let i = 0; i < 8; i++) {
//get to the correct edge
let temp = model_transform;
//scale it to the correct border size
//scale down from 2x2x2 to 2x1x0.2
model_transform = model_transform.times(Mat4.scale(Vec.of( 1, 0.5, 0.1 )));
//put it up against wall
model_transform = model_transform.times(Mat4.translation([0, 0, -9]));
//draw box
this.draw_edge(graphics_state, model_transform);
// Scale back by 2 so next square isn't messed up
model_transform = temp.times(Mat4.translation([2, 0, 0]));
}
//draw corner
model_transform = Mat4.identity();
model_transform = model_transform.times(Mat4.translation([5, 2, 5]));
//scale it to the correct border size
//scale down from 2x2x2 to 0.2x1x2
model_transform = model_transform.times(Mat4.scale(Vec.of( 0.1, 0.5, 0.1 )));
//put it up against wall
model_transform = model_transform.times(Mat4.translation([1, 0, 1]));
//draw box
this.draw_edge(graphics_state, model_transform);
//draw fourth edge of board and its corner
//get to the outside edge
model_transform = Mat4.identity();
model_transform = model_transform.times(Mat4.translation([6, 2, -10]));
//draw edges
for(let i = 0; i < 8; i++) {
//get to the correct edge
let temp = model_transform;
//scale it to the correct border size
//scale down from 2x2x2 to 2x1x0.2
model_transform = model_transform.times(Mat4.scale(Vec.of( 0.1, 0.5, 1 )));
//put it up against wall
model_transform = model_transform.times(Mat4.translation([-9, 0, 0]));
//draw box
this.draw_edge(graphics_state, model_transform);
// Scale back by 2 so next square isn't messed up
model_transform = temp.times(Mat4.translation([0, 0, 2]));
}
//draw corner
model_transform = Mat4.identity();
model_transform = model_transform.times(Mat4.translation([-12, 2, 5]));
//scale it to the correct border size
//scale down from 2x2x2 to 0.2x1x2
model_transform = model_transform.times(Mat4.scale(Vec.of( 0.1, 0.5, 0.1 )));
//put it up against wall
model_transform = model_transform.times(Mat4.translation([9, 0, 1]));
//draw box
this.draw_edge(graphics_state, model_transform);
}
//---------------------------------------------------------------------------------------------------------------
//CAMERA VIEW FUNCTIONS
handle_view(graphics_state) {
//top view
//if a button has been pushed
if (this.lookaround){
//do nothing so user can look around
}
else if(this.top) {
//invert the matrix that is the "top" view
//eye = this.topview
//poi = center of board
//top = far edge of board
if (this.rotate){
//rotate camera angle to mirror last one
// poi and eye change to -4,y,-4 since center of board offset
// flip top vector
var desired = Mat4.look_at(this.top_mirrored, Vec.of(-4,2,-4), Vec.of(0,2,6));
//map inverted matrix to camera
desired = desired.map((x, i) => Vec.from( graphics_state.camera_transform[i]).mix(x, .05));
//set equal to camera transformation
graphics_state.camera_transform = desired;
} else {
var desired = Mat4.look_at(this.topview, Vec.of(-2,2,-2), Vec.of(0,2,-10));
//map inverted matrix to camera
desired = desired.map((x, i) => Vec.from( graphics_state.camera_transform[i]).mix(x, .05));
//set equal to camera transformation
graphics_state.camera_transform = desired;
}
}
else {
//invert the matrix that is the "top" view
//eye = this.beginview
//poi = center of board
//top = far edge of board
if (this.rotate){
//rotate camera angle to mirror last one
//poi change to -4,y,-4 since center of board offset
//mirror eye, mirror top
var desired = Mat4.look_at(this.begin_mirrored, Vec.of(-4,2,-4), Vec.of(0,2,6.2));
//map inverted matrix to camera
desired = desired.map((x, i) => Vec.from( graphics_state.camera_transform[i]).mix(x, .05));
//set equal to camera transformation
graphics_state.camera_transform = desired;
} else {
var desired = Mat4.look_at(this.beginview, Vec.of(-2,2,-2), Vec.of(0,2,-10));
//map inverted matrix to camera
desired = desired.map((x, i) => Vec.from( graphics_state.camera_transform[i]).mix(x, .05));
//set equal to camera transformation
graphics_state.camera_transform = desired;
}
}
}
//-------------------------------------------------------------------------------------------------------------
//SAMPLE ANGLE FUNCTION
//returns an angle forming a sinusodial function
//returns 0 - 0.04*pi
angle(t){
var factor = 0.02*Math.PI;
this.curr_angle = factor + factor*Math.sin((Math.PI*6)*t);
return this.curr_angle;
}
//-------------------------------------------------------------------------------------------------------------
//DRAWING CHESS PIECES FUNCTIONS
//make all pieces plastic
//draw pawn
draw_pawn(graphics_state, model_transform, side){
//create base with flattened sphere - draw at center of square
let base = model_transform.times(Mat4.scale(Vec.of(0.65, 0.3, 0.65)));
base = base.times(Mat4.translation([0, -2, 0]));
let ring = model_transform.times(Mat4.scale(Vec.of(0.5, 0.1, 0.5)));
ring = ring.times(Mat4.translation([0, 0.5, 0]));
//create body of pawn
let mid = model_transform.times(Mat4.scale(Vec.of( 0.25, 0.5, 0.25)));
mid = mid.times(Mat4.translation([0,0.25,0]));
let top = model_transform.times(Mat4.scale(Vec.of( 0.35, 0.35, 0.35)));
top = top.times(Mat4.translation([0,1.5,0]));
if (side == "w"){
this.shapes.ball.draw(graphics_state, base, this.board.override({color: this.piece_color_white}));
this.shapes.ball.draw(graphics_state, ring, this.board.override({color: this.piece_color_white}));
this.shapes.ball.draw(graphics_state, mid, this.board.override({color: this.piece_color_white}));
this.shapes.ball.draw(graphics_state, top, this.board.override({color: this.piece_color_white}));
} else {
this.shapes.ball.draw(graphics_state, base, this.board.override({color: this.piece_color_black}));
this.shapes.ball.draw(graphics_state, ring, this.board.override({color: this.piece_color_black}));
this.shapes.ball.draw(graphics_state, mid, this.board.override({color: this.piece_color_black}));
this.shapes.ball.draw(graphics_state, top, this.board.override({color: this.piece_color_black}));
}
}
//draw rook
draw_rook(graphics_state, model_transform, side){
//make a base
let base = model_transform.times(Mat4.scale(Vec.of(0.65, 0.3, 0.65)));
base = base.times(Mat4.translation([0, -2, 0]));
let basering = model_transform.times(Mat4.scale(Vec.of(0.55, 0.25, 0.55)));
basering = basering.times(Mat4.translation([0, -1, 0]));
let mid = model_transform.times(Mat4.scale(Vec.of( 0.35, 0.75, 0.35)));
mid = mid.times(Mat4.translation([0,0,0]));
let midring = model_transform.times(Mat4.scale(Vec.of( 0.5, .25, 0.5)));
midring = midring.times(Mat4.translation([0,2,0]));
let edge1 = model_transform.times(Mat4.scale(Vec.of( 0.23, .45, 0.05)));
let edge2 = edge1;
let edge3 = model_transform.times(Mat4.scale(Vec.of( 0.05, .45, 0.23)));;
let edge4 = edge3;
edge1 = edge1.times(Mat4.translation([0, 2.5, 7.6]));
edge2 = edge2.times(Mat4.translation([0, 2.5, -7.6]));
edge3 = edge3.times(Mat4.translation([7.6, 2.5, 0]));
edge4 = edge4.times(Mat4.translation([-7.6, 2.5, 0]));
if (side == "w"){
this.shapes.ball.draw(graphics_state, base, this.board.override({color: this.piece_color_white}));
this.shapes.ball.draw(graphics_state, basering, this.board.override({color: this.piece_color_white}));
this.shapes.ball.draw(graphics_state, mid, this.board.override({color: this.piece_color_white}));
this.shapes.ball.draw(graphics_state, midring, this.board.override({color: this.piece_color_white}));
midring = midring.times(Mat4.translation([0,0.75,0]));
this.shapes.ball.draw(graphics_state, midring, this.board.override({color: this.piece_color_white}));
midring = midring.times(Mat4.translation([0,0.75,0]));
this.shapes.ball.draw(graphics_state, midring, this.board.override({color: this.piece_color_white}));
midring = midring.times(Mat4.translation([0,0.75,0]));
this.shapes.ball.draw(graphics_state, midring, this.board.override({color: this.piece_color_white}));
this.shapes.box.draw(graphics_state, edge1, this.board.override({color: this.piece_color_white}));
this.shapes.box.draw(graphics_state, edge2, this.board.override({color: this.piece_color_white}));
this.shapes.box.draw(graphics_state, edge3, this.board.override({color: this.piece_color_white}));
this.shapes.box.draw(graphics_state, edge4, this.board.override({color: this.piece_color_white}));
} else {
this.shapes.ball.draw(graphics_state, base, this.board.override({color: this.piece_color_black}));
this.shapes.ball.draw(graphics_state, basering, this.board.override({color: this.piece_color_black}));
this.shapes.ball.draw(graphics_state, mid, this.board.override({color: this.piece_color_black}));
this.shapes.ball.draw(graphics_state, midring, this.board.override({color: this.piece_color_black}));
midring = midring.times(Mat4.translation([0,0.75,0]));
this.shapes.ball.draw(graphics_state, midring, this.board.override({color: this.piece_color_black}));
midring = midring.times(Mat4.translation([0,0.75,0]));
this.shapes.ball.draw(graphics_state, midring, this.board.override({color: this.piece_color_black}));
midring = midring.times(Mat4.translation([0,0.75,0]));
this.shapes.ball.draw(graphics_state, midring, this.board.override({color: this.piece_color_black}));
this.shapes.box.draw(graphics_state, edge1, this.board.override({color: this.piece_color_black}));
this.shapes.box.draw(graphics_state, edge2, this.board.override({color: this.piece_color_black}));
this.shapes.box.draw(graphics_state, edge3, this.board.override({color: this.piece_color_black}));
this.shapes.box.draw(graphics_state, edge4, this.board.override({color: this.piece_color_black}));
}
}
//draw bishop
draw_bishop(graphics_state, model_transform, side){
//make a base
let base = model_transform.times(Mat4.scale(Vec.of(0.65, 0.3, 0.65)));
base = base.times(Mat4.translation([0, -2, 0]));
let basering = model_transform.times(Mat4.scale(Vec.of(0.55, 0.25, 0.55)));
basering = basering.times(Mat4.translation([0, -1, 0]));
let mid = model_transform.times(Mat4.scale(Vec.of( 0.35, 0.75, 0.35)));
mid = mid.times(Mat4.translation([0,0,0]));
let midring = model_transform.times(Mat4.scale(Vec.of( 0.45, .2, 0.45)));
midring = midring.times(Mat4.translation([0,2.55,0]));
let top = model_transform.times(Mat4.scale(Vec.of( 0.35, .7, 0.35)));
top = top.times(Mat4.translation([0,1.7,0]));
let ball = model_transform.times(Mat4.scale(Vec.of( 0.15, .15, 0.15)));
ball = ball.times(Mat4.translation([0,13,0]));
if (side == "w"){
this.shapes.ball.draw(graphics_state, base, this.board.override({color: this.piece_color_white}));
this.shapes.ball.draw(graphics_state, basering, this.board.override({color: this.piece_color_white}));
this.shapes.ball.draw(graphics_state, mid, this.board.override({color: this.piece_color_white}));
this.shapes.ball.draw(graphics_state, midring, this.board.override({color: this.piece_color_white}));
this.shapes.ball.draw(graphics_state, top, this.board.override({color: this.piece_color_white}));
this.shapes.ball.draw(graphics_state, ball, this.board.override({color: this.piece_color_white}));
} else {
this.shapes.ball.draw(graphics_state, base, this.board.override({color: this.piece_color_black}));
this.shapes.ball.draw(graphics_state, basering, this.board.override({color: this.piece_color_black}));
this.shapes.ball.draw(graphics_state, mid, this.board.override({color: this.piece_color_black}));
this.shapes.ball.draw(graphics_state, midring, this.board.override({color: this.piece_color_black}));
this.shapes.ball.draw(graphics_state, top, this.board.override({color: this.piece_color_black}));
this.shapes.ball.draw(graphics_state, ball, this.board.override({color: this.piece_color_black}));
}
}
//draw knight
draw_knight(graphics_state, model_transform, side){
//make a base
let base = model_transform.times(Mat4.scale(Vec.of(0.65, 0.3, 0.65)));
base = base.times(Mat4.translation([0, -2, 0]));
let basering = model_transform.times(Mat4.scale(Vec.of(0.55, 0.25, 0.55)));
basering = basering.times(Mat4.translation([0, -1, 0]));
let mid = model_transform.times(Mat4.scale(Vec.of( 0.35, 0.75, 0.35)));
mid = mid.times(Mat4.translation([0,0,0]));
let midring = model_transform.times(Mat4.scale(Vec.of( 0.5, .25, 0.5)));
midring = midring.times(Mat4.translation([0,2,0]));
let horse_body = model_transform.times(Mat4.scale(Vec.of( 0.45, .25, 0.25)));
horse_body = horse_body.times(Mat4.translation([0,4.25,0]));
if (side == "w"){
this.shapes.ball.draw(graphics_state, base, this.board.override({color: this.piece_color_white}));
this.shapes.ball.draw(graphics_state, basering, this.board.override({color: this.piece_color_white}));
this.shapes.ball.draw(graphics_state, mid, this.board.override({color: this.piece_color_white}));
//make thick ring
this.shapes.ball.draw(graphics_state, midring, this.board.override({color: this.piece_color_white}));
midring = midring.times(Mat4.translation([0,0.6,0]));
this.shapes.ball.draw(graphics_state, midring, this.board.override({color: this.piece_color_white}));
this.shapes.ball.draw(graphics_state, horse_body, this.board.override({color: this.piece_color_white}));
let h2 = model_transform.times(Mat4.scale(Vec.of( 0.4, .25, 0.25)));
h2 = h2.times(Mat4.translation([0,5.35,-1.25]));
this.shapes.ball.draw(graphics_state, h2, this.board.override({color: this.piece_color_white}));
let h3 = model_transform.times(Mat4.scale(Vec.of( 0.35, .25, 0.25)));
h3 = h3.times(Mat4.translation([0,6.35,-0.5]));
this.shapes.ball.draw(graphics_state, h3, this.board.override({color: this.piece_color_white}));
} else {
this.shapes.ball.draw(graphics_state, base, this.board.override({color: this.piece_color_black}));
this.shapes.ball.draw(graphics_state, basering, this.board.override({color: this.piece_color_black}));
this.shapes.ball.draw(graphics_state, mid, this.board.override({color: this.piece_color_black}));
this.shapes.ball.draw(graphics_state, midring, this.board.override({color: this.piece_color_black}));
midring = midring.times(Mat4.translation([0,0.6,0]));
this.shapes.ball.draw(graphics_state, midring, this.board.override({color: this.piece_color_black}));
this.shapes.ball.draw(graphics_state, horse_body, this.board.override({color: this.piece_color_black}));
let h2 = model_transform.times(Mat4.scale(Vec.of( 0.4, .25, 0.25)));
h2 = h2.times(Mat4.translation([0,5.35,1.25]));
this.shapes.ball.draw(graphics_state, h2, this.board.override({color: this.piece_color_black}));
let h3 = model_transform.times(Mat4.scale(Vec.of( 0.35, .25, 0.25)));
h3 = h3.times(Mat4.translation([0,6.55,0]));
this.shapes.ball.draw(graphics_state, h3, this.board.override({color: this.piece_color_black}));
}
}
//draw queen
draw_queen(graphics_state, model_transform, side){
//make a base
let base = model_transform.times(Mat4.scale(Vec.of(0.65, 0.3, 0.65)));
base = base.times(Mat4.translation([0, -2, 0]));
let basering = model_transform.times(Mat4.scale(Vec.of(0.55, 0.25, 0.55)));
basering = basering.times(Mat4.translation([0, -1, 0]));
let mid = model_transform.times(Mat4.scale(Vec.of( 0.35, 0.75, 0.35)));
mid = mid.times(Mat4.translation([0,0,0]));
let midring = model_transform.times(Mat4.scale(Vec.of( 0.45, .25, 0.45)));
midring = midring.times(Mat4.translation([0,4.5,0]));
let belt = model_transform.times(Mat4.scale(Vec.of( 0.45, .2, 0.45)));
belt = belt.times(Mat4.translation([0,2.55,0]));
let head = model_transform.times(Mat4.scale(Vec.of( 0.33, 0.25, 0.33)));
head = head.times(Mat4.translation([0,7.5,0]));
let ball = model_transform.times(Mat4.scale(Vec.of( 0.15, .15, 0.15)));
ball = ball.times(Mat4.translation([0,14.75,0]));
if (side == "w"){
this.shapes.ball.draw(graphics_state, base, this.board.override({color: this.piece_color_white}));
this.shapes.ball.draw(graphics_state, basering, this.board.override({color: this.piece_color_white}));
this.shapes.ball.draw(graphics_state, mid, this.board.override({color: this.piece_color_white}));
this.shapes.ball.draw(graphics_state, midring, this.board.override({color: this.piece_color_white}));
this.shapes.ball.draw(graphics_state, belt, this.board.override({color: this.piece_color_white}));
//make thicker ring
midring = midring.times(Mat4.translation([0,0.6,0]));
this.shapes.ball.draw(graphics_state, midring, this.board.override({color: this.piece_color_white}));
midring = midring.times(Mat4.translation([0,0.6,0]));
this.shapes.ball.draw(graphics_state, midring, this.board.override({color: this.piece_color_white}));
//make second body
mid = mid.times(Mat4.translation([0,1.5,0]));
this.shapes.ball.draw(graphics_state, mid, this.board.override({color: this.piece_color_white}));
//top of queen
this.shapes.ball.draw(graphics_state, head, this.board.override({color: this.piece_color_white}));
this.shapes.ball.draw(graphics_state, ball, this.board.override({color: this.piece_color_white}));
} else {
this.shapes.ball.draw(graphics_state, base, this.board.override({color: this.piece_color_black}));
this.shapes.ball.draw(graphics_state, basering, this.board.override({color: this.piece_color_black}));
this.shapes.ball.draw(graphics_state, mid, this.board.override({color: this.piece_color_black}));
this.shapes.ball.draw(graphics_state, midring, this.board.override({color: this.piece_color_black}));
this.shapes.ball.draw(graphics_state, belt, this.board.override({color: this.piece_color_black}));
//make thicker ring
midring = midring.times(Mat4.translation([0,0.6,0]));
this.shapes.ball.draw(graphics_state, midring, this.board.override({color: this.piece_color_black}));
midring = midring.times(Mat4.translation([0,0.6,0]));
this.shapes.ball.draw(graphics_state, midring, this.board.override({color: this.piece_color_black}));
//make second body
mid = mid.times(Mat4.translation([0,1.5,0]));
this.shapes.ball.draw(graphics_state, mid, this.board.override({color: this.piece_color_black}));
//top of queen
this.shapes.ball.draw(graphics_state, head, this.board.override({color: this.piece_color_black}));
this.shapes.ball.draw(graphics_state, ball, this.board.override({color: this.piece_color_black}));
}
}
//draw king
draw_king(graphics_state, model_transform, side){
//make a base
let base = model_transform.times(Mat4.scale(Vec.of(0.65, 0.3, 0.65)));
base = base.times(Mat4.translation([0, -2, 0]));
let basering = model_transform.times(Mat4.scale(Vec.of(0.55, 0.25, 0.55)));
basering = basering.times(Mat4.translation([0, -1, 0]));
let mid = model_transform.times(Mat4.scale(Vec.of( 0.35, 0.75, 0.35)));
mid = mid.times(Mat4.translation([0,0,0]));
let midring = model_transform.times(Mat4.scale(Vec.of( 0.5, .25, 0.5)));
midring = midring.times(Mat4.translation([0,2,0]));
if (side == "w"){
this.shapes.ball.draw(graphics_state, base, this.board.override({color: this.piece_color_white}));
this.shapes.ball.draw(graphics_state, basering, this.board.override({color: this.piece_color_white}));
this.shapes.ball.draw(graphics_state, mid, this.board.override({color: this.piece_color_white}));
//make thick ring
this.shapes.ball.draw(graphics_state, midring, this.board.override({color: this.piece_color_white}));
midring = midring.times(Mat4.translation([0,0.6,0]));
this.shapes.ball.draw(graphics_state, midring, this.board.override({color: this.piece_color_white}));
midring = midring.times(Mat4.translation([0,0.6,0]));
this.shapes.ball.draw(graphics_state, midring, this.board.override({color: this.piece_color_white}));
midring = midring.times(Mat4.translation([0,0.6,0]));
this.shapes.ball.draw(graphics_state, midring, this.board.override({color: this.piece_color_white}));
//make second body
mid = mid.times(Mat4.translation([0,1.5,0]));
this.shapes.ball.draw(graphics_state, mid, this.board.override({color: this.piece_color_white}));
//another thick ring
midring = midring.times(Mat4.translation([0,3.0,0]));
midring = midring.times(Mat4.scale([0.75,1,0.75]));
this.shapes.ball.draw(graphics_state, midring, this.board.override({color: this.piece_color_white}));
//draw cross at the top of the last ring
let edge1 = midring.times(Mat4.scale([0.65, 0.2, 0.2]));