瀏覽代碼

introduce grass into engulf

Bastien Sevajol 8 年之前
父節點
當前提交
caa8a6f995
共有 6 個文件被更改,包括 116 次插入28 次删除
  1. 23 3
      sandbox/engulf/gui.py
  2. 二進制
      sandbox/engulf/resources/cell.png
  3. 二進制
      sandbox/engulf/resources/grass.png
  4. 54 25
      sandbox/engulf/run.py
  5. 31 0
      sandbox/engulf/simulation.py
  6. 8 0
      sandbox/engulf/subject.py

+ 23 - 3
sandbox/engulf/gui.py 查看文件

2
 
2
 
3
 import cocos
3
 import cocos
4
 from cocos.sprite import Sprite
4
 from cocos.sprite import Sprite
5
-from sandbox.engulf.subject import Cell
5
+from sandbox.engulf.subject import Cell, Grass
6
 from synergine2.terminals import TerminalPackage
6
 from synergine2.terminals import TerminalPackage
7
 from synergine2_cocos2d.gui import Gui, GridLayerMixin
7
 from synergine2_cocos2d.gui import Gui, GridLayerMixin
8
 from synergine2_cocos2d.gui import MainLayer as BaseMainLayer
8
 from synergine2_cocos2d.gui import MainLayer as BaseMainLayer
22
         self.add(cell)
22
         self.add(cell)
23
 
23
 
24
 
24
 
25
-class MainLayer(GridLayerMixin, BaseMainLayer):
25
+class GrassLayer(GridLayerMixin, BaseMainLayer):
26
     def __init__(self, *args, **kwargs):
26
     def __init__(self, *args, **kwargs):
27
         super().__init__(*args, **kwargs)
27
         super().__init__(*args, **kwargs)
28
-        self.cells = CellsLayer(terminal=kwargs.pop('terminal'))
28
+        self.cells = {}
29
+
30
+    def born(self, grid_position):
31
+        grass = Sprite('resources/grass.png')
32
+        self.grid_manager.scale_sprite(grass)
33
+        self.grid_manager.position_sprite(grass, grid_position)
34
+        self.cells[grid_position] = grass
35
+        self.add(grass)
36
+
37
+
38
+class MainLayer(GridLayerMixin, BaseMainLayer):
39
+    def __init__(self, terminal, *args, **kwargs):
40
+        super().__init__(terminal, *args, **kwargs)
41
+
42
+        self.cells = CellsLayer(terminal=terminal)
29
         self.add(self.cells)
43
         self.add(self.cells)
30
 
44
 
45
+        self.grasses = GrassLayer(terminal=terminal)
46
+        self.add(self.grasses)
47
+
31
 
48
 
32
 class Game(Gui):
49
 class Game(Gui):
33
     def __init__(self, *args, **kwargs):
50
     def __init__(self, *args, **kwargs):
46
                 if isinstance(subject, Cell):
63
                 if isinstance(subject, Cell):
47
                     self.positions[subject.id] = subject.position
64
                     self.positions[subject.id] = subject.position
48
                     self.main_layer.cells.born(subject.position)
65
                     self.main_layer.cells.born(subject.position)
66
+                if isinstance(subject, Grass):
67
+                    self.positions[subject.id] = subject.position
68
+                    self.main_layer.grasses.born(subject.position)

二進制
sandbox/engulf/resources/cell.png 查看文件


二進制
sandbox/engulf/resources/grass.png 查看文件


+ 54 - 25
sandbox/engulf/run.py 查看文件

20
 """
20
 """
21
 from random import randint
21
 from random import randint
22
 
22
 
23
-from sandbox.engulf.subject import Cell
23
+from sandbox.engulf.subject import Cell, Grass
24
 from synergine2.core import Core
24
 from synergine2.core import Core
25
 from synergine2.cycle import CycleManager
25
 from synergine2.cycle import CycleManager
26
 from synergine2.terminals import TerminalManager, Terminal, TerminalPackage
26
 from synergine2.terminals import TerminalManager, Terminal, TerminalPackage
27
 from synergine2.xyz import Simulation
27
 from synergine2.xyz import Simulation
28
-from synergine2.xyz import XYZSubjects
28
+from sandbox.engulf.simulation import EngulfSubjects
29
 
29
 
30
 
30
 
31
 class Engulf(Simulation):
31
 class Engulf(Simulation):
50
         self.gui.run()
50
         self.gui.run()
51
 
51
 
52
 
52
 
53
-def get_random_subjects(
54
-        simulation: Simulation,
55
-        count: int,
56
-        x_min: int,
57
-        y_min: int,
58
-        x_max: int,
59
-        y_max: int,
60
-) -> [Cell]:
61
-    cells = XYZSubjects(simulation=simulation)
53
+def fill_with_random_cells(
54
+    subjects: EngulfSubjects,
55
+    count: int,
56
+    start_position: tuple,
57
+    end_position: tuple,
58
+) -> None:
59
+    cells = []
62
 
60
 
63
     while len(cells) < count:
61
     while len(cells) < count:
64
         position = (
62
         position = (
65
-            randint(x_min, x_max+1),
66
-            randint(y_min, y_max+1),
67
-            0
63
+            randint(start_position[0], end_position[0]+1),
64
+            randint(start_position[1], end_position[1]+1),
65
+            randint(start_position[2], end_position[2]+1),
68
         )
66
         )
69
-        if position not in cells.xyz:
70
-            cells.append(Cell(
71
-                simulation=simulation,
67
+        if position not in subjects.cell_xyz:
68
+            cell = Cell(
69
+                simulation=subjects.simulation,
72
                 position=position,
70
                 position=position,
73
-            ))
71
+            )
72
+            cells.append(cell)
73
+            subjects.append(cell)
74
 
74
 
75
-    return cells
75
+
76
+def fill_with_random_grass(
77
+    subjects: EngulfSubjects,
78
+    start_count: int,
79
+    start_position: tuple,
80
+    end_position: tuple,
81
+    density: int=10,
82
+) -> None:
83
+    grasses = []
84
+
85
+    while len(grasses) < start_count:
86
+        position = (
87
+            randint(start_position[0], end_position[0]+1),
88
+            randint(start_position[1], end_position[1]+1),
89
+            randint(start_position[2], end_position[2]+1),
90
+        )
91
+        if position not in subjects.grass_xyz:
92
+            grass = Grass(
93
+                simulation=subjects.simulation,
94
+                position=position,
95
+            )
96
+            grasses.append(grass)
97
+            subjects.append(grass)
98
+
99
+        # TODO: density
76
 
100
 
77
 
101
 
78
 def main():
102
 def main():
79
     simulation = Engulf()
103
     simulation = Engulf()
80
-    subjects = get_random_subjects(
81
-        simulation,
104
+    subjects = EngulfSubjects(simulation=simulation)
105
+    fill_with_random_cells(
106
+        subjects,
82
         30,
107
         30,
83
-        -34,
84
-        -34,
85
-        34,
86
-        34,
108
+        (-34, -34, 0),
109
+        (34, 34, 0),
110
+    )
111
+    fill_with_random_grass(
112
+        subjects,
113
+        15,
114
+        (-34, -34, 0),
115
+        (34, 34, 0),
87
     )
116
     )
88
     simulation.subjects = subjects
117
     simulation.subjects = subjects
89
 
118
 

+ 31 - 0
sandbox/engulf/simulation.py 查看文件

1
+from sandbox.engulf.subject import Cell, Grass
2
+from synergine2.xyz import XYZSubjects, XYZSubjectMixin
3
+
4
+__author__ = 'bux'
5
+
6
+
7
+class EngulfSubjects(XYZSubjects):
8
+    def __init__(self, *args, **kwargs):
9
+        super().__init__(*args, **kwargs)
10
+        # TODO: accept multiple subjects as same position
11
+        # TODO: init xyz with given list
12
+        self.cell_xyz = {}
13
+        self.grass_xyz = {}
14
+
15
+    def remove(self, value: XYZSubjectMixin):
16
+        super().remove(value)
17
+
18
+        if isinstance(value, Cell):
19
+            del self.cell_xyz[value.position]
20
+
21
+        if isinstance(value, Grass):
22
+            del self.grass_xyz[value.position]
23
+
24
+    def append(self, p_object: XYZSubjectMixin):
25
+        super().append(p_object)
26
+
27
+        if isinstance(p_object, Cell):
28
+            self.cell_xyz[p_object.position] = p_object
29
+
30
+        if isinstance(p_object, Grass):
31
+            self.grass_xyz[p_object.position] = p_object

+ 8 - 0
sandbox/engulf/subject.py 查看文件

3
 
3
 
4
 COLLECTION_CELL = 'CELL'
4
 COLLECTION_CELL = 'CELL'
5
 COLLECTION_ALIVE = 'ALIVE'
5
 COLLECTION_ALIVE = 'ALIVE'
6
+COLLECTION_EATABLE = 'EATABLE'
6
 
7
 
7
 
8
 
8
 class Cell(XYZSubjectMixin, Subject):
9
 class Cell(XYZSubjectMixin, Subject):
9
     collections = [
10
     collections = [
10
         COLLECTION_CELL,
11
         COLLECTION_CELL,
11
         COLLECTION_ALIVE,
12
         COLLECTION_ALIVE,
13
+        COLLECTION_EATABLE,
14
+    ]
15
+
16
+
17
+class Grass(XYZSubjectMixin, Subject):
18
+    collections = [
19
+        COLLECTION_EATABLE,
12
     ]
20
     ]