View Javadoc
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   * The Cell of one Bacterium.
11   * It's state is position, orientation and LifeCycle.
12   * The Cell has a CellCore with the DNA Genome for Moving around.
13   *
14   * @see CellCore
15   * @see LifeCycle
16   * @see LifeCycleStatus
17   *
18   * © 2006 - 2008 Thomas Woehlke.
19   * http://thomas-woehlke.de/p/simulated-evolution/
20   * @author Thomas Woehlke
21   * Date: 04.02.2006
22   * Time: 19:06:43
23   */
24  @Log4j2
25  @ToString(exclude = {"random"})
26  @EqualsAndHashCode(exclude = {"random"})
27  public class Cell {
28  
29      /**
30       * Contains the DNA for Random based Moving
31       */
32      private CellCore cellCore;
33  
34      /**
35       * The Cell's state is position, orientation and LifeCycle
36       */
37      private WorldPoint position;
38  
39      /**
40       * The Cell's state is position, orientation and LifeCycle
41       */
42      private Orientation orientation;
43  
44      /**
45       * The Cell's state is position, orientation and LifeCycle
46       */
47      private LifeCycle lifeCycle;
48  
49      /**
50       * The World Dimensions in which this Cell can move.
51       */
52      private WorldPoint max;
53  
54      /**
55       * Random Generator is set from outside by Constructor.
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       * The Cell moves on the Step in a Direction choosen by Random and DNA.
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      * After performing Reproduction by Cell Division this Cell is one of the two Children this Method returns the other Child.
112      *
113      * @see CellCore#performMitosis()
114      *
115      * @return the other Child
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      * @return The new Position after the last move.
126      */
127     public WorldPoint getPosition() {
128         return position;
129     }
130 
131     /**
132      * @return true, if this Cell is able to perform Reproduction by Cell Division
133      */
134     public boolean isPregnant() {
135         return lifeCycle.isPregnant();
136     }
137 
138     /**
139      * Eat the available Food in this Position
140      * @param food the available Food in this Position
141      */
142     public void eat(int food) {
143         lifeCycle.eat(food);
144     }
145 
146     /**
147      * @return true, if this Cell died of hunger
148      */
149     public boolean died() {
150         return lifeCycle.isDead();
151     }
152 
153     /**
154      * @return the LifeCycleStatus of this Cell
155      */
156     public LifeCycleStatus getLifeCycleStatus(){
157         return lifeCycle.getLifeCycleStatus();
158     }
159 
160 }