Przeglądaj źródła

engulf: grass density

Bastien Sevajol 7 lat temu
rodzic
commit
c422c0b848

+ 7 - 2
sandbox/engulf/gui.py Wyświetl plik

@@ -27,8 +27,10 @@ class GrassLayer(GridLayerMixin, BaseMainLayer):
27 27
         super().__init__(*args, **kwargs)
28 28
         self.cells = {}
29 29
 
30
-    def born(self, grid_position):
30
+    def born(self, grid_position, opacity=100):
31 31
         grass = Sprite('resources/grass.png')
32
+        grass.rotation = randint(0, 360)
33
+        grass.opacity = opacity
32 34
         self.grid_manager.scale_sprite(grass)
33 35
         self.grid_manager.position_sprite(grass, grid_position)
34 36
         self.cells[grid_position] = grass
@@ -65,4 +67,7 @@ class Game(Gui):
65 67
                     self.main_layer.cells.born(subject.position)
66 68
                 if isinstance(subject, Grass):
67 69
                     self.positions[subject.id] = subject.position
68
-                    self.main_layer.grasses.born(subject.position)
70
+                    self.main_layer.grasses.born(
71
+                        subject.position,
72
+                        subject.density,
73
+                    )

BIN
sandbox/engulf/resources/grass.png Wyświetl plik


+ 9 - 9
sandbox/engulf/run.py Wyświetl plik

@@ -18,7 +18,7 @@ Engulf is simulation containing:
18 18
       * alone/not alone: - be alone + not alone
19 19
 
20 20
 """
21
-from random import randint
21
+from random import randint, seed
22 22
 
23 23
 from sandbox.engulf.subject import Cell, Grass, COLLECTION_GRASS
24 24
 from synergine2.core import Core
@@ -26,7 +26,7 @@ from synergine2.cycle import CycleManager
26 26
 from synergine2.terminals import TerminalManager, Terminal, TerminalPackage
27 27
 from synergine2.xyz import Simulation
28 28
 from sandbox.engulf.simulation import EngulfSubjects
29
-from synergine2.xyz_utils import get_around_positions_of
29
+from synergine2.xyz_utils import get_around_positions_of, get_distance_between_points
30 30
 
31 31
 
32 32
 class Engulf(Simulation):
@@ -98,20 +98,20 @@ def fill_with_random_grass(
98 98
             grasses.append(grass)
99 99
             subjects.append(grass)
100 100
 
101
-    for grass in subjects.simulation.collections[COLLECTION_GRASS][:]:
102
-        a = 1
101
+    for grass in grasses:
103 102
         for around in get_around_positions_of(grass.position, distance=density):
104 103
             if around not in subjects.grass_xyz:
105
-                grass = Grass(
104
+                new_grass = Grass(
106 105
                     simulation=subjects.simulation,
107 106
                     position=around,
108 107
                 )
109
-                grasses.append(grass)
110
-                subjects.append(grass)
111
-                # TODO: valeur nutritive regressive avec eloignement
108
+                distance = get_distance_between_points(around, grass.position)
109
+                new_grass.density = 100 - round((distance * 100) / 7)
110
+                subjects.append(new_grass)
112 111
 
113 112
 
114 113
 def main():
114
+    seed(0)
115 115
     simulation = Engulf()
116 116
     subjects = EngulfSubjects(simulation=simulation)
117 117
     fill_with_random_cells(
@@ -122,7 +122,7 @@ def main():
122 122
     )
123 123
     fill_with_random_grass(
124 124
         subjects,
125
-        15,
125
+        5,
126 126
         (-34, -34, 0),
127 127
         (34, 34, 0),
128 128
     )

+ 17 - 0
sandbox/engulf/subject.py Wyświetl plik

@@ -20,3 +20,20 @@ class Grass(XYZSubjectMixin, Subject):
20 20
         COLLECTION_EATABLE,
21 21
         COLLECTION_GRASS,
22 22
     ]
23
+
24
+    def __init__(self, *args, **kwargs):
25
+        super().__init__(*args, **kwargs)
26
+        self._density = 100.0
27
+
28
+    @property
29
+    def density(self) -> float:
30
+        return self._density
31
+
32
+    @density.setter
33
+    def density(self, value: float) -> None:
34
+        if value > 100:
35
+            self._density = 100
36
+        elif value < 0:
37
+            self._density = 0
38
+        else:
39
+            self._density = value

+ 1 - 4
synergine2/xyz.py Wyświetl plik

@@ -4,6 +4,7 @@ from math import acos
4 4
 
5 5
 from synergine2.simulation import SubjectMechanism, Subjects, Subject
6 6
 from synergine2.simulation import Simulation as BaseSimulation
7
+from synergine2.xyz_utils import get_distance_between_points
7 8
 
8 9
 
9 10
 """
@@ -25,10 +26,6 @@ Y
25 26
 COLLECTION_XYZ = 'COLLECTION_XYZ'
26 27
 
27 28
 
28
-def get_distance_between_points(a: tuple, b: tuple) -> float:
29
-    return abs(sqrt((b[0] - a[0]) ** 2 + (b[1] - a[1]) ** 2))
30
-
31
-
32 29
 def get_degree_from_north(a, b):
33 30
     if a == b:
34 31
         return 0

+ 5 - 0
synergine2/xyz_utils.py Wyświetl plik

@@ -1,3 +1,4 @@
1
+from math import sqrt
1 2
 import collections
2 3
 
3 4
 
@@ -198,3 +199,7 @@ def get_around_positions_of(
198 199
         positions.remove(position)
199 200
 
200 201
     return positions
202
+
203
+
204
+def get_distance_between_points(a: tuple, b: tuple) -> float:
205
+    return abs(sqrt((b[0] - a[0]) ** 2 + (b[1] - a[1]) ** 2))