144 lines
3.6 KiB
Plaintext
144 lines
3.6 KiB
Plaintext
var shape = use('shape2d')
|
|
var text2d = use('text2d')
|
|
|
|
var S = 60
|
|
var GW = S * 8
|
|
var GH = S * 8
|
|
|
|
var light_color = {r: 0.93, g: 0.93, b: 0.85, a: 1}
|
|
var dark_color = {r: 0.45, g: 0.55, b: 0.35, a: 1}
|
|
var select_color = {r: 1, g: 0.84, b: 0, a: 1}
|
|
var valid_color = {r: 0.6, g: 0.8, b: 0.4, a: 1}
|
|
var hover_color = {r: 0.8, g: 0.85, b: 0.6, a: 1}
|
|
|
|
var piece_letter = {
|
|
king: "K", queen: "Q", rook: "R",
|
|
bishop: "B", knight: "N", pawn: "P"
|
|
}
|
|
var white_text = {r: 1, g: 1, b: 1, a: 1}
|
|
var black_text = {r: 0.1, g: 0.1, b: 0.1, a: 1}
|
|
|
|
var board_shapes = []
|
|
var piece_labels = {}
|
|
var status_label = null
|
|
var next_id = 0
|
|
|
|
function init(grid) {
|
|
var bx = 0, by = 0, row = null, col = null
|
|
for (by = 0; by < 8; by++) {
|
|
row = []
|
|
for (bx = 0; bx < 8; bx++) {
|
|
col = ((bx + by) & 1) ? dark_color : light_color
|
|
row[] = shape.rect({
|
|
pos: {x: bx * S + S / 2, y: by * S + S / 2},
|
|
width: S, height: S,
|
|
fill: {r: col.r, g: col.g, b: col.b, a: col.a},
|
|
plane: 'game', layer: 0
|
|
})
|
|
}
|
|
board_shapes[] = row
|
|
}
|
|
|
|
next_id = 0
|
|
grid.each(function(p) {
|
|
next_id++
|
|
p._id = next_id
|
|
piece_labels[text(next_id)] = text2d({
|
|
text: piece_letter[p.kind],
|
|
pos: {x: p.coord[0] * S + S / 2, y: p.coord[1] * S + S / 2},
|
|
size: 36,
|
|
color: (p.colour == 'white') ? white_text : black_text,
|
|
plane: 'game', layer: 1
|
|
})
|
|
})
|
|
|
|
status_label = text2d({
|
|
text: "White's turn", pos: {x: 10, y: GH - 20},
|
|
plane: 'hud', size: 14, color: {r: 1, g: 1, b: 1, a: 1}
|
|
})
|
|
}
|
|
|
|
function sync_pieces(grid) {
|
|
var keys = array(piece_labels)
|
|
var i = 0
|
|
for (i = 0; i < length(keys); i++) {
|
|
piece_labels[keys[i]].visible = false
|
|
}
|
|
grid.each(function(p) {
|
|
var lbl = piece_labels[text(p._id)]
|
|
if (!lbl) return
|
|
lbl.pos.x = p.coord[0] * S + S / 2
|
|
lbl.pos.y = p.coord[1] * S + S / 2
|
|
lbl.visible = true
|
|
})
|
|
}
|
|
|
|
function rebuild_pieces(grid) {
|
|
var keys = array(piece_labels)
|
|
var i = 0
|
|
for (i = 0; i < length(keys); i++) {
|
|
piece_labels[keys[i]].visible = false
|
|
}
|
|
grid.each(function(p) {
|
|
var key = text(p._id)
|
|
var lbl = piece_labels[key]
|
|
if (!lbl) {
|
|
lbl = text2d({
|
|
text: piece_letter[p.kind],
|
|
pos: {x: p.coord[0] * S + S / 2, y: p.coord[1] * S + S / 2},
|
|
size: 36,
|
|
color: (p.colour == 'white') ? white_text : black_text,
|
|
plane: 'game', layer: 1
|
|
})
|
|
piece_labels[key] = lbl
|
|
}
|
|
lbl.pos.x = p.coord[0] * S + S / 2
|
|
lbl.pos.y = p.coord[1] * S + S / 2
|
|
lbl.text = piece_letter[p.kind]
|
|
lbl.visible = true
|
|
})
|
|
}
|
|
|
|
function highlight(selectPos, validMoves, hover_gx, hover_gy) {
|
|
var x = 0, y = 0, col = null
|
|
for (y = 0; y < 8; y++) {
|
|
for (x = 0; x < 8; x++) {
|
|
col = ((x + y) & 1) ? dark_color : light_color
|
|
board_shapes[y][x].fill = {r: col.r, g: col.g, b: col.b, a: col.a}
|
|
}
|
|
}
|
|
|
|
if (hover_gx >= 0 && hover_gx < 8 && hover_gy >= 0 && hover_gy < 8) {
|
|
board_shapes[hover_gy][hover_gx].fill = {
|
|
r: hover_color.r, g: hover_color.g, b: hover_color.b, a: hover_color.a
|
|
}
|
|
}
|
|
|
|
if (!selectPos) return
|
|
|
|
board_shapes[selectPos[1]][selectPos[0]].fill = {
|
|
r: select_color.r, g: select_color.g, b: select_color.b, a: select_color.a
|
|
}
|
|
|
|
var i = 0, m = null
|
|
for (i = 0; i < length(validMoves); i++) {
|
|
m = validMoves[i]
|
|
board_shapes[m[1]][m[0]].fill = {
|
|
r: valid_color.r, g: valid_color.g, b: valid_color.b, a: valid_color.a
|
|
}
|
|
}
|
|
}
|
|
|
|
function set_status(msg) {
|
|
status_label.text = msg
|
|
}
|
|
|
|
return {
|
|
S: S, GW: GW, GH: GH,
|
|
init: init,
|
|
sync_pieces: sync_pieces,
|
|
rebuild_pieces: rebuild_pieces,
|
|
highlight: highlight,
|
|
set_status: set_status
|
|
}
|