Browse Source

refact image and texture constructions

Bastien Sevajol 6 years ago
parent
commit
f9f646bc86

BIN
medias/images/empty.png View File


BIN
medias/images/unknown.png View File


+ 1 - 1
opencombat/exception.py View File

@@ -9,5 +9,5 @@ class UnknownWeapon(OpenCombatException):
9 9
     pass
10 10
 
11 11
 
12
-class UnknownAnimationIndex(OpenCombatException):
12
+class UnknownFiringAnimation(OpenCombatException):
13 13
     pass

+ 80 - 56
opencombat/gui/actor.py View File

@@ -8,12 +8,14 @@ from PIL import Image
8 8
 from synergine2.config import Config
9 9
 from synergine2.simulation import Subject
10 10
 from synergine2_cocos2d.actor import Actor
11
+from synergine2_xyz.exception import UnknownAnimationIndex
11 12
 
12
-from opencombat.exception import UnknownWeapon, UnknownAnimationIndex
13
+from opencombat.exception import UnknownWeapon
14
+from opencombat.exception import UnknownFiringAnimation
13 15
 from opencombat.gui.animation import ANIMATION_CRAWL
14 16
 from opencombat.gui.animation import ANIMATION_WALK
15
-from opencombat.gui.const import POSITION_MAN_STAND_UP
16
-from opencombat.gui.const import POSITION_MAN_CRAWLING
17
+from opencombat.gui.const import MODE_MAN_STAND_UP
18
+from opencombat.gui.const import MODE_MAN_CRAWLING
17 19
 from opencombat.gui.image import TileImageCacheManager
18 20
 from opencombat.gui.weapon import RIFFLE
19 21
 from opencombat.gui.weapon import WeaponImageApplier
@@ -22,11 +24,22 @@ if typing.TYPE_CHECKING:
22 24
     from opencombat.gui.fire import GuiFiringEvent
23 25
 
24 26
 
27
+MODE_DEFAULT = 'MODE_DEFAULT'
28
+
29
+
25 30
 class BaseActor(Actor):
26 31
     position_matching = {
27
-        ANIMATION_WALK: POSITION_MAN_STAND_UP,
28
-        ANIMATION_CRAWL: POSITION_MAN_CRAWLING,
32
+        ANIMATION_WALK: MODE_MAN_STAND_UP,
33
+        ANIMATION_CRAWL: MODE_MAN_CRAWLING,
29 34
     }
35
+    mode_image_paths = {
36
+        MODE_DEFAULT: 'unknown.png',
37
+    }
38
+    modes = [
39
+        MODE_DEFAULT,
40
+    ]
41
+    weapons_firing_image_scheme = {}
42
+    weapon_image_scheme = {}
30 43
 
31 44
     def __init__(
32 45
         self,
@@ -34,20 +47,30 @@ class BaseActor(Actor):
34 47
         config: Config,
35 48
         subject: Subject,
36 49
     ) -> None:
37
-        self._mode = POSITION_MAN_STAND_UP
50
+        self._mode = MODE_MAN_STAND_UP
38 51
         self.weapon_image_applier = WeaponImageApplier(config, self)
39 52
         super().__init__(image_path, subject=subject, config=config)
40 53
 
41 54
         # Firing
42 55
         self.last_firing_time = 0
43 56
         self.firing_change_image_gap = 0.05  # seconds
44
-        self.firing_images_cache = {}  # type: TODO
45 57
 
46 58
     def get_image_cache_manager(self) -> TileImageCacheManager:
47 59
         return TileImageCacheManager(self, self.config)
48 60
 
61
+    def get_default_mode(self) -> str:
62
+        return MODE_DEFAULT
63
+
64
+    def get_mode_image_path(self, mode: str) -> str:
65
+        return self.mode_image_paths[mode]
66
+
67
+    def get_modes(self) -> typing.List[str]:
68
+        return self.modes
69
+
49 70
     @property
50 71
     def mode(self) -> str:
72
+        # FIXME: When man is moving (crawling for example), must change mode
73
+        # and man must stay "crawled"
51 74
         return self._mode
52 75
 
53 76
     @property
@@ -86,53 +109,6 @@ class BaseActor(Actor):
86 109
         except UnknownWeapon:
87 110
             return []
88 111
 
89
-    def get_modes(self) -> typing.List[str]:
90
-        return [POSITION_MAN_STAND_UP]
91
-
92
-    # def build_firing_images(self) -> None:
93
-    #     cache_dir = self.config.resolve('global.cache_dir_path')
94
-    #     for mode in self.get_modes():
95
-    #         for weapon in self.weapons:
96
-    #             images = self.weapon_image_applier.get_firing_image(
97
-    #                 mode=mode,
98
-    #                 weapon_type=weapon,
99
-    #             )
100
-    #
101
-    #             for position in range(len(images)):
102
-    #                 pass
103
-    #
104
-    #
105
-    #     for animation_name, animation_image_paths in self.animation_image_paths.items():
106
-    #         self.animation_images[animation_name] = []
107
-    #         for i, animation_image_path in enumerate(animation_image_paths):
108
-    #             final_image_path = self.path_manager.path(animation_image_path)
109
-    #             final_image = Image.open(final_image_path)
110
-    #
111
-    #             for appliable_image in self.get_animation_appliable_images(
112
-    #                 animation_name,
113
-    #                 i,
114
-    #             ):
115
-    #                 final_image.paste(
116
-    #                     appliable_image,
117
-    #                     (0, 0),
118
-    #                     appliable_image,
119
-    #                 )
120
-    #
121
-    #             # FIXME NOW: nom des image utilise au dessus
122
-    #             final_name = '_'.join([
123
-    #                 str(subject_id),
124
-    #                 ntpath.basename(final_image_path),
125
-    #             ])
126
-    #             final_path = os.path.join(cache_dir, final_name)
127
-    #
128
-    #             final_image.save(final_path)
129
-    #
130
-    #             self.animation_images[animation_name].append(
131
-    #                 pyglet.image.load(
132
-    #                     final_path,
133
-    #                 )
134
-    #             )
135
-
136 112
     def firing(self, firing: 'GuiFiringEvent') -> None:
137 113
         # FIXME: move some code ?
138 114
         now = time.time()
@@ -141,18 +117,27 @@ class BaseActor(Actor):
141 117
             firing.increment_animation_index()
142 118
 
143 119
             try:
144
-                image = self.image_cache.firing_cache.get(
120
+                image = self.image_cache_manager.firing_cache.get(
145 121
                     mode=self.mode,
146 122
                     weapon=firing.weapon,
147 123
                     position=firing.animation_index,
148 124
                 )
149 125
             except UnknownAnimationIndex:
150
-                image = self.image_cache.firing_cache.get(
126
+                image = self.image_cache_manager.firing_cache.get(
151 127
                     mode=self.mode,
152 128
                     weapon=firing.weapon,
153 129
                     position=0,
154 130
                 )
155 131
                 firing.reset_animation_index()
132
+            except UnknownFiringAnimation as exc:
133
+                self.logger.error(
134
+                    'No firing animation for actor {}({}): {}'.format(
135
+                        self.__class__.__name__,
136
+                        str(self.subject.id),
137
+                        str(exc),
138
+                    )
139
+                )
140
+                return  # There is no firing animation defined
156 141
 
157 142
             # FIXME cache: prepare before firing
158 143
             import uuid
@@ -182,6 +167,39 @@ class Man(BaseActor):
182 167
             'actors/man_c4.png',
183 168
         ]
184 169
     }
170
+    modes = [
171
+        MODE_MAN_STAND_UP,
172
+        MODE_MAN_CRAWLING,
173
+    ]
174
+    mode_image_paths = {
175
+        MODE_MAN_STAND_UP: 'actors/man.png',
176
+        MODE_MAN_CRAWLING: 'actors/man_c1.png',
177
+    }
178
+    weapon_image_scheme = {
179
+        MODE_MAN_STAND_UP: {
180
+            RIFFLE: [
181
+                'actors/man_weap1.png'
182
+            ],
183
+        },
184
+        MODE_MAN_CRAWLING: {
185
+            RIFFLE: [
186
+                'actors/man_c1_weap1.png',
187
+                'actors/man_c2_weap1.png',
188
+                'actors/man_c3_weap1.png',
189
+                'actors/man_c4_weap1.png',
190
+            ],
191
+
192
+        }
193
+    }
194
+    weapons_firing_image_scheme = {
195
+        MODE_MAN_STAND_UP: {
196
+            RIFFLE: [
197
+                'actors/man_weap1_firing1.png',
198
+                'actors/man_weap1_firing2.png',
199
+                'actors/man_weap1_firing3.png',
200
+            ],
201
+        },
202
+    }
185 203
 
186 204
     def __init__(
187 205
         self,
@@ -195,6 +213,9 @@ class Man(BaseActor):
195 213
         # TODO BS 2018-01-26: Will be managed by complex part of code
196 214
         return [RIFFLE]
197 215
 
216
+    def get_default_mode(self) -> str:
217
+        return MODE_MAN_STAND_UP
218
+
198 219
 
199 220
 class HeavyVehicle(BaseActor):
200 221
     animation_image_paths = {
@@ -205,6 +226,9 @@ class HeavyVehicle(BaseActor):
205 226
             'actors/tank1.png',
206 227
         ]
207 228
     }
229
+    mode_image_paths = {
230
+        MODE_DEFAULT: 'actors/tank1.png',
231
+    }
208 232
 
209 233
     def __init__(
210 234
         self,

+ 2 - 2
opencombat/gui/const.py View File

@@ -1,7 +1,7 @@
1 1
 # coding: utf-8
2 2
 
3
-POSITION_MAN_STAND_UP = 'POSITION_MAN_STAND_UP'
4
-POSITION_MAN_CRAWLING = 'POSITION_MAN_CRAWLING'
3
+MODE_MAN_STAND_UP = 'MODE_MAN_STAND_UP'
4
+MODE_MAN_CRAWLING = 'MODE_MAN_CRAWLING'
5 5
 FLAG_DE = 'DE'
6 6
 FLAG_COLORS = {
7 7
     FLAG_DE

+ 16 - 6
opencombat/gui/image.py View File

@@ -6,8 +6,10 @@ from synergine2.config import Config
6 6
 from synergine2_cocos2d.util import PathManager
7 7
 from synergine2_xyz.image import ImageCache
8 8
 from synergine2_xyz.image import ImageCacheManager
9
+from synergine2_xyz.exception import UnknownAnimationIndex
9 10
 
10
-from opencombat.exception import UnknownAnimationIndex
11
+from opencombat.exception import UnknownWeapon
12
+from opencombat.exception import UnknownFiringAnimation
11 13
 
12 14
 if typing.TYPE_CHECKING:
13 15
     from opencombat.gui.actor import BaseActor
@@ -31,7 +33,12 @@ class FiringImageCache(ImageCache):
31 33
         try:
32 34
             return self.cache[mode][weapon][position]
33 35
         except KeyError:
34
-            raise Exception('TODO')
36
+            raise UnknownFiringAnimation(
37
+                'Unknown firing animation for mode "{}" and weapon "{}"'.format(
38
+                    mode,
39
+                    weapon,
40
+                )
41
+            )
35 42
         except IndexError:
36 43
             raise UnknownAnimationIndex(
37 44
                 'Unknown animation index "{}" for mode "{}" and weapon "{}"'.format(
@@ -66,10 +73,13 @@ class TileImageCacheManager(ImageCacheManager):
66 73
             mode_image = Image.open(self.path_manager.path(mode_image_path))
67 74
 
68 75
             for weapon in self.actor.weapons:
69
-                images = self.actor.weapon_image_applier.get_firing_image(
70
-                    mode=mode,
71
-                    weapon_type=weapon,
72
-                )
76
+                try:
77
+                    images = self.actor.weapon_image_applier.get_firing_image(
78
+                        mode=mode,
79
+                        weapon_type=weapon,
80
+                    )
81
+                except UnknownWeapon:
82
+                    images = [Image.open(self.path_manager.path('empty.png'))]
73 83
 
74 84
                 for position in range(len(images)):
75 85
                     position_image = images[position]

+ 4 - 27
opencombat/gui/weapon.py View File

@@ -8,8 +8,8 @@ from synergine2.config import Config
8 8
 from synergine2_cocos2d.util import PathManager
9 9
 
10 10
 from opencombat.exception import UnknownWeapon
11
-from opencombat.gui.const import POSITION_MAN_STAND_UP
12
-from opencombat.gui.const import POSITION_MAN_CRAWLING
11
+from opencombat.gui.const import MODE_MAN_STAND_UP
12
+from opencombat.gui.const import MODE_MAN_CRAWLING
13 13
 
14 14
 if typing.TYPE_CHECKING:
15 15
     from opencombat.gui.actor import BaseActor
@@ -35,33 +35,10 @@ class WeaponImageApplier(ImageApplier):
35 35
         self._firing_cache = {}  # type: typing.Dict[str, Image.Image]
36 36
 
37 37
     def get_rest_images_scheme(self) -> typing.Dict[str, typing.Dict[str, typing.List[str]]]:
38
-        return {
39
-            POSITION_MAN_STAND_UP: {
40
-                RIFFLE: [
41
-                    'actors/man_weap1.png'
42
-                ],
43
-            },
44
-            POSITION_MAN_CRAWLING: {
45
-                RIFFLE: [
46
-                    'actors/man_c1_weap1.png',
47
-                    'actors/man_c2_weap1.png',
48
-                    'actors/man_c3_weap1.png',
49
-                    'actors/man_c4_weap1.png',
50
-                ],
51
-
52
-            }
53
-        }
38
+        return self.actor.weapon_image_scheme
54 39
 
55 40
     def get_firing_images_scheme(self) -> typing.Dict[str, typing.Dict[str, typing.List[str]]]:
56
-        return {
57
-            POSITION_MAN_STAND_UP: {
58
-                RIFFLE: [
59
-                    'actors/man_weap1_firing1.png',
60
-                    'actors/man_weap1_firing2.png',
61
-                    'actors/man_weap1_firing3.png',
62
-                ],
63
-            },
64
-        }
41
+        return self.actor.weapons_firing_image_scheme
65 42
 
66 43
     def get_image_for_weapon(self, mode: str, weapon_type: str) -> Image.Image:
67 44
         try: