-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheffect_system.py
More file actions
55 lines (45 loc) · 1.55 KB
/
effect_system.py
File metadata and controls
55 lines (45 loc) · 1.55 KB
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
from typing import List, Callable
from dataclasses import dataclass
from enum import Enum, auto
class Timing(Enum):
IMMEDIATE = auto()
START_OF_TURN = auto()
END_OF_TURN = auto()
ON_ATTACK = auto()
ON_DEFEND = auto()
@dataclass
class Effect:
name: str
timing: Timing
condition: Callable
action: Callable
priority: int = 0
class EffectChain:
def __init__(self):
self.effects: List[Effect] = []
def add_effect(self, effect: Effect):
self.effects.append(effect)
# Sort by priority
self.effects.sort(key=lambda x: x.priority, reverse=True)
def resolve(self, game_state):
"""Resolve all effects in chain"""
for effect in self.effects:
if effect.condition(game_state):
effect.action(game_state)
class EffectManager:
def __init__(self):
self.active_effects = []
self.pending_chain = EffectChain()
def register_effect(self, effect: Effect):
"""Register a new effect to be resolved"""
self.pending_chain.add_effect(effect)
def resolve_effects(self, timing: Timing, game_state):
"""Resolve effects for a specific timing"""
relevant_effects = [
effect for effect in self.active_effects
if effect.timing == timing
]
chain = EffectChain()
for effect in relevant_effects:
chain.add_effect(effect)
chain.resolve(game_state)