Skip to content

Commit

Permalink
first pass of break scoring / combos
Browse files Browse the repository at this point in the history
  • Loading branch information
MondayHopscotch committed Nov 6, 2023
1 parent 458e596 commit 75f70d7
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 3 deletions.
34 changes: 31 additions & 3 deletions source/matching/MatchBoard.hx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class MatchBoard extends FlxSprite {

var chains:Array<ChainNode> = [];

var comboCount = 0;

var gravity = 1.0;

public function new() {
Expand Down Expand Up @@ -199,10 +201,7 @@ class MatchBoard extends FlxSprite {
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) {
Expand Down Expand Up @@ -239,12 +238,39 @@ class MatchBoard extends FlxSprite {
}
}

var breaks:Array<ChainNode> = [];
for (chain in chains) {
if (chain.count() >= 4) {
breaks.push(chain);
}
}

if (breaks.length > 0) {
comboCount++;
score(breaks);
for (chain in breaks) {
clearChain(chain);
chains.remove(chain); // does this break the iterator?
}
QuickLog.notice('Combo: $comboCount');
} else {
// reset combo counter if nothing breaks
if (comboCount != 0) {
QuickLog.notice('---Combo reset---');
}
comboCount = 0;
}
}

function score(breaks:Array<ChainNode>) {
var baseScore = 0;
for (chain in breaks) {
baseScore += Scoring.baseScore(chain.count());
}
var comboMult = Scoring.comboScalar(comboCount);
var multiBreak = Scoring.multiBreakScalar(breaks.length);
var finalScore = baseScore * comboMult * multiBreak;
QuickLog.notice(' Points: $finalScore ($baseScore * $comboMult * $multiBreak)');
}

function findConnected(chain:ChainNode = null, visited:Array<MatchPiece>):ChainNode {
Expand Down Expand Up @@ -312,6 +338,8 @@ class MatchBoard extends FlxSprite {
return true;
}

// falling pieces aren't considered collidable. This is useful for having a column of blocks
// fall together if a block near the bottom breaks
return board[cx][cy] != null && board[cx][cy].settled;
}
}
54 changes: 54 additions & 0 deletions source/matching/Scoring.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package matching;

import flixel.math.FlxMath;

class Scoring {

// score based on number of pieces connected together upon break
private static var baseBreakScore = [
4 => 10,
5 => 15,
6 => 20,
7 => 30,
8 => 40,
9 => 50,
10 => 60,
11 => 70,
12 => 100, // this is theoretically the largest break you can form (I think...)
];

// muliplier based on number of breaks combo'd together without dropping new pieces into the board
private static var comboMultiplier = [
1 => 1,
2 => 1.2,
3 => 1.5,
4 => 2,
5 => 3, // max combo multiplier
];

/* multiplier based on making multiple breaks at the same time
* Example mutibreak '2':
* |a|b|
* |
* V
* |a|a|a| |b|b|b|
*/
private static var multibreakMultiplier = [
1 => 1,
2 => 1.1,
3 => 1.2,
4 => 1.5, // max multibreak multiplier (do we want this?)
];

public static function baseScore(n:Int):Int {
return baseBreakScore.get(Std.int(FlxMath.bound(n, 4, 12)));
}

public static function comboScalar(n:Int) {
return comboMultiplier.get(Std.int(FlxMath.bound(n, 1, 5)));
}

public static function multiBreakScalar(n:Int) {
return multibreakMultiplier.get(Std.int(FlxMath.bound(n, 1, 4)));
}
}

0 comments on commit 75f70d7

Please sign in to comment.