Skip to content

Commit

Permalink
more cleanup for controls/behavior on the board for breaks
Browse files Browse the repository at this point in the history
  • Loading branch information
MondayHopscotch committed Nov 6, 2023
1 parent 3303b60 commit 458e596
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 47 deletions.
138 changes: 115 additions & 23 deletions source/matching/MatchBoard.hx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package matching;

import flixel.tweens.FlxTween;
import matching.Chain.ChainNode;
import flixel.FlxG;
import flixel.math.FlxPoint;
Expand All @@ -8,17 +9,24 @@ import input.SimpleController;
import flixel.FlxSprite;

class MatchBoard extends FlxSprite {
private static var FAST_FALL_MOD = 5.0;
private static var FAST_FALL_MOD = 10.0;

private static var MOVE_REPEAT_INTERVAL = 0.25;

private var moveHoldTimer = 0.0;

private var boardWidth = 6;
private var boardHeight = 12;
public static var CELL_SIZE = 16;
var board = new Array<Array<MatchPiece>>();

// lets us know if the game is currently 'active' for the player, or if animations and such are playing
var playActive = true;

var activePair:MatchPair;
var fullySettled:Bool = false;

var breaks:Array<ChainNode> = [];
var chains:Array<ChainNode> = [];

var gravity = 1.0;

Expand Down Expand Up @@ -50,6 +58,32 @@ class MatchBoard extends FlxSprite {
// can't move
}
}
if (SimpleController.pressed(LEFT)) {
moveHoldTimer += elapsed;
if (moveHoldTimer >= MOVE_REPEAT_INTERVAL) {
moveHoldTimer -= MOVE_REPEAT_INTERVAL;
// TODO: Consolidate this small move check
if (activePair.moveLeft()) {
// great!
} else {
// can't move
}
}
} else if (SimpleController.pressed(RIGHT)) {
moveHoldTimer += elapsed;
if (moveHoldTimer >= MOVE_REPEAT_INTERVAL) {
moveHoldTimer -= MOVE_REPEAT_INTERVAL;
// TODO: Consolidate this small move check
if (activePair.moveRight()) {
// great!
} else {
// can't move
}
}
} else {
moveHoldTimer = 0;
}

if (SimpleController.just_pressed(RIGHT)) {
if (activePair.moveRight()) {
// great!
Expand All @@ -75,31 +109,53 @@ class MatchBoard extends FlxSprite {
activePair.finish();
activePair = null;

// reset our settled flag to ensure breaks are checked
// reset our settled flag to ensure chains are checked
fullySettled = false;
}
} else if (fullySettled) {
} else if (fullySettled && playActive) {
sendPiece();
}

checkSettled();

for (chain in breaks) {
#if debug
debugDraw();
#end

for (chain in chains) {
#if debug
chain.debugDraw();
#end

if (chain.count() >= 4) {
clearChain(chain);
breaks.remove(chain); // does this break the iterator?
}
}
function debugDraw() {
for (x in 0...board.length) {
for (y in 0...board[x].length) {
if (board[x][y] != null) {
DebugDraw.ME.drawWorldCircle(x * CELL_SIZE, y * CELL_SIZE, 3);
}
}
}
}

function clearChain(chain:ChainNode) {
playActive = false;

// TODO: need to do animations / wait for animation to finish before continuing game
var first = true;
chain.forEachNode((piece) -> {
board[piece.cx][piece.cy] = null;
FlxG.state.remove(piece);
FlxTween.tween(piece, {alpha: 0}, {
onComplete: (t) -> {
board[piece.cx][piece.cy] = null;
FlxG.state.remove(piece);

if (first) {
first = false;
// explicitly set this to false to give a chance for any further combos to carry out
fullySettled = false;
}
}
});
});
}

Expand All @@ -114,10 +170,10 @@ class MatchBoard extends FlxSprite {
continue;
}

piece.checkSettled();
updatePieceSettled(piece);
if (!piece.settled) {
piece.yVel += gravity;
if (!piece.checkSettled()) {
if (!updatePieceSettled(piece)) {
// XXX: This just feels like it has potential to be buggy
board[piece.cx][piece.cy] = null;
piece.updateCoords();
Expand All @@ -129,15 +185,44 @@ class MatchBoard extends FlxSprite {
}

if (!fullySettled && settleCheck) {
// TODO: Check for breaks and do all animations
checkBreaks();
// set this to true, let any subsequent chains detected set it back to false, if needed
playActive = true;

checkChains();
}

fullySettled = settleCheck;
}

function checkBreaks() {
breaks = [];
function updatePieceSettled(piece:MatchPiece):Bool {
if (piece.yr > 0.5 && hasCollision(piece.cx, piece.cy + 1)) {
piece.yr = 0;
piece.yVel = 0;
piece.settled = true;
// piece.updateCoords();
// board[piece.cx][piece.cy] = piece;
} else if (!hasCollision(piece.cx, piece.cy + 1)) {
// board[piece.cx][piece.cy] = null;
piece.settled = false;
var y = piece.cy;
while (y > 0) {
var p = board[piece.cx][y];
if (p != null) {
// This doesn't work as intended because the pieces still occupy the grid cell while falling, so
// these pieces are likely having `settled` set immediately back to true until the piece under it falls
// entirely to a new grid cell.

// possible solutions... don't occupy a cell until a piece is settled?
p.settled = false;
}
y--;
}
}
return piece.settled;
}

function checkChains() {
chains = [];
var checked:Array<MatchPiece> = [];
for (column in board) {
for (y in 0...column.length) {
Expand All @@ -148,11 +233,18 @@ class MatchBoard extends FlxSprite {
chain.addPiecesTo(checked);

if (chain.count() > 1) {
breaks.push(chain);
chains.push(chain);
}
}
}
}

for (chain in chains) {
if (chain.count() >= 4) {
clearChain(chain);
chains.remove(chain); // does this break the iterator?
}
}
}

function findConnected(chain:ChainNode = null, visited:Array<MatchPiece>):ChainNode {
Expand Down Expand Up @@ -191,20 +283,20 @@ class MatchBoard extends FlxSprite {
var b = activePair.b;
// Check bottom-up so we proprogate the settled-ness properly
if (b.cy > a.cy) {
if (b.checkSettled()) {
if (updatePieceSettled(b)) {
board[b.cx][b.cy] = b;
board[a.cx][a.cy] = a;
}
if (a.checkSettled()) {
if (updatePieceSettled(a)) {
board[a.cx][a.cy] = a;
board[b.cx][b.cy] = b;
}
} else {
if (a.checkSettled()) {
if (updatePieceSettled(a)) {
board[a.cx][a.cy] = a;
board[b.cx][b.cy] = b;
}
if (b.checkSettled()) {
if (updatePieceSettled(b)) {
board[a.cx][a.cy] = a;
board[b.cx][b.cy] = b;
}
Expand All @@ -220,6 +312,6 @@ class MatchBoard extends FlxSprite {
return true;
}

return board[cx][cy] != null;
return board[cx][cy] != null && board[cx][cy].settled;
}
}
15 changes: 1 addition & 14 deletions source/matching/MatchPair.hx
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,6 @@ class MatchPair {
return FlxMath.maxInt(a.cx, b.cx);
}

public function shift(dir:Cardinal) {
switch(dir) {
case E:
a.cx--;
b.cx--;
case W:
a.cx++;
b.cx++;
default:
}
}

// clockwise spin
public function spin():Bool {
// TODO: Animations for this? Tweens? Pause game while spin happens?
Expand Down Expand Up @@ -103,7 +91,7 @@ class MatchPair {
// standard spin
b.cx = a.cx - 1;
b.cy = a.cy;
} else if (!a.parent.hasCollision(a.cx - 1, a.cy)) {
} else if (!a.parent.hasCollision(a.cx + 1, a.cy)) {
// against a blocker, a moves away from blocker, b takes its place
b.cx = a.cx;
b.cy = a.cy;
Expand Down Expand Up @@ -147,7 +135,6 @@ class MatchPair {
a.sibling = null;
b.sibling = null;
}


public function adjust(x:Float, y:Float) {
a.xr += x;
Expand Down
21 changes: 11 additions & 10 deletions source/matching/MatchPiece.hx
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,15 @@ class MatchPiece extends FlxSprite {
return changed;
}

public function checkSettled():Bool {
if (yr > 0.5 && parent.hasCollision(cx, cy + 1)) {
yr = 0;
yVel = 0;
settled = true;
} else if (!parent.hasCollision(cx, cy + 1)) {
settled = false;
}
return settled;
}
// public function checkSettled():Bool {
// if (yr > 0.5 && parent.hasCollision(cx, cy + 1)) {
// yr = 0;
// yVel = 0;
// settled = true;
// } else if (!parent.hasCollision(cx, cy + 1)) {
// settled = false;
// parent.unsettleUp(cx, cy);
// }
// return settled;
// }
}

0 comments on commit 458e596

Please sign in to comment.