Refactor rendering logic in MiceMaze and Rat classes for improved clarity and efficiency

This commit is contained in:
2026-04-14 12:15:16 +02:00
parent bbafc3bbba
commit 509b3433b8
3 changed files with 455 additions and 60 deletions
+32 -56
View File
@@ -205,76 +205,52 @@ class Rat(Unit):
if direction is None:
return False
# Convert the sprite center to tunnel-local coordinates. The visible slice
# is derived from how far that center has progressed across the tunnel cell.
local_x = center_x - cell_x * self.game.cell_size
local_y = center_y - cell_y * self.game.cell_size
half_cell = self.game.cell_size / 2
cell_size = self.game.cell_size
def draw_slice(source_x, source_y, width, height, draw_x=None, draw_y=None):
if width <= 0 or height <= 0:
return False
self.game.render_engine.draw_image(
self.render_x if draw_x is None else draw_x,
self.render_y if draw_y is None else draw_y,
image,
anchor="nw",
tag="unit",
source_rect=(source_x, source_y, width, height),
dest_size=(width, height),
)
return True
if direction == "UP":
visible_ratio = max(0.0, min(1.0, (half_cell - local_y) / half_cell))
visible_ratio = max(0.0, min(1.0, (cell_size - local_y) / cell_size))
visible_height = int(round(image_size[1] * visible_ratio))
if visible_height <= 0:
return False
self.game.render_engine.draw_image(
self.render_x,
self.render_y,
image,
anchor="nw",
tag="unit",
source_rect=(0, 0, image_size[0], visible_height),
dest_size=(image_size[0], visible_height),
)
return True
return draw_slice(0, 0, image_size[0], visible_height)
if direction == "DOWN":
visible_ratio = max(0.0, min(1.0, (local_y - half_cell) / half_cell))
visible_ratio = max(0.0, min(1.0, (local_y - cell_size) / cell_size))
visible_height = int(round(image_size[1] * visible_ratio))
if visible_height <= 0:
return False
source_y = image_size[1] - visible_height
draw_y = self.render_y + source_y
self.game.render_engine.draw_image(
self.render_x,
draw_y,
image,
anchor="nw",
tag="unit",
source_rect=(0, source_y, image_size[0], visible_height),
dest_size=(image_size[0], visible_height),
)
return True
return draw_slice(0, source_y, image_size[0], visible_height, draw_y=draw_y)
if direction == "LEFT":
visible_ratio = max(0.0, min(1.0, (half_cell - local_x) / half_cell))
visible_ratio = max(0.0, min(1.0, (cell_size - local_x) / cell_size))
visible_width = int(round(image_size[0] * visible_ratio))
if visible_width <= 0:
return False
self.game.render_engine.draw_image(
self.render_x,
self.render_y,
image,
anchor="nw",
tag="unit",
source_rect=(0, 0, visible_width, image_size[1]),
dest_size=(visible_width, image_size[1]),
)
return True
return draw_slice(0, 0, visible_width, image_size[1])
visible_ratio = max(0.0, min(1.0, (local_x - half_cell) / half_cell))
visible_width = int(round(image_size[0] * visible_ratio))
if visible_width <= 0:
return False
source_x = image_size[0] - visible_width
draw_x = self.render_x + source_x
self.game.render_engine.draw_image(
draw_x,
self.render_y,
image,
anchor="nw",
tag="unit",
source_rect=(source_x, 0, visible_width, image_size[1]),
dest_size=(visible_width, image_size[1]),
)
return True
if direction == "RIGHT":
visible_ratio = max(0.0, min(1.0, (local_x - cell_size) / cell_size))
visible_width = int(round(image_size[0] * visible_ratio))
source_x = image_size[0] - visible_width
draw_x = self.render_x + source_x
return draw_slice(source_x, 0, visible_width, image_size[1], draw_x=draw_x)
return False
def _get_tunnel_entrance_direction(self, cell_x, cell_y):
directions = [