Skip to content

Commit

Permalink
Refactor first-person controller script (#7289)
Browse files Browse the repository at this point in the history
* Refactor first-person controller script

* Fixed camera init and collision and rigidibody init

* Refactor input classes to handle null values for enabled property

---------

Co-authored-by: KPal <[email protected]>
Co-authored-by: kpal81xd <[email protected]>
  • Loading branch information
3 people authored Jan 17, 2025
1 parent 112b859 commit 6a06bd4
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 77 deletions.
2 changes: 1 addition & 1 deletion examples/src/examples/camera/first-person.example.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ function createCharacterController(camera) {
});
entity.addComponent('script');
entity.script.create(FirstPersonController, {
attributes: {
properties: {
camera,
jumpForce: 850
}
Expand Down
126 changes: 50 additions & 76 deletions scripts/esm/first-person-controller.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ class KeyboardMouseInput {
if (value === this._enabled) {
return;
}
this._enabled = value;
this._enabled = value ?? this._enabled;

if (value) {
if (this._enabled) {
this._bind();
} else {
this._unbind();
Expand Down Expand Up @@ -300,9 +300,9 @@ class MobileInput {
if (value === this._enabled) {
return;
}
this._enabled = value;
this._enabled = value ?? this._enabled;

if (value) {
if (this._enabled) {
this._bind();
} else {
this._unbind();
Expand Down Expand Up @@ -575,7 +575,7 @@ class GamePadInput {
if (value === this._enabled) {
return;
}
this._enabled = value;
this._enabled = value ?? this._enabled;
}

get enabled() {
Expand Down Expand Up @@ -713,7 +713,6 @@ class FirstPersonController extends Script {
*/
_gamePadInput;


/**
* @type {number}
* @private
Expand Down Expand Up @@ -811,12 +810,7 @@ class FirstPersonController extends Script {
*/
jumpForce = 600;

/**
* @param {object} args - The script arguments.
*/
constructor(args) {
super(args);

initialize() {
// input
this._keyboardMouseInput = new KeyboardMouseInput(this.app);
this._mobileInput = new MobileInput(this.app);
Expand All @@ -833,47 +827,38 @@ class FirstPersonController extends Script {
this._gamePadInput.enabled = false;
});

const {
camera,
lookSens,
speedGround,
speedAir,
sprintMult,
velocityDampingGround,
velocityDampingAir,
jumpForce,
mobileDeadZone,
mobileTurnSpeed,
mobileRadius,
mobileDoubleTapInterval,
gamePadDeadZoneLow,
gamePadDeadZoneHigh,
gamePadTurnSpeed
} = args.attributes;

if (!camera) {
throw new Error('No camera entity found');
if (!this.camera) {
this.camera = this.entity.findComponent('camera').entity;
if (!this.camera) {
throw new Error('FirstPersonController expects a camera entity');
}
}
if (!this.entity.collision) {
this.entity.addComponent('collision', {
type: 'capsule',
radius: 0.5,
height: 2
});
}
if (!this.entity.rigidbody) {
throw new Error('No rigidbody component found');
this.entity.addComponent('rigidbody', {
type: 'dynamic',
mass: 100,
linearDamping: 0,
angularDamping: 0,
linearFactor: Vec3.ONE,
angularFactor: Vec3.ZERO,
friction: 0.5,
restitution: 0
});
}
this._rigidbody = this.entity.rigidbody;

this.camera = camera;
this.lookSens = lookSens ?? this.lookSens;
this.speedGround = speedGround ?? this.speedGround;
this.speedAir = speedAir ?? this.speedAir;
this.sprintMult = sprintMult ?? this.sprintMult;
this.velocityDampingGround = velocityDampingGround ?? this.velocityDampingGround;
this.velocityDampingAir = velocityDampingAir ?? this.velocityDampingAir;
this.jumpForce = jumpForce ?? this.jumpForce;
this.mobileDeadZone = mobileDeadZone ?? this.mobileDeadZone;
this.mobileTurnSpeed = mobileTurnSpeed ?? this.mobileTurnSpeed;
this.mobileRadius = mobileRadius ?? this.mobileRadius;
this.mobileDoubleTapInterval = mobileDoubleTapInterval ?? this.mobileDoubleTapInterval;
this.gamePadDeadZoneLow = gamePadDeadZoneLow ?? this.gamePadDeadZoneLow;
this.gamePadDeadZoneHigh = gamePadDeadZoneHigh ?? this.gamePadDeadZoneHigh;
this.gamePadTurnSpeed = gamePadTurnSpeed ?? this.gamePadTurnSpeed;
this.mobileDeadZone = this._mobileDeadZone;
this.mobileTurnSpeed = this._mobileTurnSpeed;
this.gamePadDeadZoneLow = this._gamePadDeadZoneLow;
this.gamePadDeadZoneHigh = this._gamePadDeadZoneHigh;
this.gamePadTurnSpeed = this._gamePadTurnSpeed;

this.app.on('cc:look', (movX, movY) => {
this.look.x = math.clamp(this.look.x - movY * this.lookSens, -LOOK_MAX_ANGLE, LOOK_MAX_ANGLE);
Expand Down Expand Up @@ -907,11 +892,10 @@ class FirstPersonController extends Script {
* @range [0, 0.4]
*/
set mobileDeadZone(value) {
if (value === this._mobileDeadZone) {
return;
this._mobileDeadZone = value ?? this._mobileDeadZone;
if (this._mobileInput) {
this._mobileInput.deadZone = this._mobileDeadZone;
}
this._mobileDeadZone = value;
this._mobileInput.deadZone = value;
}

get mobileDeadZone() {
Expand All @@ -925,11 +909,10 @@ class FirstPersonController extends Script {
* @type {number}
*/
set mobileTurnSpeed(value) {
if (value === this._mobileTurnSpeed) {
return;
this._mobileTurnSpeed = value ?? this._mobileTurnSpeed;
if (this._mobileInput) {
this._mobileInput.turnSpeed = this._mobileTurnSpeed;
}
this._mobileTurnSpeed = value;
this._mobileInput.turnSpeed = value;
}

get mobileTurnSpeed() {
Expand All @@ -943,10 +926,7 @@ class FirstPersonController extends Script {
* @type {number}
*/
set mobileRadius(value) {
if (value === this._mobileRadius) {
return;
}
this._mobileRadius = value;
this._mobileRadius = value ?? this._mobileRadius;
}

get mobileRadius() {
Expand All @@ -960,10 +940,7 @@ class FirstPersonController extends Script {
* @type {number}
*/
set mobileDoubleTapInterval(value) {
if (value === this._mobileDoubleTapInterval) {
return;
}
this._mobileDoubleTapInterval = value;
this._mobileDoubleTapInterval = value ?? this._mobileDoubleTapInterval;
}

get mobileDoubleTapInterval() {
Expand All @@ -978,11 +955,10 @@ class FirstPersonController extends Script {
* @range [0, 0.4]
*/
set gamePadDeadZoneLow(value) {
if (value === this._gamePadDeadZoneLow) {
return;
this._gamePadDeadZoneLow = value ?? this._gamePadDeadZoneLow;
if (this._gamePadInput) {
this._gamePadInput.deadZoneLow = this._gamePadDeadZoneLow;
}
this._gamePadDeadZoneLow = value;
this._gamePadInput.deadZoneLow = value;
}

get gamePadDeadZoneLow() {
Expand All @@ -997,11 +973,10 @@ class FirstPersonController extends Script {
* @range [0, 0.4]
*/
set gamePadDeadZoneHigh(value) {
if (value === this._gamePadDeadZoneHigh) {
return;
this._gamePadDeadZoneHigh = value ?? this._gamePadDeadZoneHigh;
if (this._gamePadInput) {
this._gamePadInput.deadZoneHigh = this._gamePadDeadZoneHigh;
}
this._gamePadDeadZoneHigh = value;
this._gamePadInput.deadZoneHigh = value;
}

get gamePadDeadZoneHigh() {
Expand All @@ -1015,11 +990,10 @@ class FirstPersonController extends Script {
* @type {number}
*/
set gamePadTurnSpeed(value) {
if (value === this._gamePadTurnSpeed) {
return;
this._gamePadTurnSpeed = value ?? this._gamePadTurnSpeed;
if (this._gamePadInput) {
this._gamePadInput.turnSpeed = this._gamePadTurnSpeed;
}
this._gamePadTurnSpeed = value;
this._gamePadInput.turnSpeed = value;
}

get gamePadTurnSpeed() {
Expand Down

0 comments on commit 6a06bd4

Please sign in to comment.