diff options
author | munvoseli <muslinmelody@gmail.com> | 2023-06-10 08:56:36 -0400 |
---|---|---|
committer | munvoseli <muslinmelody@gmail.com> | 2023-06-10 08:56:36 -0400 |
commit | ff14b7097683a457a733b23121b939b0c3a64d4b (patch) | |
tree | 352125e125f21acfcc26d03f2859549dd7154737 | |
parent | 54c282a5a52b73cda4208826b9cdd8350eb88746 (diff) |
start of movement changes
-rw-r--r-- | public/javascript/game.js | 50 |
1 files changed, 31 insertions, 19 deletions
diff --git a/public/javascript/game.js b/public/javascript/game.js index 43ab772..3eba300 100644 --- a/public/javascript/game.js +++ b/public/javascript/game.js @@ -78,6 +78,10 @@ var gameUpdateSocket; var gameUpdateStartTimestamp; var moduleList = []; +const walkAnimTime = (1 / 4) * framesPerSecond; +let walkAnimDir = -1; +let walkInputDirections = []; + // Thanks to CatTail for this snippet of code. var encodeHtmlEntity = function(str) { var buf = []; @@ -686,12 +690,14 @@ Player.prototype.walk = function(direction) { var tempPos = this.getPosInWalkDirection(direction); var tempTile = getTileBufferValue(tempPos); if (!this.canWalkThroughTile(tempTile)) { + walkAnimDir = -1; return false; } addWalkCommand(direction); this.pos.set(tempPos); placeLocalPlayerTrail(this.pos); - this.walkDelay = (1 / 8) * framesPerSecond; + walkAnimDir = direction; + this.walkDelay = walkAnimTime; return true; } @@ -1111,6 +1117,13 @@ function drawTileOnContext(context, pos, size, which) { function drawTile(pos, which) { var tempPos = pos.copy(); tempPos.scale(spriteRenderSize); + if (walkAnimDir >= 0) { + const size = spriteRenderSize; + const dx = [0,1,0,-1][walkAnimDir] * size; + const dy = [-1,0,1,0][walkAnimDir] * size; + tempPos.x += dx * localPlayer.walkDelay/walkAnimTime; + tempPos.y += dy * localPlayer.walkDelay/walkAnimTime; + } drawTileOnContext( context, tempPos, @@ -1189,22 +1202,18 @@ function startPlacingText(text) { } function localPlayerStartWalking(direction) { - if (localPlayerWalkRepeatDirection !== direction) { - localPlayer.walk(direction); - localPlayerWalkRepeatDirection = direction; - localPlayerWalkRepeatDelay = 0.35 * framesPerSecond; - localPlayerShouldStopWalkRepeat = !lKeyIsHeld; - } + let i = walkInputDirections.indexOf(direction); + if (i != -1) { + walkInputDirections.splice(i, 1); + } + walkInputDirections.push(direction); } function localPlayerStopWalking(direction) { - if (direction == localPlayerWalkRepeatDirection) { - if (localPlayerShouldStopWalkRepeat) { - localPlayerWalkRepeatDirection = null; - } else { - localPlayerShouldStopWalkRepeat = true; - } - } + let i = walkInputDirections.indexOf(direction); + if (i != -1) { + walkInputDirections.splice(i, 1); + } } function displayGuidelinePos() { @@ -1539,12 +1548,15 @@ function timerEvent() { } } } - if (localPlayerWalkRepeatDirection !== null) { - if (localPlayerWalkRepeatDelay > 0) { - localPlayerWalkRepeatDelay -= 1; - } else { - localPlayer.walk(localPlayerWalkRepeatDirection); + if (walkInputDirections.length == 0) { + if (localPlayer.walkDelay <= 0) { + walkAnimDir = -1; } + } else if (walkInputDirections.length == 1) { + localPlayer.walk(walkInputDirections[0]); + } else { + localPlayer.walk(walkInputDirections[0]); + walkInputDirections.shift(); } cameraPos.set(localPlayer.pos); var tempOffset = Math.floor(canvasSpriteSize / 2); |