66import java .io .File ;
77import java .io .IOException ;
88import java .util .Random ;
9-
109import javax .imageio .ImageIO ;
1110
1211public class Agent {
13- public int agentX ,agentY ; //x,y coordinates and rotation of the car in degrees. 0 representing right as in cartesian system(stored in velocity)
14- public Rectangle solidArea = new Rectangle (); // aka hitbox of the car
12+ static Random rand = new Random (0 );
13+
14+ 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)
15+ int screenX , screenY ; //the x,y coordinates of the car on screen
16+ Rectangle solidArea = new Rectangle (); // aka hitbox of the car
1517 public double frictionCoefficient ; //road friction: 1 in asphalt, 0.1 in grass checked by CollisionControl class
1618 public int points , laps = 0 ;
1719 public boolean onFinishLine , offFinishLine , isCollided = false ; //flags for game logic
1820 public int [] instructions = new int [100 ];
1921 int nextActionTimer ,instructionIndex ;
20- int screenMidX , screenMidY ;
2122 public BufferedImage playerModel ;
2223 GameWindow gameWindow ;
2324 Velocity velocity ;
2425 int counter ;
2526
26-
27- Random rand = new Random (0 );
28-
2927 public Agent (GameWindow gw ){
3028 this .gameWindow = gw ;
3129 setDefault ();
@@ -34,9 +32,14 @@ public void setDefault(){
3432 this .agentX =MapLoader .spawnX *gameWindow .tileSize ;
3533 this .agentY =MapLoader .spawnY *gameWindow .tileSize ;
3634 this .velocity = new Velocity ();
35+ for (int i = 0 ; i <100 ; i ++){
36+ instructions [i ] = 4 ;
37+ }
38+ /*
3739 for(int i = 0; i<50; i++){
3840 instructions[i] = rand.nextInt(5);
3941 }
42+ */
4043 switch (MapLoader .spawnDirection ) {
4144 case 0 :
4245 velocity .angle = 0 ;
@@ -61,8 +64,6 @@ public void setDefault(){
6164 try {
6265 File tmp = new File ("source/car.png" );
6366 playerModel = ImageIO .read (tmp );
64- this .screenMidX = this .gameWindow .WIDTH /2 - (playerModel .getWidth ()/4 );
65- this .screenMidY = this .gameWindow .HEIGHT /2 -(playerModel .getHeight ()/4 );
6667 } catch (IOException e ) {
6768 e .printStackTrace ();
6869 }
@@ -141,7 +142,42 @@ public void calculatePoints(){
141142 }
142143 }
143144 public void draw (Graphics2D graphics ){
144- graphics .rotate (Math .toRadians (-velocity .angle ),agentX +playerModel .getWidth ()/4 ,agentY +playerModel .getHeight ()/4 );
145- graphics .drawImage (playerModel ,agentX ,agentY ,playerModel .getWidth ()/2 ,playerModel .getHeight ()/2 ,null );
145+ int camX = gameWindow .camera .getWorldX ();
146+ int camY = gameWindow .camera .getWorldY ();
147+ int screenMidX = gameWindow .camera .getScreenMidX ();
148+ int screenMidY = gameWindow .camera .getScreenMidY ();
149+ int tileSize = gameWindow .getTileSize ();
150+
151+ int screenX = agentX - camX + screenMidX ;
152+ int screenY = agentY - camY + screenMidY ;
153+
154+ if (agentX >camX -screenMidX -1 *tileSize &&
155+ agentX <agentX +screenMidX +3 *tileSize &&
156+ agentY >agentY -screenMidY -1 *tileSize &&
157+ agentY <agentY +screenMidY +3 *tileSize ){
158+ graphics .rotate (Math .toRadians (-velocity .angle ),screenX +playerModel .getWidth ()/4 ,screenY +playerModel .getHeight ()/4 );
159+ graphics .drawImage (playerModel ,screenX ,screenY ,playerModel .getWidth ()/2 ,playerModel .getHeight ()/2 ,null );
160+ }
161+ }
162+ public String toString (){
163+ return this .agentX + " " + this .agentY ;
164+ }
165+ public int getAgentX (){
166+ return this .agentX ;
167+ }
168+ public int getAgentY (){
169+ return this .agentY ;
170+ }
171+ public int getPoints (){
172+ return this .points ;
173+ }
174+ public int getLaps (){
175+ return this .laps ;
176+ }
177+ public Velocity getVelocity (){
178+ return this .velocity ;
179+ }
180+ public boolean getIsCollided (){
181+ return this .isCollided ;
146182 }
147183}
0 commit comments