xxxxxxxxxx
487
//timer mating ritual (millis)
//change level after timer
//seagrass - anemone
//home
//frequency / lives
//how can I vary the length of the seagrass?
let frequencies = [];
let seacreatureone;
let phase = 0;
// let seacreatureoneLives = 5;
let sonarstart = true;
let shippingstart = true;
let seismicstart = true;
let seacreaturemate;
const Y_AXIS = 1;
const X_AXIS = 2;
let b1, b2, c1, c2;
let click;
let scimage;
let scmimage;
let mating = false;
let theta = 0;
let sonartitle;
let seismictitle;
let shippingtitle;
let bleachbaby;
let mode;
let storedTotalLives = 5;
function preload() {
click = loadSound("beaked-whale.mp3");
scimage = loadImage("seacreature.png");
scmimage = loadImage("seacreaturemate.png");
bleachbaby = loadImage("bleach baby.png");
sonartitle = loadImage("sonar.png");
seismictitle = loadImage("seismic.png");
shippingtitle = loadImage("shipping.png");
}
function setup() {
createCanvas(600, 400);
ellipseMode(CENTER);
// Define colors
b1 = color(40);
b2 = color(240);
click.loop();
click.rate(1);
click.play();
click.setVolume(0.4);
}
function draw() {
let n;
setGradient(0, 0, width, height, b2, b1, Y_AXIS);
switch (phase) {
case 0:
sonar();
break;
case 1:
seismic();
break;
case 2:
shipping();
break;
case 3:
push();
noStroke();
fill(255);
text('you won!!!', width/2, height/2)
pop();
break;
}
strokeWeight(10);
stroke(color (30,60));
for (let x = 0; x < width; x += 10) {
let offsetX = 0;
let offsetY = 0;
// lean (mid frequency, static)
n = noise(x * .2);
offsetX += n * 25 - 70;
// height (very high frequency becomes fully random due to aliasing)
offsetY += noise(x * 20) * 70;
// wind( low frequency, timed)
n = noise(x * .01 + millis() * -.001);
offsetX += n * 40;
line(x, width, x + offsetX + 40,340 + offsetY);
}
}
function setGradient(x, y, w, h, c1, c2, axis) {
noFill();
if (axis === Y_AXIS) {
// Top to bottom gradient
for (let i = y; i <= y + h; i++) {
let inter = map(i, y, y + h, 0, 1);
let c = lerpColor(c1, c2, inter);
stroke(c);
line(x, i, x + w, i);
}
} else if (axis === X_AXIS) {
// Left to right gradient
for (let i = x; i <= x + w; i++) {
let inter = map(i, x, x + w, 0, 1);
//moving line
let c = lerpColor(c1, c2, inter);
stroke(c);
line(i, y, i, y + h);
}
}
}
//low, mid, and high frequency
function sonar() {
if (sonarstart) {
seacreatureone = new Seacreature(storedTotalLives);
seacreaturemate = new Seacreaturemate(phase);
for (let i = 0; i <= 10; i++) {
let frequencyType = int(random(1,4));
frequencies[i] = new Frequency(random(width), random(height - 30), frequencyType);
}
sonarstart = false;
}
let d = dist(seacreatureone.x, seacreatureone.y, seacreaturemate.x, seacreaturemate.y);
let r = map(d, 0, width, 5, 1);
click.rate(r);
for (let i = 0; i < frequencies.length; i++) {
frequencies[i].shape();
frequencies[i].move(phase);
frequencies[i].checkplayercollision(seacreatureone);
}
//seacreature
seacreatureone.shape();
seacreatureone.move();
seacreatureone.home();
seacreatureone.ritual(seacreatureone.y);
seacreatureone.displayLife();
//second seacreature
seacreaturemate.shape();
seacreaturemate.move();
seacreaturemate.checkplayercollision(seacreatureone);
seacreaturemate.ritual(seacreaturemate.y);
}
//low frequency with intense intervals
function seismic() {
if (seismicstart) {
seacreatureone = new Seacreature(storedTotalLives);
seacreaturemate = new Seacreaturemate(phase);
for (let i = 0; i <= 10; i++) {
let frequencyType = 1; //int(random(1));
// random --> 0.1 0.6 0.2 0.5 0.5673645z8276345 1
// int --> 0 0 0 0 0 1
frequencies[i] = new Frequency(random(width), random(height - 30), frequencyType);
}
seismicstart = false;
}
for (let i = 0; i < frequencies.length; i++) {
frequencies[i].shape();
frequencies[i].move(phase);
frequencies[i].checkplayercollision(seacreatureone);
}
//seacreature
seacreatureone.shape();
seacreatureone.move();
seacreatureone.home();
seacreatureone.displayLife();
seacreatureone.ritual(seacreatureone.y);
//seacreaturemate
seacreaturemate.shape();
seacreaturemate.move();
seacreaturemate.checkplayercollision(seacreatureone);
seacreaturemate.ritual(seacreaturemate.y);
}
// low frequency (many)
function shipping() {
if (shippingstart) {
seacreatureone = new Seacreature(storedTotalLives);
seacreaturemate = new Seacreaturemate(phase);
for (let i = 0; i <= 10; i++) {
let frequencyType = int(random(1));
frequencies[i] = new Frequency(random(width), random(height - 30), frequencyType);
}
shippingstart = false;
}
for (let i = 0; i < frequencies.length; i++) {
frequencies[i].shape();
frequencies[i].move(phase);
frequencies[i].checkplayercollision(seacreatureone);
}
//seacreature
seacreatureone.shape();
seacreatureone.move();
seacreatureone.home();
seacreatureone.displayLife();
seacreatureone.ritual(seacreatureone.y);
//seacreaturemate
seacreaturemate.shape();
seacreaturemate.move();
seacreaturemate.checkplayercollision(seacreatureone);
seacreaturemate.ritual(seacreaturemate.y);
}
class Frequency {
constructor(x, y, type = 1) {
this.x = x;
this.y = y;
this.type = type;
this.speedlow = 0;
this.speedmed = 0;
this.speedhigh = 0;
}
shape() {
strokeWeight(0.2);
switch (this.type) {
case 1:
//low-frequency
fill(240);
break;
case 2:
//mid-frequency
fill(130);
break;
//high-frequency
case 3:
fill(color(255, 0, 0, 90));
break;
}
ellipse(this.x, this.y, 15);
}
checkplayercollision(player) {
let d = dist(player.x, player.y, this.x, this.y);
if (d < 30) {
// console.log('bumped!');
player.reduceLife()
// console.log('bumped!');
// } else {
// console.log('safe');
}
}
move(scene) {
switch (scene) {
case 0:
this.speedlow = 0.40;
this.speedmed = 0.40;
this.speedhigh = 0.40;
break;
case 1:
this.speedlow = 1;
break;
case 2:
this.speedlow = 1.3;
break;
}
switch (this.type) {
case 2:
this.y += this.speedmed;
break;
case 3:
this.y += this.speedhigh;
break;
default:
case 1:
this.y += this.speedlow;
break;
}
if (this.y > height - 10) {
this.y = 0;
this.x = random(width);
}
}
checkCollision() {
if (this.x + this.width / 2 > this.x && this.x < this.x + this.w / 2) {
// console.log("bumped");
// } else {
// console.log("safe");
}
}
// currentSeacreatureLives();
}
function currentSeacreatureLives() {
for (let i = 0; i < seacreatureone; i++) {
ellipse(i * 10, 60, 20);
}
}
class Seacreature {
// new Seacreature( 5 )
constructor(lives = 5) {
this.x = 68;
this.y = height / 2;
this.c = color(0);
this.life = lives;
this.lifeFrameCount = 0; // variable to store when we got hit
this.isMating = false;
}
shape() {
imageMode(CENTER);
image(scimage, this.x, this.y, 100, 100);
}
move() {
//up
if (!mating) {
if (keyIsDown(38)) {
this.y -= 3;
}
//down
if (keyIsDown(40)) {
this.y += 3;
}
//left
if (keyIsDown(37)) {
this.x -= 3;
}
//right
if (keyIsDown(39)) {
this.x += 3;
}
}
}
home() {
if (this.x < 0) {
phase++;
this.x = height / 2;
}
if (phase > 2) {
phase = 0;
}
}
checkCollision() {
if (seacreatureone.x + seacreatureone.width / 2 > this.x && seacreatureone.x < this.x + seacreatureone.w / 2) {
}
}
ritual(currenty) {
if (mating) {
this.y = currenty + sin(theta) * 10;
theta += 0.5;
if( this.isMating === false ){ // only do at first frame of mating
// setTimeout( function , time in milliseconds );
setTimeout( function(){
// storedTotalLives = this.life + 3;
storedTotalLives = storedTotalLives + 3;
// console.log(storedTotalLives)
// console.log('next level');
phase++;
mating = false;
}, 3000 );
}
this.isMating = true; // first frame of mating
}
}
reduceLife(){
// if more than 1 sec passed since last hit
if( frameCount > this.lifeFrameCount + 1 * 60 && mating === false ){
this.life = this.life - 1;
storedTotalLives--;
this.lifeFrameCount = frameCount;
console.log('lost 1 life; '+this.life+' left');
// lost --> back to home
if( this.life == 0 ){
console.log('you lost');
this.life = 5;
storedTotalLives = 5;
this.x = 68;
this.y = height/2;
}
}
}
displayLife(){
push();
for( let i = 0; i < this.life; i++ ){
fill(255)
circle(20+40*i,height-60,30);
}
pop();
}
}
class Seacreaturemate {
constructor(scene) {
this.scene = scene;
this.x = width - 80;
this.y = height / 2;
this.distance = 6;
this.initialy = this.y;
this.speed = .5;
}
shape() {
imageMode(CENTER);
image(scmimage, this.x, this.y, 115, 115);
}
move() {
if (this.scene == 0) {
this.distance = 100;
} else if (this.scene == 1) {
this.distance = 140;
} else {
this.distance = 200;
}
let d = dist(this.x, this.y, seacreatureone.x, seacreatureone.y);
if (d > 200) {
//seacreaturemate stop
if (this.y + 20 > this.distance + this.initialy || this.y - 20 < this.initialy - this.distance) {
this.speed *= -1;
}
this.y += this.speed;
}
}
checkplayercollision(p) {
if (dist(this.x, this.y, p.x, p.y) < 75) {
//phase ++; //change level
mating = true;
}
}
//mating ritual code
ritual(currenty) {
if (mating) {
this.y = currenty + sin(theta) * 10;
theta += 0.5;
}
}
}