Centralize player initialization to single addPlayer() method

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 <noreply@anthropic.com>
This commit is contained in:
Pierre Wessman 2025-10-03 13:07:01 +02:00
parent 71082f713a
commit a8daedf53b
2 changed files with 28 additions and 23 deletions

View File

@ -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();

View File

@ -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));