Module: Bestguigui::Assets

Defined in:
lib/bestguigui/assets.rb

Overview

Cache mémoïsé pour les images/sons/polices chargés depuis le disque — évite de recharger le même fichier à chaque fois qu'une entité qui l'utilise est recréée (ex: une nouvelle partie qui reconstruit tous ses objets).

Pas d'instance à créer ni à faire passer partout : ce sont des méthodes de module, appelables depuis n'importe où.

Bestguigui::Assets.image("gfx/snowball.png", retro: true)
Bestguigui::Assets.tiles("gfx/player.png", 128, 128, retro: true)
Bestguigui::Assets.sample("sfx/hit.mp3")
Bestguigui::Assets.song("sfx/theme.mp3")
Bestguigui::Assets.font(24, name: "gfx/MyFont.ttf")

Le cache est tenu par (chemin/taille + options) : les mêmes arguments renvoient toujours le même objet déjà chargé.

Class Method Summary collapse

Class Method Details

.clearObject

Vide le cache — utile si tu changes vraiment de contenu (rare en jam), ou en tests.



50
51
52
53
54
55
# File 'lib/bestguigui/assets.rb', line 50

def clear
  @images.clear
  @samples.clear
  @songs.clear
  @fonts.clear
end

.font(size, **options) ⇒ Object

Charger une police (surtout un .ttf custom) fait un vrai travail — rasterisation des glyphes — pas juste une lecture disque, donc aussi utile à mémoïser que les images/sons.



44
45
46
# File 'lib/bestguigui/assets.rb', line 44

def font(size, **options)
  @fonts[[size, options]] ||= Gosu::Font.new(size, **options)
end

.image(path, **options) ⇒ Object



25
26
27
# File 'lib/bestguigui/assets.rb', line 25

def image(path, **options)
  @images[[path, options]] ||= Gosu::Image.new(path, **options)
end

.sample(path) ⇒ Object



33
34
35
# File 'lib/bestguigui/assets.rb', line 33

def sample(path)
  @samples[path] ||= Gosu::Sample.new(path)
end

.song(path) ⇒ Object



37
38
39
# File 'lib/bestguigui/assets.rb', line 37

def song(path)
  @songs[path] ||= Gosu::Song.new(path)
end

.tiles(path, tile_width, tile_height, **options) ⇒ Object



29
30
31
# File 'lib/bestguigui/assets.rb', line 29

def tiles(path, tile_width, tile_height, **options)
  @images[[path, tile_width, tile_height, options]] ||= Gosu::Image.load_tiles(path, tile_width, tile_height, **options)
end