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

add "furthest X" line #55

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions bundle-bare.js

Large diffs are not rendered by default.

112 changes: 88 additions & 24 deletions bundle.js

Large diffs are not rendered by default.

20 changes: 20 additions & 0 deletions src/draw/draw-furthest-distance-flag.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
var cw_drawVirtualPoly = require("./draw-virtual-poly");
module.exports = function(ctx, camera, cw_furthestDistanceFlag) {
var camera_x = camera.pos.x;
var zoom = camera.zoom;
ctx.strokeStyle = "rgba(255, 87, 87, 0.5)";
ctx.fillStyle = "rgba(255, 87, 87, 0.5)";
ctx.lineWidth = 1 / zoom;
ctx.beginPath();

for (var f = cw_furthestDistanceFlag.GetFixtureList(); f; f = f.m_next) {
var s = f.GetShape();
var shapePosition = cw_furthestDistanceFlag.GetWorldPoint(s.m_vertices[0]).x;
if ((shapePosition > (camera_x - 5)) && (shapePosition < (camera_x + 10))) {
cw_drawVirtualPoly(ctx, cw_furthestDistanceFlag, s.m_vertices, s.m_vertexCount);
}
}

ctx.fill();
ctx.stroke();
}
32 changes: 29 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ var graph_fns = require("./draw/plot-graphs.js");
var plot_graphs = graph_fns.plotGraphs;
var cw_clearGraphics = graph_fns.clearGraphics;
var cw_drawFloor = require("./draw/draw-floor.js");
var cw_drawFurthestDistanceFlag = require("./draw/draw-furthest-distance-flag.js");

var ghost_draw_frame = ghost_fns.ghost_draw_frame;
var ghost_create_ghost = ghost_fns.ghost_create_ghost;
Expand Down Expand Up @@ -57,6 +58,7 @@ var minimapcanvas = document.getElementById("minimap");
var minimapctx = minimapcanvas.getContext("2d");
var minimapscale = 3;
var minimapfogdistance = 0;
var previousMinimapFogDistance = 0;
var fogdistance = document.getElementById("minimapfog").style;


Expand Down Expand Up @@ -92,7 +94,8 @@ var world_def = {
box2dfps: box2dfps,
motorSpeed: 20,
max_car_health: max_car_health,
schema: generationConfig.constants.schema
schema: generationConfig.constants.schema,
furthestDistance: 0,
}

var cw_deadCars;
Expand Down Expand Up @@ -129,6 +132,9 @@ function showDistance(distance, height) {
if (distance > minimapfogdistance) {
fogdistance.width = 800 - Math.round(distance + 15) * minimapscale + "px";
minimapfogdistance = distance;

// Update the previousMinimapFogDistance to retain the cleared fog in the next round
previousMinimapFogDistance = minimapfogdistance;
}
}

Expand Down Expand Up @@ -173,6 +179,7 @@ function cw_drawScreen() {
ctx.translate(200 - (camera_x * zoom), 200 + (camera_y * zoom));
ctx.scale(zoom, -zoom);
cw_drawFloor(ctx, camera, floorTiles);
cw_drawFurthestDistanceFlag(ctx, camera, currentRunner.scene.furthestDistanceFlag);
ghost_draw_frame(ctx, ghost, camera);
cw_drawCars();
ctx.restore();
Expand Down Expand Up @@ -257,8 +264,6 @@ function cw_drawMiniMap() {
var floorTiles = currentRunner.scene.floorTiles;
var last_tile = null;
var tile_position = new b2Vec2(-5, 0);
minimapfogdistance = 0;
fogdistance.width = "800px";
minimapcanvas.width = minimapcanvas.width;
minimapctx.strokeStyle = "#3F72AF";
minimapctx.beginPath();
Expand All @@ -271,6 +276,18 @@ function cw_drawMiniMap() {
minimapctx.lineTo((tile_position.x + 5) * minimapscale, (-tile_position.y + 35) * minimapscale);
}
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();

// Update the fog distance based on the previous round
minimapfogdistance = previousMinimapFogDistance;
fogdistance.width = 800 - Math.round(previousMinimapFogDistance + 15) * minimapscale + "px";
}

/* ==== END Drawing ======================================================== */
Expand Down Expand Up @@ -404,6 +421,11 @@ function cw_newRound(results) {
// RE-ENABLE GHOST
ghost_reset_ghost(ghost);
}

// set high score
var thisFurthestDistance = Math.max.apply(Math, results.map(function(o){return o.score.x;}));
world_def.furthestDistance = Math.max(thisFurthestDistance, world_def.furthestDistance);

currentRunner = worldRun(world_def, generationState.generation, uiListeners);
setupCarUI();
cw_drawMiniMap();
Expand Down Expand Up @@ -437,6 +459,7 @@ function cw_resetWorld() {
doDraw = true;
cw_stopSimulation();
world_def.floorseed = document.getElementById("newseed").value;
world_def.furthestDistance = 0;
cw_clearPopulationWorld();
cw_resetPopulationUI();

Expand All @@ -451,6 +474,9 @@ function cw_resetWorld() {
setupCarUI()
cw_drawMiniMap();

minimapfogdistance = 0;
previousMinimapFogDistance = 0;

cw_startSimulation();
}

Expand Down
18 changes: 17 additions & 1 deletion src/world/setup-scene.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ world_def = {
module.exports = function(world_def){

var world = new b2World(world_def.gravity, world_def.doSleep);

var furthestDistanceFlag = cw_createfurthestDistanceFlag(world, world_def.furthestDistance);

var floorTiles = cw_createFloor(
world,
world_def.floorseed,
Expand All @@ -35,10 +38,23 @@ module.exports = function(world_def){
return {
world: world,
floorTiles: floorTiles,
finishLine: tile_position.x
finishLine: tile_position.x,
furthestDistanceFlag: furthestDistanceFlag,
};
}

function cw_createfurthestDistanceFlag(world, furthestDistance) {
var body_def = new b2BodyDef();
body_def.position.Set(furthestDistance, 0);
var body = world.CreateBody(body_def);
var fix_def = new b2FixtureDef();
fix_def.shape = new b2PolygonShape();
fix_def.isSensor = true;
fix_def.shape.SetAsBox(0.1, 20);
body.CreateFixture(fix_def);
return body;
}

function cw_createFloor(world, floorseed, dimensions, maxFloorTiles, mutable_floor) {
var last_tile = null;
var tile_position = new b2Vec2(-5, 0);
Expand Down