Skip to content

Commit 3d30c94

Browse files
committed
Most of chapter 16
1 parent 5baeee3 commit 3d30c94

14 files changed

Lines changed: 528 additions & 48 deletions

15_game.txt

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -246,9 +246,8 @@ the `Lava` constructor uses to determine its behavior (bouncing
246246
horizontally or vertically, or dripping).
247247

248248
The player type is built with this simple constructor. It has a
249-
property `ySpeed` containing its current vertical speed, which we will
250-
use to simulate momentum and gravity (the horizontal speed will be
251-
entirely based on the arrow keys, without any momentum).
249+
property `speed` containing its current speed, which we will
250+
use to simulate momentum and gravity.
252251

253252
// include_code
254253

@@ -257,7 +256,7 @@ entirely based on the arrow keys, without any momentum).
257256
function Player(pos) {
258257
this.pos = pos.plus(new Vector(0, -0.5));
259258
this.size = new Vector(0.8, 1.5);
260-
this.ySpeed = 0;
259+
this.speed = new Vector(0, 0);
261260
}
262261
Player.prototype.type = "player";
263262
----
@@ -699,10 +698,10 @@ a size) overlaps with any non-empty space from the background grid:
699698
[source,javascript]
700699
----
701700
Level.prototype.obstacleAt = function(pos, size) {
702-
var yStart = Math.floor(pos.y);
703-
var yEnd = Math.ceil(pos.y + size.y);
704701
var xStart = Math.floor(pos.x);
705702
var xEnd = Math.ceil(pos.x + size.x);
703+
var yStart = Math.floor(pos.y);
704+
var yEnd = Math.ceil(pos.y + size.y);
706705

707706
if (xStart < 0 || xEnd > this.width || yStart < 0)
708707
return "wall";
@@ -852,11 +851,12 @@ method below implements the horizontal part.
852851
var playerXSpeed = 7;
853852

854853
Player.prototype.moveX = function(step, level, keys) {
855-
var speed = 0;
856-
if (keys.left) speed -= playerXSpeed;
857-
if (keys.right) speed += playerXSpeed;
854+
this.speed.x = 0;
855+
if (keys.left) this.speed.x -= playerXSpeed;
856+
if (keys.right) this.speed.x += playerXSpeed;
858857

859-
var newPos = this.pos.plus(new Vector(speed * step, 0));
858+
var motion = new Vector(this.speed.x * step, 0);
859+
var newPos = this.pos.plus(motion);
860860
var obstacle = level.obstacleAt(newPos, this.size);
861861
if (obstacle)
862862
level.playerTouched(obstacle);
@@ -882,15 +882,16 @@ var gravity = 30;
882882
var jumpSpeed = 17;
883883

884884
Player.prototype.moveY = function(step, level, keys) {
885-
this.ySpeed += step * gravity;
886-
var newPos = this.pos.plus(new Vector(0, this.ySpeed * step));
885+
this.speed.y += step * gravity;
886+
var motion = new Vector(0, this.speed.y * step);
887+
var newPos = this.pos.plus(motion);
887888
var obstacle = level.obstacleAt(newPos, this.size);
888889
if (obstacle) {
889890
level.playerTouched(obstacle);
890-
if (keys.up && this.ySpeed > 0)
891-
this.ySpeed = -jumpSpeed;
891+
if (keys.up && this.speed.y > 0)
892+
this.speed.y = -jumpSpeed;
892893
else
893-
this.ySpeed = 0;
894+
this.speed.y = 0;
894895
} else {
895896
this.pos = newPos;
896897
}
@@ -1081,7 +1082,7 @@ function runLevel(level, Display, andThen) {
10811082
var display = new Display(document.body, level);
10821083
runAnimation(function(step) {
10831084
level.animate(step, arrows);
1084-
display.drawFrame(level);
1085+
display.drawFrame(step);
10851086
if (level.isFinished()) {
10861087
display.clear();
10871088
if (andThen)
@@ -1260,7 +1261,7 @@ ifdef::html_target[]
12601261
var display = new Display(document.body, level);
12611262
runAnimation(function(step) {
12621263
level.animate(step, arrows);
1263-
display.drawFrame(level);
1264+
display.drawFrame(step);
12641265
if (level.isFinished()) {
12651266
display.clear();
12661267
if (andThen)

0 commit comments

Comments
 (0)