79 lines
2.4 KiB
Python
79 lines
2.4 KiB
Python
|
#!/usr/bin/python3
|
||
|
import tools.pg
|
||
|
from tools.pg import crop_surface
|
||
|
import pygame
|
||
|
|
||
|
|
||
|
class Object:
|
||
|
def __init__(self, model_path: str, model_properties_path: str=None, sprite_spacing: int=1, size_multiplier: int=1):
|
||
|
self.model_path = model_path
|
||
|
|
||
|
if model_properties_path is None:
|
||
|
self.model_properties_path = self.get_directory(model_path) + "/model_properties"
|
||
|
print(self.model_properties_path)
|
||
|
|
||
|
else:
|
||
|
self.model_properties_path = model_properties_path
|
||
|
|
||
|
self.sprite_spacing = sprite_spacing
|
||
|
self.size_multiplier = size_multiplier
|
||
|
|
||
|
self.rotation = 0
|
||
|
self.model_name = self.get_filename(model_path)
|
||
|
self.model_properties = __import__(self.model_properties_path.replace("/", "."), fromlist=self.get_directory(model_path).replace("/", ".")).properties[self.model_name]
|
||
|
self.sprite_size = self.model_properties["sprite_size"]
|
||
|
|
||
|
self.sprites = self.generate_sprites()
|
||
|
self.original_sprites = self.sprites
|
||
|
self.surface = self.generate_surface()
|
||
|
|
||
|
def get_directory(self, file_path):
|
||
|
return file_path.rsplit("/", 1)[0]
|
||
|
|
||
|
def get_filename(self, file_path):
|
||
|
return file_path.rsplit("/", 1)[1]
|
||
|
|
||
|
def generate_sprites(self):
|
||
|
model = pygame.image.load(self.model_path)
|
||
|
|
||
|
sw, sh = self.sprite_size
|
||
|
mw, mh = model.get_size()
|
||
|
|
||
|
sprites = []
|
||
|
for x in range(0, mw, sw):
|
||
|
sprite = tools.pg.crop_surface(model, (x, 0), (sw, sh))
|
||
|
sprite = pygame.transform.scale(sprite, (sprite.get_width() * self.size_multiplier, sprite.get_height() * self.size_multiplier))
|
||
|
sprites.append(sprite)
|
||
|
|
||
|
return sprites
|
||
|
|
||
|
def generate_surface(self):
|
||
|
sw, sh = self.sprites[0].get_size()
|
||
|
|
||
|
surface = pygame.Surface((sw, sh + self.sprite_spacing * len(self.sprites)), flags=pygame.SRCALPHA)
|
||
|
|
||
|
y = surface.get_height() - sh
|
||
|
|
||
|
for sprite in self.sprites:
|
||
|
surface.blit(sprite, (0, y))
|
||
|
y -= self.sprite_spacing
|
||
|
|
||
|
return surface
|
||
|
|
||
|
def rotate_model(self):
|
||
|
sprites = []
|
||
|
|
||
|
for sprite in self.original_sprites:
|
||
|
sprite = pygame.transform.rotate(sprite, self.rotation)
|
||
|
|
||
|
sprites.append(sprite)
|
||
|
|
||
|
return sprites
|
||
|
|
||
|
def blit(self, surface, position):
|
||
|
surface.blit(self.surface, position)
|
||
|
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
pass
|