xxxxxxxxxx
282
//--------TO DO-------\\
//noise for minigun and multishot
//different enemies (higher health)
//sprites (items,player,enemies,attacks)
//background (noise generated, stars and planets or surface of planet)
//waves: progression of enemies and background
//item:self destruct
//score board
//--------------------\\
function borderCheck(pos){
if(pos.x >= 0 && pos.x <= width && pos.y >= 0 && pos.y <= height){
return true;
}else{
return false;
}
}
let ship;
let bullets = [];
let enemies = [];
let items = [];
let spawnEnemyRate = 0.01;
let enemyCap = 150;
let spawnItemRate = 0.4;
let score = 0;
let time = 0;
const speed = 1;
let singlePew;
let multiPew;
//load sound
function preload(){
singlePew = loadSound('Sounds/single_shot.mp3');
multiPew = loadSound('Sounds/multiple_shot.mp3');
}
function setup() {
createCanvas(windowWidth,windowHeight);
}
function playPew(s){
//if(!s.isPlaying()){
s.play();
//}
}
//player movement
function getMovements(){
let movement = createVector();
if(keyIsDown(65)){
//A
movement.x = -speed;
}
if(keyIsDown(68)){
//D
movement.x = speed;
}
if(keyIsDown(87)){
//W
movement.y = -speed;
}
if(keyIsDown(83)){
//S
movement.y = speed;
}
ship.move(movement);
}
//shooting and pewpewpewpwepwepwpepwepwpe
function mousePressed(){
if(ship.alive){
playPew(singlePew);
bullets.push(new Bullet(ship.pos.x,ship.pos.y,ship.heading));
//add more bullets for multishot
if(ship.multishot){
playPew(singlePew);
playPew(singlePew);
bullets.push(new Bullet(ship.pos.x,ship.pos.y,ship.heading+0.5));
bullets.push(new Bullet(ship.pos.x,ship.pos.y,ship.heading-0.5));
}
}
}
//spawn enemies. Ramps up with frames
function spawnEnemy(){
if(frameCount % 500 == 0){
spawnEnemyRate += 0.01;
//console.log('spawn rate increased: ',spawnRate);
}
if(enemies.length <= enemyCap){
if(random(1) < spawnEnemyRate){
let ran = random(1);
let x,y;
let furthestSpawn = 30;
if(ran <=1/4){
//top
x = random(width);
y = random(-furthestSpawn,0);
}else if(ran <=2/4){
//bottom
x = random(width);
y = random(height,height+furthestSpawn);
}else if(ran <=3/4){
//left
x = random(-furthestSpawn,0);
y = random(height);
}else if(ran <=4/4){
//right
x = random(width,width+furthestSpawn);
y = random(height);
}
enemies.push(new Enemy(x,y))
}
}
}
//spawn items
function spawnItem(){
if(frameCount % 500 == 0){
spawnItemRate += 0.05;
}
if(items.length <= 3){
if(frameCount % 250 == 0){
if(random(1) < spawnItemRate){
items.push(new Item());
}
}
}
}
let startGame = true;
function draw() {
if(startGame){
initGame();
playGame();
startGame = false;
}
// if(ship.alive){
// playGame();
// }else{
// noLoop();
// textSize(50);
// stroke(255,0,0);
// fill(255,0,0);
// text("Get Good",width/2-110,height/2);
// }
}
function initGame(){
let bullets = [];
let enemies = [];
let items = [];
ship = new player(width/2,height-100);
items.push(new Item());
}
function playGame(){
while(ship.alive){
background(0);
spawnEnemy();
spawnItem();
getMovements();
//minigun
if(ship.minigun){
bullets.push(new Bullet(ship.pos.x,ship.pos.y,ship.heading+random(-0.1,0.1)));
playPew(singlePew);
//multishot
if(ship.multishot){
bullets.push(new Bullet(ship.pos.x,ship.pos.y,ship.heading+0.5+random(-0.1,0.1)));
bullets.push(new Bullet(ship.pos.x,ship.pos.y,ship.heading-0.5+random(-0.1,0.1)));
}
//knockback
let oppDir = p5.Vector.fromAngle(ship.heading+PI);
oppDir.normalize();
if(ship.multishot){
oppDir.mult(0.75);
}else{
oppDir.mult(0.25);
}
ship.move(oppDir);
}
//----Items----\\
for(let i = items.length - 1; i >= 0; i--){
let item = items[i];
item.show();
//item goes away and does action
if(!item.alive){
item.action(ship);
items.splice(i,1);
}
}
//----Bullets----\\
for(let i = bullets.length - 1; i >= 0; i--){
let bullet = bullets[i];
bullet.update();
bullet.show();
//check for enemy-bullet collusion
for(let enemy of enemies){
if(bullet.collision(enemy)){
bullets.splice(i,1);
enemy.alive = false;
score++;
}
}
//remove bullet
if(!bullet.alive){
bullets.splice(i,1);
}
}
//----Ship----\\
if(ship.health <= 0){
ship.alive = false;
}
ship.update();
ship.show();
//colide with enemy
for(let enemy of enemies){
if(ship.collision(enemy)){
enemy.alive = false;
if(ship.shield){
ship.shield = false;
}else{
ship.health --;
}
}
}
//collide with item
for(let item of items){
if(ship.collision(item)){
item.alive = false;
}
}
//----Enemy----\\
for(let i = enemies.length - 1; i >=0; i--){
let enemy = enemies[i];
if(enemy.alive){
enemy.arrive(ship.pos);
enemy.update();
enemy.display();
}else{
enemies.splice(i,1);
}
}
//----Text----\\\
textSize(15);
stroke(255);
fill(0);
text("Score: "+score,10, 20);
text("Health: "+ship.health,10,height-10);
let fps = frameRate().toFixed(2);
text("FPS: "+fps,width-90,20)
}
}