合体すると大きくなる
前の節では合体しても大きさは同じでした。この節では、合体したらひと回り大きいボールを 新しく作り直すようにします。スイカゲームの「合体で育つ」核です。
合体では「両方消して、少し大きい 1 個を中間の位置に作る」という流れにします。ボールを作る処理は これから何度も使うので、まずメソッドに切り出します。
今回さわる
game/:scenes/game-scene.jsを書き換え
前の節を終えていない人は、ここから始められます。
ボール生成をメソッドにまとめる
Section titled “ボール生成をメソッドにまとめる”create() の中に直接書いていた「円を作って物理ボディにする」を、spawnBall(x, y, radius) に
まとめます。半径は判定でも使うので、ボール自身に radius を持たせておきます。
scenes/game-scene.js を変更する
Section titled “scenes/game-scene.js を変更する”1313 bottom: 540,1414 wall: 121515};16const ballRadius = 20;17const grow = 10;1618
17this.radius = 25;1819this.lineY = 190;1920this.balls = [];2021this.pendingRemoval = new Set();22this.pendingMerge = [];2123this.isOver = false;2224
2325const g = this.add.graphics();4951 const dropY = 100;5052
5153 // 箱の内側からはみ出さないよう X を制限する52 const x = Phaser.Math.Clamp(pointer.x, box.left + this.radius, box.right - this.radius);54 const x = Phaser.Math.Clamp(pointer.x, box.left + ballRadius, box.right - ballRadius);5355
54 const ball = this.add.circle(x, dropY, this.radius, 0x333333);55 this.matter.add.gameObject(ball, {56 shape: {57 type: 'circle',58 radius: this.radius59 },60 restitution: 0.2, // 跳ね返りの強さ(0=跳ねない 〜 1=よく跳ねる)61 });62
56 const ball = this.spawnBall(x, dropY, ballRadius);6357 this.balls.push(ball);6458 });6559
6660 this.matter.world.on('collisionstart', (event) => {6761 for (const pair of event.pairs) {68 // gameObject を持つのがボール(壁は持たない)6962 const ballA = pair.bodyA.gameObject;7063 const ballB = pair.bodyB.gameObject;7164 if (!ballA || !ballB) continue;7265 if (this.pendingRemoval.has(ballA) || this.pendingRemoval.has(ballB)) continue;66
67 this.pendingRemoval.add(ballA);7368 this.pendingRemoval.add(ballB);69 this.pendingMerge.push({70 x: (ballA.x + ballB.x) / 2,71 y: (ballA.y + ballB.y) / 2,72 radius: ballA.radius + grow,73 });7474 }7575 });7676}7777
78spawnBall(x, y, radius) {79 const ball = this.add.circle(x, y, radius, 0x333333);80 ball.radius = radius; // 線の判定などで使うので半径を持たせておく81 this.matter.add.gameObject(ball, {82 shape: {83 type: 'circle',84 radius: radius85 },86 restitution: 0.2, // 跳ね返りの強さ(0=跳ねない 〜 1=よく跳ねる)87 });88 return ball;89}90
7891update(time, delta) {7992 if (this.pendingRemoval.size > 0) {8093 for (const ball of this.pendingRemoval) {8497 this.pendingRemoval.clear();8598}8699
100if (this.pendingMerge.length > 0) {101 for (const m of this.pendingMerge) {102 const ball = this.spawnBall(m.x, m.y, m.radius);103 this.balls.push(ball);104 }105 this.pendingMerge.length = 0;106}107
87108if (this.isOver) return;88109
89110// 落下中は速いので、止まって線を超え続けたときだけゲームオーバー(誤検出を防ぐ)90111for (const ball of this.balls) {91112 if (!ball.body) continue;92113 const isSlow = ball.body.speed < 1;93 const top = ball.y - this.radius;114 const top = ball.y - ball.radius;94115 if (!isSlow || top >= this.lineY) {95116 ball.overLineTime = 0;96117 continue;create() の落とす処理は、このメソッドを呼ぶだけになります。半径の基準値 ballRadius(20)と
合体で足す量 grow(10)も用意します。
X を箱に収める Clamp も、this.radius ではなく ballRadius を使うように直します。
合体するときの情報を貯める
Section titled “合体するときの情報を貯める”消すのと同じように、「新しく作る大きいボール」もその場では作らず予定に貯めます。
create() の状態に this.pendingMerge = [] を足し、衝突処理で 両方を消す予定にしつつ、
合体後のボールの情報を積みます。
中間の位置に、radius + grow の大きさで作る、という予定です。
予定どおり新しいボールを作る
Section titled “予定どおり新しいボールを作る”update() で、消す処理のあとに合体予定を処理します。
ゲームオーバー判定の top も、固定の this.radius ではなく、ボールごとの ball.radius を
使うように直します(const top = ball.y - ball.radius;)。
同じボールをぶつけると、消えてひと回り大きいボールが生まれます。何度か重ねて、だんだん 大きく育つのを確かめてください。次の節で、大きさに応じて色を変えます。