basic gameplay

This commit is contained in:
Victor Giers
2019-06-26 06:40:37 +02:00
parent 139ec15ec8
commit e0488dc127

View File

@@ -17,7 +17,7 @@
"use strict";
var width, height;
var rate = 16.667;
var rate = 200;
var tickMS = rate;
var previous;
var time = 0;
@@ -43,8 +43,12 @@
imageMode(CENTER);
textAlign(CENTER);
previous = millis();
for(var i = 0; i < 20; i ++){
citizen.push(new Citizen(random(width/2 - minEdge/16, width/2 + minEdge/16), random(height/2-minEdge/4, height/2+minEdge/4), color(255,255,255)));
}
}
@@ -77,55 +81,77 @@
ring.updatePixels();
ring.mask(rotatedMask);
//translate(width/2,height/2);
image(center, width/2,height/2, minEdge/2, minEdge/2);
image(ring, width/2,height/2, minEdge/2, minEdge/2);
for(var i = 0; i < citizen.length; i++){
for(var i = 0; i < citizen.length; i++){ // go with -- here
citizen[i].update();
citizen[i].display();
}
fill(255,255,255);
text(nf(angle, 0, 2), width/2,height/2, 40);
var current = millis();
var elapsed = current - previous;
previous = current;
time += elapsed;
while (time >= tickMS){
time -= tickMS;
var side = int(random(0,4));
var randcolor = color(random(0,255), random(0,255), random(0,255));
switch (side){
case(0): citizen.push(new Citizen(random(0,width), -30, randcolor));
break;
case(1): citizen.push(new Citizen(random(0,width), height+30, randcolor));
break;
case(2): citizen.push(new Citizen(-30, random(0,height), randcolor));
break;
case(3): citizen.push(new Citizen(width+30, random(0,height), randcolor));
break;
}
tickMS = random(rate/100,2*rate-(rate/100));
}
}
class Citizen{
constructor(x, y, color){
constructor(x, y, color, id){
this.x = x;
this.y = y;
this.color = color;
this.id = citizen.length;
this.speedX = ((this.x-width/2)*-1)/dist(this.x, this.y, width/2, height/2);
this.speedY = ((this.y-height/2)*-1)/dist(this.x, this.y, width/2, height/2);
this.angle = atan2(width/2 - this.x, height/2 - this.y);
this.range = random(2,minEdge/5.33);
if(this.angle + 0.4 > PI || this.angle - 0.4 < -PI){
this.invalid = true;
this.despawn();
}
}
update(){
this.angle = atan2(width/2 - this.x, height/2 - this.y);
if(this.angle + 0.4 > PI || this.angle - 0.4 < -PI){
fill(0,255,0);
text("problem!", this.x,this.y-50, 40);
}
if(dist(this.x, this.y, width/2, height/2) < minEdge/4 && dist(this.x, this.y, width/2, height/2) > minEdge/6){
if(this.invalid) this.despawn();
if(dist(this.x, this.y, width/2, height/2) < minEdge/4 && dist(this.x, this.y, width/2, height/2) > minEdge/5.33){
if(angle + 0.4 > this.angle && angle - 0.4 < this.angle){
fill(0,255,0);
text("JUHU", this.x,this.y-30, 40);
//fill(0,255,0);
//text("JUHU", this.x,this.y-30, 40);
} else {
fill(255,0,0);
text("AUA", this.x,this.y-30, 40);
this.despawn();
}
}
if(dist(this.x, this.y, width/2, height/2) > minEdge/6){
if(dist(this.x, this.y, width/2, height/2) > this.range){
this.x += this.speedX;
this.y += this.speedY;
}
}
display(){
fill(255,255,255);
fill(this.color);
ellipse(this.x, this.y, minEdge/40);
fill(0);
text(nf(this.angle, 0, 2), this.x,this.y-20, 40)
}
despawn(){
citizen.splice(this.id,1);
for(var i = this.id; i < citizen.length; i++){
citizen[i].id--;
}
}
}
function mouseClicked(){
citizen.push(new Citizen(mouseX, mouseY));
//citizen.push(new Citizen(mouseX, mouseY, color(255,255,255), citizen.length));
}
window.onresize = function() {
@@ -134,6 +160,16 @@
width = w;
height = h;
resizeCanvas(width,height);
for(var i = 0; i < 20; i ++){
citizen.push(new Citizen(random(width/2 - minEdge/16, width/2 + minEdge/16), random(height/2-minEdge/4, height/2+minEdge/4), color(255,255,255)));
}
for(var i = 0; i < citizen.length; i++){
citizen[i].speedX = ((citizen[i].x-width/2)*-1)/dist(citizen[i].x, citizen[i].y, width/2, height/2);
citizen[i].speedY = ((this.y-height/2)*-1)/dist(citizen[i].x, citizen[i].y, width/2, height/2);
citizen[i].angle = atan2(width/2 - citizen[i].x, height/2 - citizen[i].y);
citizen[i].range = random(2,minEdge/5.33);
}
};
</script>
</head>