-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAgent.java
More file actions
200 lines (192 loc) · 6.67 KB
/
Agent.java
File metadata and controls
200 lines (192 loc) · 6.67 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package JavaRacer;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import javax.imageio.ImageIO;
public class Agent {
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)
int screenX, screenY; //the x,y coordinates of the car on screen
Rectangle solidArea = new Rectangle(); // aka hitbox of the car
public double frictionCoefficient; //road friction: 1 in asphalt, 0.1 in grass checked by CollisionControl class
public int laps = 0;
public double points = 0;
public boolean onFinishLine, offFinishLine= false; //flags for game logic
public ArrayList<Integer> instructions;
int nextActionTimer,instructionIndex;
public boolean isFinished = false;
public BufferedImage playerModel;
GameWindow gameWindow;
Velocity velocity;
public Agent(GameWindow gw){
this.gameWindow = gw;
setDefault();
}
public void setDefault(){
this.agentX=MapLoader.spawnX*gameWindow.tileSize;
this.agentY=MapLoader.spawnY*gameWindow.tileSize;
this.velocity = new Velocity();
this.instructions = new ArrayList<Integer>();
switch (MapLoader.spawnDirection) {
case 0:
velocity.angle = 0;
break;
case 1:
velocity.angle = 90;
break;
case 2:
velocity.angle = 180;
break;
case 3:
velocity.angle = 270;
break;
default:
break;
}
this.solidArea.x = 30;
this.solidArea.y = 5;
this.solidArea.width = 25;
this.solidArea.height = 25;
try {
File tmp = new File("source/car.png");
playerModel = ImageIO.read(tmp);
} catch (IOException e) {
e.printStackTrace();
}
}
public void update(){
frictionCoefficient = gameWindow.collisionControl.checkCollision(this);
checkLaps();
calculatePoints();
if(nextActionTimer%5 == 0 && instructionIndex<instructions.size()){ //take action every 5 frames
int rotationAngle;
double netVelocity = velocity.netVelocity();
switch (instructions.get(instructionIndex)) {
case 0: //accelerate
velocity.accelerate(0.4*frictionCoefficient);
break;
case 1: //deaccelerate
velocity.accelerate(-0.5);
break;
case 2: //turn left
rotationAngle = 10;
if (netVelocity > 0) {
double scale = 1.0 / (1.0 + netVelocity * 0.1);
rotationAngle *= scale;
rotationAngle = Math.max(rotationAngle, 2);
}
velocity.rotate(rotationAngle);
break;
case 3: //turn right
rotationAngle = -10;
if (netVelocity > 0) {
double scale = 1.0 / (1.0 + netVelocity * 0.1);
rotationAngle *= scale;
rotationAngle = Math.min(rotationAngle, -2);
}
velocity.rotate(rotationAngle);
break;
case 4: //Do nothing (keep velocity and angle)
break;
}
instructionIndex++;
}
while(frictionCoefficient<1&&velocity.netVelocity()>10*frictionCoefficient){
velocity.accelerate(-0.5);
}
if(instructionIndex == instructions.size()-1){
this.isFinished = true;
}
this.agentX += velocity.X;
this.agentY -= velocity.Y;
nextActionTimer++;
}
public void checkLaps(){
if(onFinishLine){
if(offFinishLine&&points>(laps*2500)+1000){
laps++;
points += 5000;
}
offFinishLine = false;
}
else{
offFinishLine = true;
}
}
public void calculatePoints(){
if(frictionCoefficient<1){
points -= (velocity.netVelocity()/frictionCoefficient)/10;
}
else{
points += velocity.netVelocity()/10;
}
if(points<0){
points = 0;
}
}
public void draw(Graphics2D graphics){
int camX = gameWindow.camera.getWorldX();
int camY = gameWindow.camera.getWorldY();
int screenMidX = gameWindow.camera.getScreenMidX();
int screenMidY = gameWindow.camera.getScreenMidY();
int tileSize = gameWindow.getTileSize();
int screenX = agentX - camX + screenMidX;
int screenY = agentY - camY + screenMidY;
if(agentX>camX-screenMidX -1*tileSize&&
agentX<agentX+screenMidX+3*tileSize&&
agentY>agentY-screenMidY-1*tileSize&&
agentY<agentY+screenMidY+3*tileSize){
AffineTransform oldTransform = graphics.getTransform(); // save old graphics orientation
graphics.rotate(Math.toRadians(-velocity.angle),screenX+playerModel.getWidth()/4,screenY+playerModel.getHeight()/4);
graphics.drawImage(playerModel,screenX,screenY,playerModel.getWidth()/2,playerModel.getHeight()/2,null);
graphics.setTransform(oldTransform); // restore old graphics
}
}
public void reset(){
this.isFinished = false;
this.instructionIndex = 0;
this.points = 0;
this.laps = 0;
this.agentX=MapLoader.spawnX*gameWindow.tileSize;
this.agentY=MapLoader.spawnY*gameWindow.tileSize;
velocity.X = 0;
velocity.Y = 0;
switch (MapLoader.spawnDirection) {
case 0:
velocity.angle = 0;
break;
case 1:
velocity.angle = 90;
break;
case 2:
velocity.angle = 180;
break;
case 3:
velocity.angle = 270;
break;
default:
break;
}
}
public String toString(){
return this.agentX + " " + this.agentY;
}
public int getAgentX(){
return this.agentX;
}
public int getAgentY(){
return this.agentY;
}
public double getPoints(){
return this.points;
}
public int getLaps(){
return this.laps;
}
public Velocity getVelocity(){
return this.velocity;
}
}