Skip to content

Commit

Permalink
add high score flag
Browse files Browse the repository at this point in the history
  • Loading branch information
opyate committed May 10, 2024
1 parent f3e9ce5 commit 80664e9
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 26 deletions.
20 changes: 18 additions & 2 deletions bundle-bare.js

Large diffs are not rendered by default.

90 changes: 68 additions & 22 deletions bundle.js

Large diffs are not rendered by default.

20 changes: 20 additions & 0 deletions src/draw/draw-high-score-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_highScoreFlag) {
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_highScoreFlag.GetFixtureList(); f; f = f.m_next) {
var s = f.GetShape();
var shapePosition = cw_highScoreFlag.GetWorldPoint(s.m_vertices[0]).x;
if ((shapePosition > (camera_x - 5)) && (shapePosition < (camera_x + 10))) {
cw_drawVirtualPoly(ctx, cw_highScoreFlag, s.m_vertices, s.m_vertexCount);
}
}

ctx.fill();
ctx.stroke();
}
10 changes: 9 additions & 1 deletion 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_drawHighScoreFlag = require("./draw/draw-high-score-flag.js");

var ghost_draw_frame = ghost_fns.ghost_draw_frame;
var ghost_create_ghost = ghost_fns.ghost_create_ghost;
Expand Down Expand Up @@ -92,7 +93,8 @@ var world_def = {
box2dfps: box2dfps,
motorSpeed: 20,
max_car_health: max_car_health,
schema: generationConfig.constants.schema
schema: generationConfig.constants.schema,
highScore: 0,
}

var cw_deadCars;
Expand Down Expand Up @@ -173,6 +175,7 @@ function cw_drawScreen() {
ctx.translate(200 - (camera_x * zoom), 200 + (camera_y * zoom));
ctx.scale(zoom, -zoom);
cw_drawFloor(ctx, camera, floorTiles);
cw_drawHighScoreFlag(ctx, camera, currentRunner.scene.highScoreFlag);
ghost_draw_frame(ctx, ghost, camera);
cw_drawCars();
ctx.restore();
Expand Down Expand Up @@ -404,6 +407,11 @@ function cw_newRound(results) {
// RE-ENABLE GHOST
ghost_reset_ghost(ghost);
}

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

currentRunner = worldRun(world_def, generationState.generation, uiListeners);
setupCarUI();
cw_drawMiniMap();
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 highScoreFlag = cw_createHighScoreFlag(world, world_def.highScore);

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,
highScoreFlag: highScoreFlag,
};
}

function cw_createHighScoreFlag(world, highScore) {
var body_def = new b2BodyDef();
body_def.position.Set(highScore, 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

0 comments on commit 80664e9

Please sign in to comment.