Red square moving (he fuckin goin)
This commit is contained in:
parent
eb5bc9b2c9
commit
e79fbf72c8
@ -99,6 +99,23 @@ function move_walker(walker, possible_directions) {
|
||||
walker.y_velocity = walker.speed * Math.sin(walker.angle)
|
||||
}
|
||||
|
||||
function add_random_direction(pos) {
|
||||
let rand = Math.floor(Math.random() * 4);
|
||||
if (rand === 0) {
|
||||
pos.row++;
|
||||
pos.row = pos.row.mod(n_rows);
|
||||
} else if (rand === 1) {
|
||||
pos.row--;
|
||||
pos.row = pos.row.mod(n_rows);
|
||||
} else if (rand === 2) {
|
||||
pos.col++;
|
||||
pos.col = pos.col.mod(n_cols);
|
||||
} else {
|
||||
pos.col--;
|
||||
pos.col = pos.col.mod(n_cols);
|
||||
}
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Drawing *
|
||||
****************************************************************************/
|
||||
@ -115,6 +132,9 @@ function draw() {
|
||||
var old_n_cols = n_cols;
|
||||
n_rows = canvas.height.div(controls.cell_size);
|
||||
n_cols = canvas.width.div(controls.cell_size);
|
||||
offset_h = (canvas.width - (controls.cell_size * n_cols)).div(2);
|
||||
offset_v = (canvas.height - (controls.cell_size * n_rows)).div(2);
|
||||
offset = {h: offset_h, v: offset_v};
|
||||
// TODO: for every walker and every trail, adjust their position to the new canvas size
|
||||
// we wanna just mod it, probably. that way if you shrink it you don't
|
||||
// lose anything.
|
||||
@ -126,11 +146,14 @@ function draw() {
|
||||
// grid of all one color.
|
||||
draw_grid();
|
||||
|
||||
// draw a colored square at the walker location
|
||||
draw_grid_square(walker_pos.row, walker_pos.col, "red");
|
||||
add_random_direction(walker_pos);
|
||||
|
||||
// TODO CODE
|
||||
}
|
||||
|
||||
function draw_grid_square(row, col, color, offset) {
|
||||
function draw_grid_square(row, col, color) {
|
||||
var margin = 2;
|
||||
|
||||
ctx.fillStyle = color;
|
||||
@ -142,14 +165,10 @@ function draw_grid_square(row, col, color, offset) {
|
||||
|
||||
|
||||
function draw_grid() {
|
||||
var offset_h = (canvas.width - (controls.cell_size * n_cols)).div(2);
|
||||
var offset_v = (canvas.height - (controls.cell_size * n_rows)).div(2);
|
||||
var offset = {h: offset_h, v: offset_v};
|
||||
|
||||
// Actually draw the cells lol
|
||||
for (var row = 0; row < n_rows; row++) {
|
||||
for (var col = 0; col < n_cols; col++) {
|
||||
draw_grid_square(row, col, "#282828", offset);
|
||||
draw_grid_square(row, col, "#282828");
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -167,8 +186,10 @@ canvas.height = window.innerHeight;
|
||||
// compute the initial rows and columns
|
||||
var n_rows = canvas.height.div(controls.cell_size);
|
||||
var n_cols = canvas.width.div(controls.cell_size);
|
||||
var offset_h = (canvas.width - (controls.cell_size * n_cols)).div(2);
|
||||
var offset_v = (canvas.height - (controls.cell_size * n_rows)).div(2);
|
||||
var offset = {h: offset_h, v: offset_v};
|
||||
|
||||
// set start position of walker and start the random walk
|
||||
var walker_position = {x: n_rows.div(2), y: n_cols.div(2)};
|
||||
var trail_positions = [];
|
||||
var walker_pos = {row: n_rows.div(2), col: n_cols.div(2)};
|
||||
init_walk();
|
||||
|
Loading…
x
Reference in New Issue
Block a user