1 package org.woehlke.computer.kurzweil.tabs.simulatedevolution.model;
2
3 import lombok.EqualsAndHashCode;
4 import lombok.ToString;
5 import lombok.extern.log4j.Log4j2;
6
7 import java.util.Random;
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 @Log4j2
25 @ToString(exclude = {"random"})
26 @EqualsAndHashCode(exclude = {"random"})
27 public class Cell {
28
29
30
31
32 private CellCore cellCore;
33
34
35
36
37 private WorldPoint position;
38
39
40
41
42 private Orientation orientation;
43
44
45
46
47 private LifeCycle lifeCycle;
48
49
50
51
52 private WorldPoint max;
53
54
55
56
57 private Random random;
58
59 public Cell(WorldPoint"../../../../../../../org/woehlke/computer/kurzweil/tabs/simulatedevolution/model/WorldPoint.html#WorldPoint">WorldPoint max, WorldPoint position, Random random) {
60 this.max = new WorldPoint(max);
61 this.position = new WorldPoint(position);
62 this.random = random;
63 this.cellCore = new CellCore(random);
64 this.max.killNagative();
65 this.position.setX(random.nextInt() % max.getX());
66 this.position.setY(random.nextInt() % max.getY());
67 this.position.killNagative();
68 this.orientation = getRandomOrientation();
69 this.lifeCycle = new LifeCycle();
70 }
71
72 private Cell(int fat, CellCore rna, WorldPoint./../../../../../org/woehlke/computer/kurzweil/tabs/simulatedevolution/model/WorldPoint.html#WorldPoint">WorldPoint position, WorldPoint max, Random random) {
73 lifeCycle = new LifeCycle(fat);
74 this.max = new WorldPoint(max);
75 this.position = new WorldPoint(position);
76 this.random = random;
77 this.cellCore = rna;
78 orientation = getRandomOrientation();
79 }
80
81 private Orientation getRandomOrientation() {
82 int dnaLength = Orientation.values().length;
83 int dnaBase = random.nextInt(dnaLength);
84 if (dnaBase < 0) {
85 dnaBase *= -1;
86 }
87 return Orientation.values()[dnaBase];
88 }
89
90 private void getNextOrientation() {
91 Orientation randomOrientation = cellCore.getRandomOrientation();
92 int iOrientation = orientation.ordinal();
93 int iRandomOrientation = randomOrientation.ordinal();
94 int newOrientation = (iOrientation + iRandomOrientation) % Orientation.values().length;
95 orientation = Orientation.values()[newOrientation];
96 }
97
98
99
100
101 public void move() {
102 if(lifeCycle.move()){
103 getNextOrientation();
104 position.add(orientation.getMove());
105 position.add(max);
106 position.normalize(max);
107 }
108 }
109
110
111
112
113
114
115
116
117 public Cell performReproductionByCellDivision() {
118 CellCore rna = cellCore.performMitosis();
119 lifeCycle.haveSex();
120 Cell child = new Cell(lifeCycle.getFat(), rna, position, max, random);
121 return child;
122 }
123
124
125
126
127 public WorldPoint getPosition() {
128 return position;
129 }
130
131
132
133
134 public boolean isPregnant() {
135 return lifeCycle.isPregnant();
136 }
137
138
139
140
141
142 public void eat(int food) {
143 lifeCycle.eat(food);
144 }
145
146
147
148
149 public boolean died() {
150 return lifeCycle.isDead();
151 }
152
153
154
155
156 public LifeCycleStatus getLifeCycleStatus(){
157 return lifeCycle.getLifeCycleStatus();
158 }
159
160 }