Преглед на файлове

introduce grass into engulf

Bastien Sevajol преди 8 години
родител
ревизия
caa8a6f995
променени са 6 файла, в които са добавени 116 реда и са изтрити 28 реда
  1. 23 3
      sandbox/engulf/gui.py
  2. BIN
      sandbox/engulf/resources/cell.png
  3. BIN
      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,7 +2,7 @@ from random import randint
2 2
 
3 3
 import cocos
4 4
 from cocos.sprite import Sprite
5
-from sandbox.engulf.subject import Cell
5
+from sandbox.engulf.subject import Cell, Grass
6 6
 from synergine2.terminals import TerminalPackage
7 7
 from synergine2_cocos2d.gui import Gui, GridLayerMixin
8 8
 from synergine2_cocos2d.gui import MainLayer as BaseMainLayer
@@ -22,12 +22,29 @@ class CellsLayer(GridLayerMixin, BaseMainLayer):
22 22
         self.add(cell)
23 23
 
24 24
 
25
-class MainLayer(GridLayerMixin, BaseMainLayer):
25
+class GrassLayer(GridLayerMixin, BaseMainLayer):
26 26
     def __init__(self, *args, **kwargs):
27 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 43
         self.add(self.cells)
30 44
 
45
+        self.grasses = GrassLayer(terminal=terminal)
46
+        self.add(self.grasses)
47
+
31 48
 
32 49
 class Game(Gui):
33 50
     def __init__(self, *args, **kwargs):
@@ -46,3 +63,6 @@ class Game(Gui):
46 63
                 if isinstance(subject, Cell):
47 64
                     self.positions[subject.id] = subject.position
48 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)

BIN
sandbox/engulf/resources/cell.png Целия файл


BIN
sandbox/engulf/resources/grass.png Целия файл


+ 54 - 25
sandbox/engulf/run.py Целия файл

@@ -20,12 +20,12 @@ Engulf is simulation containing:
20 20
 """
21 21
 from random import randint
22 22
 
23
-from sandbox.engulf.subject import Cell
23
+from sandbox.engulf.subject import Cell, Grass
24 24
 from synergine2.core import Core
25 25
 from synergine2.cycle import CycleManager
26 26
 from synergine2.terminals import TerminalManager, Terminal, TerminalPackage
27 27
 from synergine2.xyz import Simulation
28
-from synergine2.xyz import XYZSubjects
28
+from sandbox.engulf.simulation import EngulfSubjects
29 29
 
30 30
 
31 31
 class Engulf(Simulation):
@@ -50,40 +50,69 @@ class GameTerminal(Terminal):
50 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 61
     while len(cells) < count:
64 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 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 102
 def main():
79 103
     simulation = Engulf()
80
-    subjects = get_random_subjects(
81
-        simulation,
104
+    subjects = EngulfSubjects(simulation=simulation)
105
+    fill_with_random_cells(
106
+        subjects,
82 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 117
     simulation.subjects = subjects
89 118
 

+ 31 - 0
sandbox/engulf/simulation.py Целия файл

@@ -0,0 +1,31 @@
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,10 +3,18 @@ from synergine2.xyz import XYZSubjectMixin
3 3
 
4 4
 COLLECTION_CELL = 'CELL'
5 5
 COLLECTION_ALIVE = 'ALIVE'
6
+COLLECTION_EATABLE = 'EATABLE'
6 7
 
7 8
 
8 9
 class Cell(XYZSubjectMixin, Subject):
9 10
     collections = [
10 11
         COLLECTION_CELL,
11 12
         COLLECTION_ALIVE,
13
+        COLLECTION_EATABLE,
14
+    ]
15
+
16
+
17
+class Grass(XYZSubjectMixin, Subject):
18
+    collections = [
19
+        COLLECTION_EATABLE,
12 20
     ]