Skip to content

Commit

Permalink
Merge pull request #1 from TheArtur128/feature_branch
Browse files Browse the repository at this point in the history
Feature branch
  • Loading branch information
emptybutton committed Oct 3, 2022
2 parents 69be3a8 + e2fc68c commit 1864de7
Show file tree
Hide file tree
Showing 6 changed files with 272 additions and 51 deletions.
4 changes: 4 additions & 0 deletions errors/core_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ class NotSupportPartError(DiscreteUnitError):
pass


class UnitPartError(UnitError):
pass


class UnsupportedUnitForHandlerError(UnitError):
pass

Expand Down
4 changes: 4 additions & 0 deletions errors/geometry_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ class GeometryError(Exception):
pass


class VectorError(GeometryError):
pass


class LineError(GeometryError):
pass

Expand Down
4 changes: 2 additions & 2 deletions example/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def update(self) -> None:
)


class UnitSpawner(PositionalUnit, DependentUnit):
class UnitSpawner(PositionalUnit, MultitaskingUnit):
_avatar_factory = CustomFactory(lambda unit: PrimitiveAvatar(unit, None))

def __init__(
Expand All @@ -93,7 +93,7 @@ def __init__(
timer: Timer
):
super().__init__(position)
super(DependentUnit, self).__init__()
super(MultitaskingUnit, self).__init__()

self.unit_factory = unit_factory
self.spawn_zone = tuple(spawn_zone)
Expand Down
86 changes: 61 additions & 25 deletions simengine/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
from sim32.interfaces import *
from sim32.renders import ResourcePack, RenderActivator, IRender
from sim32.errors.core_errors import *
from sim32.geometry import Vector, Figure
from sim32.tools import ReportAnalyzer, BadReportHandler, Report, StrictToStateMixin, LoopUpdater, CustomFactory
from sim32.geometry import Vector, Figure, Site, DynamicTransporter


class ProcessState(IUpdatable, ABC):
Expand Down Expand Up @@ -203,6 +203,8 @@ def _handle_participant(self, participant: IUpdatable) -> None:


class UnitKillProcess(FocusedEvent, WorldProcess, ManyPassProcess):
_passes = 1

def _handle_participant(self, participant: IUpdatable) -> None:
self.world.remove_inhabitant(participant)

Expand Down Expand Up @@ -299,7 +301,7 @@ def clear_completed_processes(self) -> None:
self.__completed_processes = list()


class DependentUnit(ProcessKeeper, IUpdatable, ABC):
class MultitaskingUnit(ProcessKeeper, IUpdatable, ABC):
pass


Expand All @@ -322,7 +324,7 @@ def _handle_interaction_with(self, unit: IUpdatable) -> None:
pass


class ProcessInteractiveUnit(InteractiveUnit, DependentUnit, ABC):
class ProcessInteractiveUnit(InteractiveUnit, MultitaskingUnit, ABC):
_bilateral_process_factories: Iterable[IBilateralProcessFactory | type, ]
__cash_factories_for_object: tuple[object, tuple[IBilateralProcessFactory, ]] = (object(), tuple())

Expand Down Expand Up @@ -356,6 +358,19 @@ def __get_cachedly_suported_process_factories_for(self, unit: IUpdatable) -> tup
return factories


class DependentUnit(ABC):
master: IUpdatable | None = None


class PartUnit(DependentUnit, StrictToStateMixin, StylizedMixin, IUpdatable, ABC):
_repr_fields = (Field("master"), )

def _is_correct(self) -> Report:
return Report(True) if self.master is not None else Report.create_error_report(
UnitPartError(f"Part unit {self} must have a master")
)


class MixinDiscrete(ABC):
@property
@abstractmethod
Expand All @@ -377,24 +392,54 @@ def deep_parts(self) -> frozenset[IUpdatable, ]:

class DiscreteUnit(MixinDiscrete, IUpdatable, ABC):
@property
def parts(self) -> frozenset[IUpdatable, ]:
def parts(self) -> frozenset[DependentUnit, ]:
return frozenset(self._parts)

@abstractmethod
def __create_parts__(self) -> Iterable[IUpdatable, ]:
def __create_parts__(self) -> Iterable[DependentUnit, ]:
pass

def init_parts(self, *args, **kwargs) -> None:
self._parts = set(self.__create_parts__(*args, **kwargs))
self._parts = set()

for part in self.__create_parts__(*args, **kwargs):
self._add_part(part)

def _add_part(self, part: DependentUnit) -> None:
part.master = self
self._parts.add(part)

def _remove_part(self, part: DependentUnit) -> None:
part.master = None
self._parts.remove(part)


class AnyPartMixin:
def __create_parts__(self, *parts) -> Iterable[IUpdatable, ]:
return parts


class TactileUnit(IUpdatable, ABC):
_zone_factory: IZoneFactory

def __init__(self):
self._zone = self._zone_factory(self)

@property
def zone(self) -> Figure:
return self._zone

class PositionalUnit(StylizedMixin, IUpdatable, ABC):

class PositionalUnit(TactileUnit, StylizedMixin, ABC):
_repr_fields = (Field('position'), )
_zone_factory = CustomFactory(lambda unit: Site(unit.position))

_avatar_factory: IAvatarFactory = CustomFactory(lambda unit: None)

def __init__(self, position: Vector):
super().__init__()
self._position = position
super().__init__()

self._avatar = self._avatar_factory(self)

@property
Expand Down Expand Up @@ -424,6 +469,11 @@ def move(self) -> None:
self.__previous_position = self._position
self._position = self.next_position

self._update_zone_position()

def _update_zone_position(self) -> None:
self._zone.move_by(DynamicTransporter(self.position - self.previous_position))


class InfinitelyImpulseUnit(MovableUnit, ABC):
def __init__(self, position: Vector):
Expand Down Expand Up @@ -454,20 +504,6 @@ def speed(self) -> int | float:
return self._speed


class HitboxUnit(IUpdatable, ABC):
hitbox_factories: Iterable[IHitboxFactory, ]

def __init__(self):
self._hitboxes = tuple(
hitbox_factory(self)
for hitbox_factory in self.hitbox_factories
)

@property
def hitboxes(self) -> tuple[Figure, ]:
return self._hitboxes


class Avatar(IAvatar, ABC):
def __init__(self, unit: PositionalUnit):
self._unit = unit
Expand Down Expand Up @@ -576,16 +612,16 @@ def add_process(self, process: Process) -> None:
process.world = self.world
super().add_process(process)

def _handle_units(self, units: Iterable[DependentUnit, ]) -> None:
def _handle_units(self, units: Iterable[MultitaskingUnit, ]) -> None:
self.clear_completed_processes()
self.__parse_world_processes_from(units)
self.activate_processes()

def __parse_world_processes_from(self, units: Iterable[DependentUnit, ]) -> None:
def __parse_world_processes_from(self, units: Iterable[MultitaskingUnit, ]) -> None:
for unit in units:
self.__handle_unit_processes(unit)

def __handle_unit_processes(self, unit: DependentUnit) -> None:
def __handle_unit_processes(self, unit: MultitaskingUnit) -> None:
for process in unit.processes:
if isinstance(process, WorldProcess):
unit.remove_process(process)
Expand Down
Loading

0 comments on commit 1864de7

Please sign in to comment.