Преглед на файлове

(250712) 大地图帧率优化; 鬼屋模式局部重绘优化

detopoi преди 1 година
родител
ревизия
24e7e50e0b
променени са 2 файла, в които са добавени 72 реда и са изтрити 20 реда
  1. 71 19
      client_local/level/level.py
  2. 1 1
      client_local/ui/game/game_top.py

+ 71 - 19
client_local/level/level.py

@@ -5,6 +5,8 @@ import numpy as np
 import pygame
 import os
 import ast
+import time
+from collections import defaultdict
 
 from client_local.const import game as G
 from client_local.frame import floor
@@ -68,10 +70,38 @@ def get_light_npc_coordinates():
 
 _last_light_states = None
 
+
+# def get_player_mask(mask_surface, bgl):
+#     """
+#     只在光源状态变化时才重绘遮罩;否则直接返回上次绘制结果。
+#     """
+#     global _last_light_states
+#     # 收集当前所有光源状态 (x, y, light)
+#     curr = []
+#     for p in [current_level.me] + current_level.npcs + list(current_level.obstacle_instances.values()):
+#         if p.light:
+#             curr.append((p.x_pos, p.y_pos, p.light))
+#     curr_key = tuple(curr)
+#     # 如果没变化,直接复用
+#     if curr_key == _last_light_states:
+#         return mask_surface
+#     # 状态变化:重绘
+#     _last_light_states = curr_key
+#     bgl = max(0, min(bgl, 255))
+#     fill = mask_surface.fill
+#     blit = mask_surface.blit
+#     masks = circleMasks
+#     fill((bgl, bgl, bgl, 0))
+#     for x, y, light in curr:
+#         # 叠加亮度
+#         cm = masks[light]
+#         r = light * MASK_STEP
+#         blit(cm, (x - r, y - r - 15),
+#              special_flags=pygame.BLEND_RGBA_ADD)
+#     return mask_surface
+
+
 def get_player_mask(mask_surface, bgl):
-    """
-    只在光源状态变化时才重绘遮罩;否则直接返回上次绘制结果。
-    """
     global _last_light_states
     # 收集当前所有光源状态 (x, y, light)
     curr = []
@@ -79,22 +109,36 @@ def get_player_mask(mask_surface, bgl):
         if p.light:
             curr.append((p.x_pos, p.y_pos, p.light))
     curr_key = tuple(curr)
-    # 如果没变化,直接复用
     if curr_key == _last_light_states:
         return mask_surface
-    # 状态变化:重绘
     _last_light_states = curr_key
     bgl = max(0, min(bgl, 255))
     fill = mask_surface.fill
     blit = mask_surface.blit
     masks = circleMasks
+    # 先把整表清空为 bgl 灰底
     fill((bgl, bgl, bgl, 0))
+    # ---- 新增:计算屏幕视口,并在后面用 inflate 方式做相交测试 ----
+    from client_local.const import game as G
+    # world-space 下的视口矩形
+    view_rect = pygame.Rect(
+        current_level.scroll_x_pos,
+        current_level.scroll_y_pos,
+        G.MAIN_AREA_X_POS,
+        G.MAIN_AREA_Y_POS
+    )
+    # ---------------------------------------------------------
+    view_x, view_y = current_level.scroll_x_pos, current_level.scroll_y_pos
     for x, y, light in curr:
-        # 叠加亮度
+        r  = light * MASK_STEP
+        sx = x - view_x - r           # ← 世界 → 屏幕
+        sy = y - view_y - r - 15
         cm = masks[light]
-        r = light * MASK_STEP
-        blit(cm, (x - r, y - r - 15),
-             special_flags=pygame.BLEND_RGBA_ADD)
+        # 膨胀法:把视口向外各扩 r(水平方向)和 (r+15)(垂直方向)
+        if not view_rect.inflate(2*r, 2*(r+15)).collidepoint(x, y):
+            continue
+        # 叠加亮度
+        mask_surface.blit(cm, (sx, sy), special_flags=pygame.BLEND_RGBA_ADD)
     return mask_surface
 
 
@@ -188,7 +232,8 @@ class Level:
         self.grid_damage_blood = dict()  # 网格伤害血量
         self.grid_damage_time = dict()  # 网格伤害时刻 只有糖泡爆炸时才会增加
         self.grid_damage_multiples = dict()  # 伤害次数(倍数)
-        self.grid_damage_orientations = dict()  # 网格伤害方向
+        # self.grid_damage_orientations = dict()  # 网格伤害方向
+        self.grid_damage_orientations = defaultdict(set)
         self.path_map = dict()  # 缓存怪物追踪路径
         self.grid_damage_frame = False  # 用于判断糖泡伤害瞬间与玩家位置是否重合 只有爆炸帧才是True
         self.accumulation_time = accumulation_time  # 网格伤害延迟时间
@@ -234,10 +279,11 @@ class Level:
         self.load_music()  # 加载音乐
 
         self.mask_surface = pygame.Surface((self.map_x_pos, self.map_y_pos), pygame.SRCALPHA).convert_alpha()
+
         self.haunt_Mode = False
         self.bg_light = 0
         self.underwater = False
-        self.rgb_light = [0,0,0]
+        self.rgb_light = [0, 0, 0]
         self.special_rgb = False
 
         self.boss_blood_bar_dic = {1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0}  # boss血条显示
@@ -326,14 +372,14 @@ class Level:
             for y in range(self.map_y):
                 self.grid_damage_blood[(x, y)] = 0
                 self.grid_damage_time[(x, y)] = 0
-                self.grid_damage_multiples[(x,y)] = 0
-                self.grid_damage_orientations[(x, y)] = set()
+                self.grid_damage_multiples[(x, y)] = 0
+                # self.grid_damage_orientations[(x, y)] = set()
                 self.grid_bomb_time_at[(x, y)] = 0
         self.district_square_grid = {"x1": 0, "x2": 0, "y1": 0, "y2": 0}
         self.district_square = {"x1": 0, "x2": 0, "y1": 0, "y2": 0}
         if "finish" in self.map_json["basic"].keys():
             self.finish_at = (self.map_json["basic"]["finish"][0], self.map_json["basic"]["finish"][1])
-        self.main_area = pygame.Surface((self.map_x_pos, self.map_y_pos))
+        self.main_area = pygame.Surface((self.map_x_pos, self.map_y_pos)).convert()
         self.left_black = pygame.Surface((1, 600))
 
         if "haunted" in self.map_json["basic"].keys():
@@ -742,8 +788,16 @@ class Level:
         if self.haunt_Mode:
             # 只在 get_player_mask 检测到状态变化时才重绘 mask_surface
             get_player_mask(self.mask_surface, self.bg_light)
-            # 直接将已有 surface MULT 回主画面
-            self.main_area.blit(self.mask_surface, (0, 0), special_flags=pygame.BLEND_RGBA_MULT)
+            view_rect = pygame.Rect(
+                self.scroll_x_pos,
+                self.scroll_y_pos,
+                G.MAIN_AREA_X_POS,
+                G.MAIN_AREA_Y_POS
+            ).clip(self.main_area.get_rect())
+            # 取 main_area 的子 Surface(不拷贝像素,O(1))
+            sub_main = self.main_area.subsurface(view_rect)
+            # 将 mask_surface 乘进子 Surface,而不是整张 main_area
+            sub_main.blit(self.mask_surface, (0, 0), special_flags=pygame.BLEND_RGBA_MULT)
 
         # 显示顶层特效
         for e in self.effects_front:
@@ -774,9 +828,7 @@ class Level:
         BombInstance.sound_played = False
 
         # 重置糖泡爆炸方向记录
-        for x in range(self.map_x):
-            for y in range(self.map_y):
-                self.grid_damage_orientations[(x, y)] = set()
+        self.grid_damage_orientations.clear()
 
         # print(pygame.time.get_ticks() - a)
 

+ 1 - 1
client_local/ui/game/game_top.py

@@ -7,7 +7,7 @@ class GameTop(UIInstance):
     def __init__(self):
         super().__init__("game", "gameTop")
         self.font = pygame.font.Font("res/font/simsun.ttc", 16)
-        self.rend = self.font.render("v20250708", True, (0, 196, 255))
+        self.rend = self.font.render("v20250712", True, (0, 196, 255))
 
     def draw(self, screen: pygame.Surface):
         super().draw(screen)