xxxxxxxxxx
let gameManager;
function setup() {
createCanvas(400,670);
background(20);
gameManager = new GameManager();
}
function draw() {
colorMode(RGB);
background(20);
gameManager.update();
gameManager.show();
}
function keyPressed() {
if (keyCode === LEFT_ARROW) {
if(gameManager.player.pos.x > 50) {
gameManager.player.vel = createVector(-6,0);
} else {
gameManager.player.vel = createVector();
}
} else if (keyCode === RIGHT_ARROW) {
if(gameManager.player.pos.x < (width - 50)) {
gameManager.player.vel = createVector(6,0);
} else {
gameManager.player.vel = createVector();
}
}
}
function keyReleased() {
gameManager.player.vel = createVector(0,0);
}
var GameManager = function() {
var _self;
function GameManager() {
this.player = new Player();
this.background = new Background();
this.bullets = [];
this.enemies = [];
this.particleSystems = [];
this.score = 0;
// this.enemies.push(new Enemy1(createVector(width / 2,100),150,200,createVector()));
// this.enemies.push(new Enemy2(createVector(width / 4,100),150,200,createVector()));
// this.enemies.push(new Enemy3(createVector(width / 1.5,100),150,200,createVector()));
_self = this;
}
GameManager.prototype.show = function() {
this.background.show();
this.player.show();
renderBullets();
renderEnemies();
renderParticleSystems();
this.fire();
showGUI();
}
GameManager.prototype.update = function() {
this.background.update();
this.player.update();
if(frameCount % 80 === 0) {
generateEnemies();
}
}
GameManager.prototype.fire = function() {
if(this.player.fireCooldown <= 0.2) {
let x = this.player.pos.copy(),y = this.player.pos.copy(),z = this.player.pos.copy();
// x.add(createVector(-10,5));
// z.add(createVector(10,5));
// this.bullets.push(new Bullet(x));
this.bullets.push(new Bullet(y));
// this.bullets.push(new Bullet(z));
this.player.fireCooldown = 3;
}
}
function generateEnemies() {
let enemyCount = Math.round(random(1,6));
let enemyLocX = [10,80,150,220,290,360];
for(let i = 0;i < enemyCount;i++) {
let enemyType = Math.ceil(random(3));
let enemy;
let x = random(enemyLocX);
enemyLocX.splice(enemyLocX.indexOf(x),1);
switch(enemyType) {
case 1:
enemy = new Enemy1(createVector(x,-100),150,200,createVector(0,2));
break;
case 2:
enemy = new Enemy2(createVector(x,-100),150,200,createVector(0,2));
break;
case 3:
enemy = new Enemy3(createVector(x,-100),150,200,createVector(0,));
break;
}
_self.enemies.push(enemy);
}
}
function renderParticleSystems() {
for(let i = _self.particleSystems.length - 1;i >= 0;i--) {
if(_self.particleSystems[i].isDead()) {
_self.particleSystems.splice(i,1);
} else {
_self.particleSystems[i].render();
}
}
}
function showGUI() {
push();
stroke(255,255,255);
fill(255);
textSize(20);
text('Score: ' + _self.score,10,40);
pop();
}
function renderBullets() {
for(var i = _self.bullets.length - 1;i >= 0;i--) {
if(_self.bullets[i].isDead()) {
_self.bullets.splice(i,1);
} else {
_self.bullets[i].update();
_self.bullets[i].show();
for(var j = 0;j < _self.enemies.length;j++) {
if(_self.enemies[j].isCollided(_self.bullets[i].pos)) {
_self.particleSystems.push(new ParticleSystem(_self.bullets[i].pos,4,'circle',15,15,color(220,150,205)));
_self.enemies[j].life -= 50;
_self.bullets.splice(i,1);
break;
}
}
}
}
}
function renderEnemies() {
for(var i = 0;i < _self.enemies.length;i++) {
if(_self.enemies[i].isDead()) {
_self.particleSystems.push(new ParticleSystem(_self.enemies[i].pos,4,'square',15,25,color(150,205,255)));
_self.enemies.splice(i,1);
_self.score += 10;
} else {
_self.enemies[i].update();
_self.enemies[i].show();
}
}
}
return GameManager;
}();
var Player = function() {
function Player() {
this.pos = createVector(width / 2,height - 50);
this.vel = createVector();
this.acc = createVector();
this.r = 10;
this.fireCooldown = 3;
this.life = 100;
this.originalLife = 100;
this.playerAnimation = new PlayerAnimations();
}
Player.prototype.addForce = function(force) {
this.acc.add(force);
}
Player.prototype.update = function() {
var tempX = this.pos.copy().add(this.vel).x;
if(!(tempX <= 20 || tempX >= width - 20 ))
this.pos.add(this.vel);
this.vel.add(this.acc);
this.acc.mult(0);
this.fireCooldown-= 0.3;
this.fireCooldown = constrain(this.fireCooldown,0.1,3);
this.playerAnimation.update();
}
Player.prototype.show = function() {
push();
colorMode(HSB);
noFill();
strokeWeight(5);
translate(this.pos.x,this.pos.y);
var life = map(this.life,0,this.originalLife,0,PI * 2);
stroke(155,200,205,255);
arc(0,0,50,50,0,life);
var cooldown = map(this.fireCooldown,0,3,0,PI * 2);
strokeWeight(2);
stroke(20,255,255,255);
arc(0,0,70,70,0,cooldown);
push();
translate(-40,-40);
this.playerAnimation.show();
pop();
rotate(PI / 6);
stroke(80,200,205,255);
fill(80,200,205,255);
polygon(0, 0, this.r, 3);
pop();
}
return Player;
}();
var PlayerAnimations = function() {
function PlayerAnimations() {
this.points = [];
var angle = TWO_PI / 4;
for (var a = 0; a < TWO_PI; a += angle) {
var sx = 0 + cos(a) * 70;
var sy = 0 + sin(a) * 70;
this.points.push(createVector(sx,sy));
}
}
PlayerAnimations.prototype.update = function() {
for(var i = 0;i < this.points.length;i++) {
this.points[i].x = constrain(this.points[i].x + (random(-3,3) * 0.8),0,80);
this.points[i].y = constrain(this.points[i].y + (random(-3,3) * 0.8),0,80);
}
}
PlayerAnimations.prototype.show = function() {
beginShape();
for(var i = 0;i < this.points.length;i++) {
strokeWeight(10);
stroke(180,255,255,255);
point(this.points[i].x,this.points[i].y);
strokeWeight(2);
stroke(200,255,255,255);
vertex(this.points[i].x,this.points[i].y);
}
endShape(CLOSE);
}
return PlayerAnimations;
}();
var Bullet = function() {
function Bullet(_pos) {
this.pos = _pos;
this.history = [];
this.vel = createVector(0,-6.5);
this.acc = createVector(0,-0.05);
}
Bullet.prototype.update = function() {
this.pos.add(this.vel);
this.vel.add(this.acc);
if(frameCount % 3 == 0) {
this.history.push(this.pos.copy());
if(this.history.length > 6) {
this.history.splice(0,1);
}
}
}
Bullet.prototype.show = function() {
for(var i = 0;i < this.history.length;i++) {
push();
strokeWeight(0.2);
translate(this.history[i].x,this.history[i].y);
rotate(PI / 6);
stroke(80,200,205,255);
noFill();
polygon(0, 0, 7, 3);
pop();
}
push();
translate(this.pos.x,this.pos.y);
rotate(PI / 6);
stroke(80,200,205,255);
fill(80,200,205,255);
polygon(0, 0, 10, 3);
pop();
}
Bullet.prototype.isDead = function() {
return this.pos.y <= -50;
}
return Bullet;
}();
var Background = function() {
function Background() {
this.verticalLines = [];
this.horizontalLines = [];
for(var i = 0;i < 5;i++) {
this.verticalLines.push(createVector(random(0,width - 0),0));
this.horizontalLines.push(createVector(0,random(0,height - 0)));
}
};
Background.prototype.update = function() {
for(var i = 0;i < 5;i++) {
this.verticalLines[i].x += 0.01;
this.horizontalLines[i].y += 0.01;
}
};
Background.prototype.show = function() {
for(var i = 0;i < 5;i++) {
var n = noise(this.verticalLines[i].x) * (width),
n1 = noise(this.horizontalLines[i].y) * (height);
stroke(250,255,255);
strokeWeight(0.2);
line(n,0,n,height);
line(0,n1,width,n1);
}
};
return Background;
}();
var Enemy1 = function() {
function Enemy1(_pos,_life,_damage,_vel) {
this.pos = _pos;
this.life = _life;
this.damage = _damage;
this.vel = _vel;
this.r = 35;
}
Enemy1.prototype.show = function() {
push();
translate(this.pos.x,this.pos.y);
colorMode(HSB);
noFill();
strokeWeight(2);
stroke(70,255,255,255);
rectMode(CENTER);
rotate(frameCount * 0.05);
push();
// rotate(PI / 3);
rect(-15,15,20,20);
pop();
push();
// rotate(PI / 3.4);
rect(15,15,20,20);
pop();
push();
// rotate(PI / 4);
rect(-15,-15,20,20);
pop();
push();
// rotate(PI / 2);
rect(15,-15,20,20);
pop();
fill(20,255,255,255);
ellipse(0,0,25,25);
noFill();
strokeWeight(2);
stroke(40,255,255,255);
ellipse(0,0,40,40);
pop();
}
Enemy1.prototype.update = function() {
this.pos.add(this.vel);
}
Enemy1.prototype.isDead = function() {
return this.life <= 0;
}
Enemy1.prototype.isCollided = function(pos) {
return (this.r + 20 > this.pos.dist(pos));
}
return Enemy1;
}();
var Enemy3 = function() {
function Enemy3(_pos,_life,_damage,_vel) {
this.pos = _pos;
this.life = _life;
this.damage = _damage;
this.vel = _vel;
this.r = 35;
}
Enemy3.prototype.show = function() {
push();
translate(this.pos.x,this.pos.y);
colorMode(HSB);
noFill();
strokeWeight(2);
stroke(150,255,255,255);
rectMode(CENTER);
rotate(frameCount * 0.05);
push();
// rotate(PI / 3);
rect(0,0,20,50);
pop();
push();
// rotate(PI / 3.4);
rect(0,0,20,50);
pop();
push();
// rotate(PI / 4);
rect(0,0,50,20);
pop();
push();
// rotate(PI / 2);
rect(0,0,50,20);
pop();
fill(200,255,255,255);
ellipse(0,0,25,25);
noFill();
strokeWeight(2);
stroke(40,255,255,255);
pop();
}
Enemy3.prototype.update = function() {
this.pos.add(this.vel);
}
Enemy3.prototype.isDead = function() {
return this.life <= 0;
}
Enemy3.prototype.isCollided = function(pos) {
return (this.r + 20 > this.pos.dist(pos));
}
return Enemy3;
}();
var Enemy2 = function() {
function Enemy2(_pos,_life,_damage,_vel) {
this.pos = _pos;
this.life = _life;
this.damage = _damage;
this.vel = _vel;
this.r = 35;
}
Enemy2.prototype.show = function() {
push();
translate(this.pos.x,this.pos.y);
colorMode(HSB);
noFill();
strokeWeight(2);
stroke(50,255,255,255);
line(0,-10,0,25);
rotate(-PI / 6);
stroke(120,255,255,255);
polygon(0,0,25,3);
pop();
}
Enemy2.prototype.update = function() {
this.pos.add(this.vel);
}
Enemy2.prototype.isDead = function() {
return this.life <= 0;
}
Enemy2.prototype.isCollided = function(pos) {
return (this.r + 20 > this.pos.dist(pos));
}
return Enemy2;
}();
var ParticleSystem = function() {
function ParticleSystem(_pos,_speed,_shape,_count, _radius,_color,_iterations) {
this.pos = _pos;
this.speed = _speed;
this.shape = _shape;
this.count = _count;
this.radius = _radius;
this.color = _color;
this.particles = [];
this.init();
}
ParticleSystem.prototype.init = function() {
for(let i = 0;i < this.count;i++) {
let vel = createVector(random(-1,1),random(-1,1));
vel.setMag(this.speed);
this.particles.push(new Particle(this.pos.copy(),this.shape,vel.copy(),this.radius,this.color));
}
}
ParticleSystem.prototype.render = function() {
for(let i = this.particles.length - 1;i >= 0;i--) {
if(this.particles[i].isDead()) {
this.particles.splice(i,1);
} else {
this.particles[i].update();
this.particles[i].show();
}
}
}
ParticleSystem.prototype.isDead = function() {
return this.particles.length === 0;
}
return ParticleSystem;
}();
var Particle = function() {
function Particle(_pos, _shape, _vel, _radius,_color) {
this.pos = _pos;
this.shape = _shape;
this.vel = _vel;
this.radius = _radius;
this.color = _color;
this.originalRadius = _radius;
}
Particle.prototype.show = function() {
push();
stroke(this.color,map(this.radius,0,this.originalRadius,0,255));
strokeWeight(1);
noFill();
translate(this.pos.x, this.pos.y);
switch(this.shape) {
case 'square':
rect(0,0,this.radius,this.radius);
break;
case 'triangle':
polygon(0,0,this.radius,3);
break;
case 'circle':
ellipse(0,0,this.radius,this.radius);
break;
}
pop();
}
Particle.prototype.update = function() {
this.pos.add(this.vel);
this.radius-=0.5;
}
Particle.prototype.isDead = function() {
return this.radius <= 0;
}
return Particle;
}();
function polygon(x, y, radius, npoints) {
var angle = TWO_PI / npoints;
beginShape();
for (var a = 0; a < TWO_PI; a += angle) {
var sx = x + cos(a) * radius;
var sy = y + sin(a) * radius;
vertex(sx, sy);
}
endShape(CLOSE);
}