Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Creating variables
- function dist(x1, y1, x2, y2){
- return Math.sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
- }
- function orient(x1, y1, x2, y2, x3, y3){
- return x2*y3 + x1*y2 + y1*x3 - y1*x2 - y2*x3 - y3*x1;
- }
- function areIntersecting(x1, y1, x2, y2, x3, y3, x4, y4){
- let o1 = orient(x1, y1, x3, y3, x4, y4);
- let o2 = orient(x2, y2, x3, y3, x4, y4);
- let o3 = orient(x3, y3, x1, y1, x2, y2);
- let o4 = orient(x4, y4, x1, y1, x2, y2);
- if (o1==0 || o2==0 || o3==0 || o4==0){return 0;}
- return ((o1>0)^(o2>0)) && ((o3>0)^(o4>0));
- }
- class Player{
- constructor(x, y, sx, sy, dx, dy, color, ind){
- this.x = [x, x];
- this.y = [y, y];
- this.sx = sx;
- this.sy = sy;
- this.dx = dx;
- this.dy = dy;
- this.color = color;
- this.ind = ind;
- }
- turn(targetX, targetY){
- let speed = 2.5;
- this.x.push(this.x[this.x.length-1]);
- this.y.push(this.y[this.y.length-1]);
- let lx = this.x[this.x.length-1];
- let ly = this.y[this.y.length-1];
- this.dx = (targetX-lx)/dist(lx, ly, targetX, targetY)*speed;
- this.dy = (targetY-ly)/dist(lx, ly, targetX, targetY)*speed;
- }
- update(){
- this.x[this.x.length-1] += this.dx;
- this.y[this.y.length-1] += this.dy;
- let lx = this.x[this.x.length-1], ly = this.y[this.y.length-1];
- let blx = this.x[this.x.length-2], bly = this.y[this.y.length-2];
- if (lx<0 || lx>800 || ly<0 || ly>600){
- gameover();
- }
- for (let i=0; i<players.length; ++i){
- for (let j=0; j<players[i].x.length-1; ++j){
- if (areIntersecting(blx, bly, lx, ly, players[i].x[j], players[i].y[j], players[i].x[j+1], players[i].y[j+1])){
- if (i==this.ind && j>=players[i].x.length-3){}
- else{
- gameover();
- }
- }
- }
- }
- }
- draw(){
- context.strokeStyle = this.color;
- context.beginPath();
- context.moveTo(this.x[0], this.y[0]);
- for (let i=1; i<this.x.length; ++i){
- context.lineTo(this.x[i], this.y[i]);
- }
- context.stroke();
- context.fillStyle = this.color;
- context.fillRect(this.x[this.x.length-1], this.y[this.y.length-1], this.sx, this.sy);
- }
- };
- function gameover(){
- gmover = true;
- console.log("asdf");
- }
- var gmover = false;
- var players = [new Player(100, 100, 15, 15, 2.5, 0, 'blue', 0), new Player(700, 500, 15, 15, -2.5, 0, 'red', 1)];
- function update() {
- if (gmover){return;}
- for (let i=0; i<players.length; ++i){
- players[i].update();
- }
- }
- function draw() {
- for (let i=0; i<players.length; ++i){
- players[i].draw();
- }
- // This is how you draw a rectangle
- }
- function keyup(key) {
- if (key==65){players[0].turn(mouseX, mouseY);}
- if (key==68){players[1].turn(mouseX, mouseY);}
- // Show the pressed keycode in the console
- console.log("Pressed", key);
- }
- function mouseup() {
- // Show coordinates of mouse on click
- console.log("Mouse clicked at", mouseX, mouseY);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement