Skip to content

Commit 9f940a1

Browse files
committed
added freecam, changed agent draw functions
1 parent 3ffd40c commit 9f940a1

File tree

4 files changed

+133
-39
lines changed

4 files changed

+133
-39
lines changed

JavaRacer/Agent.java

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,24 @@
66
import java.io.File;
77
import java.io.IOException;
88
import java.util.Random;
9-
109
import javax.imageio.ImageIO;
1110

1211
public 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
}

JavaRacer/Camera.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package JavaRacer;
2+
3+
public class Camera {
4+
int worldX, worldY;
5+
int screenMidX, screenMidY;
6+
KeyHandler keyHandle;
7+
GameWindow gameWindow;
8+
public Camera(GameWindow gw, KeyHandler kh, int worldX, int worldY){
9+
this.gameWindow = gw;
10+
this.keyHandle = kh;
11+
this.worldX = worldX;
12+
this.worldY = worldY;
13+
this.screenMidX = gw.WIDTH/2;
14+
this.screenMidY = gw.HEIGHT/2;
15+
}
16+
public void update(){
17+
if (keyHandle.aPressed){
18+
this.worldX -=8;
19+
}
20+
else if(keyHandle.dPressed){
21+
this.worldX += 8;
22+
}
23+
else if(keyHandle.wPressed){
24+
this.worldY -=8;
25+
}
26+
else if(keyHandle.sPressed){
27+
this.worldY += 8;
28+
}
29+
}
30+
public int getWorldX(){
31+
return this.worldX;
32+
}
33+
public void setWorldX(int worldX){
34+
this.worldX = worldX;
35+
}
36+
public int getWorldY(){
37+
return this.worldY;
38+
}
39+
public void setWorldY(int worldY){
40+
this.worldY = worldY;
41+
}
42+
public int getScreenMidX(){
43+
return this.screenMidX;
44+
}
45+
public int getScreenMidY(){
46+
return this.screenMidY;
47+
}
48+
}

JavaRacer/GameWindow.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,16 @@ public class GameWindow extends JPanel implements Runnable{
1717
TileManager tileManager = new TileManager(this);
1818
Agent[] agents = new Agent[10];
1919
CollisionControl collisionControl = new CollisionControl(this);
20+
Camera camera;
2021

2122
public GameWindow(){
2223
Color backgroundColor = new Color(34, 139, 34);
2324
for(int i = 0; i<agents.length;i++){
24-
agents[i] = new Agent(this);
25+
agents[i] = new Agent(this);
26+
agents[i].agentX += i*50;
27+
agents[i].agentY += i*50;
2528
}
26-
29+
this.camera = new Camera(this,keyHandle, agents[0].agentX, agents[0].agentY);
2730
this.setBackground(backgroundColor);
2831
this.setPreferredSize(new Dimension(WIDTH, HEIGHT));
2932
this.setDoubleBuffered(true);
@@ -55,19 +58,29 @@ public void run() {
5558
}
5659

5760
public void update(){
58-
for (Agent agent : agents) {
59-
agent.update();
61+
for(int i = 0; i<agents.length;i++){
62+
agents[i].update();
6063
}
64+
camera.update();
6165
}
6266
public void paintComponent(Graphics graphics){
6367
super.paintComponent(graphics);
6468
Graphics2D graphic = (Graphics2D)graphics;
6569

66-
tileManager.draw(graphic,this.agents[0]);
70+
tileManager.draw(graphic,camera);
6771
for (Agent agent : agents) {
6872
agent.draw(graphic);
6973
}
7074

7175
graphic.dispose();
7276
}
77+
public int getTileSize(){
78+
return this.tileSize;
79+
}
80+
public int getRow(){
81+
return this.row;
82+
}
83+
public int getCollumn(){
84+
return this.collumn;
85+
}
7386
}

JavaRacer/TileManager.java

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package JavaRacer;
22

3-
import java.awt.Color;
4-
import java.awt.Font;
53
import java.awt.Graphics2D;
64
import java.io.File;
75
import java.io.IOException;
@@ -12,12 +10,16 @@ public class TileManager {
1210
GameWindow gameWindow;
1311
MapTiles[] tiles;
1412
public int[][] map;
13+
int row,collumn,tileSize;
1514

1615
public TileManager(GameWindow gw){
1716
this.gameWindow = gw;
1817
tiles = new MapTiles[9];
1918
loadTileImages();
2019
map = MapLoader.loadMap();
20+
row = gameWindow.getRow();
21+
collumn = gameWindow.getCollumn();
22+
tileSize = gameWindow.getTileSize();
2123
}
2224
public void loadTileImages(){
2325
try {
@@ -62,29 +64,24 @@ public void loadTileImages(){
6264
e.printStackTrace();
6365
}
6466
}
65-
public void draw(Graphics2D graphics2d, Agent agent){ //Draws the map around the agent
66-
for(int i = 0; i<gameWindow.row;i++){
67-
int worldY = i*gameWindow.tileSize;
68-
int screenY = worldY - agent.agentY + agent.screenMidY;
69-
for(int j = 0; j<gameWindow.collumn; j++){
70-
int worldX = j*gameWindow.tileSize;
71-
int screenX = worldX - agent.agentX + agent.screenMidX;
72-
if(worldX>agent.agentX-agent.screenMidX -1*gameWindow.tileSize&&
73-
worldX<agent.agentX+agent.screenMidX+3*gameWindow.tileSize&&
74-
worldY>agent.agentY-agent.screenMidY-1*gameWindow.tileSize&&
75-
worldY<agent.agentY+agent.screenMidY+3*gameWindow.tileSize){
76-
graphics2d.drawImage(tiles[map[i][j]].image,screenX,screenY,gameWindow.tileSize,gameWindow.tileSize,null);
67+
public void draw(Graphics2D graphics2d, Camera camera){ //Draws the map around the agent
68+
int cameraX = camera.getWorldX();
69+
int cameraY = camera.getWorldY();
70+
int screenMidX = camera.getScreenMidX();
71+
int screenMidY = camera.getScreenMidY();
72+
for(int i = 0; i<row;i++){
73+
int worldY = i*tileSize;
74+
int screenY = worldY - cameraY + screenMidY;
75+
for(int j = 0; j<collumn; j++){
76+
int worldX = j*tileSize;
77+
int screenX = worldX - cameraX + screenMidX;
78+
if(worldX>cameraX-screenMidX -1*tileSize&&
79+
worldX<cameraX+screenMidX+3*tileSize&&
80+
worldY>cameraY-screenMidY-1*tileSize&&
81+
worldY<cameraY+screenMidY+3*tileSize){
82+
graphics2d.drawImage(tiles[map[i][j]].image,screenX,screenY,tileSize,tileSize,null);
7783
}
7884
}
7985
}
80-
graphics2d.setFont(new Font("TimesRoman", Font.BOLD, 30));
81-
graphics2d.setColor(Color.BLACK);
82-
graphics2d.drawString("Points: " + Integer.toString(agent.points), 10, 30);
83-
graphics2d.drawString("Laps: " + Integer.toString(agent.laps), 10, 60);
84-
graphics2d.drawString("Speed: " + Integer.toString((int)Math.floor(agent.velocity.netVelocity())), gameWindow.tileSize*25, 30);
85-
if(agent.isCollided){
86-
graphics2d.setColor(Color.red);
87-
graphics2d.drawString("-1000", agent.screenMidX, agent.screenMidY-50);
88-
}
8986
}
9087
}

0 commit comments

Comments
 (0)