import java.awt.*; import java.util.*; import java.awt.event.*; /** * @author Oliver Mezger * @version 4.1 */ public class Spider { static int individuals = 0; private int ortX,ortY,groesse,satt,feeling,spiderID; private final Color SCOLORS[]= {Color.green,Color.red,Color.magenta,Color.pink,Color.cyan,Color.orange,Color.white}; private final int hangAround=0, fear=1, distance=2, hunt=3, vulture=4, tired=5, dead=6; private double laufRichtung; private int alter; private double fearness; private double vitality; public Spider(int x,int y,int gr, double ft, double vt, int sa){ ortX=x; ortY=y; groesse=gr; satt=sa; feeling = hangAround; spiderID = individuals++; laufRichtung = 0; alter = 0; fearness = ft; vitality = vt; } public Spider(int x,int y,int gr, double ft, double vt){ ortX=x; ortY=y; groesse=gr; satt=gr*2; feeling = hangAround; spiderID = individuals++; laufRichtung = 0; alter = 0; fearness = ft; vitality = vt; } public int getX(){ return ortX; } public int getY(){ return ortY; } public int getGroesse(){ return groesse; } public boolean stinks(){ return feeling == dead; } public void malen(Graphics g){ checkBorder(g.getClipBounds()); g.setColor(SCOLORS[feeling]); g.fillOval(ortX-groesse/2, ortY-groesse/2, groesse, groesse); g.setColor(Color.black); if (feeling < dead){ //g.drawLine(ortX, ortY-part, ortX, ortY+part); //g.drawLine(ortX-part, ortY, ortX+part, ortY); g.drawString(Integer.toString(alter),ortX-8,ortY-5); //die Richtung g.drawLine(ortX, ortY, (int)(ortX+groesse/2*Math.sin(laufRichtung)), (int)(ortY-groesse/2*Math.cos(laufRichtung))); } else g.drawString("RIP",ortX-5,ortY-8); g.drawString(Integer.toString(spiderID),ortX-5,ortY-groesse/2+10); g.drawString("f"+(int)(fearness*100)+" v"+(int)(vitality*100)+" s"+satt,ortX-25,ortY+10); //g.drawString(Integer.toString((int)(laufRichtung/ Math.PI*180)),ortX,ortY+40); g.drawString(Integer.toString(groesse),ortX-5,ortY+groesse/2); /*part=2*groesse/3; g.drawArc(ortX-part/2, ortY-part/2, part, part, -125, 80); //die Augen g.setColor(Color.green); part=groesse/8; g.fillOval((int)(ortX-3*part), ortY-2*part, 2*part,part); g.fillOval((int)(ortX+1*part), ortY-2*part, 2*part,part); */ } void checkBorder(Rectangle r){ boolean b = false; int a = groesse; if (ortX < a){ ortX = a; b = true; } else if (ortX > r.width-a){ ortX = r.width-a; b = true; } if (ortY < a){ ortY = a; b = true; } else if (ortY > r.height-a){ ortY = r.height-a; b = true; } if (b){ laufRichtung += 2*Math.PI*(Math.random()-0.4); if (laufRichtung > 2*Math.PI) laufRichtung -= 2*Math.PI; ortX += (int)(Math.sin(laufRichtung)*groesse*vitality); ortY -= (int)(Math.cos(laufRichtung)*groesse*vitality); } } public void live(Vector v){ if (feeling == dead) return; // is Dead Spider feind = null; int nfei = 2000,nbr =2000, nfut = 2000, nas=2000; Spider brother = null; Spider futter = null; Spider ass = null; if (feeling == tired){ feeling = 0; Enumeration enum = v.elements(); while (enum.hasMoreElements()){ Spider s = (Spider) enum.nextElement(); if (s!= this){ if (s.getGroesse() > groesse){ if (dist(s) < nfei){ feind =s; nfei =dist(s); } } } } } else{ feeling = 0; Enumeration enum = v.elements(); while (enum.hasMoreElements()){ Spider s = (Spider) enum.nextElement(); if (s!= this){ if (s.stinks()){ if (dist(s) groesse){ if (dist(s) < nfei){ feind =s; nfei =dist(s); } } else if (s.getGroesse() < groesse-2){ if (dist(s) < nfut){ futter = s; nfut = dist(s); } } else{ if (dist(s) < nbr){ brother =s; nbr =dist(s); } } } } } }// Math.abs(groesse-feind.getGroesse()) if (feind != null && (nfei-3.0 < (feind.getGroesse()-groesse)*5*fearness)) feeling=fear; else if (brother != null && nbr < groesse) feeling=distance; else if (groesse > satt) feeling = tired; else if (nas < nfut){if (ass != null) feeling = vulture;} else if (futter != null) feeling = hunt; //else if (ass != null) feeling = vulture; switch (feeling){ case hangAround: // hang around ortX += (int)(Math.sin(laufRichtung)*groesse/5*vitality); ortY -= (int)(Math.cos(laufRichtung)*groesse/5*vitality); break; case fear: runAway(feind); alter++; break; case distance: runAway(brother); break; case hunt: if (hunt(futter)) v.removeElement(futter); alter++; break; case vulture: if (eatAss(ass)) v.removeElement(ass); break; case tired: alter--; if (ass != null && nas < groesse) runAway(ass); if ((double)groesse > satt*1.3){ v.addElement( new Spider(ortX,ortY,groesse/3,fearness,vitality,(int)(satt+(Math.random()-0.5)*30))); v.addElement( new Spider(ortX,ortY,groesse/3,fearness,vitality,(int)(satt+(Math.random()-0.5)*30))); feeling=dead; } break; } alter +=2; if (alter % 100 == 0) groesse -= groesse/30; if (alter > 2000) feeling = dead; } public boolean hunt(Spider f){ int dx = (f.getX()-ortX); int dy = -(f.getY()-ortY); int dist = (int)Math.sqrt(dx*dx+dy*dy); laufRichtung = Math.atan2(dx,dy); ortX += (int)(Math.sin(laufRichtung)*groesse/5*vitality); ortY -= (int)(Math.cos(laufRichtung)*groesse/5*vitality); if (dist > groesse) return false; groesse += f.groesse/2; return true; } public boolean eatAss(Spider f){ int dx = (f.getX()-ortX); int dy = -(f.getY()-ortY); int dist = (int)Math.sqrt(dx*dx+dy*dy); laufRichtung = Math.atan2(dx,dy); if (dist > groesse){ ortX += (int)(Math.sin(laufRichtung)*groesse/6*vitality); ortY -= (int)(Math.cos(laufRichtung)*groesse/6*vitality); } if (dist > groesse) return false; groesse += 1; f.groesse = f.groesse-2; if (f.groesse >1) return false; return true; } public void runAway(Spider f){ int dx = (f.getX()-ortX); int dy = -(f.getY()-ortY); int dist = (int)Math.sqrt(dx*dx+dy*dy); laufRichtung = Math.atan2(dx,dy)+ Math.PI; ortX += (int)(Math.sin(laufRichtung)*groesse*vitality); ortY -= (int)(Math.cos(laufRichtung)*groesse*vitality); } int dist(Spider a){ int dx,dy; dx = a.getX()-ortX; dy = a.getY()-ortY; return (int)Math.sqrt(dx*dx+dy*dy); } }