Оптимизация кода для увеличения FPS
Я писал редактор карт для моей игры.
Код:
from ursina import *
class Room:
def __init__(self, tag):
self.tag = tag
self.position = Vec3(0, 0, 0)
self.room_data = {}
self.wall_entities = []
self.floor_entities = []
def load_room_data(self, data: dict):
for entity in self.floor_entities:
entity.disable()
del entity
for entity in self.wall_entities:
entity.disable()
del entity
for floor_name in data["floors"]:
floor = data["floors"][floor_name]
pos = Vec3(floor[1], floor[2], floor[3])
rot = Vec3(floor[4], floor[5], floor[6])
scale = Vec3(floor[7], floor[8], floor[9])
uvs_scale = Vec2(floor[7],floor[9])
collider = floor[10]
floor_entity = Entity(model='plane', texture='../textures/tilefloor.jpg',collider=collider)
floor_entity.position = pos
floor_entity.rotation = rot
floor_entity.scale = scale
floor_entity.uvs = uvs_scale
self.floor_entities.append(floor_entity)
for wall_name in data["walls"]:
wall = data["walls"][wall_name]
pos = Vec3(wall[1], wall[2], wall[3])
rot = Vec3(wall[4], wall[5], wall[6])
scale = Vec3(wall[7], wall[8], wall[9])
collider = wall[10]
uvs_scale = Vec2(wall[7],wall[9])
if wall[0] == "lite":
wall_entity = Entity(model='quad', texture="../textures/whitewall.jpg")
else:
wall_entity = Entity(modle='quad',texture='../textures/error.png')
wall_entity.position = pos
wall_entity.rotation = rot
wall_entity.scale = scale
wall_entity.uvs = uvs_scale
self.wall_entities.append(wall_entity)
self.room_data = data
room_editor_app = Ursina()
room = Room('new_room')
room_data = {
"floors": {},
"walls": {},
"props": {},
"items": {}
}
def new_floor():
room_data["floors"][id_enter.text] = ([None,0,0,0,0,0,0,1,1,1,'box'])
def new_wall():
room_data["walls"][id_enter.text] = (['lite',0,0,0,0,0,0,1,1,1,'box'])
def update():
room.load_room_data(room_data)
def apply_settings():
id = id_enter_for_settings.text
pos = pos_entry.text
rot = rot_entry.text
scale = scale_entry.text
if id.startswith('f'):
positions = pos.split(',')
rotations = rot.split(',')
scales = scale.split(',')
room_data["floors"][id[1:]] = ([None,float(positions[0]),float(positions[1]),float(positions[2]),float(rotations[0]),float(rotations[1]),float(rotations[2]),float(scales[0]),float(scales[1]),float(scales[2]),'box'])
elif id.startswith('w'):
positions = pos.split(',')
rotations = rot.split(',')
scales = scale.split(',')
room_data["walls"][id[1:]] = (['lite',float(positions[0]),float(positions[1]),float(positions[2]),float(rotations[0]),float(rotations[1]),float(rotations[2]),float(scales[0]),float(scales[1]),float(scales[2]),'box'])
def delete_object():
id = id_enter_for_settings.text
if id.startswith('f'):
del room_data["floors"][id[1:]]
elif id.startswith('w'):
del room_data["walls"][id[1:]]
camera = EditorCamera()
new_floor = Button(text='New floor',on_click=new_floor)
new_floor.scale = Vec2(0.1, 0.05)
new_floor.position=Vec2(-0.4, 0.4)
new_floor.text_size=1
new_wall = Button(text='New wall',on_click=new_wall)
new_wall.scale = Vec2(0.1, 0.05)
new_wall.position=Vec2(-0.4, 0.3)
new_wall.text_size=1
rot_entry_text = Text("ID to spawn object")
rot_entry_text.position = Vec2(-0.65, 0.25)
id_enter = InputField('')
id_enter.position = Vec2(-0.4, 0.2)
id_enter.scale = Vec2(0.5, 0.05)
id_enter.text_size=1
id_enter_for_settings_text = Text('ID for object to setting, example - fFloor1 or wWall1')
id_enter_for_settings_text.position = Vec2(0.25, 0.45)
id_enter_for_settings = InputField('')
id_enter_for_settings.position = Vec2(0.5, 0.4)
id_enter_for_settings.scale = Vec2(0.5, 0.05)
id_enter_for_settings.text_size=1
pos_entry_text = Text("Pos (x,y,z)")
pos_entry_text.position = Vec2(0.25, 0.35)
pos_entry = InputField('0,0,0')
pos_entry.position = Vec2(0.5, 0.3)
pos_entry.scale = Vec2(0.5, 0.05)
pos_entry.text_size=1
rot_entry_text = Text("Rot (x,y,z)")
rot_entry_text.position = Vec2(0.25, 0.25)
rot_entry = InputField('')
rot_entry.position = Vec2(0.5, 0.2)
rot_entry.scale = Vec2(0.5, 0.05)
rot_entry.text_size=1
scale_entry_text = Text("Scale (x,y,z)")
scale_entry_text.position = Vec2(0.25, 0.15)
scale_entry = InputField('')
scale_entry.position = Vec2(0.5, 0.1)
scale_entry.scale = Vec2(0.5, 0.05)
scale_entry.text_size=1
button_apply = Button(text='Apply settings',on_click=lambda:apply_settings())
button_apply.scale = Vec2(0.1, 0.05)
button_apply.position=Vec2(0.5, 0)
button_apply.text_size=1
button_apply = Button(text='Delete objects',on_click=lambda:delete_object())
button_apply.scale = Vec2(0.1, 0.05)
button_apply.position=Vec2(0.5, -0.1)
button_apply.text_size=1
room_editor_app.run()
Но я столкнулся с проблемой: дело в том, что падает FPS, причём с каждым разом всё меньше и меньше (причём постепенно и медленно).
Падание FPS появляется только после применения настроек (функция apply_settings
). Скорее всего я что-то начудил в своём коде.
Подскажите, что не так?