Fix generation outside of grid, add gap slider
This commit is contained in:
parent
d49a395dbd
commit
21ea36e370
@ -72,3 +72,10 @@ button {
|
|||||||
display: flex;
|
display: flex;
|
||||||
justify-content: center;
|
justify-content: center;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.slider {
|
||||||
|
display: flex;
|
||||||
|
justify-content: center;
|
||||||
|
align-items: center;
|
||||||
|
margin-top: 3px;
|
||||||
|
}
|
||||||
|
@ -14,6 +14,10 @@
|
|||||||
<button id="solve">Solve</button>
|
<button id="solve">Solve</button>
|
||||||
<button id="directions">Directions</button>
|
<button id="directions">Directions</button>
|
||||||
</div>
|
</div>
|
||||||
|
<div class="slider">
|
||||||
|
<label>Gap</label>
|
||||||
|
<input type="range" min="15" max="80" id="gap" />
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<span id="expand-show">↑↑</span>
|
<span id="expand-show">↑↑</span>
|
||||||
</div>
|
</div>
|
||||||
@ -68,6 +72,7 @@
|
|||||||
<script src="js/cx.js"></script>
|
<script src="js/cx.js"></script>
|
||||||
<script src="js/json-ds.js"></script>
|
<script src="js/json-ds.js"></script>
|
||||||
<script src="js/sol.js"></script>
|
<script src="js/sol.js"></script>
|
||||||
|
|
||||||
<script src="js/game.js"></script>
|
<script src="js/game.js"></script>
|
||||||
<script src="js/controls.js"></script>
|
<script src="js/controls.js"></script>
|
||||||
</body>
|
</body>
|
||||||
|
9
euler-golf/js/controls.js
vendored
9
euler-golf/js/controls.js
vendored
@ -1,13 +1,10 @@
|
|||||||
const directions_modal = new Modal({
|
|
||||||
el: document.getElementById("directions-modal"),
|
|
||||||
});
|
|
||||||
|
|
||||||
document
|
document
|
||||||
.getElementById("controls-container")
|
.getElementById("controls-container")
|
||||||
.addEventListener("mouseover", () => {
|
.addEventListener("mouseover", () => {
|
||||||
document.getElementById("controls").style.display = "block";
|
document.getElementById("controls").style.display = "block";
|
||||||
document.getElementById("expand-show").style.display = "none";
|
document.getElementById("expand-show").style.display = "none";
|
||||||
});
|
});
|
||||||
|
|
||||||
document
|
document
|
||||||
.getElementById("controls-container")
|
.getElementById("controls-container")
|
||||||
.addEventListener("mouseout", () => {
|
.addEventListener("mouseout", () => {
|
||||||
@ -30,3 +27,7 @@ document.getElementById("solve").addEventListener("click", () => {
|
|||||||
document
|
document
|
||||||
.getElementById("directions")
|
.getElementById("directions")
|
||||||
.addEventListener("click", () => directions_modal.show());
|
.addEventListener("click", () => directions_modal.show());
|
||||||
|
|
||||||
|
document.getElementById("gap").addEventListener("input", function () {
|
||||||
|
state.changes.gap = Number(this.value);
|
||||||
|
});
|
||||||
|
@ -4,10 +4,12 @@ const DEFAULTS = {
|
|||||||
min_gap: 30,
|
min_gap: 30,
|
||||||
angle_multiplier: 10e-4,
|
angle_multiplier: 10e-4,
|
||||||
};
|
};
|
||||||
|
|
||||||
const CANVAS = document.getElementById("canvas");
|
const CANVAS = document.getElementById("canvas");
|
||||||
|
|
||||||
let state = {
|
let state = {
|
||||||
grid_padding: 30,
|
grid_padding: 30,
|
||||||
|
gap: DEFAULTS.min_gap,
|
||||||
canvas: CANVAS,
|
canvas: CANVAS,
|
||||||
ctx: CANVAS.getContext("2d"),
|
ctx: CANVAS.getContext("2d"),
|
||||||
last_render: 0,
|
last_render: 0,
|
||||||
@ -87,8 +89,8 @@ const rand_between = (min, max) =>
|
|||||||
Math.floor(Math.random() * (max - min + 1)) + min;
|
Math.floor(Math.random() * (max - min + 1)) + min;
|
||||||
|
|
||||||
const rand_target = (rows, cols) => {
|
const rand_target = (rows, cols) => {
|
||||||
const r = Math.floor(rows / 2);
|
const r = Math.floor((rows - 1) / 2);
|
||||||
const c = Math.floor(cols / 2);
|
const c = Math.floor((cols - 1) / 2);
|
||||||
const res = new cx(rand_between(-c, c), rand_between(-r, r));
|
const res = new cx(rand_between(-c, c), rand_between(-r, r));
|
||||||
if (!sol(res)) return rand_target(rows, cols);
|
if (!sol(res)) return rand_target(rows, cols);
|
||||||
|
|
||||||
@ -140,7 +142,7 @@ const handle_input = (state, dt) => {
|
|||||||
state = maybe_add_state_angle_move(state);
|
state = maybe_add_state_angle_move(state);
|
||||||
};
|
};
|
||||||
|
|
||||||
const render = ({ width, height, ctx, rows, cols, target } = state) => {
|
const render = ({ width, height, ctx, rows, cols, target, gap } = state) => {
|
||||||
ctx.clearRect(0, 0, width, height);
|
ctx.clearRect(0, 0, width, height);
|
||||||
ctx.fillStyle = "rgba(0, 0, 0, 0)";
|
ctx.fillStyle = "rgba(0, 0, 0, 0)";
|
||||||
ctx.fillRect(0, 0, width, height);
|
ctx.fillRect(0, 0, width, height);
|
||||||
@ -172,7 +174,6 @@ const render = ({ width, height, ctx, rows, cols, target } = state) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const grid_target = complex_to_grid(target, rows, cols);
|
const grid_target = complex_to_grid(target, rows, cols);
|
||||||
|
|
||||||
ctx.cartesian_grid(rows, cols, grid_spec, (x, y) => {
|
ctx.cartesian_grid(rows, cols, grid_spec, (x, y) => {
|
||||||
if (x == Math.floor(cols / 2) && y == Math.floor(rows / 2)) {
|
if (x == Math.floor(cols / 2) && y == Math.floor(rows / 2)) {
|
||||||
return {
|
return {
|
||||||
@ -191,6 +192,9 @@ const render = ({ width, height, ctx, rows, cols, target } = state) => {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Render gap value in slider
|
||||||
|
document.getElementById("gap").value = gap;
|
||||||
};
|
};
|
||||||
|
|
||||||
const loop = (now) => {
|
const loop = (now) => {
|
||||||
@ -198,23 +202,17 @@ const loop = (now) => {
|
|||||||
state.changes.last_render = now;
|
state.changes.last_render = now;
|
||||||
|
|
||||||
if (Object.keys(state.changes).length > 0) {
|
if (Object.keys(state.changes).length > 0) {
|
||||||
if (state.changes.width || state.changes.height) {
|
|
||||||
state.changes.rows = Math.floor(
|
|
||||||
Math.min(DEFAULTS.max_rows, state.changes.height / DEFAULTS.min_gap)
|
|
||||||
);
|
|
||||||
state.changes.cols = Math.floor(
|
|
||||||
Math.min(DEFAULTS.max_cols, state.changes.width / DEFAULTS.min_gap)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
state = { ...state, ...state.changes };
|
state = { ...state, ...state.changes };
|
||||||
|
|
||||||
|
if (state.changes.width || state.changes.height || state.changes.gap) {
|
||||||
|
state.rows = Math.floor(state.height / state.gap);
|
||||||
|
state.cols = Math.floor(state.width / state.gap);
|
||||||
|
}
|
||||||
|
|
||||||
state.changes = {};
|
state.changes = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!state.target) {
|
if (!state.target) state.target = rand_target(state.rows, state.cols);
|
||||||
state.target = rand_target(state.rows, state.cols);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!state.solution) {
|
if (!state.solution) {
|
||||||
handle_input(state, dt);
|
handle_input(state, dt);
|
||||||
@ -242,6 +240,10 @@ const reset_state = ({ rows, cols } = state) => ({
|
|||||||
});
|
});
|
||||||
|
|
||||||
// DOM
|
// DOM
|
||||||
|
const directions_modal = new Modal({
|
||||||
|
el: document.getElementById("directions-modal"),
|
||||||
|
});
|
||||||
|
|
||||||
const on_resize = () => {
|
const on_resize = () => {
|
||||||
CANVAS.width = document.body.clientWidth;
|
CANVAS.width = document.body.clientWidth;
|
||||||
CANVAS.height = document.body.clientHeight;
|
CANVAS.height = document.body.clientHeight;
|
||||||
@ -266,10 +268,7 @@ on_resize();
|
|||||||
state = reset_state(state);
|
state = reset_state(state);
|
||||||
|
|
||||||
if (!sessionStorage.getItem("seen-instructions")) {
|
if (!sessionStorage.getItem("seen-instructions")) {
|
||||||
new Modal({
|
directions_modal.show();
|
||||||
el: document.getElementById("directions-modal"),
|
|
||||||
}).show();
|
|
||||||
|
|
||||||
sessionStorage.setItem("seen-instructions", true);
|
sessionStorage.setItem("seen-instructions", true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user