Skip to content

Commit

Permalink
Fixed minor bug with conflicting inputs and made code more coherent. …
Browse files Browse the repository at this point in the history
…Also introduced strafing which is now the default behaviour of 'a' & 'd' keys.
  • Loading branch information
Joyje committed Apr 11, 2023
1 parent ca51bc2 commit fa68caa
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
17 changes: 9 additions & 8 deletions js/camera.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ function UpdateCamera()
var current = new Date().getTime();

input.keypressed = false;
if (input.leftright != 0)
if (input.lookleftright != 0)
{
camera.angle += input.leftright*0.1*(current-time)*0.03;
camera.angle += input.lookleftright*0.1*(current-time)*0.03;
input.keypressed = true;
}
if (input.forwardbackward != 0)
Expand All @@ -31,19 +31,20 @@ function UpdateCamera()
camera.y -= input.forwardbackward * Math.cos(camera.angle) * (current-time)*0.03;
input.keypressed = true;
}
if (input.updown != 0)
if (input.leftright != 0)
{
camera.height += input.updown * (current-time)*0.03;
camera.x -= input.leftright * Math.sin(1.57+camera.angle) * (current-time)*0.03;
camera.y -= input.leftright * Math.cos(1.57+camera.angle) * (current-time)*0.03;
input.keypressed = true;
}
if (input.lookup)
if (input.updown != 0)
{
camera.horizon += 2 * (current-time)*0.03;
camera.height += input.updown * (current-time)*0.03;
input.keypressed = true;
}
if (input.lookdown)
if (input.lookupdown)
{
camera.horizon -= 2 * (current-time)*0.03;
camera.horizon += input.lookupdown * (current-time)*0.03;
input.keypressed = true;
}

Expand Down
23 changes: 11 additions & 12 deletions js/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ var input =
forwardbackward: 0,
leftright: 0,
updown: 0,
lookup: false,
lookdown: false,
lookleftright: 0,
lookupdown: 0,
mouseposition: null,
keypressed: false
}
Expand Down Expand Up @@ -48,7 +48,7 @@ function DetectMouseUp()
{
input.mouseposition = null;
input.forwardbackward = 0;
input.leftright = 0;
input.lookleftright = 0;
input.updown = 0;
return;
}
Expand All @@ -57,11 +57,10 @@ function DetectMouseMove(e)
{
e.preventDefault();
if (input.mouseposition == null) return;
if (input.forwardbackward == 0) return;

var currentMousePosition = GetMousePosition(e);

input.leftright = (input.mouseposition[0] - currentMousePosition[0]) / window.innerWidth * 2;
input.lookleftright = (input.mouseposition[0] - currentMousePosition[0]) / window.innerWidth * 2;
camera.horizon = 100 + (input.mouseposition[1] - currentMousePosition[1]) / window.innerHeight * 500;
input.updown = (input.mouseposition[1] - currentMousePosition[1]) / window.innerHeight * 10;
}
Expand All @@ -73,15 +72,15 @@ function DetectKeysDown(e)
{
case 37: // left cursor
case 65: // a
input.leftright = +1.;
input.leftright = +2.;
break;
case 39: // right cursor
case 68: // d
input.leftright = -1.;
input.leftright = -2.;
break;
case 38: // cursor up
case 87: // w
input.forwardbackward = 3.;
input.forwardbackward = +3.;
break;
case 40: // cursor down
case 83: // s
Expand All @@ -94,10 +93,10 @@ function DetectKeysDown(e)
input.updown = -2.;
break;
case 69: // e
input.lookup = true;
input.lookupdown = +2;
break;
case 81: //q
input.lookdown = true;
input.lookupdown = -2;
break;
default:
return;
Expand Down Expand Up @@ -138,10 +137,10 @@ function DetectKeysUp(e)
input.updown = 0;
break;
case 69: // e
input.lookup = false;
input.lookupdown = 0;
break;
case 81: //q
input.lookdown = false;
input.lookupdown = 0;
break;
default:
return;
Expand Down

0 comments on commit fa68caa

Please sign in to comment.