Browse Source

weapon images

Bastien Sevajol 1 year ago
parent
commit
9b09e5d302

+ 3 - 0
.gitignore View File

@@ -75,3 +75,6 @@ ENV/
75 75
 
76 76
 # other
77 77
 *~
78
+
79
+# OpenCombat
80
+/cache

+ 1 - 0
config.yaml View File

@@ -19,6 +19,7 @@ game:
19 19
       draw_interior_gap: 2
20 20
 
21 21
 global:
22
+    cache_dir_path: 'cache'
22 23
     include_path:
23 24
       maps:
24 25
         - "maps"

BIN
medias/images/actors/man_weap1.png View File


BIN
medias/images/actors/src/man_weap1.xcf View File


+ 9 - 0
opencombat/exception.py View File

@@ -0,0 +1,9 @@
1
+# coding: utf-8
2
+
3
+
4
+class OpenCombatException(Exception):
5
+    pass
6
+
7
+
8
+class UnknownWeapon(OpenCombatException):
9
+    pass

+ 46 - 3
opencombat/gui/actor.py View File

@@ -1,12 +1,15 @@
1 1
 # coding: utf-8
2
-import pyglet
2
+import typing
3
+
4
+from PIL import Image
3 5
 from synergine2.config import Config
4 6
 
5 7
 from synergine2.simulation import Subject
6 8
 from synergine2_cocos2d.actor import Actor
7 9
 from opencombat.gui.animation import ANIMATION_WALK
8 10
 from opencombat.gui.animation import ANIMATION_CRAWL
9
-
11
+from opencombat.gui.weapon import WeaponImageApplier
12
+from opencombat.gui.weapon import RIFFLE
10 13
 
11 14
 FLAG_DE = 'DE'
12 15
 FLAG_URSS = 'URSS'
@@ -15,8 +18,43 @@ FLAG_COLORS = {
15 18
     FLAG_DE
16 19
 }
17 20
 
21
+MAN_STAND_UP = 'MAN_STAND_UP'
22
+
23
+
24
+class BaseActor(Actor):
25
+    def __init__(
26
+        self,
27
+        image_path: str,
28
+        config: Config,
29
+        subject: Subject,
30
+    ) -> None:
31
+        self.weapon_image_applier = WeaponImageApplier(config, self)
32
+        super().__init__(image_path, subject=subject, config=config)
18 33
 
19
-class Man(Actor):
34
+    @property
35
+    def mode(self) -> str:
36
+        return MAN_STAND_UP
37
+
38
+    @property
39
+    def weapons(self) -> typing.List[str]:
40
+        return []
41
+
42
+    def get_default_appliable_images(self) -> typing.List[Image.Image]:
43
+        if not self.weapons:
44
+            return []
45
+
46
+        return [
47
+            self.weapon_image_applier.get_default_image_for_weapon(
48
+                self.mode,
49
+                self.weapons[0],
50
+            )
51
+        ]
52
+
53
+    def firing(self) -> None:
54
+        pass
55
+
56
+
57
+class Man(BaseActor):
20 58
     animation_image_paths = {
21 59
         ANIMATION_WALK: [
22 60
             'actors/man.png',
@@ -43,6 +81,11 @@ class Man(Actor):
43 81
     ) -> None:
44 82
         super().__init__('actors/man.png', subject=subject, config=config)
45 83
 
84
+    @property
85
+    def weapons(self) -> typing.List[str]:
86
+        # TODO BS 2018-01-26: Will be managed by complex part of code
87
+        return [RIFFLE]
88
+
46 89
 
47 90
 class HeavyVehicle(Actor):
48 91
     animation_image_paths = {

+ 18 - 2
opencombat/gui/base.py View File

@@ -346,13 +346,29 @@ class Game(TMXGui):
346 346
         def gunshot_sound():
347 347
             self.sound_lib.get_sound('gunshot_default').play()
348 348
 
349
+        def actor_firing():
350
+            shooter_actor.firing(event.weapon_type)
351
+
349 352
         # To avoid all in same time
350 353
         # TODO BS 2018-01-24: This should be unecessary when core events sending will be
351 354
         # base on time base instead cycle base. Remove it to ensure.
352 355
         delay = random.uniform(0.0, 0.6)
353 356
 
354
-        self.layer_manager.edit_layer.append_callback(gunshot_trace, duration=0.1, delay=delay)
355
-        self.layer_manager.edit_layer.append_callback(gunshot_sound, duration=0.0, delay=delay)
357
+        self.layer_manager.edit_layer.append_callback(
358
+            gunshot_trace,
359
+            duration=0.1,
360
+            delay=delay,
361
+        )
362
+        self.layer_manager.edit_layer.append_callback(
363
+            gunshot_sound,
364
+            duration=0.0,
365
+            delay=delay,
366
+        )
367
+        self.layer_manager.edit_layer.append_callback(
368
+            actor_firing,
369
+            duration=0.2,  # TODO BS 2018-01-25: Wil depend of weapon type
370
+            delay=delay,
371
+        )
356 372
 
357 373
     def subject_die(self, event: DieEvent) -> None:
358 374
         killed_actor = self.layer_manager.subject_layer.subjects_index[event.shoot_subject_id]

+ 49 - 0
opencombat/gui/weapon.py View File

@@ -0,0 +1,49 @@
1
+# coding: utf-8
2
+import typing
3
+
4
+from PIL import Image
5
+from synergine2.config import Config
6
+from synergine2_cocos2d.util import PathManager
7
+
8
+from opencombat.exception import UnknownWeapon
9
+
10
+if typing.TYPE_CHECKING:
11
+    from opencombat.gui.actor import BaseActor
12
+
13
+RIFFLE = 'RIFFLE'
14
+
15
+
16
+class ImageApplier(object):
17
+    pass  # FIXME: refact here
18
+
19
+
20
+class WeaponImageApplier(ImageApplier):
21
+    def __init__(
22
+        self,
23
+        config: Config,
24
+        actor: 'BaseActor',
25
+    ) -> None:
26
+        self.actor = actor
27
+        self._images_scheme = self.get_images_scheme()
28
+        self.path_manager = PathManager(config.resolve('global.include_path.graphics'))
29
+
30
+    def get_images_scheme(self) -> typing.Dict[str, typing.Dict[str, str]]:
31
+        from opencombat.gui.actor import MAN_STAND_UP
32
+        return {
33
+            MAN_STAND_UP: {
34
+                RIFFLE: 'actors/man_weap1.png',
35
+            }
36
+        }
37
+
38
+    def get_default_image_for_weapon(self, mode: str, weapon_type: str) -> Image.Image:
39
+        try:
40
+            # FIXME Cache
41
+            image_file_path = self.path_manager.path(
42
+                self._images_scheme[mode][weapon_type],
43
+            )
44
+            return Image.open(image_file_path)
45
+        except KeyError:
46
+            raise UnknownWeapon(
47
+                'Unknown weapon "{}" for mode "{}"'.format(weapon_type, mode),
48
+            )
49
+

+ 4 - 0
opencombat/simulation/event.py View File

@@ -8,6 +8,8 @@ from opencombat.const import COLLECTION_ALIVE
8 8
 if typing.TYPE_CHECKING:
9 9
     from opencombat.simulation.subject import TileSubject
10 10
 
11
+DEFAULT_WEAPON_TYPE = 'DEFAULT_WEAPON_TYPE'
12
+
11 13
 
12 14
 class NewVisibleOpponent(Event):
13 15
     def __init__(
@@ -34,9 +36,11 @@ class FireEvent(Event):
34 36
         self,
35 37
         shooter_subject_id: int,
36 38
         target_position: typing.Tuple[int, int],
39
+        weapon_type: str=DEFAULT_WEAPON_TYPE,
37 40
     ) -> None:
38 41
         self.shooter_subject_id = shooter_subject_id
39 42
         self.target_position = target_position
43
+        self.weapon_type = weapon_type
40 44
 
41 45
 
42 46
 class DieEvent(Event):