~sircmpwn/tetrominoes

cc5cb0de09974748760f47e4ea8dd0124c9f775f — Drew DeVault 2 years ago 3d9c3e6
Refactor collision detection
2 files changed, 28 insertions(+), 25 deletions(-)

M notes.txt
M update.ha
M notes.txt => notes.txt +0 -1
@@ 1,7 1,6 @@
- ghost blocks
- instant place
- fix thumbsticks
- fix L rotations
- scorekeeping/display
- clear completed rows (+animation)
- collision detection on rotations

M update.ha => update.ha +28 -24
@@ 105,7 105,8 @@ fn do_spawn(state: *state) void = {
fn do_fall(state: *state) void = {
	let height = 0;
	activesize(state, null, &height);
	if (canmove(state, 0, 1)) {
	const active = &state.board.active[state.board.active_i];
	if (canmove(state, active, 0, 1)) {
		state.board.active_y += 1;
	} else {
		commit(state);


@@ 113,29 114,6 @@ fn do_fall(state: *state) void = {
	};
};

fn canmove(state: *state, dx: int, dy: int) bool = {
	let board = &state.board.cells;
	let active = &state.board.active[state.board.active_i];
	const active_x = state.board.active_x;
	const active_y = state.board.active_y;
	for (let y = 0; y < TETROMINO_HEIGHT; y += 1)
	for (let x = 0; x < TETROMINO_WIDTH; x += 1) {
		const active_ix = y * TETROMINO_WIDTH + x;
		if (active[active_ix] == 0) {
			continue;
		};
		if (y + active_y < 0) {
			continue;
		};
		const x = x + dx, y = y + dy;
		const board_ix = (y + active_y) * BOARD_WIDTH + (x + active_x);
		if (board_ix: size >= len(board) || board[board_ix] != 0) {
			return false;
		};
	};
	return true;
};

fn commit(state: *state) void = {
	let board = &state.board.cells;
	let active = &state.board.active[state.board.active_i];


@@ 203,3 181,29 @@ fn activesize(
	case null => void;
	};
};

fn canmove(
	state: *state,
	active: *[TETROMINO_LEN]color,
	dx: int, dy: int,
) bool = {
	let board = &state.board.cells;
	const active_x = state.board.active_x;
	const active_y = state.board.active_y;
	for (let y = 0; y < TETROMINO_HEIGHT; y += 1)
	for (let x = 0; x < TETROMINO_WIDTH; x += 1) {
		const active_ix = y * TETROMINO_WIDTH + x;
		if (active[active_ix] == 0) {
			continue;
		};
		if (y + active_y < 0) {
			continue;
		};
		const x = x + dx, y = y + dy;
		const board_ix = (y + active_y) * BOARD_WIDTH + (x + active_x);
		if (board_ix: size >= len(board) || board[board_ix] != 0) {
			return false;
		};
	};
	return true;
};