1 package org.woehlke.simulation.evolution.dom;
2
3 import org.woehlke.simulation.evolution.beans.SimGenPoint;
4
5 import java.util.Random;
6
7
8
9
10
11
12
13
14 public class SimGenCell implements ISimGenCell {
15 private SimGenPoint pos;
16 private SimGenPoint max;
17 private Random random;
18 private ISimGenCore core;
19 private ESimGenDna orientation;
20
21 private ISimGenLifeCycle lifeCycle;
22
23 public SimGenCell(SimGenPoint max, SimGenPoint pos, Random random) {
24 this.max = new SimGenPoint(max);
25 this.pos = new SimGenPoint(pos);
26
27
28 this.random = random;
29 this.core = new SimGenCore(random);
30 this.max.killNagative();
31 this.pos.setX(random.nextInt() % max.getX());
32 this.pos.setY(random.nextInt() % max.getY());
33 this.pos.killNagative();
34 this.orientation = getRandomOrientation();
35 this.lifeCycle = new SimGenLifeCycle();
36 }
37
38 private ESimGenDna getRandomOrientation() {
39 int dnaLength = ESimGenDna.values().length;
40 int dnaBase = random.nextInt(dnaLength);
41 if (dnaBase < 0) {
42 dnaBase *= -1;
43 }
44 return ESimGenDna.values()[dnaBase];
45 }
46
47 private void getNextOrientation() {
48 ESimGenDna randomOrientation = core.getRandomOrientation();
49 int iOrientation = orientation.ordinal();
50 int iRandomOrientation = randomOrientation.ordinal();
51 int newOrientation = (iOrientation + iRandomOrientation) % ESimGenDna.values().length;
52 orientation = ESimGenDna.values()[newOrientation];
53 }
54
55 public void move() {
56
57
58 lifeCycle.move();
59 getNextOrientation();
60 SimGenPoint move = new SimGenPoint(0, 0);
61 switch (orientation) {
62 case FORWARD:
63 move = new SimGenPoint(0, 2);
64 break;
65 case RIGHT:
66 move = new SimGenPoint(2, 1);
67 break;
68 case RIGHT_RIGHT:
69 move = new SimGenPoint(2, -1);
70 break;
71 case BACKWARDS:
72 move = new SimGenPoint(0, -2);
73 break;
74 case LEFT_LEFT:
75 move = new SimGenPoint(-2, -1);
76 break;
77 case LEFT:
78 move = new SimGenPoint(-2, 1);
79 break;
80 }
81 pos.add(move);
82 pos.add(max);
83 pos.normalize(max);
84
85
86
87
88
89 }
90
91 public ISimGenCell cellDivisionFactory() {
92 ISimGenCore rna = core.mitosisFactory();
93 lifeCycle.haveSex();
94 ISimGenCell child = new SimGenCell(lifeCycle.getFat(), rna, pos, max, random);
95 return child;
96 }
97
98 private SimGenCell(int fat, ISimGenCore rna, SimGenPoint pos, SimGenPoint max, Random random) {
99 lifeCycle = new SimGenLifeCycle(fat);
100 this.max = new SimGenPoint(max);
101 this.pos = new SimGenPoint(pos);
102 this.random = random;
103 this.core = rna;
104 orientation = getRandomOrientation();
105 }
106
107 public SimGenPoint getPos() {
108 return pos;
109 }
110
111 public boolean isPregnant() {
112 return lifeCycle.isPregnant();
113 }
114
115 public void eat() {
116 lifeCycle.eat();
117 }
118
119 public void died() {
120 lifeCycle.isDead();
121 }
122
123 public SimGenPoint getMax() {
124 return max;
125 }
126
127 public void setMax(SimGenPoint max) {
128 this.max = max;
129 }
130
131 }