-
Notifications
You must be signed in to change notification settings - Fork 12
/
entity.py
659 lines (538 loc) · 19.9 KB
/
entity.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
# Copyright (c) 2023 Martín Abente Lahaye.
#
# This file is part of Gameeky
# (see gameeky.tchx84.dev).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from __future__ import annotations
import math
from copy import deepcopy
from typing import Dict, List, Optional, Tuple, cast, TYPE_CHECKING
from gi.repository import GObject, GLib
if TYPE_CHECKING:
from .scene import Scene
from .definitions import Density, Recovery, Delay
from .actuators.base import Actuator, ActuatorRegistry
from .actuators.transmutes import Actuator as TransmutesActuator
from .actuators.teleports import Actuator as TeleportsActuator
from .actuators.teleports_i import Actuator as TeleportsIActuator
from .actuators.deteriorates import Actuator as DeterioratesActuator
from .actuators.exhausts import Actuator as ExhaustsActuator
from .actuators.destroys import Actuator as DestroysActuator
from .actuators.destroys_i import Actuator as DestroysIActuator
from .actuators.follows import Actuator as FollowsActuator
from .actuators.roams import Actuator as RoamsActuator
from .actuators.targets import Actuator as TargetsActuator
from .actuators.uses import Actuator as UsesActuator
from .actuators.takes import Actuator as TakesActuator
from .actuators.interacts import Actuator as InteractsActuator
from .actuators.spawns import Actuator as SpawnsActuator
from .actuators.drops import Actuator as DropsActuator
from .actuators.triggers import Actuator as TriggersActuator
from .actuators.triggers_i import Actuator as TriggersIActuator
from .actuators.requires import Actuator as RequiresActuator
from .actuators.activates import Actuator as ActivatesActuator
from .actuators.activates_i import Actuator as ActivatesIActuator
from .actuators.propulses import Actuator as PropulsesActuator
from .actuators.collapses import Actuator as CollapsesActuator
from .actuators.collapses_t import Actuator as CollapsesTActuator
from .actuators.affects import Actuator as AffectsActuator
from .actuators.affects_i import Actuator as AffectsIActuator
from .actuators.affects_u import Actuator as AffectsUActuator
from .actuators.aggroes import Actuator as AggroesActuator
from .actuators.says import Actuator as SaysActuator
from .actuators.says_i import Actuator as SaysIActuator
from .actuators.rotates_i import Actuator as RotatesIActuator
from .handlers.base import Handler
from .handlers.destroy import Handler as DestroyHandler
from .handlers.drop import Handler as DropHandler
from .handlers.exhaust import Handler as ExhaustHandler
from .handlers.idle import Handler as IdleHandler
from .handlers.interact import Handler as InteractHandler
from .handlers.move import Handler as MoveHandler
from .handlers.take import Handler as TakeHandler
from .handlers.use import Handler as UseHandler
from .handlers.rotate import Handler as RotateHandler
from ...common.logger import logger
from ...common.scanner import Description
from ...common.vector import Vector
from ...common.definitions import Action, Direction, EntityType, State
from ...common.entity import Entity as CommonEntity
from ...common.utils import (
clamp,
division,
element,
remove_source_id,
add_timeout_source,
)
class Entity(CommonEntity, GObject.GObject):
__gsignals__ = {
"told": (GObject.SignalFlags.RUN_LAST, None, (str,)),
}
__handler_by_action__ = {
Action.DESTROY: DestroyHandler,
Action.DROP: DropHandler,
Action.EXHAUST: ExhaustHandler,
Action.IDLE: IdleHandler,
Action.INTERACT: InteractHandler,
Action.MOVE: MoveHandler,
Action.TAKE: TakeHandler,
Action.USE: UseHandler,
Action.ROTATE: RotateHandler,
}
__actuator_by_name__ = {
TransmutesActuator.name: TransmutesActuator,
TeleportsActuator.name: TeleportsActuator,
TeleportsIActuator.name: TeleportsIActuator,
DeterioratesActuator.name: DeterioratesActuator,
ExhaustsActuator.name: ExhaustsActuator,
DestroysActuator.name: DestroysActuator,
DestroysIActuator.name: DestroysIActuator,
FollowsActuator.name: FollowsActuator,
RoamsActuator.name: RoamsActuator,
TargetsActuator.name: TargetsActuator,
UsesActuator.name: UsesActuator,
TakesActuator.name: TakesActuator,
InteractsActuator.name: InteractsActuator,
SpawnsActuator.name: SpawnsActuator,
DropsActuator.name: DropsActuator,
TriggersActuator.name: TriggersActuator,
TriggersIActuator.name: TriggersIActuator,
RequiresActuator.name: RequiresActuator,
ActivatesActuator.name: ActivatesActuator,
ActivatesIActuator.name: ActivatesIActuator,
PropulsesActuator.name: PropulsesActuator,
CollapsesActuator.name: CollapsesActuator,
CollapsesTActuator.name: CollapsesTActuator,
AffectsActuator.name: AffectsActuator,
AffectsIActuator.name: AffectsIActuator,
AffectsUActuator.name: AffectsUActuator,
AggroesActuator.name: AggroesActuator,
SaysActuator.name: SaysActuator,
SaysIActuator.name: SaysIActuator,
RotatesIActuator.name: RotatesIActuator,
}
def __init__(
self,
stamina: float,
durability: float,
weight: float,
strength: float,
recovery: float,
removable: bool,
takeable: bool,
usable: bool,
density: Density,
target_type: EntityType,
name: str,
dialogue: str,
actuators: List[str],
target_name: str,
radius: int,
rate: float,
scene: Scene,
overrides: Optional[Description],
playable: bool,
*args,
**kargs,
) -> None:
CommonEntity.__init__(self, *args, **kargs)
GObject.GObject.__init__(self)
self.strength = strength
self.recovery = clamp(Recovery.MAX, Recovery.MIN, recovery)
self.removable = removable
self.takeable = takeable
self.usable = usable
self.density = clamp(Density.SOLID, Density.VOID, density)
self.target_id: Optional[int] = None
self.target_name = target_name
self.target_type = target_type
self.name = name
self.dialogue = dialogue
self.actuators: List[Actuator] = []
self.radius = radius
self.rate = rate
self.action = Action.IDLE
self._weight = weight
self._durability = durability
self._stamina = stamina
self._max_durability = durability
self._max_stamina = stamina
self._default_density = density
self._default_visible = self.visible
self._playable = playable
self._scene = scene
# Keep original overrides for save files
self._overrides = overrides
self._next_action = Action.IDLE
self._next_value = 0.0
self._held: Optional["Entity"] = None
self._held_by: Optional["Entity"] = None
self._spawned = EntityType.EMPTY
self._destination = Vector()
self._delay = clamp(Delay.MAX, Delay.MIN, Delay.MAX - self.recovery)
self._handlers: Dict[Action, Handler] = {}
for action, HandlerClass in self.__handler_by_action__.items():
self._handlers[action] = HandlerClass(self)
self._handler = self._handlers[Action.IDLE]
for actuator in actuators:
if ActuatorClass := self.__actuator_by_name__.get(actuator):
self.actuators.append(ActuatorClass(self))
elif ActuatorClass := ActuatorRegistry.find(actuator):
try:
self.actuators.append(ActuatorClass(self))
except Exception as e:
logger.error(e)
self._timeout_source_id: Optional[int] = None
def _prepare(self) -> None:
if self._handler.busy is True:
return
handler = self._handlers[self._next_action]
if handler.prepare(self._next_value) is False:
handler = self._handlers[Action.IDLE]
handler.prepare(self._next_value)
self._handler = handler
def _update_flags(self) -> None:
self._spawned = EntityType.EMPTY
def _update_actuators(self) -> None:
for actuator in self.actuators:
try:
actuator.tick()
except Exception as e:
logger.error(e)
def _update_held(self) -> None:
if self.held is None:
return
self.held.direction = self.direction
self.held.position = self.position_at(self.direction)
def tick(self) -> None:
self._update_flags()
self._update_actuators()
self._update_held()
if self.blocked is True:
return
self._prepare()
self._handler.tick()
def perform(self, action: Action, value: float = 0) -> None:
self._next_action = action
self._next_value = value
def stop(self) -> None:
self._handler.cancel()
def activate(self) -> None:
for actuator in self.actuators:
if actuator.activatable is True:
try:
actuator.activate()
except Exception as e:
logger.error(e)
def drop(self, state: Optional[State] = None) -> None:
if self.held is None:
return
if state is not None:
self.held.state = state
self.held.secure()
self.held.restore()
self.held = None
def fall(self) -> None:
if self.held_by is not None:
self.held_by.drop()
def spawn(self) -> None:
self._spawned = self.target_type
if self.held is not None and self.held.target_type != EntityType.EMPTY:
self._spawned = self.held.target_type
def position_at(self, direction: Direction) -> Vector:
return self._scene.partition.get_position_for_direction(
self.position,
direction,
)
def inline_with(self, entity: "Entity") -> bool:
if self.position.x == entity.position.x:
return True
if self.position.y == entity.position.y:
return True
return False
def secure(self) -> None:
self.position = Vector(
math.floor(self.position.x),
math.floor(self.position.y),
math.floor(self.position.z),
)
def restore(self) -> None:
self.visible = self._default_visible
self.density = self._default_density
def targets(self, entity: "Entity") -> bool:
# If no particular target then it could target any entity
if not self.target_id and not self.target_name and not self.target_type:
return True
if self.target_id == entity.id:
return True
if self.target_name == entity.name:
return True
if self.target_type == entity.type_id:
return True
return False
def targets_by_type(self, entity: "Entity") -> bool:
# if no particular type then target any
if not self.target_type:
return True
return self.target_type == entity.type_id
def tell(self, text: str) -> None:
if self._timeout_source_id is not None:
remove_source_id(self._timeout_source_id)
self._timeout_source_id = add_timeout_source(250, self.__on_told, (text,))
def __on_told(self, text: str) -> int:
self.emit("told", text)
self._timeout_source_id = None
return GLib.SOURCE_REMOVE
def shutdown(self) -> None:
if self._timeout_source_id is not None:
remove_source_id(self._timeout_source_id)
@property
def horizontally(self) -> bool:
return self.direction in [Direction.EAST, Direction.WEST]
@property
def vertically(self) -> bool:
return self.direction in [Direction.SOUTH, Direction.NORTH]
@property
def busy(self) -> bool:
return self._handler.busy
@property
def playable(self) -> bool:
return self._playable
@property
def mutable(self) -> bool:
return len(self.actuators) > 0
@property
def interactable(self) -> bool:
for actuator in self.actuators:
if actuator.interactable is True:
return True
return False
@property
def blocked(self) -> bool:
return self.state in [
State.DESTROYED,
State.HELD,
]
@property
def removed(self) -> bool:
return self.state == State.DESTROYED and self.removable and not self.playable
@property
def spawned(self) -> EntityType:
return self._spawned
@property
def spawned_at(self) -> Vector:
position = self.position
if self.held is not None and self.held.target_type != EntityType.EMPTY:
position = self.held.position
return position.copy()
@property
def target(self) -> Optional["Entity"]:
if self.target_id:
return self.scene.find_by_id(self.target_id)
elif self.target_name:
return self.scene.find_by_name(self.target_name)
else:
return None
@target.setter
def target(self, target: Optional["Entity"]) -> None:
if not target:
self.target_id = None
self.target_name = ""
else:
self.target_id = target.id
self.target_name = target.name
@property
def held(self) -> Optional["Entity"]:
return self._held
@held.setter
def held(self, held: Optional["Entity"]) -> None:
# Unset reference from the previous held entity
if self._held is not None:
self._held.held_by = None
# Set reference to the new held entity
if held is not None:
held.held_by = self
self._held = held
@property
def held_by(self) -> Optional["Entity"]:
return self._held_by
@held_by.setter
def held_by(self, held_by: Optional["Entity"]) -> None:
self._held_by = held_by
@property
def delay(self) -> float:
return self._delay
@property
def weight(self) -> float:
weight = self._weight
if self.held is not None:
weight += self.held.weight
return weight
@property
def position(self) -> Vector:
return self._position
@position.setter
def position(self, position) -> None:
self._scene.partition.remove(self)
self._position = position.copy()
self._scene.partition.add(self)
@property
def destination(self) -> Vector:
return self._destination
@destination.setter
def destination(self, destination) -> None:
self._destination.x = math.floor(destination.x)
self._destination.y = math.floor(destination.y)
self._destination.z = math.floor(destination.z)
@property
def durability(self) -> float:
return self._durability
@durability.setter
def durability(self, durability) -> None:
self._durability = clamp(self._max_durability, 0, durability)
self.status = self.normalized_durability
@property
def stamina(self) -> float:
return self._stamina
@stamina.setter
def stamina(self, stamina) -> None:
self._stamina = clamp(self._max_stamina, 0, stamina)
@property
def normalized_durability(self) -> float:
return division(self._durability, self._max_durability)
@property
def normalized_stamina(self) -> float:
return division(self._stamina, self._max_stamina)
@property
def obstacles(self) -> List["Entity"]:
return cast(List["Entity"], self._scene.partition.find_by_direction(self))
@property
def obstacle(self) -> Optional["Entity"]:
obstacles = []
for obstacle in self.obstacles:
if obstacle.density == Density.SOLID:
obstacles.append(obstacle)
return element(obstacles, -1)
@property
def surfaces(self) -> List["Entity"]:
return cast(
List["Entity"],
self._scene.partition.find_by_position(self.position),
)
@property
def surface(self) -> Optional["Entity"]:
return element(self.surfaces, 0)
@property
def overlay(self) -> Optional["Entity"]:
return element(self.surfaces, -1)
@property
def surroundings(self) -> List["Entity"]:
surroundings = []
entities = cast(
List["Entity"],
self._scene.partition.find_by_distance(
target=self,
distance_x=self.radius,
distance_y=self.radius,
),
)
for entity in entities:
if entity.density == Density.SOLID:
surroundings.append(entity)
return surroundings
@property
def scene(self) -> Scene:
return self._scene
@property
def description(self) -> Description:
return Description(
type_id=self.type_id,
position=Description(
x=math.floor(self.position.x),
y=math.floor(self.position.y),
z=math.floor(self.position.z),
),
overrides=self._overrides,
)
class EntityRegistry:
__entities__: Dict[int, Description] = {}
@classmethod
def reset(cls) -> None:
cls.__entities__ = {}
@classmethod
def register(cls, description: Description) -> None:
cls.__entities__[description.id] = description
@classmethod
def find(cls, type_id: int) -> Description:
return cls.__entities__[type_id]
@classmethod
def find_and_override(
cls,
type_id: int,
overrides: Optional[Description],
) -> Tuple[int, Description]:
type_id = type_id if type_id in cls.__entities__ else EntityType.PLAYER
registry = cls.__entities__[type_id]
description = deepcopy(registry.game.default)
if overrides is not None:
for attribute in vars(overrides):
setattr(
description,
attribute,
getattr(overrides, attribute),
)
return type_id, description
@classmethod
def new_from_values(
cls,
id: int,
type_id: int,
position: Vector,
overrides: Optional[Description],
scene: Scene,
playable: bool,
) -> Entity:
type_id, description = cls.find_and_override(
type_id=type_id,
overrides=overrides,
)
return Entity(
id=id,
type_id=type_id,
position=position,
stamina=description.stamina,
durability=description.durability,
weight=description.weight,
strength=description.strength,
recovery=description.recovery,
removable=description.removable,
takeable=description.takeable,
usable=description.usable,
density=description.density,
visible=description.visible,
target_type=description.target_type,
name=description.name,
dialogue=description.dialogue,
actuators=description.actuators,
target_name=description.target_name,
radius=description.radius,
rate=description.rate,
luminance=description.luminance,
direction=Direction[description.direction.upper()],
state=State[description.state.upper()],
overrides=overrides,
scene=scene,
playable=playable,
)