-
Notifications
You must be signed in to change notification settings - Fork 1
/
Peon.js
149 lines (127 loc) · 5.17 KB
/
Peon.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
import * as THREE from '../libs/three.module.js'
import { Ficha } from './Ficha.js'
class Peon extends Ficha {
constructor(tablero,color,fila,columna) {
super(tablero,color,"Peon");
// Puntos para crear el perfil del peon
this.points = [];
this.points.push (new THREE.Vector2 (0, 0, 0));
this.points.push (new THREE.Vector2 (2, 0, 0));
this.points.push (new THREE.Vector2 (2, 0.5, 0));
this.points.push (new THREE.Vector2 (1.1, 0.5, 0));
this.points.push (new THREE.Vector2 (0.75, 2.1, 0));
this.points.push (new THREE.Vector2 (0.5, 2.6, 0));
this.points.push (new THREE.Vector2 (0.8, 2.7, 0));
this.points.push (new THREE.Vector2 (0.5, 2.8, 0));
this.points.push (new THREE.Vector2 (0.8, 3.1, 0));
this.points.push (new THREE.Vector2 (1, 3.6, 0));
this.points.push (new THREE.Vector2 (1, 4.1, 0));
this.points.push (new THREE.Vector2 (0.8, 4.4, 0));
this.points.push (new THREE.Vector2 (0.7, 4.5, 0));
this.points.push (new THREE.Vector2 (0.5, 4.7, 0));
this.points.push (new THREE.Vector2 (0, 4.7, 0));
var revolGeom = new THREE.LatheGeometry( this.points, 16, 0, 2*Math.PI );
//Se establece el color del peon (material)
if (color === 0){
var revolMat = new THREE.MeshPhongMaterial({color: 0xffffff});
}
else{
var revolMat = new THREE.MeshPhongMaterial({color: 0x2D2C2C});
}
//Se crea el mesh con la geometria y el material
this.revol = new THREE.Mesh(revolGeom, revolMat);
this.add (this.revol);
}
//Devuelve un Object3D con los movimientos amarillos
getMovimientos(){
var movimientos = new THREE.Object3D();
var vacio = true;
//Si es blanco el peon va hacia delante en el tablero
if(this.color===0){
//Primero el movimiento normal
if (this.fila<=6){
if(this.tablero.hayFichaEnLaCasilla(this.fila+1,this.columna,this.color) === 0){
var mov = this.createMovimiento(this.fila+1,this.columna);
if (mov != null){
movimientos.add(mov);
vacio = false;
}
}
//Comer
if (this.columna>=1){
if(this.tablero.hayFichaEnLaCasilla(this.fila+1,this.columna-1,this.color) === 1){
var mov = this.createMovimiento(this.fila+1,this.columna-1);
if (mov != null){
movimientos.add(mov);
vacio = false;
}
}
}
if (this.columna<=6){
if(this.tablero.hayFichaEnLaCasilla(this.fila+1,this.columna+1,this.color) === 1){
var mov = this.createMovimiento(this.fila+1,this.columna+1);
if (mov != null){
movimientos.add(mov);
vacio = false;
}
}
}
}
if (this.fila === 1){
if(this.tablero.hayFichaEnLaCasilla(this.fila+1,this.columna,this.color) === 0 && this.tablero.hayFichaEnLaCasilla(this.fila+2,this.columna,this.color) === 0){
var mov = this.createMovimiento(this.fila+2,this.columna);
if (mov != null){
movimientos.add(mov);
vacio = false;
}
}
}
}
//Si es negro el peon va hacia atras en el tablero
else{
if (this.fila>=1){
if(this.tablero.hayFichaEnLaCasilla(this.fila-1,this.columna,this.color) === 0){
var mov = this.createMovimiento(this.fila-1,this.columna);
if (mov != null){
movimientos.add(mov);
vacio = false;
}
}
//Comer
if (this.columna>=1){
if(this.tablero.hayFichaEnLaCasilla(this.fila-1,this.columna-1,this.color) === 1){
var mov = this.createMovimiento(this.fila-1,this.columna-1);
if (mov != null){
movimientos.add(mov);
vacio = false;
}
}
}
if (this.columna<=6){
if(this.tablero.hayFichaEnLaCasilla(this.fila-1,this.columna+1,this.color) === 1){
var mov = this.createMovimiento(this.fila-1,this.columna+1);
if (mov != null){
movimientos.add(mov);
vacio = false;
}
}
}
}
if (this.fila === 6){
if(this.tablero.hayFichaEnLaCasilla(this.fila-1,this.columna,this.color) === 0 && this.tablero.hayFichaEnLaCasilla(this.fila-2,this.columna,this.color) === 0){
var mov = this.createMovimiento(this.fila-2,this.columna);
if (mov != null){
movimientos.add(mov);
vacio = false;
}
}
}
}
if (vacio){
return null;
}else{
return movimientos;
}
}
}
export { Peon };