diff --git a/scripts/pokedex.gd b/scripts/pokedex.gd new file mode 100644 index 0000000..e0cb3df --- /dev/null +++ b/scripts/pokedex.gd @@ -0,0 +1,88 @@ +# res://scripts/pokedex.gd (dans le jeu, pas dans l'addon) +extends Node + +var _paths : Dictionary = {} # nom d'API -> chemin du .tres +var _id_to_name : Dictionary = {} # id -> nom d'API +var _cache : Dictionary = {} # nom -> PokemonData + +var _moves : Dictionary = {} # nom -> MoveData +var _types : Dictionary = {} # nom -> TypeData +var _abilities : Dictionary = {} # nom -> AbilityData + +func _ready() -> void: + var dir := PokeApiSettings.pokemon_dir() + for file in ResourceLoader.list_directory(dir): + if file.ends_with("/"): continue # sous-dossier + var info := PokeApiSettings.parse_pokemon_stem(file.get_basename()) + if info.is_empty(): continue + _paths[info["name"]] = dir + file + _id_to_name[info["id"]] = info["name"] + +#---------- pokémon ---------- + +## key : un id (int) ou un nom d'API (String, ex. "pikachu") +## Renvoie aussi bien une espèce de base qu'une forme (méga, gigamax, régionale), +## puisque toutes sont sauvegardées comme des .tres indépendants dans le même dossier. +func get_pokemon(key : Variant) -> PokemonData: + var poke_name : String + if key is int: + poke_name = _id_to_name.get(key, "") + else: + poke_name = String(key).to_lower() + if !_paths.has(poke_name): return null + if !_cache.has(poke_name): + _cache[poke_name] = load(_paths[poke_name]) as PokemonData + return _cache[poke_name] + +func has_pokemon(key : Variant) -> bool: + if key is int: return _id_to_name.has(key) + return _paths.has(String(key).to_lower()) + +## Une forme méga/gmax précise d'un pokémon, ex. get_form(charizard, "mega-x"). +func get_form(d : PokemonData, kind : String) -> PokemonData: + if d == null: return null + for f in d.forms: + if f.kind == kind: return f.variant + return null + +func all_ids() -> Array: + var ids : Array = _id_to_name.keys() + ids.sort() + return ids + +func all_names() -> Array: + return _paths.keys() + +#---------- capacités / types / talents ---------- + +func get_move(move_name : String) -> MoveData: + move_name = move_name.to_lower() + if !_moves.has(move_name): + var path := PokeApiSettings.moves_dir() + move_name + ".tres" + if !ResourceLoader.exists(path): return null + _moves[move_name] = load(path) as MoveData + return _moves[move_name] + +func get_type(type_name : String) -> TypeData: + type_name = type_name.to_lower() + if !_types.has(type_name): + var path := PokeApiSettings.types_dir() + type_name + ".tres" + if !ResourceLoader.exists(path): return null + _types[type_name] = load(path) as TypeData + return _types[type_name] + +func get_ability(ability_name : String) -> AbilityData: + ability_name = ability_name.to_lower() + if !_abilities.has(ability_name): + var path := PokeApiSettings.abilities_dir() + ability_name + ".tres" + if !ResourceLoader.exists(path): return null + _abilities[ability_name] = load(path) as AbilityData + return _abilities[ability_name] + +#---------- cache ---------- + +func clear_cache() -> void: + _cache.clear() + _moves.clear() + _types.clear() + _abilities.clear()