66import java .awt .image .BufferedImage ;
77import java .io .File ;
88import java .io .IOException ;
9- import java .util .Random ;
9+ import java .util .ArrayList ;
10+
1011import javax .imageio .ImageIO ;
1112
1213public class Agent {
13- static Random rand = new Random (0 );
14-
1514 int agentX ,agentY ; //x,y coordinates in the world and rotation of the car in degrees. 0 representing right as in cartesian system(stored in velocity)
1615 int screenX , screenY ; //the x,y coordinates of the car on screen
1716 Rectangle solidArea = new Rectangle (); // aka hitbox of the car
1817 public double frictionCoefficient ; //road friction: 1 in asphalt, 0.1 in grass checked by CollisionControl class
1918 public int points , laps = 0 ;
2019 public boolean onFinishLine , offFinishLine , isCollided = false ; //flags for game logic
21- public int [] instructions = new int [ 100 ] ;
20+ public ArrayList < Integer > instructions ;
2221 int nextActionTimer ,instructionIndex ;
22+ public boolean isFinished = false ;
2323 public BufferedImage playerModel ;
2424 GameWindow gameWindow ;
2525 Velocity velocity ;
26- int counter ;
2726
2827 public Agent (GameWindow gw ){
2928 this .gameWindow = gw ;
@@ -32,12 +31,8 @@ public Agent(GameWindow gw){
3231 public void setDefault (){
3332 this .agentX =MapLoader .spawnX *gameWindow .tileSize ;
3433 this .agentY =MapLoader .spawnY *gameWindow .tileSize ;
35- this .velocity = new Velocity ();
36-
37- for (int i = 0 ; i <50 ; i ++){
38- instructions [i ] = rand .nextInt (5 );
39- }
40-
34+ this .velocity = new Velocity ();
35+ this .instructions = new ArrayList <Integer >();
4136 switch (MapLoader .spawnDirection ) {
4237 case 0 :
4338 velocity .angle = 0 ;
@@ -58,7 +53,6 @@ public void setDefault(){
5853 this .solidArea .y = 5 ;
5954 this .solidArea .width = 25 ;
6055 this .solidArea .height = 25 ;
61-
6256 try {
6357 File tmp = new File ("source/car.png" );
6458 playerModel = ImageIO .read (tmp );
@@ -70,10 +64,10 @@ public void update(){
7064 frictionCoefficient = gameWindow .collisionControl .checkCollision (this );
7165 checkLaps ();
7266 calculatePoints ();
73- if (nextActionTimer %5 == 0 && instructionIndex <instructions .length ){
67+ if (nextActionTimer %5 == 0 && instructionIndex <instructions .size ()){ //take action every 5 frames
7468 int rotationAngle ;
7569 double netVelocity = velocity .netVelocity ();
76- switch (instructions [ instructionIndex ] ) {
70+ switch (instructions . get ( instructionIndex ) ) {
7771 case 0 : //accelerate
7872 velocity .accelerate (0.4 *frictionCoefficient );
7973 break ;
@@ -107,6 +101,9 @@ public void update(){
107101 while (frictionCoefficient <1 &&velocity .netVelocity ()>10 *frictionCoefficient ){
108102 velocity .accelerate (-0.5 );
109103 }
104+ if (instructionIndex == instructions .size ()-1 ){
105+ this .isFinished = true ;
106+ }
110107 this .agentX += velocity .X ;
111108 this .agentY -= velocity .Y ;
112109 nextActionTimer ++;
@@ -159,6 +156,30 @@ public void draw(Graphics2D graphics){
159156 graphics .setTransform (oldTransform ); // restore old graphics
160157 }
161158 }
159+ public void reset (){
160+ this .isFinished = false ;
161+ this .agentX =MapLoader .spawnX *gameWindow .tileSize ;
162+ this .agentY =MapLoader .spawnY *gameWindow .tileSize ;
163+ velocity .X = 0 ;
164+ velocity .Y = 0 ;
165+ switch (MapLoader .spawnDirection ) {
166+ case 0 :
167+ velocity .angle = 0 ;
168+ break ;
169+ case 1 :
170+ velocity .angle = 90 ;
171+ break ;
172+ case 2 :
173+ velocity .angle = 180 ;
174+ break ;
175+ case 3 :
176+ velocity .angle = 270 ;
177+ break ;
178+ default :
179+ break ;
180+ }
181+
182+ }
162183 public String toString (){
163184 return this .agentX + " " + this .agentY ;
164185 }
0 commit comments