Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function d(x1, y1, x2, y2){
- return Math.sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));
- }
- function areCircleColliding(c1, c2){
- return d(c1.x, c1.y, c2.x, c2.y) < c1.r + c2.r;
- }
- class Player{
- constructor(x, y, color){
- this.x = x;
- this.y = y;
- this.r = 20;
- this.speed = 5;
- this.color = color;
- this.reload_time = 20;
- this.reload = 0;
- this.hp = 20;
- }
- shoot(tX, tY){
- if (this.reload <= 0){
- b.push(new Bullet(this.x, this.y, tX, tY, this.color));
- this.reload = this.reload_time;
- }
- }
- update(){
- this.reload--;
- for (let i=0; i<b.length; ++i){
- if (b[i].color != this.color && areCircleColliding(b[i], this)){
- this.hp--;
- b[i] = b[b.length-1];
- b.pop();
- --i;
- }
- }
- if (this.x < 0) this.x = 0;
- if (this.x > 800) this.x = 800;
- if (this.y < 0) this.y = 0;
- if (this.y > 600) this.y = 600;
- }
- moveKeyboard(){
- if (isKeyPressed[87]) this.y -= this.speed;
- if (isKeyPressed[83]) this.y += this.speed;
- if (isKeyPressed[65]) this.x -= this.speed;
- if (isKeyPressed[68]) this.x += this.speed;
- }
- moveEnemy(){
- let xrun=0, yrun=0, mint=-1;
- for (let i=0; i<b.length; ++i){
- if (b[i].color != this.color){
- let alpha = Math.atan2(b[i].dy, b[i].dx) - Math.atan2(this.y-b[i].y, this.x - b[i].x);
- //console.log("alpha:", alpha);
- let dist = d(this.x, this.y, b[i].x, b[i].y) * Math.cos(alpha);
- //console.log("dist:", dist);
- let t = Math.max(dist/b[i].speed, 0);
- //console.log("t:", t);
- let bx = b[i].x + b[i].dx*t;
- let by = b[i].y + b[i].dy*t;
- if (d(bx, by, this.x, this.y) < this.r + b[i].r + this.r){
- if (mint==-1 || t < mint){
- mint = t;
- xrun = bx;
- yrun = by;
- }
- }
- }
- }
- if (mint != -1){
- let dist = d(xrun, yrun, this.x, this.y);
- let dx = (this.x-xrun)/dist*this.speed;
- let dy = (this.y-yrun)/dist*this.speed;
- if (dist == 0){
- dx = 0
- dy = this.speed;
- }
- this.x += dx;
- this.y += dy;
- }
- let ind = -1, mind = -1;
- for (let i=0; i<p.length; ++i){
- if (p[i].color != this.color && (mind == -1 || mind > d(this.x, this.y, p[i].x, p[i].y))){
- ind = i;
- mind = d(this.x, this.y, p[i].x, p[i].y);
- }
- }
- if (ind != -1){
- this.shoot(p[ind].x, p[ind].y);
- }
- }
- draw(){
- if (this.hp <= 0) return;
- context.fillStyle = this.color;
- context.beginPath();
- context.arc(this.x, this.y, this.r, 0, 2*Math.PI);
- context.fill();
- }
- };
- class Bullet{
- constructor(x, y, tX, tY, color){
- this.x = x;
- this.y = y;
- this.r = 5;
- this.color = color;
- this.speed = 12;
- let dist = d(x, y, tX, tY);
- this.dx = (tX-x)/dist*this.speed;
- this.dy = (tY-y)/dist*this.speed;
- }
- update(){
- this.x += this.dx;
- this.y += this.dy;
- }
- draw(){
- context.fillStyle = this.color;
- context.beginPath();
- context.arc(this.x, this.y, this.r, 0, 2*Math.PI);
- context.fill();
- }
- };
- var p = [];
- for (let i=0; i<13; ++i){
- if (i%2 == 0){
- p[i] = new Player(Math.random()*800, Math.random()*600, "blue");
- }else{
- p[i] = new Player(Math.random()*800, Math.random()*600, "red");
- }
- }
- var b = [];
- function update() {
- for (let i=0; i<p.length; ++i){
- if (p[i].hp <= 0){
- p[i] = p[p.length-1];
- p.pop();
- i--;
- }else{
- p[i].update();
- p[i].moveEnemy();
- }
- }
- for (let i=0; i<b.length; ++i) b[i].update();
- }
- function draw() {
- for (let i=0; i<p.length; ++i){
- p[i].draw();
- }
- for (let i=0; i<b.length; ++i) b[i].draw();
- };
- function keyup(key) {
- };
- function mouseup() {
- p[0].shoot(mouseX, mouseY);
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement