Added Cache and some improvements.
This commit is contained in:
parent
5c5ed49a9b
commit
8da526f2ca
2 changed files with 26 additions and 9 deletions
33
engine.py
33
engine.py
|
@ -1,17 +1,17 @@
|
|||
#!/usr/bin/python3
|
||||
import pygame
|
||||
from dataclasses import dataclass
|
||||
from wobbl_tools import pg
|
||||
from wobbl_tools import data_file
|
||||
|
||||
|
||||
class Object:
|
||||
def __init__(self, sprite_path: str="sprites/coffee_cup.png", sprite_spacing: int=1, rotation: int=45, size_multiplier: int=16):
|
||||
class Model:
|
||||
def __init__(self, sprite_path: str="sprites/coffee_cup.png", size_multiplier: int=16, sprite_spacing: int=1, rotation: int=45):
|
||||
self.sprite_path = sprite_path
|
||||
self.sprite_spacing = sprite_spacing
|
||||
self.rotation = rotation
|
||||
self.size_multiplier = size_multiplier
|
||||
|
||||
self.cache = {}
|
||||
|
||||
self.sprite_size = tuple(get_json(get_dir(sprite_path) + "/sprite_properties.json")[get_filename(sprite_path)]["size"])
|
||||
|
||||
self.original_sprites = self.get_sprites()
|
||||
|
@ -34,6 +34,9 @@ class Object:
|
|||
return sprites
|
||||
|
||||
def rotate_sprites(self):
|
||||
if self.rotation in self.cache:
|
||||
return self.original_sprites
|
||||
|
||||
sprites = []
|
||||
|
||||
for sprite in self.original_sprites:
|
||||
|
@ -44,6 +47,9 @@ class Object:
|
|||
return sprites
|
||||
|
||||
def generate_surface(self):
|
||||
if self.rotation in self.cache:
|
||||
return self.cache[self.rotation]
|
||||
|
||||
sw, sh = self.sprites[0].get_size()
|
||||
|
||||
surface = pygame.Surface((sw, sh + self.sprite_spacing * len(self.sprites)), flags=pygame.SRCALPHA)
|
||||
|
@ -56,12 +62,27 @@ class Object:
|
|||
|
||||
surface = pygame.transform.scale(surface, (surface.get_width() * self.size_multiplier, surface.get_height() * self.size_multiplier))
|
||||
|
||||
self.cache[self.rotation] = surface
|
||||
|
||||
return surface
|
||||
|
||||
def update(self):
|
||||
return self.rotate_sprites(), self.generate_surface()
|
||||
|
||||
|
||||
class Object:
|
||||
def __init__(self, model: Model, position: tuple, rotation: int=45):
|
||||
self.model = model
|
||||
self.position = position
|
||||
self.rotation = rotation
|
||||
|
||||
def update(self):
|
||||
self.model.update()
|
||||
|
||||
def draw(self, surface):
|
||||
surface.blit(self.model.cache[self.rotation])
|
||||
|
||||
|
||||
def get_dir(path: str):
|
||||
return path.rsplit("/", 1)[0]
|
||||
|
||||
|
@ -76,7 +97,3 @@ def get_json(path: str):
|
|||
file.close()
|
||||
|
||||
return data
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
bla = Object()
|
||||
|
|
|
@ -15,7 +15,7 @@ class ModelViewer:
|
|||
self.window_size = window_size
|
||||
self.fps = fps
|
||||
self.rotating = True
|
||||
self.model = engine.Object(size_multiplier=16)
|
||||
self.model = engine.Model(size_multiplier=4, sprite_spacing=1)
|
||||
self.rotation_cooldown = 5
|
||||
|
||||
self.clock = pygame.time.Clock()
|
||||
|
|
Loading…
Reference in a new issue