Skip to content
Snippets Groups Projects
Commit 37ea6303 authored by Alexandre  DELEFOSSE's avatar Alexandre DELEFOSSE
Browse files

spawn de nourriture aléatoire + bouton

parent 22d75bd9
No related branches found
No related tags found
No related merge requests found
......@@ -8,24 +8,19 @@ import java.awt.geom.Point2D;
abstract public class Fourmie {
protected double angle;
protected int vitesseMax;
protected int duree_vie;
protected long timeNaissance;
protected int dureeVie;
protected long dateNaissance;
protected int force;
protected Rectangle.Double box;
protected int taille;
protected Point2D.Double vitesse;
protected Color couleur;
protected double timeAction;
protected long timeBefore;
protected long tempsReference;
protected Colonie maColonie;
protected int distColonieMax;
protected int metier;
protected Monde monde;
public double getTimeAction() {
return timeAction;
}
public double getX() {
return box.x;
}
......@@ -48,10 +43,10 @@ abstract public class Fourmie {
this.couleur = couleur;
}
public int getDuree_vie() {
return duree_vie;
return dureeVie;
}
public void setDuree_vie(int duree_vie) {
this.duree_vie = duree_vie;
this.dureeVie = duree_vie;
}
public int getForce() {
return force;
......@@ -96,8 +91,8 @@ abstract public class Fourmie {
monde = m;
metier = 0;
angle = 0;
timeBefore = System.currentTimeMillis();
timeNaissance = System.currentTimeMillis();
tempsReference = System.currentTimeMillis();
dateNaissance = System.currentTimeMillis();
vitesse = new Point2D.Double(0,0);
maColonie = col;
}
......
......@@ -47,11 +47,18 @@ public class Main {
Thread me = new Thread(r);
me.start();
}
});
JButton Nourriture = new JButton("Nourriture");
Nourriture.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
monde.spawnNourriture();
}
);
});
JPanel boutonPanel = new JPanel();
boutonPanel.add(ouvriere);
boutonPanel.add(Nourriture);
boutonPanel.add(run);
screen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
......
......@@ -3,12 +3,15 @@ package fr.isima.fourmisses;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
public class Monde {
private List<Colonie> colonies;
private List<Nourriture> nourriture;
private List<Pheromone> pheromones;
private List<Pheromone> pheromonesMortes;
private static double intervalleNourriture = 10;
private static long beforeTemps=0;
public List<Colonie> getColonies() {
return colonies;
......@@ -27,19 +30,6 @@ public class Monde {
Colonie col = new Colonie(randomX, randomY, this);
col.addFourmie(0);
colonies.add(col);
randomX = 600;
randomY = 200;
Nourriture n = new Nourriture(randomX, randomY);
nourriture.add(n);
randomX = 1200;
randomY = 800;
n = new Nourriture(randomX, randomY);
nourriture.add(n);
randomX = 1300;
randomY = 400;
n = new Nourriture(randomX, randomY);
nourriture.add(n);
}
public Monde(int largeur, int hauteur) {
......@@ -62,7 +52,18 @@ public class Monde {
pheromones.add(p);
}
public void spawnNourriture() {
beforeTemps = System.currentTimeMillis();
int randomX = ThreadLocalRandom.current().nextInt(400, 1200);
int randomY = ThreadLocalRandom.current().nextInt(200, 800);
Nourriture n = new Nourriture(randomX, randomY);
nourriture.add(n);
}
public void update() {
if(beforeTemps + (intervalleNourriture*1000) < System.currentTimeMillis()) {
spawnNourriture();
}
Iterator<Nourriture> iter = nourriture.iterator();
while (iter.hasNext()) {
Nourriture n = iter.next();
......
......@@ -8,6 +8,7 @@ import java.util.concurrent.ThreadLocalRandom;
public class Ouvriere extends Fourmie {
private int nourriture;
private double tempsChangement;
private static double tempsPheromones = 0.7 + (double)ThreadLocalRandom.current().nextInt(50, 100)*0.3/100;
private static Color idleColor = Color.blue;
private static Color goHomeColor = Color.green;
......@@ -18,9 +19,9 @@ public class Ouvriere extends Fourmie {
metier = 1;
} else {
couleur = idleColor;
if(timeBefore + (timeAction*1000) < System.currentTimeMillis()) {
if(tempsReference + (tempsChangement*1000) < System.currentTimeMillis()) {
timeBefore = System.currentTimeMillis();
tempsReference = System.currentTimeMillis();
if(distanceToColonie() > distColonieMax) {
nouvelleTrajectoire((int)angleTo(maColonie.getBox()));
......@@ -38,9 +39,9 @@ public class Ouvriere extends Fourmie {
metier = 0;
} else {
couleur = workingColor;
if(timeBefore + (timeAction*1000) < System.currentTimeMillis()) {
if(tempsReference + (tempsChangement*1000) < System.currentTimeMillis()) {
timeBefore = System.currentTimeMillis();
tempsReference = System.currentTimeMillis();
if(distanceToColonie() > distColonieMax) {
nouvelleTrajectoire((int)angleTo(maColonie.getBox()));
......@@ -75,8 +76,8 @@ public class Ouvriere extends Fourmie {
if(nourriture == 0) {
metier = 0;
} else {
if(timeBefore + (tempsPheromones*1000) < System.currentTimeMillis()) {
timeBefore = System.currentTimeMillis();
if(tempsReference + (tempsPheromones*1000) < System.currentTimeMillis()) {
tempsReference = System.currentTimeMillis();
monde.addPheromone(new Pheromone((int)box.x, (int)box.y, (int)angleTo(maColonie.getBox())+(int)angle, monde));
}
couleur = goHomeColor;
......@@ -111,11 +112,11 @@ public class Ouvriere extends Fourmie {
public Ouvriere(Point p, Colonie col, Monde m) {
super(p, col, m);
this.force = 16;
this.duree_vie = 30;
this.dureeVie = 30;
this.couleur = idleColor;
this.taille = 5;
this.vitesseMax = 1;
this.timeAction = 2;
this.tempsChangement = 2;
this.distColonieMax = 400;
this.nourriture = 0;
box = new Rectangle.Double(p.x, p.y, taille, taille);
......@@ -125,7 +126,7 @@ public class Ouvriere extends Fourmie {
public void update() {
super.update();
if(System.currentTimeMillis() > timeNaissance + (duree_vie*1000))
if(System.currentTimeMillis() > dateNaissance + (dureeVie*1000))
maColonie.notifMort(this);
}
}
......@@ -6,10 +6,12 @@ import java.awt.Rectangle;
public class Reine extends Fourmie {
private double tempsPonte;
private void pondre() {
if(timeBefore + (timeAction*1000) < System.currentTimeMillis() && maColonie.getNourriture()>=2) {
if(tempsReference + (tempsPonte*1000) < System.currentTimeMillis() && maColonie.getNourriture()>=2) {
timeBefore = System.currentTimeMillis();
tempsReference = System.currentTimeMillis();
maColonie.addNourriture(-2);
maColonie.setLarves(maColonie.getLarves()+1);
......@@ -30,8 +32,8 @@ public class Reine extends Fourmie {
public Reine(Point p, Colonie col, Monde m) {
super(p, col, m);
this.force = 0;
this.duree_vie = 3600;
this.timeAction = 0.1;
this.dureeVie = 3600;
this.tempsPonte = 0.2;
this.couleur = Color.magenta;
this.taille = 5;
box = new Rectangle.Double(p.x, p.y, taille, taille);
......@@ -39,7 +41,7 @@ public class Reine extends Fourmie {
public void update() {
super.update();
if(System.currentTimeMillis() > timeNaissance + (duree_vie*1000))
if(System.currentTimeMillis() > dateNaissance + (dureeVie*1000))
maColonie.notifMort(this);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment