class Grenade { PVector pos; PVector vel; int life; Grenade() { pos = new PVector(player_pos.x, player_pos.y); vel = new PVector(mouseX - player_pos.x, mouseY - player_pos.y); vel.div(GRENADE_LIFE); vel.y -= GRENADE_LAUNCH_KICK; life = GRENADE_LIFE; } void tick() { pos.add(vel); vel.y += GRENADE_GRAVITY; life--; } } class Explosion { PVector pos; int life; float radius; Explosion(PVector p, float r) { radius = r; life = EXPLOSION_LIFE; pos = new PVector(p.x, p.y); } void eatDirt() { for(int x = (int) (pos.x - radius); x < pos.x + radius; x++) for(int y = (int) (pos.y - radius); y < pos.y + radius; y++) { if(x < width && x >= 0 && y < height && y >= 0) if(inRadius(pos, new PVector(x, y), radius)) { ground[x][y] = 0; } } if(inRadius(pos, player_pos, radius)) { gameover = true; gameover_text = "GAME OVER:\ngot blown up"; } } void tick() { life--; } void draw() { noStroke(); if(life + 6 >= EXPLOSION_LIFE) { fill(255); ellipse(pos.x, pos.y, radius, radius); } else { fill(255, 255 * life / EXPLOSION_LIFE, 0); float explogress = map(life, EXPLOSION_LIFE, 0, 0, 1); float er = min(explogress, 1-explogress) * 2 * radius; ellipse(pos.x, pos.y + radius - (radius * 2 * explogress), er, er); } } } class Meteor { PVector pos; PVector vel; PVector lead; boolean warned = false; boolean sounded = false; Meteor() { float final_x = random(width); float top_x = random(width); PVector final_pos = new PVector(final_x, height); PVector top_pos = new PVector(top_x, 0); PVector veldir = PVector.sub(final_pos, top_pos); veldir.normalize(); vel = PVector.mult(veldir, METEOR_SPEED); lead = PVector.mult(vel, METEOR_WARN); pos = PVector.sub(final_pos, PVector.mult(vel, METEOR_SPAWN_AHEAD)); } void tick() { pos.add(vel); if(PVector.add(pos, lead).y > 0) { if(physics_frame % 10 == 0) particles.add(new Ripple(PVector.add(pos, lead))); if(!warned) if(!muted) meteorWarn.trigger();//TODO warning sound warned = true; } if(pos.y > 0) { particles.add(new Firetrail(pos)); if(!sounded) if(!muted) meteorFall.trigger(); sounded = true; } } void draw() { fill(255, 255, 0); noStroke(); ellipse(pos.x, pos.y, 10, 10); } } class Firetrail extends Particle { Firetrail(PVector p) { super(p, randomVector(random(0.3)), 90); } void draw() { noStroke(); fill(255, 0, 0); ellipse(pos.x, pos.y, 15*life/maxlife, 15*life/maxlife); } } class Ripple extends Particle { Ripple(PVector p) { super(p, new PVector(0, 0), METEOR_WARN - GRENADE_LIFE); } void draw() { noFill(); stroke(128, 64, 96); float r = sin(life*PI/maxlife) * 50; ellipse(pos.x, pos.y, r, r); } }