332 lines
11 KiB
Python
332 lines
11 KiB
Python
# -*- coding: utf-8 -*-
|
|
# view/widgets/endgame_fx_overlay.py
|
|
from __future__ import annotations
|
|
|
|
from collections import deque
|
|
import random
|
|
import time
|
|
import tkinter as tk
|
|
|
|
from resources.code_res.game_constants import (
|
|
FONT_COLOR_TITLE_MAIN,
|
|
FONT_SIZE_SMALL_MIN,
|
|
)
|
|
from resources.code_res.i18n import t
|
|
from view.widgets.modal_veil import ModalVeil
|
|
|
|
|
|
class EndgameFxOverlay:
|
|
"""
|
|
Vorschalt-Animation bei Spielende über dem gesperrten, abgedunkelten Spieltisch:
|
|
- Sieg ('win'): Zufällig über den Bildschirm verteilte 'bat_explosion.gif' &
|
|
'pumpkin_explosion.gif' an wechselnden Punkten.
|
|
- Niederlage ('lose'): 'foul_pumpkin.gif' zufällig über den Bildschirm verteilt.
|
|
- Erst nach einem Klick (oder Space/Enter/Esc) geht es weiter zum Ergebnis-Screen.
|
|
"""
|
|
|
|
GRACE_SEC = 0.20
|
|
FX_TAG = "endgame_fx_item"
|
|
HINT_TAG = "endgame_fx_hint"
|
|
_SEQ = 0
|
|
|
|
@classmethod
|
|
def play(
|
|
cls,
|
|
root: tk.Misc,
|
|
assets,
|
|
outcome: str | bool | None,
|
|
on_continue,
|
|
target_canvas: tk.Canvas | None = None,
|
|
) -> "EndgameFxOverlay | None":
|
|
if outcome is True:
|
|
mode = "win"
|
|
elif outcome is False:
|
|
mode = "lose"
|
|
elif isinstance(outcome, str) and outcome.lower() in ("win", "lose"):
|
|
mode = outcome.lower()
|
|
else:
|
|
on_continue()
|
|
return None
|
|
|
|
return cls(root, assets, mode, on_continue, target_canvas=target_canvas)
|
|
|
|
def __init__(
|
|
self,
|
|
root: tk.Misc,
|
|
assets,
|
|
mode: str,
|
|
on_continue,
|
|
target_canvas: tk.Canvas | None = None,
|
|
):
|
|
EndgameFxOverlay._SEQ += 1
|
|
self._id = EndgameFxOverlay._SEQ
|
|
self.root = root.winfo_toplevel()
|
|
self.assets = assets
|
|
self.mode = mode
|
|
self.on_continue = on_continue
|
|
|
|
self._finished = False
|
|
self._armed_press = False
|
|
self._start_ts = time.monotonic()
|
|
self._jobs: set[str] = set()
|
|
self._active_spots: dict[int, tuple[int, int, int]] = {}
|
|
self._recent_spots: deque[tuple[int, int]] = deque(maxlen=5)
|
|
self._photo_refs: list = []
|
|
|
|
self._click_tag = f"EndgameClickTag_{self._id}"
|
|
self._tagged_widgets: list[tk.Misc] = []
|
|
|
|
self._veil = ModalVeil(
|
|
self.root,
|
|
self.assets,
|
|
modal_widget=None,
|
|
target_canvas=target_canvas,
|
|
dim_alpha=136,
|
|
)
|
|
self.canvas = self._veil.canvas
|
|
|
|
if self.canvas is None or not self._has_any_frames():
|
|
self._veil.release()
|
|
self.on_continue()
|
|
return
|
|
|
|
self._install_click_catcher()
|
|
self._draw_continue_banner()
|
|
self._start_choreography()
|
|
|
|
def _has_any_frames(self) -> bool:
|
|
probes = ("bat_explosion", "pumpkin_explosion") if self.mode == "win" else ("foul_pumpkin",)
|
|
for name in probes:
|
|
frames, _ = self.assets.get_win_lose_gif_sequence(name, (160, 160))
|
|
if frames:
|
|
return True
|
|
return False
|
|
|
|
def _schedule(self, delay_ms: int, fn, *args) -> str | None:
|
|
if self._finished:
|
|
return None
|
|
job_holder: list[str] = []
|
|
|
|
def _wrapped():
|
|
if job_holder:
|
|
self._jobs.discard(job_holder[0])
|
|
if not self._finished and self._canvas_ok():
|
|
fn(*args)
|
|
|
|
try:
|
|
jid = self.root.after(max(1, int(delay_ms)), _wrapped)
|
|
job_holder.append(jid)
|
|
self._jobs.add(jid)
|
|
return jid
|
|
except Exception:
|
|
return None
|
|
|
|
def _canvas_ok(self) -> bool:
|
|
try:
|
|
return bool(self.canvas is not None and self.canvas.winfo_exists())
|
|
except Exception:
|
|
return False
|
|
|
|
def _install_click_catcher(self):
|
|
for seq in ("<Enter>", "<Motion>", "<B1-Motion>", "<Double-Button-1>", "<MouseWheel>"):
|
|
self.root.bind_class(self._click_tag, seq, lambda _e: "break")
|
|
self.root.bind_class(self._click_tag, "<ButtonPress-1>", self._on_press)
|
|
self.root.bind_class(self._click_tag, "<ButtonRelease-1>", self._on_release)
|
|
self.root.bind_class(self._click_tag, "<ButtonPress-3>", self._on_press)
|
|
self.root.bind_class(self._click_tag, "<ButtonRelease-3>", self._on_release)
|
|
self.root.bind_class(self._click_tag, "<KeyPress>", self._on_key)
|
|
|
|
self._tag_tree(self.root)
|
|
|
|
def _tag_tree(self, node: tk.Misc):
|
|
try:
|
|
tags = list(node.bindtags())
|
|
if self._click_tag not in tags:
|
|
node.bindtags((self._click_tag, *tags))
|
|
self._tagged_widgets.append(node)
|
|
for child in node.winfo_children():
|
|
self._tag_tree(child)
|
|
except tk.TclError:
|
|
pass
|
|
|
|
def _on_press(self, _event=None):
|
|
if (time.monotonic() - self._start_ts) >= self.GRACE_SEC:
|
|
self._armed_press = True
|
|
return "break"
|
|
|
|
def _on_release(self, _event=None):
|
|
if self._armed_press and (time.monotonic() - self._start_ts) >= self.GRACE_SEC:
|
|
self.finish()
|
|
return "break"
|
|
|
|
def _on_key(self, event):
|
|
if (time.monotonic() - self._start_ts) >= self.GRACE_SEC:
|
|
if event.keysym in ("space", "Return", "KP_Enter", "Escape"):
|
|
self.finish()
|
|
return "break"
|
|
|
|
def _draw_continue_banner(self):
|
|
if not self._canvas_ok():
|
|
return
|
|
cw = max(320, self.canvas.winfo_width())
|
|
ch = max(240, self.canvas.winfo_height())
|
|
|
|
txt_ph = self.assets.get_goth_text_photo(
|
|
t("endgame_click_continue"),
|
|
FONT_SIZE_SMALL_MIN + 2,
|
|
FONT_COLOR_TITLE_MAIN,
|
|
)
|
|
bw = txt_ph.width() + 54
|
|
bh = max(38, txt_ph.height() + 14)
|
|
box_ph = self.assets.get_box_photo("box_purple", (bw, bh))
|
|
self._photo_refs.extend([txt_ph, box_ph] if box_ph else [txt_ph])
|
|
|
|
cx = cw // 2
|
|
cy = ch - bh // 2 - 14
|
|
if box_ph:
|
|
self.canvas.create_image(
|
|
cx, cy, image=box_ph, anchor="center", tags=(self.FX_TAG, self.HINT_TAG)
|
|
)
|
|
self.canvas.create_image(
|
|
cx, cy, image=txt_ph, anchor="center", tags=(self.FX_TAG, self.HINT_TAG)
|
|
)
|
|
|
|
def _start_choreography(self):
|
|
if self.mode == "win":
|
|
self._schedule(40, self._win_explosion_wave, True)
|
|
else:
|
|
self._schedule(50, self._lose_pumpkin_wave, True)
|
|
|
|
def _pick_random_spot(self, size_px: int) -> tuple[int, int]:
|
|
cw = max(360, self.canvas.winfo_width())
|
|
ch = max(260, self.canvas.winfo_height())
|
|
half = size_px // 2
|
|
x_min = half + 18
|
|
x_max = max(x_min + 20, cw - half - 18)
|
|
y_min = half + 14
|
|
y_max = max(y_min + 20, ch - half - 64)
|
|
|
|
best_xy = (random.randint(x_min, x_max), random.randint(y_min, y_max))
|
|
min_dist_sq = int((size_px * 0.78) ** 2)
|
|
|
|
for _ in range(18):
|
|
rx = random.randint(x_min, x_max)
|
|
ry = random.randint(y_min, y_max)
|
|
|
|
too_close = any(
|
|
(rx - ax) ** 2 + (ry - ay) ** 2 < min_dist_sq
|
|
for ax, ay, _asz in self._active_spots.values()
|
|
)
|
|
if not too_close:
|
|
too_close = any(
|
|
(rx - px) ** 2 + (ry - py) ** 2 < int(min_dist_sq * 0.65)
|
|
for px, py in self._recent_spots
|
|
)
|
|
if not too_close:
|
|
best_xy = (rx, ry)
|
|
break
|
|
|
|
self._recent_spots.append(best_xy)
|
|
return best_xy
|
|
|
|
def _spawn_one_shot_sprite(self, gif_name: str, scale_factor: float):
|
|
if self._finished or not self._canvas_ok():
|
|
return
|
|
cw = max(360, self.canvas.winfo_width())
|
|
ch = max(260, self.canvas.winfo_height())
|
|
sz = max(125, min(250, int(min(cw, ch) * scale_factor)))
|
|
frames, durs = self.assets.get_win_lose_gif_sequence(gif_name, (sz, sz))
|
|
if not frames:
|
|
return
|
|
|
|
sx, sy = self._pick_random_spot(sz)
|
|
item_id = self.canvas.create_image(
|
|
sx, sy, image=frames[0], anchor="center", tags=(self.FX_TAG,)
|
|
)
|
|
self._active_spots[item_id] = (sx, sy, sz)
|
|
self.canvas.tag_raise(self.HINT_TAG)
|
|
|
|
def _step_sprite(idx: int):
|
|
if self._finished or not self._canvas_ok():
|
|
return
|
|
if idx >= len(frames):
|
|
self._active_spots.pop(item_id, None)
|
|
try:
|
|
self.canvas.delete(item_id)
|
|
except Exception:
|
|
pass
|
|
return
|
|
self.canvas.itemconfigure(item_id, image=frames[idx])
|
|
self._schedule(durs[idx], _step_sprite, idx + 1)
|
|
|
|
self._schedule(durs[0], _step_sprite, 1)
|
|
|
|
def _win_explosion_wave(self, initial_burst: bool = False):
|
|
if self._finished or not self._canvas_ok():
|
|
return
|
|
scales = (0.24, 0.29, 0.34)
|
|
sprites = ("bat_explosion", "pumpkin_explosion")
|
|
if initial_burst:
|
|
self._spawn_one_shot_sprite(random.choice(sprites), random.choice(scales))
|
|
self._schedule(
|
|
140,
|
|
self._spawn_one_shot_sprite,
|
|
random.choice(sprites),
|
|
random.choice(scales),
|
|
)
|
|
elif len(self._active_spots) < 4:
|
|
self._spawn_one_shot_sprite(random.choice(sprites), random.choice(scales))
|
|
|
|
self._schedule(random.randint(250, 420), self._win_explosion_wave, False)
|
|
|
|
def _lose_pumpkin_wave(self, initial_burst: bool = False):
|
|
if self._finished or not self._canvas_ok():
|
|
return
|
|
scales = (0.23, 0.28, 0.33)
|
|
if initial_burst:
|
|
self._spawn_one_shot_sprite("foul_pumpkin", 0.29)
|
|
self._schedule(190, self._spawn_one_shot_sprite, "foul_pumpkin", 0.24)
|
|
elif len(self._active_spots) < 3:
|
|
self._spawn_one_shot_sprite("foul_pumpkin", random.choice(scales))
|
|
|
|
self._schedule(random.randint(330, 510), self._lose_pumpkin_wave, False)
|
|
|
|
def cancel(self):
|
|
self._cleanup_only()
|
|
|
|
def _cleanup_only(self):
|
|
if self._finished:
|
|
return
|
|
self._finished = True
|
|
|
|
for jid in list(self._jobs):
|
|
try:
|
|
self.root.after_cancel(jid)
|
|
except Exception:
|
|
pass
|
|
self._jobs.clear()
|
|
self._active_spots.clear()
|
|
self._photo_refs.clear()
|
|
|
|
if self._canvas_ok():
|
|
try:
|
|
self.canvas.delete(self.FX_TAG)
|
|
except Exception:
|
|
pass
|
|
|
|
for w in self._tagged_widgets:
|
|
try:
|
|
if w.winfo_exists():
|
|
tags = [tg for tg in w.bindtags() if tg != self._click_tag]
|
|
w.bindtags(tuple(tags))
|
|
except Exception:
|
|
pass
|
|
self._tagged_widgets.clear()
|
|
self._veil.release()
|
|
|
|
def finish(self):
|
|
if self._finished:
|
|
return
|
|
self._cleanup_only()
|
|
if callable(self.on_continue):
|
|
self.on_continue() |