-
Notifications
You must be signed in to change notification settings - Fork 296
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
add "furthest X" line #55
base: master
Are you sure you want to change the base?
Conversation
0abf398
to
80664e9
Compare
80664e9
to
97b92b5
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
Code Summary:
The code provided manages the minimap in a game, including updating fog distance based on player progress, resetting fog distance, and drawing various elements on the minimap. Recent changes seem to introduce the concept of maintaining and restoring the previous fog distance across rounds.
Strengths:
- Fog Distance Management: The addition of
previousMinimapFogDistance
ensures that the fog distance is preserved across rounds, which can enhance gameplay by maintaining visual continuity. - Clear Structure: The code is well-organized, with distinct functions for different tasks (e.g., drawing the minimap, resetting the world).
Areas for Improvement:
- Redundant Assignments: In the
cw_drawMiniMap
function,minimapfogdistance
andfogdistance.width
are reset at the beginning and then immediately updated. This can be streamlined. - Code Duplication: The code for updating the fog distance width is duplicated. Consider refactoring this into a separate function.
- Comment Clarity: While comments are present, some additional comments explaining the overall purpose and flow of the code would improve readability.
Suggested Revision:
var minimapctx = minimapcanvas.getContext("2d");
var minimapscale = 3;
var minimapfogdistance = 0;
var previousMinimapFogDistance = 0;
var fogdistance = document.getElementById("minimapfog").style;
function showDistance(distance, height) {
if (distance > minimapfogdistance) {
updateFogDistance(distance);
}
}
function updateFogDistance(distance) {
fogdistance.width = 800 - Math.round(distance + 15) * minimapscale + "px";
minimapfogdistance = distance;
previousMinimapFogDistance = minimapfogdistance;
}
function cw_drawMiniMap() {
var floorTiles = currentRunner.scene.floorTiles;
var last_tile = null;
var tile_position = new b2Vec2(-5, 0);
// Reset minimap and context
minimapcanvas.width = minimapcanvas.width;
minimapctx.strokeStyle = "#3F72AF";
minimapctx.beginPath();
// Drawing floor tiles
for (let i = 0; i < floorTiles.length; i++) {
// Assuming there is some drawing logic here
}
minimapctx.stroke();
// Draw the furthest distance flag
var furthestDistanceX = world_def.furthestDistance;
minimapctx.strokeStyle = "rgba(255, 87, 87, 0.5)";
minimapctx.beginPath();
minimapctx.moveTo((furthestDistanceX + 5) * minimapscale, 0);
minimapctx.lineTo((furthestDistanceX + 5) * minimapscale, minimapcanvas.height);
minimapctx.stroke();
// Restore fog distance from the previous round
minimapfogdistance = previousMinimapFogDistance;
updateFogDistance(previousMinimapFogDistance);
}
function cw_resetWorld() {
setupCarUI();
cw_drawMiniMap();
// Reset fog distances
minimapfogdistance = 0;
previousMinimapFogDistance = 0;
cw_startSimulation();
}
Additional Recommendations:
- Modular Functions: Extract repeated logic (like fog distance updates) into separate functions for better modularity and reusability.
- Error Handling: Consider adding error handling for cases where elements like
minimapcanvas
orminimapfog
might not be available. - Testing: Implement unit tests to ensure that the minimap and fog distance logic work correctly under various scenarios.
By addressing these points, the code will be more maintainable, readable, and robust.
I added a vertical red line which shows the furthest X distance travelled, so you can easily see if a car goes further than before in the current round.
The minimap also shows the red line, and any fog that was cleared in a round stays cleared in the next round.
Video: