var Player = { x: 0, y: 0, rotation: 0, pitch: 0, /** Screen columns per frame. 240 is sharp on ~1080p and cheap (~240 drawSprite calls). */ horizontalResolution: 240, gunX: 0.6, gunY: 0.6, currentGun: 0, guns: 3, resetToSpawn: function () { var spawn = World.spawnPoint(); this.x = spawn.x; this.y = spawn.y; this.rotation = 0; this.pitch = 0; }, applyLookDelta: function (movementX, movementY) { this.pitch += movementY * 0.01; this.rotation += movementX * 0.001; if (this.pitch > Math.PI) { this.pitch = Math.PI; } if (this.pitch < -Math.PI) { this.pitch = -Math.PI; } if (this.rotation > Math.PI * 2) { this.rotation -= Math.PI * 2; } if (this.rotation < 0) { this.rotation += Math.PI * 2; } }, /** Legacy angles: 0 = +X (east), increases toward +Y. */ forwardVector: function (radian) { return { x: Math.cos(radian), y: Math.sin(radian) }; }, tryMove: function (rot, speed, deltaTime) { if (World.rayCast(this.x, this.y, rot).distance <= 5) { return; } var forward = this.forwardVector(rot); this.x += forward.x * deltaTime * speed; this.y += forward.y * deltaTime * speed; }, update: function (deltaTime) { if (GameInput.isHeld(37) || GameInput.isHeld(65)) { var rotL = this.rotation - Math.PI / 2; if (rotL < 0) { rotL += Math.PI * 2; } this.tryMove(rotL, 60, deltaTime); } if (GameInput.isHeld(39) || GameInput.isHeld(68)) { var rotR = this.rotation + Math.PI / 2; if (rotR > Math.PI * 2) { rotR -= Math.PI * 2; } this.tryMove(rotR, 60, deltaTime); } if (GameInput.isHeld(38) || GameInput.isHeld(87)) { this.tryMove(this.rotation, 60, deltaTime); } if (GameInput.isHeld(40) || GameInput.isHeld(83)) { var rotB = this.rotation + Math.PI; if (rotB > Math.PI * 2) { rotB -= Math.PI * 2; } if (World.rayCast(this.x, this.y, rotB).distance > 5) { var forward = this.forwardVector(this.rotation); this.x -= forward.x * deltaTime * 40; this.y -= forward.y * deltaTime * 40; } } if (GameInput.isHeld(188)) { this.horizontalResolution -= this.horizontalResolution * 0.1 * deltaTime; if (this.horizontalResolution < 30) { this.horizontalResolution = 30; } } if (GameInput.isHeld(190)) { this.horizontalResolution += this.horizontalResolution * 0.1 * deltaTime; } } };