Skip to content

Commit

Permalink
Merge pull request #7 from Yareaj/dev
Browse files Browse the repository at this point in the history
Added a step counter for the game!
  • Loading branch information
Yareaj authored May 28, 2023
2 parents fa4990e + 44df5bc commit a0aabb2
Show file tree
Hide file tree
Showing 15 changed files with 38 additions and 21 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ A JavaScript implementation of the 🎮 [Sokoban](https://en.wikipedia.org/wiki/
- [Footstep](./assets/sounds/levelUp.wav) sound effect was taken from [freesound.org](https://freesound.org/people/EVRetro/sounds/501102/)
- All of the game's image assets were retrieved from [Kenney.nl](https://kenney.nl/assets/sokoban)
- The original level processor and map format was based on [csixteen](https://github.com/csixteen/sokoban)'s implementation of Sokoban (`Thanks for the help Pedro :)`)
- The idea of implementing the [Sokoban Level Format](http://sokobano.de/wiki/index.php?title=Level_format) was implemented based on [krzysu](https://github.com/krzysu/elm-sokoban-player)'s implementation of Sokoban
- The idea of implementing the [Sokoban Level Format](http://sokobano.de/wiki/index.php?title=Level_format), as well as taking the Fredoka font was implemented based on [krzysu](https://github.com/krzysu/elm-sokoban-player)'s implementation of Sokoban
Binary file removed assets/fonts/Averta.otf
Binary file not shown.
Binary file added assets/fonts/Fredoka-Bold.ttf
Binary file not shown.
Binary file added assets/fonts/Fredoka-Medium.ttf
Binary file not shown.
Binary file added assets/fonts/Fredoka-Regular.ttf
Binary file not shown.
Binary file removed assets/fonts/Syntax.otf
Binary file not shown.
Binary file removed assets/fonts/SyntaxBlack.otf
Binary file not shown.
Binary file removed assets/fonts/SyntaxBold.otf
Binary file not shown.
2 changes: 1 addition & 1 deletion assets/json/buttonStyles.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"background-color" : "#4a3429",
"border" : "none",
"border-radius" : "3px",
"font-family" : "SyntaxBold",
"font-family" : "Fredoka-Medium",
"font-size" : "23px",
"color" : "#ffdeb3"
}
1 change: 1 addition & 0 deletions functions/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ function mapReload() {

// Reset control variables
placedTargets = 0;
stepsTaken = 0;
levelPass = false;
successAudio = true;

Expand Down
29 changes: 17 additions & 12 deletions functions/levelHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@ function preload() {
}

// Import the button's fonts into the project
loadFont('./assets/fonts/Averta.otf');
loadFont('./assets/fonts/Syntax.otf');
loadFont('./assets/fonts/SyntaxBlack.otf');
loadFont('./assets/fonts/SyntaxBold.otf');
loadFont('./assets/fonts/Fredoka-Bold.ttf');
loadFont('./assets/fonts/Fredoka-Medium.ttf');
loadFont('./assets/fonts/Fredoka-Regular.ttf');

// Import the map string into the global variable
mapOutline = loadStrings(`./assets/levels/level${levelId}.txt`);
Expand Down Expand Up @@ -97,12 +96,8 @@ function setup() {
menuButton.mousePressed(toMenu);

// Set up the buttons and style them respectively
const buttonPropList = Object.keys(buttonStyles);
for (let propIterator = 0; propIterator<buttonPropList.length ; propIterator++) {
const propertyName = buttonPropList[propIterator];
menuButton.style(propertyName, buttonStyles[propertyName]);
nextButton.style(propertyName, buttonStyles[propertyName]);
};
applyStyles(menuButton, buttonStyles);
applyStyles(nextButton, buttonStyles);
}

function draw() {
Expand All @@ -125,19 +120,29 @@ function draw() {
outline: 'green'
});

// Add the step counter
fill('#ffdeb3');
textFont('Fredoka-Medium');
textSize(Quadrille.CELL_LENGTH*0.5);
const xCordSteps = ((width+(Quadrille.CELL_LENGTH/2))/2)-(textWidth('Steps'));
text(`Steps: ${stepsTaken}`, xCordSteps, Quadrille.CELL_LENGTH/1.5);

// Stop game execution upon finishing the level and create the buttons
if (levelPass) {
// Create screening
fill(color('rgba(36, 166, 91, 0.80)'));
rect(0,0, Quadrille.CELL_LENGTH*columns, Quadrille.CELL_LENGTH*rows);
// Display the done level text
textFont('SyntaxBold');
textFont('Fredoka-Medium');
textSize(40);
fill('#000');
fill('#4a3429');
text('Level Passed', menuButton.x-(textWidth('Level Passed')/5), menuButton.y-menuButton.height*1);
// Show the menu button and next level buttons
menuButton.show();
nextButton.show();

textSize(23);
text(`Steps: ${stepsTaken}`, xCordSteps, menuButton.y+(23*2.2));

// Reproduce levelUp audio and make sure it only happens once
if (successAudio) {
Expand Down
4 changes: 3 additions & 1 deletion functions/movement.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ function playerMove(direction) {
} else if (direction == 'right') {
playerPos.col += 1;
}
stepsTaken += 1;
return sounds.step.play();
}
}
Expand All @@ -57,6 +58,7 @@ function playerMove(direction) {
} else if (direction == 'right') {
playerPos.col += 1;
}


stepsTaken += 1;
sounds.step.play();
}
9 changes: 9 additions & 0 deletions functions/objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,13 @@ function boxOnTarget(boxIndex) {
const placedBoxes = boxesQuadrilles.filter(box => box[0].read(0,0) == images.blocks.boxSecured);
placedTargets = placedBoxes.length;
levelPass = targetQuadrilles.length == placedTargets;
}

// Function to iterate and apply styles to a given object
function applyStyles(object, jsonStyles) {
const objectPropList = Object.keys(jsonStyles);
for (let propIterator = 0; propIterator<objectPropList.length ; propIterator++) {
const propertyName = objectPropList[propIterator];
object.style(propertyName, jsonStyles[propertyName]);
};
}
8 changes: 4 additions & 4 deletions globalVariables.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ let placedTargets = 0, levelPass = false, successAudio = true;
Quadrille.CELL_LENGTH = 50;
Quadrille.OUTLINE_WEIGHT = 0;

// Definition of the buttons' data objects
let menuButton, menuButtonData;

// Definition of the map and it's dimensions
let mapOutline, columns, rows;

// The ID of the map to load!
let levelId = 0;

// Define the button styles
let buttonStyles;
let buttonStyles;

// Define the step counter variable
let stepsTaken = 0;
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ let head;

function preload() {
head = loadImage('./assets/player/head.png');
loadFont('./assets/fonts/SyntaxBlack.otf');
loadFont('./assets/fonts/Fredoka-Medium.ttf');
}

function setup() {
Expand All @@ -18,7 +18,7 @@ function draw() {
background('#6f6f');
image(head, 0, 0);

textFont('SyntaxBlack');
textFont('Fredoka-Medium');
textSize(40);
fill('#000');
const w = textWidth('Sokoban Worlds');
Expand Down

0 comments on commit a0aabb2

Please sign in to comment.