Change imate to type, remove error message, add server side collisions (they don't work yet), added "proper" world generation

This commit is contained in:
2023-03-10 11:13:54 -08:00
parent c82fbdddbc
commit d424aea7a1
2 changed files with 34 additions and 11 deletions

View File

@ -22,12 +22,12 @@ class vector {
} }
class node { class node {
constructor(position, rotation, radius, size, image) { constructor(position, rotation, radius, size, type) {
this.position = position; this.position = position;
this.rotation = rotation; this.rotation = rotation;
this.radius = radius; this.radius = radius;
this.size = size; this.size = size;
this.image = image; this.type = type;
nodes.add(this); nodes.add(this);
} }

View File

@ -53,14 +53,12 @@ function message(type, data, from) {
if ("position" in data) { if ("position" in data) {
try { try {
let new_position = new Engine.vector(data.position.x, data.position.y); let new_position = new Engine.vector(data.position.x, data.position.y);
//console.log(new_position);
if (from.position.distance_to(new_position) <= max_player_speed / server_fps) { if (from.position.distance_to(new_position) <= max_player_speed / server_fps) {
from.position.x = new_position.x; from.position.x = new_position.x;
from.position.y = new_position.y; from.position.y = new_position.y;
} }
else { else {
console.log(from.username + " moved too fast!"); //console.log(from.username + " moved too fast!");
console.log(from.position.distance_to(new_position));
var to_send = { var to_send = {
"type": "position_update", "type": "position_update",
"data": { "data": {
@ -127,7 +125,17 @@ server.on("connection", function (socket, request) {
}); });
}); });
function do_collisions() {
characters.forEach(function(this_character) {
nodes.forEach(function(this_node) {
Engine.collide(this_node, this_character);
});
});
}
function server_tick() { function server_tick() {
do_collisions();
var char_array = Array.from(characters); var char_array = Array.from(characters);
//send other players //send other players
for (let this_player_num in char_array) { for (let this_player_num in char_array) {
@ -156,16 +164,31 @@ function server_tick() {
setInterval(server_tick, 1000 / server_fps); setInterval(server_tick, 1000 / server_fps);
function create_boulder(position) { function random_rot() {
nodes.add(new Engine.node(position, 0, game_data.world.biome.forest.boulder.radius, game_data.world.biome.forest.boulder.size, "boulder")); let rot = Math.floor(Math.random() * 360);
let rot_rad = rot * (Math.PI/180);
return rot_rad;
} }
function create_tree(position) { function random_pos() {
nodes.add(new Engine.node(position, 0, game_data.world.biome.forest.tree.radius, game_data.world.biome.forest.tree.size, "tree")); return new Engine.vector((Math.random() * 10000)-5000, (Math.random() * 10000)-5000);
}
function create_boulder(position, rotation) {
nodes.add(new Engine.node(position, rotation, game_data.world.biome.forest.boulder.radius, game_data.world.biome.forest.boulder.size, "boulder"));
}
function create_tree(position, rotation) {
nodes.add(new Engine.node(position, rotation, game_data.world.biome.forest.tree.radius, game_data.world.biome.forest.tree.size, "tree"));
} }
function generate_world() { function generate_world() {
create_tree(new Engine.vector(70, 70)); /*create_tree(new Engine.vector(70, 70), random_rot());
create_boulder(new Engine.vector(-100, 0)); create_boulder(new Engine.vector(-100, 0), random_rot());
create_boulder(new Engine.vector(-230, 0), random_rot());*/
for (let i=0; i<100; i++) {
create_tree(random_pos(), random_rot());
create_boulder(random_pos(), random_rot());
}
} }
generate_world(); generate_world();