xxxxxxxxxx
460
//angleMode = 'degrees';
var projectiles = [];
function Track() {};
var track;
function Mover() {};
var actuator;
function Arm() {};
var mover;
var k = 10;
function setup() {
angleMode(DEGREES);
createCanvas(800, 600);
function Track(r, a, c) {
this.radius = r;
this.r = 360 / a + 1;
this.angle = a;
this.center = c;
this.track = [];
this.track2 = [];
this.init = function() {
for (var i = 0; i < this.r; i++) {
var x = this.center + (this.radius - 0) * cos(this.angle * i);
var y = this.center + (this.radius - 120) * sin(this.angle * i);
var x2 = this.center + (this.radius - 50) * cos(this.angle * i);
var y2 = this.center + (this.radius - 150) * sin(this.angle * i);
this.track.push([x, y]);
this.track2.push([x2, y2]);
}
};
this.draw = function() {
beginShape();
for (var i = 0; i < (this.track.length - 1); i++) {
var a = this.track[i];
var b = this.track[i + 1];
noFill();
vertex(a[0], a[1]);
vertex(b[0], b[1]);
}
endShape();
beginShape();
for (var i = 0; i < (this.track2.length - 1); i++) {
var a = this.track2[i];
var b = this.track2[i + 1];
noFill();
vertex(a[0], a[1]);
vertex(b[0], b[1]);
}
endShape();
};
};
function Mover() {
this.x = 200;
this.y = 200;
this.vx = 0;
this.vy = 0;
this.ax = 0;
this.ay = 0;
this.dir = 0;
this.acceleration = 0.1;
this.totalAc = 0;
this.topspeed = k;
this.xoff = 1000;
this.yoff = 0;
this.r = 16;
this.update = function() {
//var mouse = new PVector(mouseX, mouseY);
var mx = mouseX;
var my = mouseY;
var dy = (my - this.y) * 0.005;
var a = 1.00;
var dx = (mx - this.x) * 0.005;
var dir = atan2(dy, dx) * 1;
this.dir = (map(dir, -180, 180, 0, 360) - 1) * a;
if (my > this.y) {
dy = -dy;
}
if (mx > this.x) {
dx = -dx;
}
// dx = dx*10;
// dy = dy*10;
var d = dist(mx, my, this.x, this.y);
this.ax = 100 / (d * d) * 1;
this.ay = 100 / (d * d) * 1;
if (my > this.y) {
this.ax = -this.ax;
}
if (mx > this.x) {
this.ay = -this.ay;
}
this.vx += this.ax + dx * cos(this.dir);
this.vy += this.ay + dy * sin(this.dir);
if (this.vx >= k) {
this.vx = k;
}
if (this.vx <= -k) {
this.vx = -k;
}
if (this.vy >= k) {
this.vy = k;
}
if (this.vy <= -k) {
this.vy = -k;
}
this.x += this.vx;
this.y += this.vy;
};
this.display = function() {
var angle = this.dir;
stroke(0, 0, 0);
//strokeWeight(-1);
//noStroke();
fill(127, 127, 127);
push();
rectMode(CENTER);
translate(this.x, this.y);
rotate(angle + 90);
rect(15, 0, 10, 45);
rect(-15, 0, 10, 45);
fill(0, 72, 255);
rect(0, 0, 30, 40, 15);
fill(255, 0, 0);
rect(-10, 10, 10, 10, 50);
fill(186, 180, 180);
pop();
};
this.checkEdges = function() {
if (this.x > width) {
this.x = 0;
} else if (this.x < 0) {
this.x = width;
}
if (this.y > height) {
this.y = 0;
} else if (this.y < 0) {
this.y = height;
}
};
this.keys = function() {
if (this.left) {
this.dir -= 5;
}
if (this.right) {
this.dir += 5;
}
if (this.forward) {
this.vy += -this.acceleration;
this.y += this.vy * sin(this.dir);
this.x += this.vy * cos(this.dir);
}
if (this.back) {
this.vy += this.acceleration;
this.y += this.vy * sin(this.dir);
this.x += this.vy * cos(this.dir);
}
if (!this.forward && this.vy < 0) {
this.vy += 0.05;
this.y += this.vy * sin(this.dir);
this.x += this.vy * cos(this.dir);
}
if (!this.back && this.vy > 0) {
this.vy += -0.05;
this.y += this.vy * sin(this.dir);
this.x += this.vy * cos(this.dir);
}
if (this.vy >= k) {
this.vy = k;
}
if (this.vy <= -k) {
this.vy = -k;
}
if (this.vy <= 0.05 && this.vy >= -0.05) {
this.vy = 0;
}
fill(161, 161, 161);
text(this.vy, 10, 10);
text(this.dir, 10, 30);
};
this.rays = function() {
var theta = 0;
var angle = 1;
var radius = 200;
var r2 = 30;
for (var i = 0; i < 360; i += angle) {
var x = this.x + radius * cos(angle * i);
var y = this.y + radius * sin(angle * i);
var x1 = this.x + r2 * cos(angle * i);
var y1 = this.y + r2 * sin(angle * i);
//stroke(255, 0, 0);
//line(x1,y1,x,y);
stroke(0);
}
};
};
function Arm(x, y) {
this.x = x;
this.y = mover.y + mover.vy * sin(mover.dir);
this.dtheta = 0;
this.dir = mover.dir + 180 + this.dtheta;
this.length = 50;
this.vy = 0;
this.t = 0;
this.ammo = [];
this.draw = function() {
push();
translate(this.x, this.y);
rectMode(CORNER);
rotate(this.dir);
rect(-10, -5, this.length, 10);
pop();
this.x = mover.x;
this.y = mover.y;
//text(this.dir,10,110);
this.x = mover.x + mover.vy * cos(mover.dir);
this.y = mover.y + mover.vy * sin(mover.dir);
};
this.keys = function() {
if (this.left) {
this.dtheta += -5;
}
if (this.right) {
this.dtheta += 5;
}
if (this.forward && this.length < 150) {
this.vy += 0.1;
this.length += this.vy;
}
if (this.back && this.length > 30) {
this.vy += -0.1;
this.length += this.vy;
}
if (this.vy >= 5) {
this.vy = 5;
}
if (this.vy <= -k) {
this.vy = -k;
}
fill(161, 161, 161);
text(this.vy, 10, 50);
this.dir = mover.dir + 180 + this.dtheta;
};
this.shoot = function() {
var x = this.x + this.length * cos(this.dir);
var y = this.y + this.length * sin(this.dir);
if (this.fire) {
this.t++;
text(this.t, 10, 70);
}
if (this.t % 10 === 1) {
this.ammo.push(new Projectile(x, y, this.dir, mover.vy, mover.vy));
}
for (var i = 0; i < this.ammo.length; i++) {
var a = this.ammo[i];
//a.dir = 0;
fill(225, 255, 0);
//ellipse(a.x,a.y,10,10);
}
if (this.ammo.length > 10) {
this.ammo.splice(0, 1);
}
fill(161, 161, 161);
text(this.x, 10, 90);
};
this.update = function() {
};
};
function Projectile(x, y, dir, vx, vy) {
this.x = x;
this.y = y;
this.vx = -vx;
this.vy = -vy;
this.ac = 0.5;
this.dir = dir;
this.draw = function() {
fill(225, 255, 0);
ellipse(this.x, this.y, 10, 10);
fill(163, 163, 163);
};
this.update = function() {
this.vx += this.ac;
this.vy += this.ac;
if (mover.vy >= 0) {
if (this.vx >= k + mover.vy) {
this.vx = k + mover.vy;
}
if (this.vx <= -k + mover.vy) {
this.vx = -k + mover.vy;
}
if (this.vy >= k + mover.vy) {
this.vy = k + mover.vy;
}
if (this.vy <= -k + mover.vy) {
this.vy = -k + mover.vy;
}
}
if (mover.vy <= 0) {
if (this.vx >= k - mover.vy) {
this.vx = k - mover.vy;
}
if (this.vx <= -k - mover.vy) {
this.vx = -k - mover.vy;
}
if (this.vy >= k - mover.vy) {
this.vy = k - mover.vy;
}
if (this.vy <= -k - mover.vy) {
this.vy = -k - mover.vy;
}
}
this.x += (this.vx) * cos(this.dir);
this.y += (this.vy) * sin(this.dir);
this.ac -= 0.001;
if (this.ac >= -0.05 && this.ac <= 0.05) {
this.ac = 0;
}
};
};
track = new Track(200, 40, 200);
track.init();
mover = new Mover();
actuator = new Arm(mover.x, mover.y, mover.vy);
}
function draw() {
background(169, 230, 232);
//mover.keys();
noStroke();
mover.update();
mover.checkEdges();
mover.display();
mover.rays();
actuator.draw();
actuator.keys();
actuator.shoot();
for (var i = 0; i < actuator.ammo.length; i++) {
var a = actuator.ammo[i];
//a.dir = actuator.dir;
a.draw();
a.update();
}
track.draw();
};
function keyPressed() {
if (keyCode === 37) {
mover.left = true;
}
if (keyCode === 39) {
mover.right = true;
}
if (keyCode === 38 && keyCode !== 32) {
mover.forward = true;
}
if (keyCode === 40 && keyCode !== 32) {
mover.back = true;
}
if (keyCode === 65) {
actuator.left = true;
}
if (keyCode === 68) {
actuator.right = true;
}
if (keyCode === 32) {
mover.back = false;
mover.forward = false;
mover.vy = 0;
}
if (keyCode === 87) {
actuator.forward = true;
}
if (keyCode === 83) {
actuator.back = true;
}
if (keyCode === 69) {
actuator.fire = true;
}
};
function keyReleased() {
if (keyCode === 37) {
mover.left = false;
}
if (keyCode === 39) {
mover.right = false;
}
if (keyCode === 38) {
mover.forward = false;
}
if (keyCode === 40) {
mover.back = false;
}
if (keyCode === 65) {
actuator.left = false;
}
if (keyCode === 68) {
actuator.right = false;
}
if (keyCode === 87) {
actuator.forward = false;
actuator.vy = 0;
}
if (keyCode === 83) {
actuator.back = false;
actuator.vy = 0;
}
if (keyCode === 69) {
actuator.fire = false;
actuator.t = 0;
}
};