function PlayerController(player, soundEffects) { this.player = player; this.soundEffects = soundEffects; this.directionX = 0; this.directionZ = 0; this.jump = false; } PlayerController.prototype.update = function() { if (this.directionX == 0 && this.directionZ == 0) { this.player.intendedPlanarVelocity.x = 0.0; this.player.intendedPlanarVelocity.y = 0.0; } else { if (this.directionX != 0 && this.directionZ != 0) { this.player.intendedPlanarVelocity.x = Math.sqrt(0.5) * this.player.moveSpeed * this.directionX; this.player.intendedPlanarVelocity.y = Math.sqrt(0.5) * this.player.moveSpeed * this.directionZ; } else { this.player.intendedPlanarVelocity.x = this.player.moveSpeed * this.directionX; this.player.intendedPlanarVelocity.y = this.player.moveSpeed * this.directionZ; } } if (this.jump && this.player.onSurface) { this.soundEffects.play("jump"); this.player.push(Vector3.withValues(0.0, this.player.jumpForceY - this.player.velocity.y, 0.0)); this.player.onSurface = false; } this.jump = false; } PlayerController.prototype.keyDown = function(charCode) { switch (charCode) { case Event.KEY_LEFT: this.directionX = -1; break; case Event.KEY_RIGHT: this.directionX = 1; break; case Event.KEY_DOWN: this.directionZ = 1; break; case Event.KEY_UP: this.directionZ = -1; break; case 32: this.jump = true; break; } } PlayerController.prototype.keyUp = function(charCode) { switch (charCode) { case Event.KEY_LEFT: if (this.directionX == -1) { this.directionX = 0; } break; case Event.KEY_RIGHT: if (this.directionX == 1) { this.directionX = 0; } break; case Event.KEY_DOWN: if (this.directionZ == 1) { this.directionZ = 0; } break; case Event.KEY_UP: if (this.directionZ == -1) { this.directionZ = 0; } break; } }