moved game tick function, fixed node rendering, independence of camera

This commit is contained in:
2023-03-03 14:35:13 -08:00
parent 2859e30482
commit 26217a9506
2 changed files with 25 additions and 18 deletions

View File

@ -1,6 +1,7 @@
var canvas; var canvas;
var canvas_center = new vector(0, 0); var canvas_center = new vector(0, 0);
var mouse_position = new vector(0, 0); var mouse_position = new vector(0, 0);
var camera = new vector(0, 0);
function load_image(path) { function load_image(path) {
let image = new Image(); let image = new Image();
@ -9,8 +10,8 @@ function load_image(path) {
} }
function global_to_canvas(pos) { function global_to_canvas(pos) {
let tempx = -player.position.x + pos.x + canvas_center.x; let tempx = -camera.x + pos.x + canvas_center.x;
let tempy = -player.position.y + pos.y + canvas_center.y; let tempy = -camera.y + pos.y + canvas_center.y;
return new vector(tempx, tempy); return new vector(tempx, tempy);
} }

View File

@ -90,18 +90,6 @@ function key_up(e) {
} }
} }
function game_tick() {
//calculations
do_collisions();
do_movement();
//rendering
render_background();
render_characters();
render_nodes();
render_usernames();
}
function init_player() { function init_player() {
let player_vector = new vector(0, 0); let player_vector = new vector(0, 0);
player = new character(player_vector, 0, 0, username); player = new character(player_vector, 0, 0, username);
@ -209,13 +197,25 @@ document.onmousemove = function(e) {
//position.rot = point_towards([xenter, yenter], e) + rotate_offset; //position.rot = point_towards([xenter, yenter], e) + rotate_offset;
mouse_position.x = e.pageX; mouse_position.x = e.pageX;
mouse_position.y = e.pageY; mouse_position.y = e.pageY;
}; }
function game_tick() {
//calculations
do_movement();
do_collisions();
move_camera();
//rendering
render_background();
render_characters();
render_nodes();
render_usernames();
}
function render_characters() { function render_characters() {
let ctx = canvas.getContext("2d"); let ctx = canvas.getContext("2d");
characters.forEach(function(itr, idx) { characters.forEach(function(itr, idx) {
ctx.save(); ctx.save();
//draw character
let character_position = global_to_canvas(itr.position); let character_position = global_to_canvas(itr.position);
ctx.translate(character_position.x, character_position.y); ctx.translate(character_position.x, character_position.y);
ctx.rotate(itr.rotation); ctx.rotate(itr.rotation);
@ -266,14 +266,20 @@ function render_nodes() {
let position = global_to_canvas(itr.position); let position = global_to_canvas(itr.position);
let rotation = itr.rotation; let rotation = itr.rotation;
let image = game_data.world.biome.forest[itr.image].img; let image = game_data.world.biome.forest[itr.image].img;
//let image = game_data.world.biome.forest.tree.img;
ctx.translate(position.x, position.y); ctx.translate(position.x, position.y);
ctx.rotate(rotation); ctx.rotate(rotation);
ctx.drawImage(image, image.width/2, image.height/2, dwidth=itr.size, dheight=itr.size); ctx.drawImage(image, -itr.size/2, -itr.size/2, dwidth=itr.size, dheight=itr.size);
ctx.restore(); ctx.restore();
}); });
} }
function move_camera() {
camera.x = player.position.x;
camera.y = player.position.y;
}
function open_socket() { function open_socket() {
server = new WebSocket("ws://NUC-server-1:8080"); server = new WebSocket("ws://NUC-server-1:8080");
server.onopen = function() { server.onopen = function() {