コンテンツにスキップ

合体すると大きくなる

合体すると大きくなる

前の節では合体しても大きさは同じでした。この節では、合体したらひと回り大きいボールを 新しく作り直すようにします。スイカゲームの「合体で育つ」核です。

合体では「両方消して、少し大きい 1 個を中間の位置に作る」という流れにします。ボールを作る処理は これから何度も使うので、まずメソッドに切り出します。

今回さわる game/: scenes/game-scene.js を書き換え

前の節を終えていない人は、ここから始められます。

この節を始めるときのファイル

ボール生成をメソッドにまとめる

Section titled “ボール生成をメソッドにまとめる”

create() の中に直接書いていた「円を作って物理ボディにする」を、spawnBall(x, y, radius) に まとめます。半径は判定でも使うので、ボール自身に radius を持たせておきます。

scenes/game-scene.js
13
13
bottom: 540,
14
14
wall: 12
15
15
};
16
const ballRadius = 20;
17
const grow = 10;
16
18
17
this.radius = 25;
18
19
this.lineY = 190;
19
20
this.balls = [];
20
21
this.pendingRemoval = new Set();
22
this.pendingMerge = [];
21
23
this.isOver = false;
22
24
23
25
const g = this.add.graphics();
scenes/game-scene.js
49
51
const dropY = 100;
50
52
51
53
// 箱の内側からはみ出さないよう 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);
53
55
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.radius
59
},
60
restitution: 0.2, // 跳ね返りの強さ(0=跳ねない 〜 1=よく跳ねる)
61
});
62
56
const ball = this.spawnBall(x, dropY, ballRadius);
63
57
this.balls.push(ball);
64
58
});
65
59
66
60
this.matter.world.on('collisionstart', (event) => {
67
61
for (const pair of event.pairs) {
68
// gameObject を持つのがボール(壁は持たない)
69
62
const ballA = pair.bodyA.gameObject;
70
63
const ballB = pair.bodyB.gameObject;
71
64
if (!ballA || !ballB) continue;
72
65
if (this.pendingRemoval.has(ballA) || this.pendingRemoval.has(ballB)) continue;
66
67
this.pendingRemoval.add(ballA);
73
68
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
});
74
74
}
75
75
});
76
76
}
77
77
78
spawnBall(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: radius
85
},
86
restitution: 0.2, // 跳ね返りの強さ(0=跳ねない 〜 1=よく跳ねる)
87
});
88
return ball;
89
}
90
78
91
update(time, delta) {
79
92
if (this.pendingRemoval.size > 0) {
80
93
for (const ball of this.pendingRemoval) {
scenes/game-scene.js
84
97
this.pendingRemoval.clear();
85
98
}
86
99
100
if (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
87
108
if (this.isOver) return;
88
109
89
110
// 落下中は速いので、止まって線を超え続けたときだけゲームオーバー(誤検出を防ぐ)
90
111
for (const ball of this.balls) {
91
112
if (!ball.body) continue;
92
113
const isSlow = ball.body.speed < 1;
93
const top = ball.y - this.radius;
114
const top = ball.y - ball.radius;
94
115
if (!isSlow || top >= this.lineY) {
95
116
ball.overLineTime = 0;
96
117
continue;

create() の落とす処理は、このメソッドを呼ぶだけになります。半径の基準値 ballRadius(20)と 合体で足す量 grow(10)も用意します。

X を箱に収める Clamp も、this.radius ではなく ballRadius を使うように直します。

消すのと同じように、「新しく作る大きいボール」もその場では作らず予定に貯めます。 create() の状態に this.pendingMerge = [] を足し、衝突処理で 両方を消す予定にしつつ、 合体後のボールの情報を積みます。

中間の位置に、radius + grow の大きさで作る、という予定です。

予定どおり新しいボールを作る

Section titled “予定どおり新しいボールを作る”

update() で、消す処理のあとに合体予定を処理します。

ゲームオーバー判定の top も、固定の this.radius ではなく、ボールごとの ball.radius を 使うように直します(const top = ball.y - ball.radius;)。

同じボールをぶつけると、消えてひと回り大きいボールが生まれます。何度か重ねて、だんだん 大きく育つのを確かめてください。次の節で、大きさに応じて色を変えます。

↗ 新しいタブで開く
このステップの完成イメージ(実際に触って動かせます)

この節を終えたときのディレクトリ

Section titled “この節を終えたときのディレクトリ”
この節を終えたときのファイル