-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCode.txt
More file actions
122 lines (98 loc) · 3.43 KB
/
Code.txt
File metadata and controls
122 lines (98 loc) · 3.43 KB
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
// Include this https://labs.gooengine.com/goochess/js/ToledoChess2.js
function mod(v, n) {
return (v % n + n) % n;
}
function getParent( ent, times) {
for( var i=0; i<times; i++) {
if( ent.transformComponent.parent)
ent = ent.transformComponent.parent.entity;
}
return ent;
}
function getToledoChessPos( x, z) {
x = x - mod(x, 2.5) + 1.25;
z = z - mod(z, 2.5) + 1.25;
x = (x / 1.25 + 7)/2;
z = (-z / 1.25 + 7)/2;
return (9-z)*10 + x+1;
}
function get3DPos( toledoChessPos ) {
var t = toledoChessPos;
var file = 9 - Math.floor(t/10);
var rank = (t%10)-1
var x = (rank*2-7)*1.25;
var z = -(file*2-7)*1.25;
return [ x, 1, z ];
}
function containsPoint( box, point) {
return box.center.x + box.xExtent > point[0] && box.center.x - box.xExtent < point[0] &&
box.center.y + box.yExtent > point[1] && box.center.y - box.yExtent < point[1] &&
box.center.z + box.zExtent > point[2] && box.center.z - box.zExtent < point[2];
}
function getEntityAt( ctx, point) {
var found;
ctx.world.getEntities().forEach(function(entity) {
if( entity.meshRendererComponent && containsPoint( entity.meshRendererComponent.worldBound, point)) {
if( entity.name != 'Torus')
found = entity;
}
});
return found;
}
var setup = function(args, ctx, goo) {
var toledoChess = getToledoChess();
toledoChess.aiCallback = function(player, from, to) {
console.log( player, from, to);
var fromPiece = getParent( getEntityAt( ctx, get3DPos( from)), 2);
var to3D = get3DPos( to);
var toEntity = getEntityAt( ctx, to3D);
if( toEntity) {
getParent( toEntity, 2).removeFromWorld();
}
to3D[1] = 0.5;
ctx.world.by.name('Torus').first().setTranslation(to3D);
to3D[1] = fromPiece.getTranslation().y;
fromPiece.setTranslation( to3D);
}
toledoChess.checkmateCallback = function(player) {
var cm = ctx.world.by.name('Checkmate').first();
cm.hidden = false;
cm.htmlComponent.hidden = false;
var text;
if(player>0) {
text = '<p style="background:white; font-size:18px;padding:4px;margin:0;border-radius:3px">Checkmate, you won !</p>';
} else {
text = '<p style="background:white; font-size:18px;padding:4px;margin:0;border-radius:3px">Checkmate, you lost !</p>';
}
cm.htmlComponent.domElement.innerHTML = text;
}
ctx.worldData.mc = function (e) {
ctx.world.gooRunner.pick( e.offsetX, e.offsetY, function (index, depth) {
if( index < 0)
return;
var clicked = ctx.world.entityManager.getEntityByIndex(index);
if( clicked.name == 'Board') { // board
var camera = ctx.activeCameraEntity.cameraComponent.camera;
var pos = camera.getWorldPosition( e.offsetX, e.offsetY, ctx.viewportWidth, ctx.viewportHeight, depth);
toledoChess.OnClick( getToledoChessPos( pos.x, pos.z));
pos.x = pos.x - mod(pos.x, 2.5) + 1.25;
pos.y = 0.5;
pos.z = pos.z - mod(pos.z, 2.5) + 1.25;
ctx.world.by.name('Torus').first().setTranslation(pos);
} else {
var s = getParent( clicked, 2);
var t = s.getTranslation();
toledoChess.OnClick( getToledoChessPos(t.x, t.z));
ctx.world.by.name('Torus').first().setTranslation( t.x, 0.5, t.z);
//entity.meshRendererComponent.materials[0].uniforms.materialAmbient = [1,0,0,0];
}
});
}
document.body.addEventListener('mousedown', ctx.worldData.mc, false);
};
var cleanup = function(args, ctx, goo) {
document.body.removeEventListener('mousedown', ctx.worldData.mc, false);
};
var update = function(args, ctx, goo) {
};
var parameters = [];