-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
93 lines (74 loc) · 2.23 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import Ship from './Ship.js';
import Vector from './Vector.js';
import { checkImage } from './helpers.js';
import BlackHole from './BlackHole.js';
//constants
const CamfollowPlayer = true;
//Setup
const canvas = document.getElementById('canvas0');
const ctx = canvas.getContext('2d');
//Disable mouse leaving screen
// canvas.requestPointerLock = canvas.requestPointerLock || canvas.mozrequestPointerLock || canvas.webkitrequestPointerLock;
// canvas.onclick = function () {
// canvas.requestPointerLock();
// };
let windowWidth = window.innerWidth;
let windowHeight = window.innerHeight;
canvas.width = windowWidth;
canvas.height = windowHeight;
function resizeCanvas() {
windowWidth = window.innerWidth;
windowHeight = window.innerHeight;
canvas.width = windowWidth;
canvas.height = windowHeight;
console.log("Window was resized");
}; resizeCanvas();
let canvasVector = new Vector(canvas.width/2, canvas.height/2);
let ship = new Ship();
let hole = new BlackHole();
//create grid canvas
// ship.acceleration.set(0.1, 0.1);
// ship.velocity.set(10, 0);
function mouseMoved(e) {
const { clientX: x, clientY: y } = e; //Object destructuring
hole.update(x, y);
var coor = "Coordinates: (" + x + "," + y + ")";
document.getElementById("cords").innerHTML = coor;
}
function update() {
const holeAttract = hole.attract(canvasVector, ship);
console.log(holeAttract);
ship.update(holeAttract.x, holeAttract.y);
//ship.update();
}
function draw() {
// ctx.save();
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.setTransform(1, 0, 0, 1, 0, 0);
ctx.translate(-ship.pos.x + canvas.width / 2, -ship.pos.y + canvas.height / 2);
ship.draw(ctx);
//Draw test object
ctx.fillStyle = "#000000";
ctx.beginPath();
ctx.arc(100, 100, 20 * 2, 0, 2 * Math.PI);
ctx.fill();
ctx.closePath();
// ctx.restore();
}
function gameloop() {
update();
// ctx.save();
draw();
requestAnimationFrame(gameloop);
// ctx.restore();
}
window.addEventListener('resize', resizeCanvas, false);
canvas.addEventListener('mousemove', mouseMoved, false);
const imgPromises = [];
imgPromises.push(checkImage(ship.image));
// ship.draw(ctx);
Promise.all(imgPromises)
.then(() => {
console.log("Loaded all Images");
gameloop();
});