import java.awt.*; import java.util.Random; public class Bird { private final int GROUNDTOLERANCE = 75; int framedelay; int delay_counter = 0; int curframe = 0; int maxframes; double x, y, dx, dy, ddx, ddy; int groundlevel; Graphics g; public boolean alive = true; Bird prev; Bird next; public Bird(int x, int y, int groundlevel, int maxframes) { Random r = new Random(); this.x = x; this.y = y; this.dx = (r.nextDouble() * r.nextDouble() - .5) * 20; this.dy = (r.nextDouble() * r.nextDouble() + .5) * 10; if (r.nextInt() %2 == 1) this.dx *= -1; if (r.nextInt() %2 == 1) this.dy *= -1; // this.dx = (r.nextDouble() * r.nextDouble() - 0.25) * 100; //this.dy = (r.nextDouble() * r.nextDouble() - 0.25) * 100; this.ddx = 0; this.ddy = 0; this.framedelay = 3; // this.framedelay = (int)(90 - this.dx - this.dy); this.maxframes = maxframes; this.next = null; this.groundlevel = groundlevel; // p("DX: "+dx+", DY:"+dy+"."); } public Bird(int x, int y, double dx, double dy, int groundlevel, int maxframes) { this.x = x; this.y = y; this.dx = dx; this.dy = dy; this.ddx = 0; this.ddy = 0; this.framedelay = 3; // this.framedelay = (int)(90 - this.dx - this.dy); this.maxframes = maxframes; this.next = null; this.groundlevel = groundlevel; } public Bird(int x, int y, double dx, double dy, double ddx, double ddy, int groundlevel, int maxframes) { this.x = x; this.y = y; this.dx = dx; this.dy = dy; this.ddx = ddx; this.ddy = ddy; this.framedelay = 3; // this.framedelay = (int)(90 - this.dx - this.dy); this.maxframes = maxframes; this.next = null; this.groundlevel = groundlevel; } public int getX() { return (int)x; } public int getY() { return (int)y; } public void animate() { curframe = ++curframe % maxframes; } public void motion() { if (groundlevel != 0) { if (y < groundlevel) if ((int)dy <= 0) push(0, (groundlevel - y) / 500); else if (y > (groundlevel - 50)) push(0, (y - groundlevel - 50) / 500); else {} else { if (dy > 0) { ddy = 0; ddx = 0; dy = 0; dx = 0; } else { dy = dy * 1.25; } } } if (dy == 0) framedelay = 12; else if (Math.abs(dy) > 20) framedelay = 3; else if (Math.abs(dy) > 10) framedelay = 5; else if (Math.abs(dy) > 1) framedelay = 3; else framedelay = 12; // if (dy != 0) // p("dy: "+dy+"; fd: "+framedelay); accelerate(); move(); } public void push(double ddx, double ddy) { this.ddx += ddx; this.ddy += ddy; } public void accelerate() { dx += ddx; dy += ddy; } public void move() { x += dx; y += dy; } public void action() { motion(); if ((delay_counter++ % framedelay) == 0) { animate(); } } public void shoot(int x, int y, boolean silenced) { double d; if ((d = dist(getX(), getY(), x, y)) < 22) { Random r = new Random(); alive = false; dy = 0; ddy = 2; curframe = 0; } else if (!silenced) scare(x, y); } public void scare(int x, int y) { double d; if ((d = dist(getX(), getY(), x, y)) < 100) { Random r = new Random(); dy -= (r.nextDouble() * r.nextDouble() + .5) * (100 - d) / 2; dx = (r.nextDouble() * r.nextDouble() - .5) * (100 - d) / 2; } } public void insertAfter(Bird b) { if (next != null) { next.prev = b; b.next = next; } next = b; b.prev = this; } public void destroy() { if (next != null) next.prev = prev; prev.next = next; } private void p(String s) { System.out.println(s); } private void p(Exception e) { System.out.println(e); } private double dist(int x, int y, int xx, int yy) { return Math.sqrt((x - xx) * (x - xx) + (y - yy) * (y - yy)); } }