Files
prosperon/examples/chess/host.ce
2026-02-26 08:13:27 -06:00

185 lines
5.2 KiB
Plaintext

var core = use('core')
var camera = use('camera')
var compositor = use('compositor')
var input = use('input')
var Grid = use('examples/chess/grid')
var MovementSystem = use('examples/chess/movement')
var pieces = use('examples/chess/pieces')
var rules = use('examples/chess/rules')
var board_view = use('examples/chess/board_view')
var game_state = use('examples/chess/game_state')
var grid = Grid(8, 8)
grid.width = 8
grid.height = 8
var mover = MovementSystem(grid, rules)
pieces.startingPosition(grid)
board_view.init(grid)
var S = board_view.S, GW = board_view.GW, GH = board_view.GH
var game_cam = camera.make({width: GW, height: GH, pos: {x: GW / 2, y: GH / 2}})
var hud_cam = camera.make({width: GW, height: GH, pos: {x: GW / 2, y: GH / 2}})
input.configure({
action_map: {
select: ['mouse_button_left'],
cancel: ['escape', 'mouse_button_right']
}
})
// Network state
var client_actor = null
board_view.set_status("Waiting for client on port 7777...")
$portal(function(connection) {
log.console("Client connected!")
client_actor = connection
send(client_actor, {type: "assign", color: "black"})
send(client_actor, game_state.serialize(grid, mover.turn))
board_view.set_status("White's turn - Game started!")
}, 7777)
$receiver(function(msg) {
var from = null, to = null, cell = null, piece = null
if (msg.type == "move") {
from = [msg.fx, msg.fy]
to = [msg.tx, msg.ty]
cell = grid.at(from)
if (!length(cell)) {
send(client_actor, {type: "result", ok: false, error: "no piece"})
return
}
piece = cell[0]
if (piece.colour != "black") {
send(client_actor, {type: "result", ok: false, error: "not your piece"})
return
}
if (mover.turn != "black") {
send(client_actor, {type: "result", ok: false, error: "not your turn"})
return
}
if (!rules.canMove(piece, from, to, grid)) {
send(client_actor, {type: "result", ok: false, error: "illegal move"})
return
}
if (mover.tryMove(piece, to)) {
board_view.sync_pieces(grid)
board_view.set_status(mover.turn + "'s turn")
send(client_actor, game_state.serialize(grid, mover.turn))
} else {
send(client_actor, {type: "result", ok: false, error: "move rejected"})
}
}
})
// Local input — host plays white
var selectPos = null
var validMoves = []
var hover_gx = -1, hover_gy = -1
function compute_valid_moves(from) {
validMoves = []
var piece = grid.at(from)[0]
if (!piece) return
var x = 0, y = 0, to = null, dest = null
for (y = 0; y < 8; y++) {
for (x = 0; x < 8; x++) {
to = [x, y]
dest = grid.at(to)
if (length(dest) && dest[0].colour == piece.colour) continue
if (rules.canMove(piece, from, to, grid))
validMoves[] = to
}
}
}
var game_input = {
on_input: function(action, data) {
var clicked = null, cell = null, is_valid = false, i = 0, src_piece = null
if (!data.pressed) return
if (mover.turn != 'white') return
if (action == 'cancel') {
selectPos = null
validMoves = []
board_view.highlight(selectPos, validMoves, hover_gx, hover_gy)
return
}
if (action == 'select' && hover_gx >= 0 && hover_gx < 8 && hover_gy >= 0 && hover_gy < 8) {
clicked = [hover_gx, hover_gy]
cell = grid.at(clicked)
if (selectPos) {
is_valid = false
for (i = 0; i < length(validMoves); i++) {
if (validMoves[i][0] == clicked[0] && validMoves[i][1] == clicked[1]) {
is_valid = true
break
}
}
if (is_valid) {
src_piece = grid.at(selectPos)[0]
if (src_piece && mover.tryMove(src_piece, clicked)) {
board_view.sync_pieces(grid)
board_view.set_status(mover.turn + "'s turn")
if (client_actor) {
send(client_actor, game_state.serialize(grid, mover.turn))
}
}
selectPos = null
validMoves = []
} else if (length(cell) && cell[0].colour == 'white') {
selectPos = clicked
compute_valid_moves(selectPos)
} else {
selectPos = null
validMoves = []
}
} else {
if (length(cell) && cell[0].colour == 'white') {
selectPos = clicked
compute_valid_moves(selectPos)
}
}
board_view.highlight(selectPos, validMoves, hover_gx, hover_gy)
}
}
}
input.player1().possess(game_input)
var comp_config = {
clear: {r: 0.15, g: 0.15, b: 0.2, a: 1},
planes: [
{name: 'game', camera: game_cam, resolution: {width: GW, height: GH}, presentation: 'letterbox'},
{name: 'hud', camera: hud_cam, resolution: {width: GW, height: GH}, presentation: 'stretch'}
]
}
core.start({
width: 640, height: 640, title: "Chess - Host (White)",
input: function(ev) {
var wp = null
if (ev.type == 'mouse_motion') {
wp = game_cam.window_to_world(ev.pos[0], ev.pos[1])
if (wp) {
hover_gx = floor(wp.x / S)
hover_gy = floor(wp.y / S)
}
}
},
update: function(dt) {
board_view.highlight(selectPos, validMoves, hover_gx, hover_gy)
},
render: function() {
return compositor.execute(compositor.compile(comp_config))
}
})