From d424aea7a14634e0068b48f82f5c322b66bfe0fb Mon Sep 17 00:00:00 2001 From: zakarya Date: Fri, 10 Mar 2023 11:13:54 -0800 Subject: [PATCH] Change imate to type, remove error message, add server side collisions (they don't work yet), added "proper" world generation --- engine.js | 4 ++-- server.js | 41 ++++++++++++++++++++++++++++++++--------- 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/engine.js b/engine.js index 9f0ca14..38008c8 100644 --- a/engine.js +++ b/engine.js @@ -22,12 +22,12 @@ class vector { } class node { - constructor(position, rotation, radius, size, image) { + constructor(position, rotation, radius, size, type) { this.position = position; this.rotation = rotation; this.radius = radius; this.size = size; - this.image = image; + this.type = type; nodes.add(this); } diff --git a/server.js b/server.js index b0910ce..25a3193 100644 --- a/server.js +++ b/server.js @@ -53,14 +53,12 @@ function message(type, data, from) { if ("position" in data) { try { 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) { from.position.x = new_position.x; from.position.y = new_position.y; } else { - console.log(from.username + " moved too fast!"); - console.log(from.position.distance_to(new_position)); + //console.log(from.username + " moved too fast!"); var to_send = { "type": "position_update", "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() { + do_collisions(); + var char_array = Array.from(characters); //send other players for (let this_player_num in char_array) { @@ -156,16 +164,31 @@ function server_tick() { setInterval(server_tick, 1000 / server_fps); -function create_boulder(position) { - nodes.add(new Engine.node(position, 0, game_data.world.biome.forest.boulder.radius, game_data.world.biome.forest.boulder.size, "boulder")); +function random_rot() { + let rot = Math.floor(Math.random() * 360); + let rot_rad = rot * (Math.PI/180); + return rot_rad; } -function create_tree(position) { - nodes.add(new Engine.node(position, 0, game_data.world.biome.forest.tree.radius, game_data.world.biome.forest.tree.size, "tree")); +function random_pos() { + 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() { - create_tree(new Engine.vector(70, 70)); - create_boulder(new Engine.vector(-100, 0)); + /*create_tree(new Engine.vector(70, 70), random_rot()); + 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(); \ No newline at end of file