test_obstacle_top_update.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. """TOP_UPDATE must extend visibility without changing depth ordering or defaults."""
  2. import enum
  3. from types import SimpleNamespace
  4. import unittest
  5. from unittest.mock import Mock
  6. import numpy as np
  7. import pygame
  8. from client_local.utils.transparent_range import AlphaTransition, normalize_ranges
  9. from test_render_optimizations import load_definitions
  10. class TopUpdateTest(unittest.TestCase):
  11. def setUp(self):
  12. self.flags = SimpleNamespace(GAME_SQUARE=40, HALF_GAME_SQUARE=20,
  13. MAIN_AREA_X_POS=80, MAIN_AREA_Y_POS=52)
  14. self.ns = load_definitions("client_local/level/level.py", {"pygame": pygame, "G": self.flags,
  15. "Y_SCALE_FACTOR": 1, "BombInstance": SimpleNamespace(sound_played=False)}, classes={"Level": {
  16. "get_render_view_rect", "refresh_top_update_obstacles", "update_obstacles_update_list", "update"}})
  17. cl = self.cl = self.ns["Level"]()
  18. cl.scroll_x_pos = cl.scroll_y_pos = 0
  19. cl.main_area = pygame.Surface((800, 1200))
  20. cl.top_update_obstacles = []
  21. cl._visible_top_update_obstacles = ()
  22. cl.water_reflect_obstacles = []
  23. cl.transport_obstacle = []
  24. cl.slide_orientation = {}
  25. cl.block = np.zeros((2, 30, 31), dtype=int)
  26. cl.block_flame = np.zeros((2, 30, 31), dtype=int)
  27. cl.obstacle_instances = {}
  28. cl.obstacle_instances_update = []
  29. cl.map_text_triggers = []
  30. cl.obstacle_instances_need_to_update = True
  31. cl._static_dirty = True
  32. cl._static_sorted = []
  33. cl.current_time = 1000
  34. class Updatable:
  35. def __init__(self, x, y):
  36. self.x, self.y = x, y
  37. self.x_pos, self.y_pos = x*40+20, y*40+20
  38. def get_y(self):
  39. return self.y
  40. ns = load_definitions("client_local/sprite/obstacle_instance.py", {"pygame": pygame, "enum": enum,
  41. "G": self.flags, "Updatable": Updatable, "level": SimpleNamespace(current_level=cl),
  42. "AlphaTransition": AlphaTransition, "normalize_ranges": normalize_ranges},
  43. classes={"ObstacleInstance": None, "ObstacleState": None})
  44. self.Obstacle = ns["ObstacleInstance"]
  45. def obstacle(self, x=1, y=3, enabled=True, frames=None):
  46. if frames is None:
  47. image = pygame.Surface((30, 140), pygame.SRCALPHA)
  48. image.fill((40, 90, 220, 255))
  49. frames = [SimpleNamespace(image=image, cx=0, cy=-100)]
  50. data = dict(WIDTH=1, HEIGHT=1, BLOCK=[[[0]], [[0]]], BLOCK_FLAME=[[[0]], [[0]]],
  51. CAN_HIDE=False, BREAKABLE=False, INTERVAL=10, TOP_UPDATE=enabled, STAND=frames)
  52. obstacle = self.Obstacle(x, y, self.cl.obstacle_instances, data, {})
  53. obstacle.update = Mock(wraps=obstacle.update)
  54. obstacle.draw = Mock(wraps=obstacle.draw)
  55. obstacle.get_draw_image = Mock(wraps=obstacle.get_draw_image)
  56. return obstacle
  57. def prepare_pipeline(self):
  58. cl = self.cl
  59. for name in ("bomb_instances", "wanderers", "teammates", "npcs", "pets", "ghosts", "flame_instances",
  60. "skill_instances", "effects_behind", "effects_front", "effects_screen", "ui_instances_top", "ui_instances_bot"):
  61. setattr(cl, name, [])
  62. cl.score_board = cl.time_board = None
  63. cl.intro_chat_started = cl.intro_chat_active = True
  64. cl.grid_damage_frame = 0
  65. cl.item_instances = {}
  66. cl._cached_item_count = 0
  67. cl.transport_buff_num = 0
  68. cl.grid_damage_orientations = set()
  69. cl.floor_image = pygame.Surface(cl.main_area.get_size())
  70. cl.floor_image.fill((0, 0, 0))
  71. cl.left_black = pygame.Surface((1, 1))
  72. cl.me = SimpleNamespace(update=Mock(), draw=Mock(), get_y=lambda: 0)
  73. cl.draw_dynamic_object = lambda obj: obj.draw(cl.main_area)
  74. for name in ("run_earth_quake_effect", "display_frame_rate", "start_ending_sequence_if_needed", "pass_map",
  75. "portal_map", "draw_background_png", "apply_light_masks", "update_particles"):
  76. setattr(cl, name, Mock())
  77. return pygame.Surface((100, 100))
  78. def test_default_false_keeps_offscreen_anchor_culled(self):
  79. tree = self.obstacle(enabled=False)
  80. screen = self.prepare_pipeline()
  81. self.cl.update(screen)
  82. self.assertEqual(self.cl.top_update_obstacles, [])
  83. self.assertEqual(self.cl.obstacle_instances_update, [])
  84. tree.update.assert_not_called()
  85. tree.draw.assert_not_called()
  86. self.assertEqual(tuple(self.cl.main_area.get_at((45, 25)))[:3], (0, 0, 0))
  87. def test_top_is_rendered_when_anchor_below_view_and_keeps_y_sort(self):
  88. tree = self.obstacle()
  89. screen = self.prepare_pipeline()
  90. self.assertEqual(tree.rect.size, (1, 1))
  91. self.cl.update(screen)
  92. self.assertEqual(self.cl.obstacle_instances_update, [])
  93. self.assertEqual(self.cl._visible_top_update_obstacles, (tree,))
  94. self.assertEqual(tuple(self.cl.main_area.get_at((45, 25)))[:3], (40, 90, 220))
  95. tree.update.assert_called_once_with()
  96. tree.draw.assert_called_once()
  97. red = pygame.Surface((10, 10))
  98. red.fill((220, 30, 30))
  99. front = SimpleNamespace(remain_blood=10, update=Mock(), get_y=lambda: 4,
  100. draw=lambda dest: dest.blit(red, (40, 20)))
  101. self.cl.npcs = [front]
  102. self.cl.update(screen)
  103. self.assertEqual(tuple(self.cl.main_area.get_at((45, 25)))[:3], (220, 30, 30))
  104. def test_union_of_animation_frames_wakes_up_a_long_frame(self):
  105. short = pygame.Surface((30, 20), pygame.SRCALPHA)
  106. short.fill((10, 20, 30, 255))
  107. tall = pygame.Surface((30, 140), pygame.SRCALPHA)
  108. tall.fill((40, 90, 220, 255))
  109. tree = self.obstacle(frames=[SimpleNamespace(image=tall, cx=0, cy=-100),
  110. SimpleNamespace(image=short, cx=0, cy=0)])
  111. self.assertIs(tree.image, short) # Current short frame is entirely outside the screen.
  112. screen = self.prepare_pipeline()
  113. tree.obstacle_timer = -1000
  114. self.cl.update(screen)
  115. self.assertIs(tree.image, tall)
  116. self.assertEqual(tuple(self.cl.main_area.get_at((45, 25)))[:3], (40, 90, 220))
  117. def test_camera_exit_and_reentry_resume_without_duplicate_updates(self):
  118. tree = self.obstacle(y=1)
  119. tree.obstacle["WIDTH"] = 2
  120. self.cl.obstacle_instances[(2, 1)] = tree
  121. screen = self.prepare_pipeline()
  122. self.cl.update(screen)
  123. self.assertGreaterEqual(self.cl.obstacle_instances_update.count(tree), 2)
  124. self.assertEqual(tree.update.call_count, 1)
  125. self.cl.scroll_x_pos = 400
  126. self.cl.obstacle_instances_need_to_update = True
  127. self.cl.update(screen)
  128. self.assertEqual(tree.update.call_count, 1)
  129. self.assertEqual(self.cl._visible_top_update_obstacles, ())
  130. self.cl.scroll_x_pos = 0
  131. self.cl.obstacle_instances_need_to_update = True
  132. self.cl.update(screen)
  133. self.assertEqual(tree.update.call_count, 2)
  134. self.assertEqual(tree.get_draw_image.call_count, 2) # Multi-cell draw calls still blit once per frame.
  135. self.assertFalse(tree.has_updated)
  136. def test_bounds_cache_tracks_position_type_changes_and_uninstall(self):
  137. tree = self.obstacle()
  138. bounds = tree.get_top_update_bounds()
  139. self.assertEqual(bounds, pygame.Rect(40, 20, 30, 140))
  140. tree.x += 2
  141. self.assertEqual(tree.get_top_update_bounds().x, bounds.x+80)
  142. tree.x -= 2
  143. image = pygame.Surface((70, 200))
  144. tree.obstacle["STAND"] = [SimpleNamespace(image=image, cx=-10, cy=-180)]
  145. tree.current_type += 1
  146. self.assertEqual(tree.get_top_update_bounds(), pygame.Rect(30, -60, 70, 200))
  147. tree.uninstall()
  148. self.assertNotIn(tree, self.cl.top_update_obstacles)
  149. self.cl.refresh_top_update_obstacles()
  150. self.assertEqual(self.cl._visible_top_update_obstacles, ())
  151. def test_horizontal_edges_and_earthquake_use_image_bounds(self):
  152. frame = SimpleNamespace(image=pygame.Surface((30, 140)), cx=-2, cy=-100)
  153. tree = self.obstacle(x=2, frames=[frame])
  154. self.cl.update_obstacles_update_list()
  155. self.cl.refresh_top_update_obstacles()
  156. self.assertEqual(self.cl._visible_top_update_obstacles, (tree,)) # Image enters at x=78; grid starts at x=80.
  157. tree.x = 3
  158. self.cl.refresh_top_update_obstacles()
  159. self.assertEqual(self.cl._visible_top_update_obstacles, ())
  160. tree.x = 1
  161. self.ns["Y_SCALE_FACTOR"] = 0.8
  162. self.cl.refresh_top_update_obstacles()
  163. self.assertEqual(self.cl._visible_top_update_obstacles, (tree,))
  164. if __name__ == "__main__":
  165. unittest.main()