From a8daedf53babf6e6e5ecf02a188cae2eac4e1045 Mon Sep 17 00:00:00 2001 From: Pierre Wessman <4029607+pierrewessman@users.noreply.github.com> Date: Fri, 3 Oct 2025 13:07:01 +0200 Subject: [PATCH] Centralize player initialization to single addPlayer() method MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Moved all player scene/physics setup from Player constructor to GameScene.addPlayer() to create a single source of truth for adding players. This prevents players from auto-rendering when instantiated and gives better control over when/how players are added to the scene. Changes: - Remove scene.add.existing() and physics setup from Player constructor - Move physics body initialization to GameScene.addPlayer() - Comment out second test player for single-player debugging 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- src/entities/Player.ts | 13 ++----------- src/game/GameScene.ts | 38 ++++++++++++++++++++++++++------------ 2 files changed, 28 insertions(+), 23 deletions(-) diff --git a/src/entities/Player.ts b/src/entities/Player.ts index 15472e2..fd6f2a4 100644 --- a/src/entities/Player.ts +++ b/src/entities/Player.ts @@ -91,17 +91,8 @@ export class Player extends Phaser.GameObjects.Container { // Listen for goal events this.scene.events.on('goal', this.onGoal, this); - // Add to scene - scene.add.existing(this); - scene.physics.add.existing(this); - - this.body = this.body as Phaser.Physics.Arcade.Body; - - // Set physics body (circular, centered on container) - const radius = this.playerPosition === 'G' ? PLAYER_RADIUS_GOALIE : PLAYER_RADIUS_SKATER; - this.body.setCircle(radius); - this.body.setOffset(-radius, -radius); // Center the body on the container - this.body.setCollideWorldBounds(true); + // Note: Player must be added to scene via scene.add.existing(player) and scene.physics.add.existing(player) + // This is handled by GameScene.addPlayer() method, which also sets up the physics body this.createSprite(); diff --git a/src/game/GameScene.ts b/src/game/GameScene.ts index eadfad3..84c80d1 100644 --- a/src/game/GameScene.ts +++ b/src/game/GameScene.ts @@ -18,6 +18,8 @@ import { SHOT_SPEED, TACKLE_SUCCESS_MODIFIER, TACKLE_PUCK_LOOSE_CHANCE, + PLAYER_RADIUS_GOALIE, + PLAYER_RADIUS_SKATER, TACKLE_MIN_SPEED, TACKLE_ANGLE_HEAD_ON_THRESHOLD, TACKLE_ANGLE_SIDE_THRESHOLD, @@ -89,20 +91,20 @@ export class GameScene extends Phaser.Scene { { speed: 70, skill: 75, tackling: 70, balance: 75, handling: 80 } ); - // Create one away defender at (15, 0) - right side for defending - const awayDefender = new Player( - this, - 'away-LD', - 'away', - 'LD', - -15, - 0, - { speed: 80, skill: 70, tackling: 85, balance: 80, handling: 65 } - ); - // Add players to tracking array and physics group this.addPlayer(homeCenter); - this.addPlayer(awayDefender); + + // Commented out for testing single player + // const awayDefender = new Player( + // this, + // 'away-LD', + // 'away', + // 'LD', + // -15, + // 0, + // { speed: 80, skill: 70, tackling: 85, balance: 80, handling: 65 } + // ); + // this.addPlayer(awayDefender); // Setup player-player collisions for entire group this.physics.add.collider(this.playerGroup, this.playerGroup, (obj1, obj2) => { @@ -114,6 +116,18 @@ export class GameScene extends Phaser.Scene { * Add a player to the scene (can be called at any time for debugging) */ addPlayer(player: Player) { + // Add to scene and enable physics + this.add.existing(player); + this.physics.add.existing(player); + + // Setup physics body + player.body = player.body as Phaser.Physics.Arcade.Body; + const radius = player.playerPosition === 'G' ? PLAYER_RADIUS_GOALIE : PLAYER_RADIUS_SKATER; + player.body.setCircle(radius); + player.body.setOffset(-radius, -radius); + player.body.setCollideWorldBounds(true); + + // Add to tracking arrays and groups this.players.push(player); this.playerGroup.add(player); this.behaviorTrees.set(player.id, new BehaviorTree(player));