Skip to content

Commit

Permalink
adjust lines for the book
Browse files Browse the repository at this point in the history
  • Loading branch information
candelibas committed Apr 23, 2024
1 parent 13fa5a6 commit d982862
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 69 deletions.
12 changes: 8 additions & 4 deletions dungeon/src/gameobjects/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ export default class Player {
The init method is called from the constructor and in this case, it has several jobs. This is just a conventional class that contains a compound body: it consists of different bodies for the player, and we need to add them to the Matter world. We also need to add the player sprite to the scene and set up the animations. Finally, we need to add the colliders and events that will be used to control the player. If you set the debug to true you'll see the different bodies that make up the player. The ones on the sides it's used to detect collisions with walls and be able to climb up.
*/
init(x, y) {
// Before Matter's update, reset our record of what surfaces the player is touching.
// Before Matter's update,
// reset our record of what surfaces the player is touching.
this.scene.matter.world.on("beforeupdate", this.resetTouching, this);
this.sprite = this.scene.matter.add.sprite(0, 0, "player", 0);

const { Body, Bodies } = Phaser.Physics.Matter.Matter; // Native Matter modules
// Native Matter modules
const { Body, Bodies } = Phaser.Physics.Matter.Matter;
const { width: w, height: h } = this.sprite;

const mainBody = Bodies.rectangle(0, 5, w - 14, h - 10, {
Expand All @@ -50,7 +52,8 @@ export default class Player {
});
this.sprite
.setExistingBody(compoundBody)
.setFixedRotation() // Sets inertia to infinity so the player can't rotate
.setFixedRotation()
// Sets inertia to infinity so the player can't rotate
.setPosition(x, y);

this.addEvents();
Expand Down Expand Up @@ -138,7 +141,8 @@ export default class Player {
This is the method that is called when the player collides with something. We use it to detect collisions with the walls and the ground. We also use it to detect collisions with the sensors that we added to the player. This is used to detect collisions with the walls and the ground.
*/
onSensorCollide({ bodyA, bodyB, pair }) {
if (bodyB.isSensor) return; // We only care about collisions with physical objects
// We only care about collisions with physical objects
if (bodyB.isSensor) return;
if (bodyA === this.sensors.left) {
this.friction();
this.onWall = true;
Expand Down
9 changes: 6 additions & 3 deletions fate/src/scenes/story.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,9 +253,12 @@ This is a long text that explains the story of the game. It is typed and then re
});
const text =
" GAGARIN WAS SENT ON A SECRET MISSION\nBEYOND THE OORT CLOUD, " +
"PROPELLED BY\nNUCLEAR DETONATIONS.\n\nHE HAS NOW PASSED THE FRONTIER OF\nOUR SOLAR SYSTEM\n" +
"HIS MISSION:\nTO SET 20 PROBES AND RECOLLECT DATA\nFROM THE DEADLIEST STELLAR OBJECT:\n" +
"A NEUTRINO STAR!\n\nHE HAS TO AVOID INCOMING DEBRIS\nAND GET AS CLOSE AS POSSIBLE TO THE STAR.\n" +
"PROPELLED BY\nNUCLEAR DETONATIONS.\n\n
HE HAS NOW PASSED THE FRONTIER OF\nOUR SOLAR SYSTEM\n" +
"HIS MISSION:\nTO SET 20 PROBES AND RECOLLECT DATA
\nFROM THE DEADLIEST STELLAR OBJECT:\n" +
"A NEUTRINO STAR!\n\nHE HAS TO AVOID INCOMING DEBRIS
\nAND GET AS CLOSE AS POSSIBLE TO THE STAR.\n" +
"THAT WILL MEAN CERTAIN DEATH, BUT ALSO\nA MASSIVE ACHIEVEMENT " +
"FOR SOVIET SCIENTISTS!\n\n" +
"THE FATAL FATE OF GAGARIN IS NOW TIED\nTO THE GLORIOUS FATE " +
Expand Down
3 changes: 2 additions & 1 deletion mars/src/scenes/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ export default class Game extends Phaser.Scene {
this.addPlayer();
this.addOxygen();

// this.input.keyboard.on("keydown-ENTER", () => this.skipThis(), this); // for testing
// this.input.keyboard.on("keydown-ENTER",
// () => this.skipThis(), this); // for testing
this.cameras.main.startFollow(this.player, true, 0.05, 0.05, 0, 0);
this.loadAudios();
this.addEffects();
Expand Down
98 changes: 45 additions & 53 deletions runner/src/scenes/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,16 @@ export default class Game extends Phaser.Scene {
this.score = 0;
}

/*
Here we do several things.
- We use the `create` method to initialize the game.
- We set some variables to store width and height that we may need later.,
- We set the background color, and create the player, the obstacles, and the coins.
- We also create the keyboard input to listen to the space key.
- Also, we add a collider between the player and the obstacles and an overlap
between the player and the coins. The key part there is to set a function that will be called when the player overlaps with a coin or hits an obstacle.
*/
/*
Here we do several things.
- We use the `create` method to initialize the game.
- We set some variables to store width and height that we may need later.,
- We set the background color, and create the player, the obstacles, and the coins.
- We also create the keyboard input to listen to the space key.
- Also, we add a collider between the player and the obstacles and an overlap
between the player and the coins. The key part there is to set a function that will be called when the player overlaps with a coin or hits an obstacle.
*/
create() {
this.width = this.sys.game.config.width;
this.height = this.sys.game.config.height;
Expand Down Expand Up @@ -97,13 +92,13 @@ export default class Game extends Phaser.Scene {
this.playMusic();

/*
We use the `pointerdown` event to listen to the mouse click or touch event.
*/
We use the `pointerdown` event to listen to the mouse click or touch event.
*/
this.input.on("pointerdown", (pointer) => this.jump(), this);

/*
We use `updateScoreEvent` to update the score every 100ms so the player can see the score increasing as long as he survives.
*/
We use `updateScoreEvent` to update the score every 100ms so the player can see the score increasing as long as he survives.
*/
this.updateScoreEvent = this.time.addEvent({
delay: 100,
callback: () => this.updateScore(),
Expand All @@ -112,30 +107,30 @@ export default class Game extends Phaser.Scene {
});
}

/*
This method is called when the player hits an obstacle. We stop the updateScoreEvent so the score doesn't increase anymore.
/*
This method is called when the player hits an obstacle. We stop the updateScoreEvent so the score doesn't increase anymore.
And obviously, we finish the scene.
*/
And obviously, we finish the scene.
*/
hitObstacle(player, obstacle) {
this.updateScoreEvent.destroy();
this.finishScene();
}

/*
This method is called when the player hits a coin. We play a sound, update the score, and destroy the coin.
*/
/*
This method is called when the player hits a coin. We play a sound, update the score, and destroy the coin.
*/
hitCoin(player, coin) {
this.playAudio("coin");
this.updateScore(1000);
coin.destroy();
}

/*
We use this `loadAudios` method to load all the audio files that we need for the game.
/*
We use this `loadAudios` method to load all the audio files that we need for the game.
Then we'll play them using the `playAudio` method.
*/
Then we'll play them using the `playAudio` method.
*/
loadAudios() {
this.audios = {
jump: this.sound.add("jump"),
Expand All @@ -149,8 +144,8 @@ export default class Game extends Phaser.Scene {
}

/*
This method is specific to the music. We use it to play the theme music in a loop.
*/
This method is specific to the music. We use it to play the theme music in a loop.
*/
playMusic(theme = "theme") {
this.theme = this.sound.add(theme);
this.theme.stop();
Expand All @@ -165,11 +160,11 @@ export default class Game extends Phaser.Scene {
});
}

/*
This is the game loop. The function is called every frame.
/*
This is the game loop. The function is called every frame.
Here is where we can check if a key was pressed or the situation of the player to act accordingly. We use the `update` method to check if the player pressed the space key.
*/
Here is where we can check if a key was pressed or the situation of the player to act accordingly. We use the `update` method to check if the player pressed the space key.
*/
update() {
if (Phaser.Input.Keyboard.JustDown(this.SPACE)) {
this.jump();
Expand All @@ -180,11 +175,11 @@ export default class Game extends Phaser.Scene {
}
}

/*
This is the method that we use to make the player jump. A jump is just a velocity in the Y-axis. Gravity will do the rest.
/*
This is the method that we use to make the player jump. A jump is just a velocity in the Y-axis. Gravity will do the rest.
We also play a jumping sound and we add a tween to rotate the player while jumping.
*/
We also play a jumping sound and we add a tween to rotate the player while jumping.
*/
jump() {
if (!this.player.body.blocked.down) return;
this.player.body.setVelocityY(-300);
Expand All @@ -198,28 +193,25 @@ export default class Game extends Phaser.Scene {
});
}

/*
What should we do when we finish the game scene?
- Stop the theme music
- Play the dead sound
- Set the score in the registry to show it in the `gameover` scene.
/*
What should we do when we finish the game scene?
- Start the `gameover` scene.
- Stop the theme music
- Play the dead sound
- Set the score in the registry to show it in the `gameover` scene.
- Start the `gameover` scene.
*/
*/
finishScene() {
this.theme.stop();
this.playAudio("dead");
this.registry.set("score", "" + this.score);
this.scene.start("gameover");
}

/*
This method is called every 100ms and it is used to update the score and show it on the screen.
*/
/*
This method is called every 100ms and it is used to update the score and show it on the screen.
*/
updateScore(points = 1) {
this.score += points;
this.scoreText.setText(this.score);
Expand Down
14 changes: 10 additions & 4 deletions zenbaki/dist/assets/html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,15 @@ <h2>Options</h2>
</header>
<form action="game.html">
<div><label for="channel">Channel</label></div>
<div><input type="text" name="channel" id="channel" value="devdiaries"></div>
<div>
<input type="text" name="channel"
id="channel" value="devdiaries"></div>
<div><label for="background">Background Color</label></div>
<div><input type="color" name="background" id="background", value = '#00b140'></div>
<div><input type="color" name="background"
id="background", value = '#00b140'></div>
<div><label for="foreground">Foreground Color</label></div>
<div><input type="color" name="foreground" id="foreground", value = '#F0EAD6'></div>
<div><input type="color" name="foreground"
id="foreground", value = '#F0EAD6'></div>
<br />
<div><input type="submit", value = 'PLAY'></div>
</form>
Expand Down Expand Up @@ -62,7 +66,9 @@ <h2>Game Rules</h2>
const listBlock = document.getElementById('listBlock');

function toggleList() {
listBlock.style.display = listBlock.style.display === 'none'|| listBlock.style.display !== 'flex' ? 'flex' : 'none';
listBlock.style.display =
listBlock.style.display === 'none' ||
listBlock.style.display !== 'flex' ? 'flex' : 'none';
}

</script>
Expand Down
14 changes: 10 additions & 4 deletions zenbaki/public/assets/html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,15 @@ <h2>Options</h2>
</header>
<form action="game.html">
<div><label for="channel">Channel</label></div>
<div><input type="text" name="channel" id="channel" value="devdiaries"></div>
<div>
<input type="text" name="channel"
id="channel" value="devdiaries"></div>
<div><label for="background">Background Color</label></div>
<div><input type="color" name="background" id="background", value = '#00b140'></div>
<div><input type="color" name="background"
id="background", value = '#00b140'></div>
<div><label for="foreground">Foreground Color</label></div>
<div><input type="color" name="foreground" id="foreground", value = '#F0EAD6'></div>
<div><input type="color" name="foreground"
id="foreground", value = '#F0EAD6'></div>
<br />
<div><input type="submit", value = 'PLAY'></div>
</form>
Expand Down Expand Up @@ -62,7 +66,9 @@ <h2>Game Rules</h2>
const listBlock = document.getElementById('listBlock');

function toggleList() {
listBlock.style.display = listBlock.style.display === 'none'|| listBlock.style.display !== 'flex' ? 'flex' : 'none';
listBlock.style.display =
listBlock.style.display === 'none' ||
listBlock.style.display !== 'flex' ? 'flex' : 'none';
}

</script>
Expand Down

0 comments on commit d982862

Please sign in to comment.