-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMap.java
118 lines (89 loc) · 2.48 KB
/
Map.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import processing.core.PConstants;
import processing.core.PGraphics;
public class Map {
int width;
int height;
int time = 0;
Main main;
MapLayer foodLayer;
CreatureLayer creatureLayer;
public Map( Main m, int w, int h ) {
this.main = m;
this.width = w;
this.height = h;
foodLayer = new MapLayer(this, 110,30);
creatureLayer = new CreatureLayer(this, 350, 100);
}
public void Tick() {
time++;
foodLayer.Tick();
creatureLayer.Tick();
creatureLayer.checkMating();
for( int i = 0; i < Settings.foodSpawnsPerTick; i++ ) {
foodLayer.addEntity(new FoodItem((int)(main.random(1)*256)));
}
}
public PGraphics drawMap(PGraphics graphics ) {
float cellWidth = (float)graphics.width / width;
float cellHeight = (float)graphics.height / height;
graphics.beginDraw();
graphics.background(0);
graphics.colorMode(PConstants.HSB, 255);
graphics.noStroke();
for ( MapEntity e : foodLayer.entityList ) {
graphics.fill(foodLayer.layerDrawingHue, foodLayer.getLayerDrawingSaturation, e.getDrawingValue() * 255);
graphics.rect(e.positionX * cellWidth, e.positionY * cellHeight, cellWidth, cellHeight);
}
for ( MapEntity e : creatureLayer.entityList ) {
graphics.stroke(creatureLayer.layerDrawingHue, creatureLayer.getLayerDrawingSaturation, e.getDrawingValue() * 255);
graphics.fill(creatureLayer.layerDrawingHue, creatureLayer.getLayerDrawingSaturation, e.getDrawingValue() * 255);
graphics.rect(e.positionX * cellWidth, e.positionY * cellHeight, cellWidth*2, cellHeight*2);
}
graphics.endDraw();
return graphics;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public float eatAt(int posX, int posY) {
posX = wrapX(posX);
posY = wrapY(posY);
float energy = 0;
if(foodLayer.entities[posX][posY] != null) {
FoodItem food = (FoodItem)foodLayer.entities[posX][posY];
energy = food.getEnergy();
foodLayer.removeEntity(food);
}
return energy;
}
public float getFoodAt(int x, int y) {
x = wrapX(x);
y = wrapY(y);
if(foodLayer.entities[x][y] != null) {
return ((FoodItem)foodLayer.entities[x][y]).getEnergy();
}
else return 0;
}
public int wrapX(int i) {
int newX = i % width;
// Handle wrapping for negative coordinates
if (newX < 0) {
newX += width;
}
return newX;
}
public int wrapY(int i) {
int newY = i % height;
// Handle wrapping for negative coordinates
if (newY < 0) {
newY += height;
}
return newY;
}
public int getTime() {
return time;
}
}