Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ramp #26

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open

Ramp #26

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

71 changes: 71 additions & 0 deletions BilTransport.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import java.awt.*;
import java.awt.geom.Point2D;
import java.util.ArrayDeque;
import java.util.Deque;

public class BilTransport extends Car implements HasRamp {
private Ramp ramp = new Ramp();
private boolean rampState;
private Deque<Car> flaket;
private final int MAXLOAD;

public BilTransport(){
super(2, 90, Color.red, "BilTransport");
this.flaket = new ArrayDeque<>();
this.rampState = true;
this.MAXLOAD = 5;
}

public Deque<Car> getFlak() { return flaket; }
public Ramp getRamp() { return ramp; }

@Override
public void raiseRamp() {
ramp.raiseRamp(getCurrentSpeed(), rampState);
}

@Override
public void lowerRamp() {
ramp.lowerRamp(getCurrentSpeed(), rampState);
}

public void loadCar(Car car) {
if(!ramp.getRampState() && flaket.size() <= MAXLOAD && car.getPosition().distance(getPosition()) <= 5.0) {
flaket.push(car);
updateBilTransport();
} else throw new IllegalArgumentException("Cannot load Car!");
}

public void updateBilTransport () {
for (Car c: this.flaket) {
c.getPosition().setLocation(getPosition());
}
}

public void unLoadCar() {
if(!rampState && !flaket.isEmpty()) {
flaket.removeLast();
}
}

@Override
public void move() {
if (!ramp.getRampState()) {
} else {
super.move();
updateBilTransport();
}

}

private double speedFactor() {
double trimFactor = 0.8;
return this.getCurrentSpeed() * 0.01 * trimFactor;
}
protected void incrementSpeed(double amount) {
this.setCurrentSpeed(Math.min(this.getCurrentSpeed() + speedFactor() * amount, this.getEnginePower()));
}
protected void decrementSpeed(double amount) {
this.setCurrentSpeed(Math.max(this.getCurrentSpeed() - speedFactor() * amount, 0));
}
}
123 changes: 123 additions & 0 deletions Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import java.awt.*;
import java.awt.geom.Point2D;

public abstract class Car implements Movable {
private final int nrDoors; // Number of doors on the car
private final double enginePower; // Engine power of the car
private double currentSpeed; // The current speed of the car // behövs pågrund att den behöver uppdateras i volvo, Saab
private Color color; // Color of the car
private final String modelName; // The car model name
private int direction; //
private double xPos;
private double yPos;
private Point2D.Double cordination;

public Car(int nrDoors, double enginePower, Color color, String modelName) {
this.nrDoors = nrDoors;
this.cordination = new Point2D.Double(xPos, yPos);
this.enginePower = enginePower;
this.color = color;
this.modelName = modelName;
this.direction = 0;
this.yPos = 0.0;
this.xPos = 0.0;
}

// Getters och setters
public void setXPos(double x) { cordination.setLocation(x, yPos);}
public void setYPos(double y) { cordination.setLocation(xPos, y);}
public double getxPos() {
return xPos;
}
public double getyPos() {
return yPos;
}
public Point2D.Double getPosition(){
return cordination;
}
public int getNrDoors() {return nrDoors;}
public void setCurrentSpeed(double amount) {currentSpeed = amount;}
public double getEnginePower() {
return enginePower;
}

public double getCurrentSpeed() {
return currentSpeed;
}

public Color getColor() {
return color;
}

public void setColor(Color clr) {
color = clr;
}

public void startEngine() {
currentSpeed = 0.1;
}

public void stopEngine() {
currentSpeed = 0;
}

public void setDirection(int i) {
direction = i;
}

public int getDirection() {
return direction;
}

public void move() {
switch (direction) {
case 0: // north
getPosition().setLocation(xPos,yPos + currentSpeed);
break;
case 1: // east
getPosition().setLocation(xPos + currentSpeed,yPos);
break;
case 2: // south
getPosition().setLocation(xPos,yPos - currentSpeed);
break;
case 3: // west
getPosition().setLocation(xPos - currentSpeed,yPos);
break;
default:
System.out.println("unknown direction");
break;
}
}

public void turnleft() {
direction ++;
if (direction > 4) {
direction = 0;
}
}

public void turnright() {
direction --;
if (direction < 0) {
direction = 4;
}
}

public void gas(double amount) {
if ((currentSpeed >= 0 && currentSpeed <= getEnginePower()) && (amount >= 0 && amount <= 1)) {
incrementSpeed(amount);
currentSpeed = Math.min(currentSpeed, getEnginePower());
}
this.move();
}

public void brake(double amount) {
if ((currentSpeed >= 0 && currentSpeed <= getEnginePower()) && (amount >= 0 && amount <= 1)) {
decrementSpeed(amount);
currentSpeed = Math.max(currentSpeed, 0);
}
}

protected void decrementSpeed(double amount) {};
protected void incrementSpeed(double amount) {};
}
24 changes: 24 additions & 0 deletions CarRun.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class CarRun {

public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setLocationRelativeTo(null);
frame.setTitle("2d car");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);

JPanel jpanel = new JPanel();
frame.add(jpanel);
frame.pack();
frame.setVisible(true);

jpanel.startGameThread();
}
}


Loading