-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGrassField.java
More file actions
70 lines (57 loc) · 1.92 KB
/
GrassField.java
File metadata and controls
70 lines (57 loc) · 1.92 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
package agh.ics.oop.model;
import agh.ics.oop.model.util.MapVisualizer;
import java.util.*;
public class GrassField extends AbstractWorldMap {
private int grass_amount;
private final Map<Vector2d, Grass> grass = new HashMap<>();
public GrassField(int grass_amount) {
this.grass_amount = grass_amount;
generatePositions();
}
public void generatePositions() {
int maxX = (int) Math.sqrt((double) grass_amount * 10);
int maxY = maxX;
Set<Vector2d> positions = new HashSet<>();
while (positions.size() < grass_amount) {
int x = (int) (Math.random() * maxX);
int y = (int) (Math.random() * maxY);
Vector2d vec = new Vector2d(x, y);
calculateMapSize(vec);
Vector2d position = new Vector2d(x, y);
positions.add(position);
}
for(Vector2d position : positions) {
grass.put(position, new Grass(position));
}
}
@Override
public boolean isOccupied(Vector2d position) {
return (super.isOccupied(position) || grass.containsKey(position));
}
@Override
public WorldElement objectAt(Vector2d position) {
if (grass.containsKey(position)) {
return grass.get(position);
}
return super.objectAt(position);
}
public Map<Vector2d, Grass> getGrass(){
return grass;
}
public Map<Vector2d, Animal> getAnimals(){
return animals;
}
@Override
public Map<Vector2d, WorldElement> getElements(){
Map<Vector2d, WorldElement> all_animals = super.getElements();
for(Map.Entry<Vector2d, Grass> entry : grass.entrySet()){
Vector2d key = entry.getKey();
WorldElement element = entry.getValue();
all_elements.put(key, element);
}
return all_elements;
}
public String getId(){
return "GrassField Map nr " + ID;
}
}