Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor level utility functions into appropriate classes #2

Open
kevincoleman opened this issue Oct 4, 2018 · 0 comments
Open

Refactor level utility functions into appropriate classes #2

kevincoleman opened this issue Oct 4, 2018 · 0 comments

Comments

@kevincoleman
Copy link
Owner

There are several game functions in the level component that could probably be moved to a more semantic location:

sokoban/src/Level.jsx

Lines 144 to 151 in d7406e3

checkWin() {
return (
this.state.level.chart.filter(block => block.type == "box diamond")
.length +
1 >=
this.state.diamonds.length
);
}

sokoban/src/Level.jsx

Lines 153 to 166 in d7406e3

getBlock(x, y) {
if (
x > 0 &&
x <= this.state.level.width &&
y > 0 &&
y < this.state.level.height
) {
return this.state.level.chart.filter(
block => block.x == x && block.y == y
)[0];
} else {
return { type: "edge", x: x, y: y };
}
}

sokoban/src/Level.jsx

Lines 168 to 174 in d7406e3

isSolid(block) {
return (
block.type.indexOf("brick") >= 0 ||
block.type.indexOf("box") >= 0 ||
block.type.indexOf("edge") >= 0
);
}

sokoban/src/Level.jsx

Lines 176 to 221 in d7406e3

isBlocked(x, y, direction) {
switch (direction) {
case "left":
if (this.getBlock(x - 1, y).type == "brick") {
return true;
} else {
return (
// note: because of order of operations, if the closer block is not solid, this won’t check the farther one
this.isSolid(this.getBlock(x - 1, y)) &&
this.isSolid(this.getBlock(x - 2, y))
);
}
case "right":
if (this.getBlock(x + 1, y).type.indexOf("brick") >= 0) {
return true;
} else {
return (
// note: because of order of operations, if the closer block is not solid, this won’t check the farther one
this.isSolid(this.getBlock(x + 1, y)) &&
this.isSolid(this.getBlock(x + 2, y))
);
}
case "up":
if (this.getBlock(x, y - 1).type.indexOf("brick") >= 0) {
return true;
} else {
return (
// note: because of order of operations, if the closer block is not solid, this won’t check the farther one
this.isSolid(this.getBlock(x, y - 1)) &&
this.isSolid(this.getBlock(x, y - 2))
);
}
case "down":
if (this.getBlock(x, y + 1).type.indexOf("brick") >= 0) {
return true;
} else {
return (
// note: because of order of operations, if the closer block is not solid, this won’t check the farther one
this.isSolid(this.getBlock(x, y + 1)) &&
this.isSolid(this.getBlock(x, y + 2))
);
}
default:
return `${direction} is unblocked, or you’re checking the edge of the map from ${x}, ${y}.`;
}
}

sokoban/src/Level.jsx

Lines 233 to 246 in d7406e3

// update the clock
let totalSeconds = 0;
let countTimer = () => {
++totalSeconds;
let hour = Math.floor(totalSeconds / 3600);
let minute = Math.floor((totalSeconds - hour * 3600) / 60);
let seconds = totalSeconds - (hour * 3600 + minute * 60);
this.setState({
time: `${pad(hour, 2)}:${pad(minute, 2)}:${pad(seconds, 2)}`
});
};
setInterval(countTimer, 1000);
}

These need assessment to see if they could be put in a better place. Open to suggestions for how to remove the context-reliant nature of them as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant