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.
 
 

194 lines
5.7 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;
}
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;
this.attachSidekick(platform, number);
}
create() {
this.platformGroup = this.physics.add.group();
this.bgGroup = this.physics.add.group();
for (let i = 0; i < 2; i++) {
let bg = this.bgGroup.create(
this.game.config.width * (i + 1 / 2),
this.game.config.height / 2,
"bg"
);
bg.setImmovable(true);
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 = false;
this.ball.displayWidth = 120;
this.ball.displayHeight = 120;
this.ball.setSize(160, 250, true);
// this.ball.setScale(0.5);
let platformX = this.ball.x;
for (let i = 0; i < 4; i++) {
this.newPlatForm(platformX);
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.bgGroup.setVelocityX(-gameOptions.platformSpeed * 0.5);
this.platformGroup.setVelocityX(-gameOptions.platformSpeed);
}
stopPlatforms() {
this.bgGroup.setVelocityX(0);
this.platformGroup.setVelocityX(0);
}
getRightmostPlatform() {
let rightmostPlatform = 0;
this.platformGroup.getChildren().forEach((platform) => {
rightmostPlatform = Math.max(rightmostPlatform, platform.x);
});
return rightmostPlatform;
}
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(1);
setTimeout(() => {
this.newPlatForm(
this.getRightmostPlatform() -
Number(platform.name) * blockWidth +
Phaser.Math.Between(
gameOptions.platformDistanceRange[0],
gameOptions.platformDistanceRange[1]
)
);
});
}
this.platformGroup.remove(platform, true, true);
}
});
this.bgGroup.getChildren().forEach((bg) => {
if (bg.getBounds().right < 0) {
bg.x += this.game.config.width * 2;
}
}, this);
if (this.ball.y > this.game.config.height) {
this.game.onLose && this.game.onLose();
this.scene.pause();
}
}
}
export default PlayScene;