This repository has been archived by the owner on Oct 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCity.java
75 lines (61 loc) · 2.59 KB
/
City.java
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
package ldts.dino.model.game.elements.obstacles;
import ldts.dino.gui.LanternaGUI;
import ldts.dino.model.game.elements.Element;
import ldts.dino.model.game.elements.Ground;
import ldts.dino.utils.Position;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import static java.lang.Math.max;
import static java.lang.Math.min;
public class City extends Obstacle {
private List<Building> buildings;
private final Random random;
private Position lastPosition;
public City() {
super(new Position(LanternaGUI.WIDTH, Ground.HEIGHT), 0, 0);
this.buildings = new ArrayList<>();
this.random = new Random();
this.lastPosition = new Position(0, Ground.HEIGHT);
addBuildings();
}
public void addBuildings() {
int numberOfBuildings = random.nextInt(3) + 1;
boolean isSmall = random.nextBoolean();
for (int i = 0; i < numberOfBuildings; i++) {
Building newBuilding;
if (isSmall) {
Position nextPosition = new Position(lastPosition.getX(), Ground.HEIGHT - Building.SMALL_BUILDING_HEIGHT);
newBuilding = Building.Factory.buildSmall(nextPosition);
} else {
Position nextPosition = new Position(lastPosition.getX(), Ground.HEIGHT - Building.LARGE_BUILDING_HEIGHT);
newBuilding = Building.Factory.buildLarge(nextPosition);
}
this.setWidth(this.getWidth() + newBuilding.getWidth());
this.setHeight(max(this.getHeight(), newBuilding.getHeight()));
this.buildings.add(newBuilding);
this.lastPosition = new Position(newBuilding.getPosition().getX() + newBuilding.getWidth() + 1, newBuilding.getPosition().getY());
this.getPosition().setY(min(getPosition().getY(), newBuilding.getPosition().getY()));
}
}
public List<Building> getBuildings() {
return buildings;
}
@Override
public boolean isColliding(Element element) {
Position cityPos = this.getPosition();
for(Building building : buildings) {
Position buildingPosition = building.getPosition();
Position newPos = new Position(cityPos.getX() + buildingPosition.getX(), buildingPosition.getY());
Building buildingToCollide = Building.Factory.build(newPos, building.getHeight()) ;
if(buildingToCollide.isColliding(element)) {
return true;
}
}
return false;
}
@Override
public void move() {
this.getPosition().setX(this.getPosition().getX() - getSpeed());
}
}