| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- """TOP_UPDATE must extend visibility without changing depth ordering or defaults."""
- import enum
- from types import SimpleNamespace
- import unittest
- from unittest.mock import Mock
- import numpy as np
- import pygame
- from client_local.utils.transparent_range import AlphaTransition, normalize_ranges
- from test_render_optimizations import load_definitions
- class TopUpdateTest(unittest.TestCase):
- def setUp(self):
- self.flags = SimpleNamespace(GAME_SQUARE=40, HALF_GAME_SQUARE=20,
- MAIN_AREA_X_POS=80, MAIN_AREA_Y_POS=52)
- self.ns = load_definitions("client_local/level/level.py", {"pygame": pygame, "G": self.flags,
- "Y_SCALE_FACTOR": 1, "BombInstance": SimpleNamespace(sound_played=False)}, classes={"Level": {
- "get_render_view_rect", "refresh_top_update_obstacles", "update_obstacles_update_list", "update"}})
- cl = self.cl = self.ns["Level"]()
- cl.scroll_x_pos = cl.scroll_y_pos = 0
- cl.main_area = pygame.Surface((800, 1200))
- cl.top_update_obstacles = []
- cl._visible_top_update_obstacles = ()
- cl.water_reflect_obstacles = []
- cl.transport_obstacle = []
- cl.slide_orientation = {}
- cl.block = np.zeros((2, 30, 31), dtype=int)
- cl.block_flame = np.zeros((2, 30, 31), dtype=int)
- cl.obstacle_instances = {}
- cl.obstacle_instances_update = []
- cl.map_text_triggers = []
- cl.obstacle_instances_need_to_update = True
- cl._static_dirty = True
- cl._static_sorted = []
- cl.current_time = 1000
- class Updatable:
- def __init__(self, x, y):
- self.x, self.y = x, y
- self.x_pos, self.y_pos = x*40+20, y*40+20
- def get_y(self):
- return self.y
- ns = load_definitions("client_local/sprite/obstacle_instance.py", {"pygame": pygame, "enum": enum,
- "G": self.flags, "Updatable": Updatable, "level": SimpleNamespace(current_level=cl),
- "AlphaTransition": AlphaTransition, "normalize_ranges": normalize_ranges},
- classes={"ObstacleInstance": None, "ObstacleState": None})
- self.Obstacle = ns["ObstacleInstance"]
- def obstacle(self, x=1, y=3, enabled=True, frames=None):
- if frames is None:
- image = pygame.Surface((30, 140), pygame.SRCALPHA)
- image.fill((40, 90, 220, 255))
- frames = [SimpleNamespace(image=image, cx=0, cy=-100)]
- data = dict(WIDTH=1, HEIGHT=1, BLOCK=[[[0]], [[0]]], BLOCK_FLAME=[[[0]], [[0]]],
- CAN_HIDE=False, BREAKABLE=False, INTERVAL=10, TOP_UPDATE=enabled, STAND=frames)
- obstacle = self.Obstacle(x, y, self.cl.obstacle_instances, data, {})
- obstacle.update = Mock(wraps=obstacle.update)
- obstacle.draw = Mock(wraps=obstacle.draw)
- obstacle.get_draw_image = Mock(wraps=obstacle.get_draw_image)
- return obstacle
- def prepare_pipeline(self):
- cl = self.cl
- for name in ("bomb_instances", "wanderers", "teammates", "npcs", "pets", "ghosts", "flame_instances",
- "skill_instances", "effects_behind", "effects_front", "effects_screen", "ui_instances_top", "ui_instances_bot"):
- setattr(cl, name, [])
- cl.score_board = cl.time_board = None
- cl.intro_chat_started = cl.intro_chat_active = True
- cl.grid_damage_frame = 0
- cl.item_instances = {}
- cl._cached_item_count = 0
- cl.transport_buff_num = 0
- cl.grid_damage_orientations = set()
- cl.floor_image = pygame.Surface(cl.main_area.get_size())
- cl.floor_image.fill((0, 0, 0))
- cl.left_black = pygame.Surface((1, 1))
- cl.me = SimpleNamespace(update=Mock(), draw=Mock(), get_y=lambda: 0)
- cl.draw_dynamic_object = lambda obj: obj.draw(cl.main_area)
- for name in ("run_earth_quake_effect", "display_frame_rate", "start_ending_sequence_if_needed", "pass_map",
- "portal_map", "draw_background_png", "apply_light_masks", "update_particles"):
- setattr(cl, name, Mock())
- return pygame.Surface((100, 100))
- def test_default_false_keeps_offscreen_anchor_culled(self):
- tree = self.obstacle(enabled=False)
- screen = self.prepare_pipeline()
- self.cl.update(screen)
- self.assertEqual(self.cl.top_update_obstacles, [])
- self.assertEqual(self.cl.obstacle_instances_update, [])
- tree.update.assert_not_called()
- tree.draw.assert_not_called()
- self.assertEqual(tuple(self.cl.main_area.get_at((45, 25)))[:3], (0, 0, 0))
- def test_top_is_rendered_when_anchor_below_view_and_keeps_y_sort(self):
- tree = self.obstacle()
- screen = self.prepare_pipeline()
- self.assertEqual(tree.rect.size, (1, 1))
- self.cl.update(screen)
- self.assertEqual(self.cl.obstacle_instances_update, [])
- self.assertEqual(self.cl._visible_top_update_obstacles, (tree,))
- self.assertEqual(tuple(self.cl.main_area.get_at((45, 25)))[:3], (40, 90, 220))
- tree.update.assert_called_once_with()
- tree.draw.assert_called_once()
- red = pygame.Surface((10, 10))
- red.fill((220, 30, 30))
- front = SimpleNamespace(remain_blood=10, update=Mock(), get_y=lambda: 4,
- draw=lambda dest: dest.blit(red, (40, 20)))
- self.cl.npcs = [front]
- self.cl.update(screen)
- self.assertEqual(tuple(self.cl.main_area.get_at((45, 25)))[:3], (220, 30, 30))
- def test_union_of_animation_frames_wakes_up_a_long_frame(self):
- short = pygame.Surface((30, 20), pygame.SRCALPHA)
- short.fill((10, 20, 30, 255))
- tall = pygame.Surface((30, 140), pygame.SRCALPHA)
- tall.fill((40, 90, 220, 255))
- tree = self.obstacle(frames=[SimpleNamespace(image=tall, cx=0, cy=-100),
- SimpleNamespace(image=short, cx=0, cy=0)])
- self.assertIs(tree.image, short) # Current short frame is entirely outside the screen.
- screen = self.prepare_pipeline()
- tree.obstacle_timer = -1000
- self.cl.update(screen)
- self.assertIs(tree.image, tall)
- self.assertEqual(tuple(self.cl.main_area.get_at((45, 25)))[:3], (40, 90, 220))
- def test_camera_exit_and_reentry_resume_without_duplicate_updates(self):
- tree = self.obstacle(y=1)
- tree.obstacle["WIDTH"] = 2
- self.cl.obstacle_instances[(2, 1)] = tree
- screen = self.prepare_pipeline()
- self.cl.update(screen)
- self.assertGreaterEqual(self.cl.obstacle_instances_update.count(tree), 2)
- self.assertEqual(tree.update.call_count, 1)
- self.cl.scroll_x_pos = 400
- self.cl.obstacle_instances_need_to_update = True
- self.cl.update(screen)
- self.assertEqual(tree.update.call_count, 1)
- self.assertEqual(self.cl._visible_top_update_obstacles, ())
- self.cl.scroll_x_pos = 0
- self.cl.obstacle_instances_need_to_update = True
- self.cl.update(screen)
- self.assertEqual(tree.update.call_count, 2)
- self.assertEqual(tree.get_draw_image.call_count, 2) # Multi-cell draw calls still blit once per frame.
- self.assertFalse(tree.has_updated)
- def test_bounds_cache_tracks_position_type_changes_and_uninstall(self):
- tree = self.obstacle()
- bounds = tree.get_top_update_bounds()
- self.assertEqual(bounds, pygame.Rect(40, 20, 30, 140))
- tree.x += 2
- self.assertEqual(tree.get_top_update_bounds().x, bounds.x+80)
- tree.x -= 2
- image = pygame.Surface((70, 200))
- tree.obstacle["STAND"] = [SimpleNamespace(image=image, cx=-10, cy=-180)]
- tree.current_type += 1
- self.assertEqual(tree.get_top_update_bounds(), pygame.Rect(30, -60, 70, 200))
- tree.uninstall()
- self.assertNotIn(tree, self.cl.top_update_obstacles)
- self.cl.refresh_top_update_obstacles()
- self.assertEqual(self.cl._visible_top_update_obstacles, ())
- def test_horizontal_edges_and_earthquake_use_image_bounds(self):
- frame = SimpleNamespace(image=pygame.Surface((30, 140)), cx=-2, cy=-100)
- tree = self.obstacle(x=2, frames=[frame])
- self.cl.update_obstacles_update_list()
- self.cl.refresh_top_update_obstacles()
- self.assertEqual(self.cl._visible_top_update_obstacles, (tree,)) # Image enters at x=78; grid starts at x=80.
- tree.x = 3
- self.cl.refresh_top_update_obstacles()
- self.assertEqual(self.cl._visible_top_update_obstacles, ())
- tree.x = 1
- self.ns["Y_SCALE_FACTOR"] = 0.8
- self.cl.refresh_top_update_obstacles()
- self.assertEqual(self.cl._visible_top_update_obstacles, (tree,))
- if __name__ == "__main__":
- unittest.main()
|