View Javadoc
1   package org.woehlke.simulation.evolution.model;
2   
3   import java.util.Random;
4   
5   /**
6    * (C) 2006 - 2008 Thomas Woehlke.
7    * http://thomas-woehlke.de/p/simulated-evolution/
8    * @author Thomas Woehlke
9    * Date: 04.02.2006
10   * Time: 19:06:43
11   */
12  public class Cell {
13  
14      private Point position;
15      private Dna orientation;
16      private Point max;
17      private Random random;
18      private CellCore core;
19  
20      private LifeCycle lifeCycle;
21  
22      public Cell(Point max, Point position, Random random) {
23          this.max = new Point(max);
24          this.position = new Point(position);
25          this.random = random;
26          this.core = new CellCore(random);
27          this.max.killNagative();
28          this.position.setX(random.nextInt() % max.getX());
29          this.position.setY(random.nextInt() % max.getY());
30          this.position.killNagative();
31          this.orientation = getRandomOrientation();
32          this.lifeCycle = new LifeCycle();
33      }
34  
35      private Cell(int fat, CellCore rna, Point position, Point max, Random random) {
36          lifeCycle = new LifeCycle(fat);
37          this.max = new Point(max);
38          this.position = new Point(position);
39          this.random = random;
40          this.core = rna;
41          orientation = getRandomOrientation();
42      }
43  
44      private Dna getRandomOrientation() {
45          int dnaLength = Dna.values().length;
46          int dnaBase = random.nextInt(dnaLength);
47          if (dnaBase < 0) {
48              dnaBase *= -1;
49          }
50          return Dna.values()[dnaBase];
51      }
52  
53      private void getNextOrientation() {
54          Dna randomOrientation = core.getRandomOrientation();
55          int iOrientation = orientation.ordinal();
56          int iRandomOrientation = randomOrientation.ordinal();
57          int newOrientation = (iOrientation + iRandomOrientation) % Dna.values().length;
58          orientation = Dna.values()[newOrientation];
59      }
60  
61      public void move() {
62          if(lifeCycle.move()){
63              getNextOrientation();
64              position.add(orientation.getMove());
65              position.add(max);
66              position.normalize(max);
67          }
68      }
69  
70      public Cell cellDivisionFactory() {
71          CellCore rna = core.mitosisFactory();
72          lifeCycle.haveSex();
73          Cell child = new Cell(lifeCycle.getFat(), rna, position, max, random);
74          return child;
75      }
76  
77      public Point getPosition() {
78          return position;
79      }
80  
81      public boolean isPregnant() {
82          return lifeCycle.isPregnant();
83      }
84  
85      public void eat(int food) {
86          lifeCycle.eat(food);
87      }
88  
89      public boolean died() {
90          return lifeCycle.isDead();
91      }
92  
93      public LifeCycleStatus getLifeCycleStatus(){
94          return lifeCycle.getLifeCycleStatus();
95      }
96  
97  }