Skip to content

Instantly share code, notes, and snippets.

@AhmedSa-mir
Created April 13, 2018 18:44
Show Gist options
  • Save AhmedSa-mir/0bf3f1960b926c6ea6fd6bbc3c482b65 to your computer and use it in GitHub Desktop.
Save AhmedSa-mir/0bf3f1960b926c6ea6fd6bbc3c482b65 to your computer and use it in GitHub Desktop.
Simulation Loop
// Player attributes:
// 1 = ID
// 2 = Tail
// 3 = Block
// 4 = Shadow
while (true) {
for(player in rooms[room_name].players) {
let time = process.hrtime();
let delta_time = player.last_time_stamp - time;
player.last_time_stamp = time;
let last_position = player.position;
player.position.x += speed * delta_time;
player.position.y += speed * delta_time;
let x_delta = Math.abs(player.position.x - last_position.x);
let y_delta = Math.abs(player.position.y - last_position.y);
while(x_delta > 0) {
if (player.position.x > last_position.x) {
player.position.x--;
} else {
player.position.x++;
}
let cell = rooms[room_name].grid[player.position.x][player.position.y];
if(x_delta < 1){
//TODO: Put head
}
else if(cell == 1 || cell == player.ID + 1){ // Border || Own tail
//TODO: Die
}
else if(cell == player.ID + 2){ // Own block
//TODO: Fill path
}
else if(cell == 0 || cell % 3 == 0){ // Not empty
//TODO: Put tail
}
else {
//TODO: Kill or Eat
}
x_delta--;
}
while(y_delta > 0) {
if (player.position.y > last_position.y) {
player.position.y--;
} else {
player.position.y++;
}
let cell = rooms[room_name].grid[player.position.x][player.position.y];
if(y_delta < 1){
//TODO: Put head
}
else if(cell == 1 || cell == player.ID + 1){ // Border || Own tail
//TODO: Die
}
else if(cell == player.ID + 2){ // Own block
//TODO: Fill path
}
else if(cell == 0 || cell % 3 == 0){ // Not empty
//TODO: Put tail
}
else {
//TODO: Kill or Eat
}
y_delta--;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment