runic-client/script/client.js

51 lines
1.0 KiB
JavaScript
Raw Normal View History

2023-03-03 12:22:49 -08:00
var canvas;
var canvas_center = new vector(0, 0);
var mouse_position = new vector(0, 0);
var camera = new vector(0, 0);
2023-03-09 10:14:54 -08:00
var lastupdate = Date.now();
2023-03-03 12:22:49 -08:00
function load_image(path) {
let image = new Image();
image.src = path;
return image;
}
function global_to_canvas(pos) {
let tempx = -camera.x + pos.x + canvas_center.x;
let tempy = -camera.y + pos.y + canvas_center.y;
2023-03-03 12:22:49 -08:00
return new vector(tempx, tempy);
}
function resize_canvas() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
canvas_center.x = canvas.width / 2;
canvas_center.y = canvas.height / 2;
}
function clear() {
const ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, canvas.width, canvas.height);
}
function init() {
canvas = document.getElementById("main_canvas");
resize_canvas();
document.onkeydown = key_down;
document.onkeyup = key_up;
game_init();
}
function tick() {
2023-03-09 10:14:54 -08:00
let now = Date.now();
let delta = now - lastupdate;
lastupdate = now;
2023-03-03 12:22:49 -08:00
resize_canvas(canvas);
2023-03-09 10:14:54 -08:00
game_tick(delta);
2023-03-03 12:22:49 -08:00
}
function game_ready() {
setInterval(tick, 1000 / fps);
}