-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlant.java
149 lines (111 loc) · 2.82 KB
/
Plant.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
public class Plant extends Object {
static final int DEFAULT_ENERGY = 50, SEEDING_DISTANCE = 30,
LIFETIME = 1000, TEMP_RANGE = 20;
int energy = DEFAULT_ENERGY;
int life;
int temp;
final String DNA;
Color leafColor;
public Plant(Game game, int x, int y, String pDNA) {
super(game);
setCoordinates(x, y);
DNA = pDNA;
intializePlant();
if (!isTraversable(x, y)) {
remove();
}
}
protected void intializePlant() {
String[] strands = DNA.split(DNA_SPLIT);
int[] ints = new int[strands.length];
for (int i = 0; i < strands.length; i++) {
ints[i] = Integer.parseInt(strands[i]);
}
Random r = new Random();
life = LIFETIME + r.nextInt(LIFETIME);
temp = ints[3];
leafColor = new Color(ints[0], ints[1], ints[2]);
}
@Override
protected void action() {
if (!checkForDeath()) {
life--;
energy++;
if (life == LIFETIME / 2) {
Random r = new Random();
if (r.nextBoolean()) {
reproduce();
}
}
boolean loop = true;
while (energy > DEFAULT_ENERGY * 2 && loop) {
loop = growLeaf();
}
} else {
reproduce();
}
}
protected boolean checkForDeath() {
if (life <= 0) {
remove();
return true;
}
return false;
}
protected boolean growLeaf() {
for (int i = -1; i <= 1; i++) {
for (int i1 = -1; i1 <= 1; i1++) {
if (Game.validPos(getXCoord() + i, getYCoord() + i1)
&& game.isEmpty(getXCoord() + i, getYCoord() + i1)) {
new Food(game, getXCoord() + i, getYCoord() + i1, this);
energy -= DEFAULT_ENERGY;
return true;
}
}
}
return false;
}
protected void reproduce() {
Random r = new Random();
for (int i = 0; i < 10; i++) {
int x = r.nextInt(SEEDING_DISTANCE*2) - SEEDING_DISTANCE;
int y = r.nextInt(SEEDING_DISTANCE*2) - SEEDING_DISTANCE;
if (Game.validPos(getXCoord() + x, getYCoord() + y)
&& game.isEmpty(getXCoord() + x, getYCoord() + y)) {
new Plant(game, getXCoord() + x, getYCoord() + y, DNA);
energy -= DEFAULT_ENERGY * 5;
return;
}
}
}
protected boolean isTraversable(int x, int y) {
if (Game.validPos(x, y)) {
Terrain t = game.terrainMap[x][y];
int tTemp = t.temp;
if (tTemp >= temp - TEMP_RANGE && tTemp <= temp + TEMP_RANGE) {
//Checks that no plants are adjacent
for (int i = -1; i <= 1; i++) {
for (int i1 = -1; i1 <= 1; i1++) {
if (Game.validPos(x + i, y + i1)) {
Object obj = game.getObject(x + i, y + i1);
if (obj instanceof Plant && obj != this) {
return false;
}
}
}
}
return true;
}
}
return false;
}
@Override
protected void paint(Graphics g) {
g.setColor(new Color(193, 68, 35));
g.fillRect(getXCoord() * game.zoomLevel, getYCoord() * game.zoomLevel,
game.zoomLevel, game.zoomLevel);
}
}