Optimization on level loading

main
Yan Wittmann 2023-03-27 13:08:22 +02:00
parent 48c8163609
commit 44d3ab01bf
3 changed files with 31 additions and 15 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.8 KiB

After

Width:  |  Height:  |  Size: 2.2 KiB

View File

@ -29,8 +29,10 @@ class LoadedLevel:
def load_level(self, level: Level): def load_level(self, level: Level):
for row_number, row in enumerate(level.tiles): for row_number, row in enumerate(level.tiles):
for tile_number, tile in enumerate(row): for tile_number, tile in enumerate(row):
world_position = (tile_number, row_number)
position = (tile_number * ConstantsParser.CONFIG.block_size[0], position = (tile_number * ConstantsParser.CONFIG.block_size[0],
row_number * ConstantsParser.CONFIG.block_size[1]) row_number * ConstantsParser.CONFIG.block_size[1])
tile['world_position'] = world_position
tile['position'] = position tile['position'] = position
name = tile['name'] name = tile['name']
@ -47,6 +49,28 @@ class LoadedLevel:
self.sprite_manager.add_ui_element(DrawLayers.OBJECTS, element) self.sprite_manager.add_ui_element(DrawLayers.OBJECTS, element)
element.load(self.sprite_manager, self.spritesheet_manager, level) element.load(self.sprite_manager, self.spritesheet_manager, level)
if isinstance(element, SimpleBlockLevelElement):
if self.is_surrounded_by_blocks(world_position, level.tiles):
element.is_collider = False
# self.sprite_manager.remove_ui_element(element)
def is_surrounded_by_blocks(self, position, tiles):
for i in range(-1, 2):
for j in range(-1, 2):
if i == 0 and j == 0:
continue
if position[0] + i < 0 or position[0] + i >= len(tiles[0]):
continue
if position[1] + j < 0 or position[1] + j >= len(tiles):
continue
tile = tiles[position[1] + j][position[0] + i]
if not tile['name'] in ['#', '+', '-', 'l', 'r', '~', '|']:
return False
return True
def destroy_level(self): def destroy_level(self):
for block in self.blocks: for block in self.blocks:
self.sprite_manager.remove_ui_element(block) self.sprite_manager.remove_ui_element(block)

View File

@ -3,13 +3,11 @@ from physics.TickData import TickData
from sprite.DynamicSprite import DynamicSprite from sprite.DynamicSprite import DynamicSprite
from sprite.Spritesheet import Spritesheet from sprite.Spritesheet import Spritesheet
from ui_elements.KeyManager import KeyManager from ui_elements.KeyManager import KeyManager
from ui_elements.TextLabel import TextLabel
class PlayerSprite(DynamicSprite): class PlayerSprite(DynamicSprite):
def __init__(self, spritesheet: Spritesheet): def __init__(self, spritesheet: Spritesheet):
super().__init__(spritesheet) super().__init__(spritesheet)
self.cheat_fly = False
self.jump_time = -1 self.jump_time = -1
self.allowed_jump_time = 12 self.allowed_jump_time = 12
@ -33,7 +31,6 @@ class PlayerSprite(DynamicSprite):
if self.motion[0] > -self.max_motion_horizontal_via_input: if self.motion[0] > -self.max_motion_horizontal_via_input:
self.motion = (self.motion[0] - self.acceleration_horizontal, self.motion[1]) self.motion = (self.motion[0] - self.acceleration_horizontal, self.motion[1])
if not self.cheat_fly:
if tick_data.key_manager.is_keymap_down(KeyManager.KEY_UP): if tick_data.key_manager.is_keymap_down(KeyManager.KEY_UP):
if self.jump_time < 0 and self.get_collides_with_direction(CollisionDirection.BOTTOM): if self.jump_time < 0 and self.get_collides_with_direction(CollisionDirection.BOTTOM):
self.jump_time = self.allowed_jump_time self.jump_time = self.allowed_jump_time
@ -42,8 +39,3 @@ class PlayerSprite(DynamicSprite):
self.motion = (self.motion[0], self.motion[1] - 0.5) self.motion = (self.motion[0], self.motion[1] - 0.5)
if self.jump_time >= 0: if self.jump_time >= 0:
self.jump_time -= 1 self.jump_time -= 1
else:
if tick_data.key_manager.is_keymap_down(KeyManager.KEY_UP):
self.motion = (self.motion[0], self.motion[1] - 2)
if tick_data.key_manager.is_keymap_down(KeyManager.KEY_DOWN):
self.motion = (self.motion[0], self.motion[1] + 2)