-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.js
More file actions
145 lines (119 loc) · 4.61 KB
/
Main.js
File metadata and controls
145 lines (119 loc) · 4.61 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
var collisionButton = document.getElementById("absorb");
var loopSpaceButton = document.getElementById("loopedSpace");
var canvas = document.getElementById("mainCanvas");
var graphics = canvas.getContext("2d");
canvas.width = document.body.clientWidth * 0.8;
canvas.height = document.body.clientHeight * 0.8;
var canvasWidth = canvas.width;
var canvasHeight = canvas.height;
var generateWidth = canvasWidth;
var generateHeight = canvasHeight;
var generateStartX = (canvasWidth / 2) - (generateWidth / 2);
var generateStartY = (canvasHeight / 2) - (generateHeight / 2);
var planetList = [];
var absorption = false;
var loopSpaceBool = false;
// On buttonClick --> Add new planet
function addPlanet(event) {
xPos = event.clientX;
yPos = event.clientY;
planetList.push(new Planet(xPos, yPos, randNum(1, 15)));
}
function setAbsorption() {
if (absorption == false) {
absorption = true;
collisionButton.textContent = "Disable Collission"
return;
}
absorption = false;
collisionButton.textContent = "Enable Collision"
}
function loopSpace() {
if (loopSpaceBool == false) {
loopSpaceBool = true;
loopSpaceButton.textContent = "Disable Looping";
return;
}
loopSpaceBool = false;
loopSpaceButton.textContent = "Enable Looping";
}
function reset() {
planetList.splice(0, planetList.length);
}
// Func for generating random number --> For spawning planets
function randNum(min, max) {
return Math.random() * (max - min) + min;
}
function setup() {
if (absorption) {
var planetNum = 500;
} else {
var planetNum = 1900;
}
for (var i = 0; i < planetNum; i++) {
//planetList.push(new Planet(randNum(0, canvasWidth), randNum(0, canvasHeight), randNum(3, 17)));
planetList.push(new Planet(randNum(generateStartX, generateWidth), randNum(generateStartY, generateHeight), randNum(3, 17)));
}
}
function update() {
for (var i = 0; i < planetList.length; i++) {
planetList[i].move(planetList);
//Delete planets outside of canvas
if (loopSpaceBool == false){
if (planetList[i].getPosition()[0] > canvasWidth + 500) {
planetList.splice(i, 1);
}
if (planetList[i].getPosition()[0] < 0 - 500) {
planetList.splice(i, 1);
}
if (planetList[i].getPosition()[1] > canvasHeight + 500) {
planetList.splice(i, 1);
}
if (planetList[i].getPosition()[1] < 0 - 500) {
planetList.splice(i, 1);
}
} else if (loopSpaceBool == true) {
if (planetList[i].getPosition()[0] > canvasWidth) {
planetList[i].position[0] = 0;
planetList[i].setVelocity(0, 0);
}
if (planetList[i].getPosition()[0] < 0) {
planetList[i].position[0] = canvasWidth;
planetList[i].setVelocity(0, 0);
}
if (planetList[i].getPosition()[1] > canvasHeight) {
planetList[i].position[1] = 0;
planetList[i].setVelocity(0, 0);
}
if (planetList[i].getPosition()[1] < 0) {
planetList[i].position[1] = canvasHeight;
planetList[i].setVelocity(0, 0);
}
}
// Absorbing lesser planets
if (absorption == true) {
if (planetList.length > 1) {
var planets = planetList[i].getCollidingPlanets(planetList);
for (var j = 0; j < planets.length; j++) {
var disposal = planetList.indexOf(planets[j]);
planetList[i].addMass(planets[j].getMass());
// Changing velocity on impact based on current velocities and masses
var velX = planetList[i].getVelocity()[0] + (planets[j].getVelocity()[0] * planets[j].getMass() / planetList[i].getMass()**2);
var velY = planetList[i].getVelocity()[1] + (planets[j].getVelocity()[1] * planets[j].getMass() / planetList[i].getMass()**2);
planetList.splice(disposal, 1);
}
}
}
}
render();
}
function render() {
graphics.beginPath();
graphics.fillStyle = "#000000";
graphics.fillRect(0, 0, canvasWidth, canvasHeight);
graphics.closePath();
for (var i = 0; i < planetList.length; i++) {
planetList[i].draw(graphics);
}
}
setInterval(update, 40);