xxxxxxxxxx
let r;
let shadows;
let stars;
let STARNUM = 300;
// code = [duration, power, rotation]
//let codes = [[1.2, 0.08, 0], [0.5, 0.03, 0], [0.1, 1, 90], [0.4, 0.5, -90], [7, 0.02, 45]];
let codes = [];
function setup() {
angleMode(DEGREES);
createCanvas(windowWidth, windowHeight);
background(0);
codes = [];
shadows = [];
stars = [];
createStars();
r = new rocket(windowWidth / 2, windowHeight - 10, 10, 20);
//r.engageThrust(1.2, 0.08, 2 );
}
function draw() {
background(0);
// ----------- Stars -----------
for(var st of stars) {
st.update();
st.draw();
}
// -----------------------------
if (codes.length == 0) {
codes.push([random(0.1, 0.5), random(0.05, 0.1), random(-90, 90)]);
}
// Check code update
if (r.firing === false && codes.length > 0) {
c = codes.shift();
r.engageThrust(c[0], c[1], c[2])
}
// ----------- Window Scrolling -------------------
// Y axis
if (r.pos.y < windowHeight / 2) {
var diff = abs((windowHeight / 2) - r.pos.y);
r.pos.y += diff;
for (var sh of shadows) {
sh.y += diff;
}
for (var starY of stars){
starY.y += diff * (starY.size * 3 / 100);
}
}
// Moving Left
if (r.pos.x < windowWidth * 0.3) {
var diffX1 = abs((windowWidth * 0.3) - r.pos.x);
r.pos.x += diffX1;
for (var shX1 of shadows) {
shX1.x += diffX1;
}
for (var starX1 of stars){
starX1.x += diffX1 * (starX1.size * 3 / 100);
}
}
// Moving Right
if (r.pos.x > windowWidth * 0.7) {
var diffX2 = abs((windowWidth * 0.7) - r.pos.x);
r.pos.x -= diffX2;
for (var shX2 of shadows) {
shX2.x -= diffX2;
}
for (var starX2 of stars){
starX2.x -= diffX2 * (starX2.size * 3 / 100);
}
}
//-----------------------------------------------------
r.draw();
r.update();
if (shadows.length > 300) {
shadows.shift();
}
for (var s of shadows) {
s.update();
s.draw();
}
// edge check
if (r.pos.x < 0 || r.pos.x > windowWidth || r.pos.y < -10 || r.pos.y > windowHeight) {
setup();
}
}
function createStars() {
// Create a number of random stars
for (var i = 0; i < STARNUM; i++) {
stars.push(new star());
}
}
function star() {
this.x = random(-10, windowWidth + 10);
this.y = random(-10, windowHeight + 10);
this.chance = random(1, 100);
this.rgb = [random(255), random(255), random(255)];
if (this.chance > 5){
this.size = random(1, 5);
} else {
this.size = random(10, 30);
}
this.reSize = function reSize() {
this.chance = random(1, 100);
this.rgb = [random(255), random(255), random(255)];
if (this.chance > 5){
this.size = random(1, 5);
} else {
this.size = random(10, 30);
}
}
this.update = function update() {
// Scroll off left
if (this.x < -10) {
this.x = windowWidth + 10;
//this.y = random(-10, windowHeight + 10);
this.reSize();
}
// Scroll off right
if (this.x > windowWidth + 10){
this.x = -10;
//this.y = random(-10, windowHeight + 10);
this.reSize();
}
// Scroll off bottom
if (this.y > windowHeight + 10){
//this.x = random(-10, windowWidth + 10);
this.y = random(-20, -10);
this.reSize();
}
}
this.draw = function draw() {
push();
strokeWeight(this.size);
if (this.size > 5) {
stroke(this.rgb[0], this.rgb[1], this.rgb[2]);
} else {
stroke('white');
}
point(this.x, this.y);
pop();
}
}