Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package robots;
import javax.media.j3d.Transform3D;
import javax.vecmath.Color3f;
import javax.vecmath.Point3d;
import javax.vecmath.Vector3d;
import objets.Invocateur;
import simbad.sim.Agent;
import simbad.sim.RangeSensorBelt;
import simbad.sim.RobotFactory;
import simbad.sim.SimpleAgent;
import simbad.sim.Simulator;
import simbad.sim.World;
public class RobotIA extends Agent
{	
	private final static double  DUREE_VIE               = 1500;
	private final static double  VITESSE_DEPLACEMENT_MAX = 3;
	private final static int     VALEUR_POINTS           = 100;
	private final static Color3f COULEUR_PROJECTILES     = new Color3f(255,0,0);
	private final static Color3f COULEUR_ROBOT           = new Color3f(0,255,0);
	
	Simulator       simulateur;
	RangeSensorBelt sonars;
	Vector3d        posCanon;
	int             frequenceTir;
	int             vitesse;
	
	// Etats du robot
	boolean tourne;
	char  direction;    // G = Gauche, D = Droit
	int tempsDebutVirage;
	
	public RobotIA ( Vector3d pos, String name, Simulator simulator )
	{
		super(pos, name);
		this.simulateur = simulator;
		this.sonars = RobotFactory.addSonarBeltSensor(this,1);
		this.frequenceTir = (int)(Math.random()*500 + 50);
		this.tourne = true;
		this.direction = 'D';
		this.tempsDebutVirage = 0;
		
		//this.setColor(COULEUR_ROBOT);
		//System.out.println("FreqTir = " + this.frequenceTir);
		
	}
	
	public int getValeurPoints() { return this.VALEUR_POINTS; }
	
	public void tirer ()
	{	
		Point3d pos = new Point3d();
		this.getCoords(pos);
		
		// Les calculs ci-dessous sont nécessaires ! Ils permettent de faire apparaître un projectile sous le robot ( et non à coté ).
		double posXCanon  = pos.getX();
		double posYCanon  = pos.getY();
		double posZCanon;
		if ( this.direction == 'D') posZCanon = pos.getZ()-2;
		else posZCanon = pos.getZ()+2;
		Vector3d posCanon = new Vector3d(posXCanon, posYCanon, posZCanon);
		
		Vector3d rot = this.instantRotation;
		double rotY = rot.getY();
		Projectile projectile = new Projectile(posCanon, rotY, COULEUR_PROJECTILES, this, this.simulateur);
		
		//System.out.println(String.format("Projectile crée en [%f,%f,%f]", posXCanon, posYCanon, posZCanon) );
		
	}
	
	public void performBehavior()
	{
		this.setTranslationalVelocity( VITESSE_DEPLACEMENT_MAX );
		
		// Le supprimer si jamais détruit
		if ( this.getCounter() >= DUREE_VIE ) this.simulateur.removeAgent(this);
		
		// Détecter si touché par projectile
		if ( anOtherAgentIsVeryNear() )
		{
			SimpleAgent agent = getVeryNearAgent();
			if ( agent instanceof Projectile )
			{
				Projectile p = (Projectile)agent;
				if ( p.getEquipe() instanceof RobotJoueur ) this.simulateur.removeAgent(this);
			}
			
		}
		
		// Cas où il fait demi-tour
		if ( this.tourne )
		{
			if ( this.direction == 'D' ) this.setRotationalVelocity(3.0);
			else this.setRotationalVelocity(-3.0);
			
			if ( ( this.tempsDebutVirage - getCounter() ) % 60 == 0 )
			{
				this.setRotationalVelocity(0.0);
				this.tourne = false;
				
				if ( this.direction == 'D' ) this.direction = 'G';
				else this.direction = 'D';
				
			}
			
		}
		// Cas normaux
		else 
		{
			//Toutes les 50 frames
			if ( getCounter() % 50 == 0)
			{
				// Vérifier la distance de l'obstacle devant
				if ( sonars.getMeasurement(0) < 5.0 ) 
				{
					this.tourne = true;
					this.tempsDebutVirage = this.getCounter();
				}
				
			}
			
		}
		
		// Action Tirer
		if ( getCounter()%this.frequenceTir == 0 ) this.tirer();
		
	}
	
}