Files
cell/examples/chess/main.js
John Alanbrook 901012064a
Some checks failed
Build and Deploy / build-windows (CLANG64) (push) Has been cancelled
Build and Deploy / package-dist (push) Has been cancelled
Build and Deploy / deploy-itch (push) Has been cancelled
Build and Deploy / deploy-gitea (push) Has been cancelled
Build and Deploy / build-macos (push) Has been cancelled
Build and Deploy / build-linux (push) Has been cancelled
add chess example
2025-05-18 08:57:34 -05:00

118 lines
4.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/* main.js runs the demo with your prototype-based grid */
var res = 160
var internal_res = 480
var render = use('render');
render.initialize({ width:res, height:res, resolution_x:internal_res, resolution_y:internal_res, mode:'letterbox' });
var os = use('os');
var draw2d = use('draw2d');
var gfx = use('graphics');
/* camera unchanged */
var camera = { size:[internal_res,internal_res], transform:os.make_transform(), fov:50, near_z:0,
far_z:1000, surface:undefined, viewport:{x:0,y:0,width:1,height:1},
ortho:true, anchor:[0,0] };
/*──── import our pieces + systems ───────────────────────────────────*/
var Grid = use('grid'); // your new ctor
var MovementSystem = use('movement').MovementSystem;
var startingPos = use('pieces').startingPosition;
var rules = use('rules');
/*──── build board ───────────────────────────────────────────────────*/
var grid = new Grid(8, 8);
grid.width = 8; // (the ctor didnt store them)
grid.height = 8;
var mover = new MovementSystem(grid, rules);
startingPos(grid);
/*──── mouse → click-to-move ─────────────────────────────────────────*/
var selectPos = null;
prosperon.window.on_mousedown = function (btn, mx, my) {
if (btn !== 0) return;
var c = [Math.floor(mx / 60), Math.floor(my / 60)];
if (!grid.inBounds(c)) return;
var cell = grid.at(c);
if (selectPos && mover.tryMove(grid.at(selectPos)[0], c)) {
selectPos = null; // made a move
return;
}
if (cell.length && cell[0].colour === mover.turn) {
selectPos = c; // pick up piece
} else {
selectPos = null;
}
};
/*──── drawing helpers ───────────────────────────────────────────────*/
/* ── constants ─────────────────────────────────────────────────── */
var S = 60; // square size in px
var light = [0.93,0.93,0.93,1];
var dark = [0.25,0.25,0.25,1];
/* ── draw one 8×8 chess board ──────────────────────────────────── */
function drawBoard() {
for (var y = 0; y < 8; ++y)
for (var x = 0; x < 8; ++x) {
draw2d.rectangle(
{ x: x*S, y: y*S, width: S, height: S },
{ thickness: 0, color: ((x+y)&1) ? dark : light }
);
}
}
/* ── draw every live piece ─────────────────────────────────────── */
function drawPieces() {
grid.each(function (piece) {
if (piece.captured) return;
var r = { x: piece.coord[0]*S, y: piece.coord[1]*S,
width:S, height:S };
draw2d.image(piece.sprite, r, 0, [0,0], [0,0], {mode:"nearest"});
});
}
/*──── main loop ─────────────────────────────────────────────────────*/
var last = os.now(), fpsTimer = 0, fpsCount = 0;
function loop() {
var now = os.now(), dt = now - last; last = now;
render.clear([22/255, 120/255, 194/255, 1]);
render.camera(camera);
drawBoard();
drawPieces();
render.present();
fpsTimer += dt; fpsCount++;
if (fpsTimer >= 0.5) {
prosperon.window.title =
'Chess demo ' + mover.turn + '\'s move FPS ' + (fpsCount / fpsTimer).toFixed(1);
fpsTimer = fpsCount = 0;
}
$_.delay(loop, Math.max(0, (1 / 60) - (os.now() - now)));
}
loop();
var os = use('os')
var ioguy = {
__ACTORDATA__: {
id: os.ioactor()
}
}
$_.send(ioguy, {
type: "subscribe",
actor: $_
})
$_.receiver(e => {
if (e.type === 'quit')
os.exit()
else
console.log(json.encode(e))
})