BaseBug._set_body_part()   A
last analyzed

Complexity

Conditions 3

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 3
1
from intelligine.core.exceptions import BodyPartAlreadyExist
2
from intelligine.synergy.object.Transportable import Transportable
3
from intelligine.cst import COL_ALIVE, COLONY, ACTION_DIE, BRAIN
4
from intelligine.simulation.object.brain.Brain import Brain
5
from intelligine.cst import ALIVE, ATTACKABLE
6
from synergine.core.Signals import Signals
7
8
9
class BaseBug(Transportable):
10
11
    _body_parts = {}
12
    _brain_class = Brain
13
14
    def __init__(self, collection, context):
15
        super().__init__(collection, context)
16
        context.metas.states.add_list(self.get_id(), [ALIVE, ATTACKABLE])
17
        context.metas.collections.add(self.get_id(), COL_ALIVE)
18
        context.metas.value.set(COLONY, self.get_id(), collection.get_id())
19
        self._life_points = 10
20
        self._alive = True
21
        self._movements_count = -1
22
        self._brain = self._brain_class(self._context, self)
23
        self._context.metas.value.set(BRAIN, self.__class__, self._brain_class)
24
        self._parts = {}
25
        self._init_parts()
26
27
    def die(self):
28
        self._set_alive(False)
29
        self._remove_state(ALIVE)
30
        self._remove_state(ATTACKABLE)
31
        self._remove_col(COL_ALIVE)
32
33
    def _init_parts(self):
34
        for body_part_name in self._body_parts:
35
            self._set_body_part(body_part_name, self._body_parts[body_part_name](self, self._context))
36
37
    def _set_body_part(self, name, body_part, replace=False):
38
        if name in self._parts and not replace:
39
            raise BodyPartAlreadyExist()
40
        self._parts[name] = body_part
41
42
    def get_body_part(self, name):
43
        return self._parts[name]
44
45
    def hurted(self, points):
46
        self._life_points -= points
47
        if self.get_life_points() <= 0 and self.is_alive():
48
            self.die()
49
            Signals.signal(ACTION_DIE).send(obj=self, context=self._context)
50
51
    def is_alive(self):
52
        return self._alive
53
54
    def _set_alive(self, alive):
55
        self._alive = bool(alive)
56
57
    def get_life_points(self):
58
        return self._life_points
59
60
    def set_position(self, point):
61
        super().set_position(point)
62
        self._movements_count += 1
63
64
    def get_movements_count(self):
65
        return self._movements_count
66
67
    def get_brain(self):
68
        return self._brain
69