function preload() { this.load.image("ball", "pangball.png"); }; const width = 400; const height = 400; const POWER = 0.5; function create() { globalThis.game = this; this.matter.world.setBounds(0, 0, width, height); this.player = this.matter.add.image( width / 2, height / 2, "ball" ) .setAngle(270) .setCircle() .setFixedRotation() .setFrictionAir(0.05) .setMass(300) .setName("player"); const { Bodies } = Phaser.Physics.Matter.Matter; const position = 16; const ray = 10; const base = Bodies.circle(0, 0, 16); const circleA = Bodies.circle(-position, 0, ray, { isSensor: true, label: 'left' }); const circleB = Bodies.circle(position, 0, ray, { isSensor: true, label: 'right' }); const circleC = Bodies.circle(0, -position, ray, { isSensor: true, label: 'top' }); const circleD = Bodies.circle(0, position, ray, { isSensor: true, label: 'bottom' }); const compoundBody = Phaser.Physics.Matter.Matter.Body.create({ angle: 270, frictionAir: 0.05, mass: 300, parts: [base, circleA, circleB, circleC, circleD], inertia: Infinity }); this.ball = this.matter.add.image(0, 0, "ball") .setExistingBody(compoundBody) .setPosition(width / 2 + 50, height / 2) .setName("ball"); this.ball.setFixedRotation(); this.moveInputs = this.input.keyboard.addKeys({ up: "up", down: "down", left: "left", right: "right", x: "x" }); this.collWithBall = false; this.sensorLabel; this.matter.world.on("collisionstart", (event, bodyA, bodyB) => { if (bodyA?.gameObject?.name && bodyB?.gameObject?.name) { console.log(bodyA?.gameObject?.name, bodyB?.gameObject?.name); this.collWithBall = true; this.sensorLabel = getSensorLabel(event.pairs); //console.log( ... event.pairs); //if (this.moveInputs.x.isDown) { //kickBall(event.pairs); // }; } else { this.collWithBall = false; }; }); }; function update() { const { player } = this; if (this.moveInputs.left.isDown) { player.thrustBack(0.1); } else if (this.moveInputs.right.isDown) { player.thrust(0.1); }; if (this.moveInputs.up.isDown) { player.thrustLeft(0.1); } else if (this.moveInputs.down.isDown) { player.thrustRight(0.1); }; if (this.moveInputs.x.isDown && this.collWithBall) { kickBall(this.sensorLabel); }; }; function getSensorLabel (collisionPairs) { let label; collisionPairs.forEach(pair => { if (pair.isSensor) { const body = pair.bodyB; label = body.label; ball = body.gameObject; console.log("it's a sensor"); console.log("object B", { label, name: ball.name }); console.log("object A", { label: pair.bodyA.label, name: pair.bodyA.gameObject?.name }); } }); return label; }; function kickBall (direction) { if (direction === 'left') { this.ball.thrust(POWER); } else if (direction === 'right') { this.ball.thrustBack(POWER); console.log("yes_right"); } else if (direction === 'top') { this.ball.thrustRight(POWER); } else if (direction === 'bottom') { this.ball.thrustLeft(POWER); } else { console.log("not a label", { direction }); }; this.collWithBall = false; }; function initGame() { const game = new Phaser.Game({ type: Phaser.AUTO, width, height, parent: "game", backgroundColor: '#1b1464', physics: { default: "matter", matter: { debug: true, gravity: { x: 0, y: 0 } } }, scene: { preload, create, update, kickBall } }); }; document.addEventListener("DOMContentLoaded", initGame); {"mode":"full","isActive":false}