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
     pass
9
     pass
10
 
10
 
11
 
11
 
12
-class UnknownAnimationIndex(OpenCombatException):
12
+class UnknownFiringAnimation(OpenCombatException):
13
     pass
13
     pass

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

8
 from synergine2.config import Config
8
 from synergine2.config import Config
9
 from synergine2.simulation import Subject
9
 from synergine2.simulation import Subject
10
 from synergine2_cocos2d.actor import Actor
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
 from opencombat.gui.animation import ANIMATION_CRAWL
15
 from opencombat.gui.animation import ANIMATION_CRAWL
14
 from opencombat.gui.animation import ANIMATION_WALK
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
 from opencombat.gui.image import TileImageCacheManager
19
 from opencombat.gui.image import TileImageCacheManager
18
 from opencombat.gui.weapon import RIFFLE
20
 from opencombat.gui.weapon import RIFFLE
19
 from opencombat.gui.weapon import WeaponImageApplier
21
 from opencombat.gui.weapon import WeaponImageApplier
22
     from opencombat.gui.fire import GuiFiringEvent
24
     from opencombat.gui.fire import GuiFiringEvent
23
 
25
 
24
 
26
 
27
+MODE_DEFAULT = 'MODE_DEFAULT'
28
+
29
+
25
 class BaseActor(Actor):
30
 class BaseActor(Actor):
26
     position_matching = {
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
     def __init__(
44
     def __init__(
32
         self,
45
         self,
34
         config: Config,
47
         config: Config,
35
         subject: Subject,
48
         subject: Subject,
36
     ) -> None:
49
     ) -> None:
37
-        self._mode = POSITION_MAN_STAND_UP
50
+        self._mode = MODE_MAN_STAND_UP
38
         self.weapon_image_applier = WeaponImageApplier(config, self)
51
         self.weapon_image_applier = WeaponImageApplier(config, self)
39
         super().__init__(image_path, subject=subject, config=config)
52
         super().__init__(image_path, subject=subject, config=config)
40
 
53
 
41
         # Firing
54
         # Firing
42
         self.last_firing_time = 0
55
         self.last_firing_time = 0
43
         self.firing_change_image_gap = 0.05  # seconds
56
         self.firing_change_image_gap = 0.05  # seconds
44
-        self.firing_images_cache = {}  # type: TODO
45
 
57
 
46
     def get_image_cache_manager(self) -> TileImageCacheManager:
58
     def get_image_cache_manager(self) -> TileImageCacheManager:
47
         return TileImageCacheManager(self, self.config)
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
     @property
70
     @property
50
     def mode(self) -> str:
71
     def mode(self) -> str:
72
+        # FIXME: When man is moving (crawling for example), must change mode
73
+        # and man must stay "crawled"
51
         return self._mode
74
         return self._mode
52
 
75
 
53
     @property
76
     @property
86
         except UnknownWeapon:
109
         except UnknownWeapon:
87
             return []
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
     def firing(self, firing: 'GuiFiringEvent') -> None:
112
     def firing(self, firing: 'GuiFiringEvent') -> None:
137
         # FIXME: move some code ?
113
         # FIXME: move some code ?
138
         now = time.time()
114
         now = time.time()
141
             firing.increment_animation_index()
117
             firing.increment_animation_index()
142
 
118
 
143
             try:
119
             try:
144
-                image = self.image_cache.firing_cache.get(
120
+                image = self.image_cache_manager.firing_cache.get(
145
                     mode=self.mode,
121
                     mode=self.mode,
146
                     weapon=firing.weapon,
122
                     weapon=firing.weapon,
147
                     position=firing.animation_index,
123
                     position=firing.animation_index,
148
                 )
124
                 )
149
             except UnknownAnimationIndex:
125
             except UnknownAnimationIndex:
150
-                image = self.image_cache.firing_cache.get(
126
+                image = self.image_cache_manager.firing_cache.get(
151
                     mode=self.mode,
127
                     mode=self.mode,
152
                     weapon=firing.weapon,
128
                     weapon=firing.weapon,
153
                     position=0,
129
                     position=0,
154
                 )
130
                 )
155
                 firing.reset_animation_index()
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
             # FIXME cache: prepare before firing
142
             # FIXME cache: prepare before firing
158
             import uuid
143
             import uuid
182
             'actors/man_c4.png',
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
     def __init__(
204
     def __init__(
187
         self,
205
         self,
195
         # TODO BS 2018-01-26: Will be managed by complex part of code
213
         # TODO BS 2018-01-26: Will be managed by complex part of code
196
         return [RIFFLE]
214
         return [RIFFLE]
197
 
215
 
216
+    def get_default_mode(self) -> str:
217
+        return MODE_MAN_STAND_UP
218
+
198
 
219
 
199
 class HeavyVehicle(BaseActor):
220
 class HeavyVehicle(BaseActor):
200
     animation_image_paths = {
221
     animation_image_paths = {
205
             'actors/tank1.png',
226
             'actors/tank1.png',
206
         ]
227
         ]
207
     }
228
     }
229
+    mode_image_paths = {
230
+        MODE_DEFAULT: 'actors/tank1.png',
231
+    }
208
 
232
 
209
     def __init__(
233
     def __init__(
210
         self,
234
         self,

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

1
 # coding: utf-8
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
 FLAG_DE = 'DE'
5
 FLAG_DE = 'DE'
6
 FLAG_COLORS = {
6
 FLAG_COLORS = {
7
     FLAG_DE
7
     FLAG_DE

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

6
 from synergine2_cocos2d.util import PathManager
6
 from synergine2_cocos2d.util import PathManager
7
 from synergine2_xyz.image import ImageCache
7
 from synergine2_xyz.image import ImageCache
8
 from synergine2_xyz.image import ImageCacheManager
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
 if typing.TYPE_CHECKING:
14
 if typing.TYPE_CHECKING:
13
     from opencombat.gui.actor import BaseActor
15
     from opencombat.gui.actor import BaseActor
31
         try:
33
         try:
32
             return self.cache[mode][weapon][position]
34
             return self.cache[mode][weapon][position]
33
         except KeyError:
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
         except IndexError:
42
         except IndexError:
36
             raise UnknownAnimationIndex(
43
             raise UnknownAnimationIndex(
37
                 'Unknown animation index "{}" for mode "{}" and weapon "{}"'.format(
44
                 'Unknown animation index "{}" for mode "{}" and weapon "{}"'.format(
66
             mode_image = Image.open(self.path_manager.path(mode_image_path))
73
             mode_image = Image.open(self.path_manager.path(mode_image_path))
67
 
74
 
68
             for weapon in self.actor.weapons:
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
                 for position in range(len(images)):
84
                 for position in range(len(images)):
75
                     position_image = images[position]
85
                     position_image = images[position]

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

8
 from synergine2_cocos2d.util import PathManager
8
 from synergine2_cocos2d.util import PathManager
9
 
9
 
10
 from opencombat.exception import UnknownWeapon
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
 if typing.TYPE_CHECKING:
14
 if typing.TYPE_CHECKING:
15
     from opencombat.gui.actor import BaseActor
15
     from opencombat.gui.actor import BaseActor
35
         self._firing_cache = {}  # type: typing.Dict[str, Image.Image]
35
         self._firing_cache = {}  # type: typing.Dict[str, Image.Image]
36
 
36
 
37
     def get_rest_images_scheme(self) -> typing.Dict[str, typing.Dict[str, typing.List[str]]]:
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
     def get_firing_images_scheme(self) -> typing.Dict[str, typing.Dict[str, typing.List[str]]]:
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
     def get_image_for_weapon(self, mode: str, weapon_type: str) -> Image.Image:
43
     def get_image_for_weapon(self, mode: str, weapon_type: str) -> Image.Image:
67
         try:
44
         try: