public void render() { Graphics g = screen.getGraphics(); // Drawing Things! sky.render(g); level.render( g, (int) sX, (int) sY, (pixel.width / Tile.tileSize) + 2, (pixel.height / Tile.tileSize) + 2); character.render(g); inventory.render(g); health.render(g); for (int i = 0; i < mob.toArray().length; i++) { mob.get(i).render(g); } g = getGraphics(); g.drawImage(screen, 0, 0, size.width, size.height, 0, 0, pixel.width, pixel.height, null); g.dispose(); }
public void trace(float x, float y) { println(x); if (frameCounter > 2 && mousePressed) { coords.add(new PVector(x, y)); frameCounter = 0; if (coordCounter > 0) { PVector tempCoord = (PVector) coords.get(coordCounter - 1); line(x, y, tempCoord.x, tempCoord.y); } coordCounter++; } // println(coordCounter); for (int i = 0; i < coords.size(); i++) { PVector tempCoordnear = (PVector) coords.get(i); float coordDist = dist(x, y, tempCoordnear.x, tempCoordnear.y); if (coordDist < distance) { stroke(255, 0, 0); line(x, y, tempCoordnear.x, tempCoordnear.y); } } frameCounter++; }
public void draw() { background(255); // Turn off highlighting for all obstalces for (int i = 0; i < obstacles.size(); i++) { Obstacle o = (Obstacle) obstacles.get(i); o.highlight(false); } // Act on all boids for (int i = 0; i < boids.size(); i++) { Boid b = (Boid) boids.get(i); b.avoid(obstacles); b.run(); } // Display the obstacles for (int i = 0; i < obstacles.size(); i++) { Obstacle o = (Obstacle) obstacles.get(i); o.display(); } // Instructions textFont(f); fill(0); text( "Hit space bar to toggle debugging lines.\nClick the mouse to generate a new boids.", 10, height - 30); }
public void drawOrbits(ArrayList alObjectsArchive) { // println("SIZE:" + alObjectsArchive.size()); // ArrayList alObjectsArchive = timeline.getObjectStateArchive(); ArrayList alPrevPos = new ArrayList(); ArrayList alColors = new ArrayList(); alColors.add(color(255, 0, 0)); alColors.add(color(255, 255, 0)); alColors.add(color(255, 0, 255)); // for (int i = timeline.getTimeIdx(); i >= 0 && i > (timeline.getTimeIdx() - 1 - 100); i--) for (int i = 0; i < alObjectsArchive.size(); i++) { ArrayList objects = (ArrayList) alObjectsArchive.get(i); for (int j = 0; j < objects.size(); j++) { CelestialObject obj = (CelestialObject) objects.get(j); // CelestialObject obj = (CelestialObject)objects.get(1); PVector pos = obj.getPosition(); // stroke(0, 0, 255); stroke((Integer) alColors.get(j)); if (alPrevPos.size() == objects.size()) { PVector prevPos = (PVector) alPrevPos.get(j); line(prevPos.x, prevPos.y, pos.x, pos.y); alPrevPos.set(j, pos); } else alPrevPos.add(pos); } } }
/* -------------------------------------------------------------*/ private void objectCollision(ArrayList movingObjects) { for (int i = 0; i < movingObjects.size(); i++) { // make sure not testing if collided with yourself :P if (((SpaceObject) movingObjects.get(i)).getObjCount() != objectNum) { SpaceObject object = (SpaceObject) movingObjects.get(i); // find distance vector between two objects int x = pos_x - object.getXPos(); int y = pos_y - object.getYPos(); double distance = Math.sqrt(x * x + y * y); // has it collided with the object? if (distance < (radius + object.getRadius())) { // has it collided with a BULLET (or MISSILE)? if (object.isBullet()) { // do nothing } // is it another SPACESHIP? (INSTANT DEATH) else if (object.isSpaceShip()) { // do nothing } // collided with anything else (e.g PLANET): (INSTANT DEATH) else { collision.play(); kill(movingObjects); // object has died } } } } // end for loop }
public void mouseClicked(MouseEvent e) { int x = e.getX(); int y = e.getY(); for (int i = 0; i < targets.size(); i++) { if (x > targets.get(i).getX() && x < targets.get(i).getX() + targets.get(i).getSize() && y > targets.get(i).getY() && y < targets.get(i).getY() + targets.get(i).getSize()) { targets.remove(i); } } }
public void calculateForces(ArrayList objects) { for (int i = 0; i < objects.size(); i++) { CelestialObject obj = (CelestialObject) objects.get(i); ArrayList forces = obj.getForces(); float totalForceX = 0; float totalForceY = 0; for (int j = 0; j < forces.size(); j++) { totalForceX += ((PVector) forces.get(j)).x; totalForceY += ((PVector) forces.get(j)).y; } PVector newAccel = new PVector(totalForceX / obj.getMass(), totalForceY / obj.getMass()); obj.setAcceleration(newAccel); } for (int i = 0; i < objects.size(); i++) { CelestialObject obj1 = (CelestialObject) objects.get(i); float forceX = 0; float forceY = 0; obj1.clearForces(); if (obj1.getClass() == Star.class) { println(obj1.getVelocity()); continue; } for (int j = 0; j < objects.size(); j++) { CelestialObject obj2 = (CelestialObject) objects.get(j); if (i == j) continue; PVector pvDistance = PVector.sub(obj2.getPosition(), obj1.getPosition()); // println("distance: x:" + pvDistance.x + " y:" + pvDistance.y); float distance = sqrt(sq(pvDistance.y) + sq(pvDistance.x)); float angle = degrees(atan2(pvDistance.y, pvDistance.x)); float force = (G * obj1.getMass() * obj2.getMass()) / sq(distance); forceX = force * cos(radians(angle)); forceY = force * sin(radians(angle)); // println("FORCES on " + obj1.getName() + ":" + forceX + "," + forceY); obj1.addForce(new PVector(forceX, forceY)); println(); } } }
public void setup() { size(600, 400); smooth(); f = createFont("Georgia", 12, true); obstacles = new ArrayList(); boids = new ArrayList(); // A little algorithm to pick a bunch of random obstacles that don't overlap for (int i = 0; i < 100; i++) { float x = random(width); float y = random(height); float r = random(50 - i / 2, 50); boolean ok = true; for (int j = 0; j < obstacles.size(); j++) { Obstacle o = (Obstacle) obstacles.get(j); if (dist(x, y, o.loc.x, o.loc.y) < o.radius + r + 20) { ok = false; } } if (ok) obstacles.add(new Obstacle(x, y, r)); } // Starting with three boids boids.add(new Boid(new PVector(random(width), random(height)), 3f, 0.2f)); boids.add(new Boid(new PVector(random(width), random(height)), 3f, 0.1f)); boids.add(new Boid(new PVector(random(width), random(height)), 2f, 0.05f)); }
// ------------------------------------------------------------------------------------------------------------------------------------------------------- // Patterns public void newPattern() { int prob = (int) (Math.random() * probs.get(level - 1) + 1); Pattern p = new Pattern(0, -300, prob, (int) (backy * 0.1)); if (checkSpawn(new Rectangle(p.getX(), p.getY(), p.getWidth(), p.getHeight())) == true) { for (Coin i : p.getCoins()) { coinList.add(i); } for (Star i : p.getStars()) { starList.add(i); } for (Box i : p.getBoxes()) { boxList.add(i); } for (Jumper i : p.getJumpers()) { jumperList.add(i); } for (Enemy i : p.getEnemies()) { enemyList.add(i); } for (Spikes i : p.getSpikes()) { spikeList.add(i); } for (Powerup i : p.getPowerUps()) { pupList.add(i); } for (Rectangle i : p.getRects()) { rectList.add(i); } } }
public void manageStuff() { // organizes the the stuff in the stats list to useable stuff int c = 0; money = Integer.parseInt(stats.get(c)); c++; for (int i = 0; i < Integer.parseInt(stats.get(1)); i++) { powerUps.add(stats.get(i + 2)); c++; } c++; // System.out.println(Integer.parseInt(stats.get(1))+1); // System.out.println(c); for (int i = c + 1; i < c + Integer.parseInt(stats.get(c)) + 1; i++) { chars.add(stats.get(i)); } }
/* -------------------------------------------------------------*/ private void gravityEffect(ArrayList movingObjects) { // find effect of gravity on this objects from all other objects for (int i = 0; i < movingObjects.size(); i++) { // reset variables double add_vx = 0.0; double add_vy = 0.0; SpaceObject object = (SpaceObject) movingObjects.get(i); if (object.getObjCount() != objectNum && // ignore yourself (distance = 0!) !object.isBullet() && // ignore bullets !object.isSpaceShip()) // ignore spaceships { // find distance vector between planet and ball int x = pos_x - object.getXPos(); int y = pos_y - object.getYPos(); double distance = Math.sqrt(x * x + y * y); // find effect of planet on velocity double add_vec = (SpaceObject.G * k * object.getMass() * ballMass) / (distance * distance); add_vx = -(x / distance) * add_vec; add_vy = -(y / distance) * add_vec; // add objects speeds onto spaceship speed (clip speed if too large) x_speed += add_vx; y_speed += add_vy; } } }
// Does a deepcopy of an array list public static ArrayList cloneArrayList(ArrayList al) { ArrayList alNew = new ArrayList(al.size()); for (int i = 0; i < al.size(); i++) { alNew.add(((CelestialObject) al.get(i)).clone()); } return alNew; }
public void updateFile() { // writes highscore, coins and updates eveything else try { outFile = new PrintWriter(new BufferedWriter(new FileWriter("stats.txt"))); outFile.println("" + (money + coins)); outFile.println("" + powerUps.size()); for (int i = 0; i < powerUps.size(); i++) { outFile.println("" + powerUps.get(i)); // writes the highscore in the file } outFile.println("" + chars.size()); for (int i = 0; i < chars.size(); i++) { outFile.println("" + chars.get(i)); } if (Integer.parseInt(stats.get(stats.size() - 3)) < score) { outFile.println(score); } else { outFile.println(stats.get(stats.size() - 3)); } if (Integer.parseInt(stats.get(stats.size() - 2)) < score) { outFile.println(height); } else { outFile.println(stats.get(stats.size() - 2)); } outFile.println(Integer.parseInt(stats.get(stats.size() - 1)) + height); outFile.close(); } catch (IOException ex) { System.out.println("yooo stop noobing out"); } }
public void avoid(ArrayList obstacles) { // Make a vector that will be the position of the object // relative to the Boid rotated in the direction of boid's velocity PVector closestRotated = new PVector(sight + 1, sight + 1); float closestDistance = 99999; Obstacle avoid = null; // Let's look at each obstacle for (int i = 0; i < obstacles.size(); i++) { Obstacle o = (Obstacle) obstacles.get(i); float d = PVector.dist(loc, o.loc); PVector dir = vel.get(); dir.normalize(); PVector diff = PVector.sub(o.loc, loc); // Now we use the dot product to rotate the vector that points from boid to obstacle // Velocity is the new x-axis PVector rotated = new PVector(diff.dot(dir), diff.dot(getNormal(dir))); // Is the obstacle in our path? if (PApplet.abs(rotated.y) < (o.radius + r)) { // Is it the closest obstacle? if ((rotated.x > 0) && (rotated.x < closestRotated.x)) { closestRotated = rotated; avoid = o; } } } // Can we actually see the closest one? if (PApplet.abs(closestRotated.x) < sight) { // The desired vector should point away from the obstacle // The closer to the obstacle, the more it should steer PVector desired = new PVector(closestRotated.x, -closestRotated.y * sight / closestRotated.x); desired.normalize(); desired.mult(closestDistance); desired.limit(maxspeed); // Rotate back to the regular coordinate system rotateVector(desired, vel.heading2D()); // Draw some debugging stuff if (debug) { stroke(0); line(loc.x, loc.y, loc.x + desired.x * 10, loc.y + desired.y * 10); avoid.highlight(true); } // Apply Reynolds steering rules desired.sub(vel); desired.limit(maxforce); acc.add(desired); } }
// Does a deepcopy of an array list public ArrayList cloneArrayList(ArrayList al) { ArrayList alNew = new ArrayList(al.size()); for (int i = 0; i < al.size(); i++) { PVector pv = (PVector) al.get(i); alNew.add(new PVector(pv.x, pv.y)); } return alNew; }
public void draw() { background(255); for (int i = 0; i < balls.size(); i++) { Ball ball = (Ball) balls.get(i); ball.calc(); ball.display(); } }
public void mouseClicked() { ArrayList objects = timeline.getStatefulObjects(); for (int i = 0; i < objects.size(); i++) { CelestialObject obj = (CelestialObject) objects.get(i); if (obj.isMouseOver()) { println(obj.getName() + " clicked!"); break; } } }
/** Update all of the Ball's and draw them */ public void update() { if (balls.size() != 0) { for (int i = 0; i < balls.size(); i++) { Ball b = (Ball) balls.get(i); b.update(); b.attract = kelly; b.drawBall(); } } }
public void tick() { if (inGame) { level.tick( (int) sX, (int) sY, (pixel.width / Tile.tileSize) + 2, (pixel.height / Tile.tileSize) + 2); character.tick(); sky.tick(); health.tick(); for (int i = 0; i < mob.toArray().length; i++) { mob.get(i).tick(); } } }
public void draw() { // background(255); fill(255); rect(-4, -4, width + 4, height + 4); for (int i = skaters.size() - 1; i >= 0; i--) { Skater skater = (Skater) skaters.get(i); // if (mousePressed) { skater.trace(mouseX, mouseY); // blob[i].x etc. println(i); } }
public void draw() { background(0); fill(255); if (!paused) timeline.moveForward(); if (dragging) drawOrbits(timeline.getFutureObjectStates()); else drawOrbits(timeline.getPastObjectStates()); ArrayList objects = timeline.getStatefulObjects(); for (int i = 0; i < objects.size(); i++) { CelestialObject obj = (CelestialObject) objects.get(i); obj.display(); } }
/** If a key is pressed perform the respective actions */ public void keyPressed() { // Add 'stems' to the balls if (keyCode == SHIFT) { stems = !stems; for (int i = 0; i < balls.size(); i++) { Ball b = (Ball) balls.get(i); b.STEM = stems; } } // toggle repaint background else if (key == 'b') REPAINT = !REPAINT; // Empty the ArrayList of Balls else if (key == 'x') balls.clear(); // Add a ball else if (key == 'f') addBall(); }
public int moveBackward() { intTimeIdx--; if (intTimeIdx < 0) { intTimeIdx = 0; } else { // println("before:" + ((CelestialObject)alStatefulObjects.get(0)).getPosition().x + // "," + ((CelestialObject)alStatefulObjects.get(0)).getPosition().y); setCurrentState(cloneArrayList((ArrayList) alObjectStateArchive.get(intTimeIdx))); // println("before:" + ((CelestialObject)alStatefulObjects.get(0)).getPosition().x + // "," + ((CelestialObject)alStatefulObjects.get(0)).getPosition().y); } sliderTimeline.setValue(intTimeIdx); return intTimeIdx; }
public int moveForward() { // println("forward!"); intTimeIdx++; // if the future values have already been calculated, just fetch them instead of calculating // them again if (alObjectStateArchive.size() > intTimeIdx) setCurrentState(cloneArrayList((ArrayList) alObjectStateArchive.get(intTimeIdx))); else { // println("calculating..."); sim.calculateForces(alStatefulObjects); alObjectStateArchive.add(cloneArrayList(alStatefulObjects)); } sliderTimeline.setValue(intTimeIdx); return intTimeIdx; }
public void mouseDragged() { if (paused) { ArrayList objects = timeline.getStatefulObjects(); for (int i = 0; i < objects.size(); i++) { CelestialObject obj = (CelestialObject) objects.get(i); if (obj.isMouseOver()) { dragging = true; PVector pos = obj.getPosition(); pos.x = mouseX; pos.y = mouseY; timeline.reset(); timeline.setCurrentState(objects); sliderTimeline.setValue(0); break; } } } }
public void drawPowerUpEffect(Graphics g) { if (player1.getPower().equals("Magnet")) { Image magpic = magnetList.get((int) count % 6); g.drawImage( magpic, player1.getX() - ((magpic.getWidth(null) - player1.getWidth()) / 2), player1.getY() - ((magpic.getHeight(null) - player1.getHeight()) / 2), magpic.getWidth(null), magpic.getHeight(null), this); count += 0.1; } else if (player1.getPower().equals("Ball")) { g.drawImage( ballPower, player1.getX() - ballPower.getWidth(null) / 2 + 17, player1.getY() + player1.getHeight() - 20, ballPower.getWidth(null), ballPower.getHeight(null), this); } else if (player1.getPower().equals("Sheild")) { g.drawImage( sheildPower, player1.getX() - ((sheildPower.getWidth(null) - player1.getWidth()) / 2), player1.getY() - ((sheildPower.getHeight(null) - player1.getHeight()) / 2), sheildPower.getWidth(null), sheildPower.getHeight(null), this); } else if (player1.getPower().equals("Umbrella")) { g.drawImage( umbrellaPower, player1.getX() - (umbrellaPower.getWidth(null) / 2) + 20, player1.getY() - umbrellaPower.getHeight(null) + 40, umbrellaPower.getWidth(null), umbrellaPower.getHeight(null), this); } else if (player1.getPower().equals("")) { } }
public void setCurrentState(ArrayList alState) { for (int i = 0; i < alStatefulObjects.size(); i++) { alStatefulObjects.set(i, alState.get(i)); } }