|
| 1 | +package JavaRacer; |
| 2 | + |
| 3 | +import java.awt.Graphics2D; |
| 4 | +import java.awt.Rectangle; |
| 5 | +import java.awt.image.BufferedImage; |
| 6 | +import java.io.File; |
| 7 | +import java.io.IOException; |
| 8 | +import java.util.Random; |
| 9 | + |
| 10 | +import javax.imageio.ImageIO; |
| 11 | + |
| 12 | +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 |
| 15 | + public double frictionCoefficient; //road friction: 1 in asphalt, 0.1 in grass checked by CollisionControl class |
| 16 | + public int points, laps = 0; |
| 17 | + public boolean onFinishLine, offFinishLine, isCollided = false; //flags for game logic |
| 18 | + public int[] instructions = new int[100]; |
| 19 | + int nextActionTimer,instructionIndex; |
| 20 | + int screenMidX, screenMidY; |
| 21 | + public BufferedImage playerModel; |
| 22 | + GameWindow gameWindow; |
| 23 | + Velocity velocity; |
| 24 | + int counter; |
| 25 | + |
| 26 | + |
| 27 | + Random rand = new Random(0); |
| 28 | + |
| 29 | + public Agent(GameWindow gw){ |
| 30 | + this.gameWindow = gw; |
| 31 | + setDefault(); |
| 32 | + } |
| 33 | + public void setDefault(){ |
| 34 | + this.agentX=MapLoader.spawnX*gameWindow.tileSize; |
| 35 | + this.agentY=MapLoader.spawnY*gameWindow.tileSize; |
| 36 | + this.velocity = new Velocity(); |
| 37 | + for(int i = 0; i<50; i++){ |
| 38 | + instructions[i] = rand.nextInt(5); |
| 39 | + } |
| 40 | + switch (MapLoader.spawnDirection) { |
| 41 | + case 0: |
| 42 | + velocity.angle = 0; |
| 43 | + break; |
| 44 | + case 1: |
| 45 | + velocity.angle = 90; |
| 46 | + break; |
| 47 | + case 2: |
| 48 | + velocity.angle = 180; |
| 49 | + break; |
| 50 | + case 3: |
| 51 | + velocity.angle = 270; |
| 52 | + break; |
| 53 | + default: |
| 54 | + break; |
| 55 | + } |
| 56 | + this.solidArea.x = 30; |
| 57 | + this.solidArea.y = 5; |
| 58 | + this.solidArea.width = 25; |
| 59 | + this.solidArea.height = 25; |
| 60 | + |
| 61 | + try { |
| 62 | + File tmp = new File("source/car.png"); |
| 63 | + playerModel = ImageIO.read(tmp); |
| 64 | + this.screenMidX = this.gameWindow.WIDTH/2 - (playerModel.getWidth()/4); |
| 65 | + this.screenMidY = this.gameWindow.HEIGHT/2 -(playerModel.getHeight()/4); |
| 66 | + } catch (IOException e) { |
| 67 | + e.printStackTrace(); |
| 68 | + } |
| 69 | + } |
| 70 | + public void update(){ |
| 71 | + frictionCoefficient = gameWindow.collisionControl.checkCollision(this); |
| 72 | + checkLaps(); |
| 73 | + calculatePoints(); |
| 74 | + if(nextActionTimer%5 == 0 && instructionIndex<instructions.length){ |
| 75 | + int rotationAngle = 5; |
| 76 | + double netVelocity = velocity.netVelocity(); |
| 77 | + switch (instructions[instructionIndex]) { |
| 78 | + case 0: //accelerate |
| 79 | + velocity.accelerate(0.2*frictionCoefficient); |
| 80 | + break; |
| 81 | + case 1: //deaccelerate |
| 82 | + velocity.accelerate(-0.4); |
| 83 | + break; |
| 84 | + case 2: //turn left |
| 85 | + rotationAngle = 10; |
| 86 | + if (netVelocity > 0) { |
| 87 | + double scale = 1.0 / (1.0 + netVelocity * 0.1); |
| 88 | + rotationAngle *= scale; |
| 89 | + rotationAngle = Math.max(rotationAngle, 2); |
| 90 | + } |
| 91 | + velocity.rotate(rotationAngle); |
| 92 | + break; |
| 93 | + case 3: //turn right |
| 94 | + rotationAngle = -10; |
| 95 | + if (netVelocity > 0) { |
| 96 | + double scale = 1.0 / (1.0 + netVelocity * 0.1); |
| 97 | + rotationAngle *= scale; |
| 98 | + rotationAngle = Math.min(rotationAngle, -2); |
| 99 | + } |
| 100 | + velocity.rotate(rotationAngle); |
| 101 | + break; |
| 102 | + case 4: //Do nothing (keep velocity and angle) |
| 103 | + break; |
| 104 | + } |
| 105 | + instructionIndex++; |
| 106 | + } |
| 107 | + |
| 108 | + while(frictionCoefficient<1&&velocity.netVelocity()>10*frictionCoefficient){ |
| 109 | + velocity.accelerate(-0.5); |
| 110 | + } |
| 111 | + this.agentX += velocity.X; |
| 112 | + this.agentY -= velocity.Y; |
| 113 | + nextActionTimer++; |
| 114 | + } |
| 115 | + public void checkLaps(){ |
| 116 | + if(onFinishLine){ |
| 117 | + if(offFinishLine&&points>(laps*2500)+1000){ |
| 118 | + laps++; |
| 119 | + points += 5000; |
| 120 | + } |
| 121 | + offFinishLine = false; |
| 122 | + } |
| 123 | + else{ |
| 124 | + offFinishLine = true; |
| 125 | + } |
| 126 | + } |
| 127 | + public void calculatePoints(){ |
| 128 | + if(frictionCoefficient<1){ |
| 129 | + if(!isCollided){ |
| 130 | + points-=1000; |
| 131 | + isCollided = true; |
| 132 | + } |
| 133 | + points -= 2*velocity.netVelocity()/frictionCoefficient; |
| 134 | + } |
| 135 | + else{ |
| 136 | + isCollided = false; |
| 137 | + points += velocity.netVelocity(); |
| 138 | + } |
| 139 | + if(points<0){ |
| 140 | + points = 0; |
| 141 | + } |
| 142 | + } |
| 143 | + 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); |
| 146 | + } |
| 147 | +} |
0 commit comments