Added sprite properties files.

This commit is contained in:
The Wobbler 2024-07-19 15:30:43 +02:00
parent 98f6f9fa7e
commit 9b1d91934b
4 changed files with 60 additions and 19 deletions

View file

@ -0,0 +1,7 @@
{
"Bla": {
"path": "../assets/stacked_sprites/animated/bla/",
"layers": 3,
"scale": 16
}
}

View file

@ -0,0 +1,12 @@
{
"Robot": {
"path": "../assets/stacked_sprites/Robot.png",
"layers": 16,
"scale": 8
},
"Building": {
"path": "../assets/stacked_sprites/Building.png",
"layers": 80,
"scale": 4
}
}

View file

@ -1,12 +1,23 @@
#!/usr/bin/python3 #!/usr/bin/python3
import pygame import pygame
import json
def load_json(path):
file = open(path, "r")
data = json.load(file)
file.close()
return data
vec2 = pygame.math.Vector2 vec2 = pygame.math.Vector2
RES = WIDTH, HEIGHT = vec2(1400, 800) RES = WIDTH, HEIGHT = vec2(1400, 800)
CENTER = H_WIDTH, H_HEIGHT = RES // 2 CENTER = H_WIDTH, H_HEIGHT = RES // 2
TILE_SIZE = 250 TILE_SIZE = 250
DEFAULT_SCALE = 4
PLAYER_SPEED = 0.4 PLAYER_SPEED = 0.4
PLAYER_ROT_SPEED = 0.0015 PLAYER_ROT_SPEED = 0.0015
@ -16,23 +27,26 @@ NUM_ANGLES = 180 # multiple of 360
TRANSP_COLOR = (45, 54, 76) TRANSP_COLOR = (45, 54, 76)
OUTLINE = True OUTLINE = True
SPRITE_ATTRS = { SPRITE_ATTRS = load_json("../assets/stacked_sprites/sprite_properties.json")
"Robot": { ANIMATED_SPRITE_ATTRS = load_json("../assets/stacked_sprites/animated/sprite_properties.json")
"path": "../assets/stacked_sprites/Robot.png",
"layers": 16,
"scale": 8
},
"Building": {
"path": "../assets/stacked_sprites/Building.png",
"layers": 80,
"scale": 4
}
}
ANIMATED_SPRITE_ATTRS = { # SPRITE_ATTRS = {
"Bla": { # "Robot": {
"path": "../assets/stacked_sprites/animated/bla/", # "path": "../assets/stacked_sprites/Robot.png",
"layers": 3, # "layers": 16,
"scale": 16 # "scale": 8
} # },
} # "Building": {
# "path": "../assets/stacked_sprites/Building.png",
# "layers": 80,
# "scale": 4
# }
# }
# ANIMATED_SPRITE_ATTRS = {
# "Bla": {
# "path": "../assets/stacked_sprites/animated/bla/",
# "layers": 3,
# "scale": 16
# }
# }

View file

@ -15,6 +15,10 @@ class StackedSprite(pygame.sprite.Sprite):
super().__init__(self.group) super().__init__(self.group)
self.attrs = SPRITE_ATTRS[name] # get attributes self.attrs = SPRITE_ATTRS[name] # get attributes
if not "scale" in self.attrs:
self.attrs["scale"] = DEFAULT_SCALE
self.y_offset = vec2(0, -self.attrs["layers"] / 2 * self.attrs["scale"]) # calculate the y offset because the - self.y_offset = vec2(0, -self.attrs["layers"] / 2 * self.attrs["scale"]) # calculate the y offset because the -
# images position in pygame is the position of the top left corner of the image and we use the bottom position # images position in pygame is the position of the top left corner of the image and we use the bottom position
self.cache = self.app.cache.stacked_sprite_cache self.cache = self.app.cache.stacked_sprite_cache
@ -57,6 +61,10 @@ class AnimatedStackedSprite(pygame.sprite.Sprite): # stacked sprite class for a
super().__init__(self.group) super().__init__(self.group)
self.attrs = ANIMATED_SPRITE_ATTRS[name] self.attrs = ANIMATED_SPRITE_ATTRS[name]
if not "scale" in self.attrs:
self.attrs["scale"] = DEFAULT_SCALE
self.attrs["animated"] = True # set the animated attribute to true so that the cache knows that this sprite - self.attrs["animated"] = True # set the animated attribute to true so that the cache knows that this sprite -
self.y_offset = vec2(0, -self.attrs["layers"] / 2 * self.attrs["scale"]) # is animated self.y_offset = vec2(0, -self.attrs["layers"] / 2 * self.attrs["scale"]) # is animated
self.cache = self.app.cache.stacked_sprite_cache self.cache = self.app.cache.stacked_sprite_cache