You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
190 lines
5.5 KiB
190 lines
5.5 KiB
import Phaser from "phaser";
|
|
import { gameOptions } from "../constant/index";
|
|
function importAll(r) {
|
|
let obj = {};
|
|
r.keys().forEach((key) => {
|
|
obj[key.replace("./", "").replace(".png", "")] = r(key);
|
|
});
|
|
return obj;
|
|
}
|
|
|
|
const images = importAll(
|
|
require.context("../assets/ballmove", false, /\.(png|jpe?g|svg)$/)
|
|
);
|
|
const blockNames = ["bu", "gr", "og", "pu", "red", "ye", "fr", "ice"];
|
|
const blockWidth = (2 / 3) * 100;
|
|
const blockHeight = blockWidth * 2;
|
|
class PlayScene extends Phaser.Scene {
|
|
constructor() {
|
|
super({
|
|
key: "PlayScene",
|
|
});
|
|
}
|
|
|
|
preload() {
|
|
Object.entries(images).forEach(([k, v]) => {
|
|
this.load.image(k, v);
|
|
});
|
|
}
|
|
newBlock(x, y, tex) {
|
|
const block = this.platformGroup.create(x, y, tex);
|
|
block.setBounce(0, 1);
|
|
block.body.checkCollision.down = false;
|
|
block.body.checkCollision.up = true;
|
|
block.body.checkCollision.left = false;
|
|
block.body.checkCollision.right = false;
|
|
return block;
|
|
}
|
|
attachSidekick(platform, num) {
|
|
const {
|
|
x,
|
|
y,
|
|
texture: { key },
|
|
} = platform;
|
|
switch (num) {
|
|
case 2:
|
|
platform.x -= blockWidth / 2;
|
|
{
|
|
let platform = this.newBlock(x + blockWidth / 2, y, key);
|
|
platform.setImmovable(true);
|
|
platform.displayWidth = blockWidth;
|
|
platform.displayHeight = blockHeight;
|
|
}
|
|
break;
|
|
case 3:
|
|
platform.x += blockWidth;
|
|
{
|
|
let platform = this.newBlock(x, y, key);
|
|
platform.setImmovable(true);
|
|
platform.displayWidth = blockWidth;
|
|
platform.displayHeight = blockHeight;
|
|
}
|
|
{
|
|
let platform = this.newBlock(x - blockWidth, y, key);
|
|
platform.setImmovable(true);
|
|
platform.displayWidth = blockWidth;
|
|
platform.displayHeight = blockHeight;
|
|
platform.body.checkCollision.left = true;
|
|
}
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
newPlatForm(x) {
|
|
const y =
|
|
(this.game.config.height / 6) * 5 +
|
|
Phaser.Math.Between(
|
|
gameOptions.platformHeightRange[0],
|
|
gameOptions.platformHeightRange[1]
|
|
);
|
|
const tex = blockNames[Math.floor(Math.random() * blockNames.length)];
|
|
let platform = this.newBlock(x, y, tex);
|
|
platform.setImmovable(true);
|
|
platform.displayWidth = blockWidth;
|
|
platform.displayHeight = blockHeight;
|
|
|
|
const number = Math.floor(Math.random() * 3 + 1);
|
|
platform.name = number;
|
|
if (number === 2 || number === 1) platform.body.checkCollision.left = true;
|
|
this.attachSidekick(platform, number);
|
|
}
|
|
create() {
|
|
this.physics.world.setFPS(30);
|
|
this.platformGroup = this.physics.add.group();
|
|
const bg = this.add.image(
|
|
this.game.config.width / 2,
|
|
this.game.config.height / 2,
|
|
"bg"
|
|
);
|
|
bg.displayWidth = this.game.config.width;
|
|
bg.displayHeight = this.game.config.height;
|
|
this.ball = this.physics.add.sprite(
|
|
this.game.config.width * gameOptions.ballPosition,
|
|
(this.game.config.height / 4) * 3 - gameOptions.bounceHeight,
|
|
"ball"
|
|
);
|
|
this.ball.body.gravity.y = gameOptions.ballGravity;
|
|
this.ball.setBounce(0, 1);
|
|
this.ball.body.checkCollision.down = true;
|
|
this.ball.body.checkCollision.up = false;
|
|
this.ball.body.checkCollision.left = false;
|
|
this.ball.body.checkCollision.right = true;
|
|
this.ball.displayWidth = 120;
|
|
this.ball.displayHeight = 120;
|
|
this.ball.setSize(160, 250, true);
|
|
// this.ball.setScale(0.5);
|
|
this.platformX = this.ball.x;
|
|
|
|
for (let i = 0; i < 4; i++) {
|
|
this.newPlatForm(this.platformX);
|
|
this.platformX += Phaser.Math.Between(
|
|
gameOptions.platformDistanceRange[0],
|
|
gameOptions.platformDistanceRange[1]
|
|
);
|
|
}
|
|
this.input.on("pointerdown", this.movePlatforms, this);
|
|
this.input.on("pointerup", this.stopPlatforms, this);
|
|
this.score = 0;
|
|
this.scoreText1 = this.add.text(20, 20, "", {
|
|
fontSize: 20,
|
|
});
|
|
this.updateScore(this.score);
|
|
}
|
|
updateScore(inc) {
|
|
this.score += inc;
|
|
this.scoreText1.text = "Game Score: " + this.score;
|
|
}
|
|
|
|
movePlatforms() {
|
|
this.platformGroup.setVelocityX(-gameOptions.platformSpeed);
|
|
}
|
|
stopPlatforms() {
|
|
this.platformGroup.setVelocityX(0);
|
|
}
|
|
getRightmostPlatform() {
|
|
return this.platformGroup
|
|
.getChildren()
|
|
.filter(({ name }) => name)
|
|
.reduce(
|
|
(rightmostPlatform, platform) =>
|
|
Math.max(rightmostPlatform, platform.x),
|
|
0
|
|
);
|
|
}
|
|
update() {
|
|
const result = this.physics.world.collide(this.platformGroup, this.ball);
|
|
if (
|
|
result &&
|
|
this.ball.body.velocity.y > 0 &&
|
|
this.ball.y + 60 < this.game.config.height
|
|
) {
|
|
this.ball.body.velocity.y = -this.ball.body.velocity.y;
|
|
}
|
|
const children = this.platformGroup.getChildren();
|
|
children.forEach((platform) => {
|
|
if (platform.getBounds().right < 0) {
|
|
if (platform.name) {
|
|
this.updateScore(3 - Number(platform.name));
|
|
this.stopPlatforms();
|
|
const platformX =
|
|
this.getRightmostPlatform() +
|
|
Phaser.Math.Between(
|
|
gameOptions.platformDistanceRange[0],
|
|
gameOptions.platformDistanceRange[1]
|
|
);
|
|
this.newPlatForm(platformX);
|
|
this.movePlatforms();
|
|
}
|
|
this.platformGroup.remove(platform, true, true);
|
|
}
|
|
});
|
|
|
|
if (this.ball.y > this.game.config.height) {
|
|
this.game.onLose && this.game.onLose();
|
|
this.scene.pause();
|
|
}
|
|
}
|
|
}
|
|
|
|
export default PlayScene;
|
|
|