115 lines
3.1 KiB
GDScript
115 lines
3.1 KiB
GDScript
class_name PokemonTeam
|
|
extends Resource
|
|
|
|
signal team_changed
|
|
signal pokemon_added(pokemon : PokemonInstance, index : int)
|
|
signal pokemon_removed(pokemon : PokemonInstance, index : int)
|
|
|
|
const MAX_SIZE : int = 6
|
|
|
|
@export var members : Array[PokemonInstance] = []
|
|
|
|
#---------- infos ----------
|
|
|
|
func size() -> int:
|
|
return members.size()
|
|
|
|
func is_empty() -> bool:
|
|
return members.is_empty()
|
|
|
|
func is_full() -> bool:
|
|
return members.size() >= MAX_SIZE
|
|
|
|
func get_pokemon(index : int) -> PokemonInstance:
|
|
return members[index] if _is_valid_index(index) else null
|
|
|
|
#---------- gestion ----------
|
|
|
|
func add_pokemon(pokemon : PokemonInstance) -> bool:
|
|
#renvoie false si l'equipe est pleine (l'appelant decide quoi en faire)
|
|
if pokemon == null or is_full() or members.has(pokemon): return false
|
|
members.append(pokemon)
|
|
pokemon_added.emit(pokemon, members.size() - 1)
|
|
team_changed.emit()
|
|
return true
|
|
|
|
func remove_at(index : int) -> PokemonInstance:
|
|
if !_is_valid_index(index): return null
|
|
var pokemon : PokemonInstance = members[index]
|
|
members.remove_at(index)
|
|
pokemon_removed.emit(pokemon, index)
|
|
team_changed.emit()
|
|
return pokemon
|
|
|
|
func swap(index_a : int, index_b : int) -> bool:
|
|
if index_a == index_b or !_is_valid_index(index_a) or !_is_valid_index(index_b): return false
|
|
var tmp : PokemonInstance = members[index_a]
|
|
members[index_a] = members[index_b]
|
|
members[index_b] = tmp
|
|
team_changed.emit()
|
|
return true
|
|
|
|
func replace_at(index : int, pokemon : PokemonInstance) -> PokemonInstance:
|
|
#remplace le pokemon a cet index et renvoie l'ancien (utilise pour les echanges avec le PC)
|
|
if pokemon == null or !_is_valid_index(index) or members.has(pokemon): return null
|
|
var old : PokemonInstance = members[index]
|
|
members[index] = pokemon
|
|
pokemon_removed.emit(old, index)
|
|
pokemon_added.emit(pokemon, index)
|
|
team_changed.emit()
|
|
return old
|
|
|
|
#---------- combat ----------
|
|
|
|
func get_first_able_index() -> int:
|
|
for i in members.size():
|
|
if !members[i].is_fainted(): return i
|
|
return -1
|
|
|
|
func get_first_able() -> PokemonInstance:
|
|
return get_pokemon(get_first_able_index())
|
|
|
|
func has_able_pokemon() -> bool:
|
|
return get_first_able_index() != -1
|
|
|
|
func get_able_pokemon() -> Array[PokemonInstance]:
|
|
var result : Array[PokemonInstance] = []
|
|
for p in members:
|
|
if !p.is_fainted(): result.append(p)
|
|
return result
|
|
|
|
func heal_all() -> void:
|
|
for p in members: p.heal_full()
|
|
|
|
#---------- montures ----------
|
|
|
|
func get_mountable() -> Array[PokemonInstance]:
|
|
var result : Array[PokemonInstance] = []
|
|
for p in members:
|
|
if p.can_be_mounted(): result.append(p)
|
|
return result
|
|
|
|
func has_mountable() -> bool:
|
|
for p in members:
|
|
if p.can_be_mounted(): return true
|
|
return false
|
|
|
|
#---------- interne ----------
|
|
|
|
func _is_valid_index(index : int) -> bool:
|
|
return index >= 0 and index < members.size()
|
|
|
|
#---------- sauvegarde ----------
|
|
|
|
func to_dict() -> Dictionary:
|
|
var list : Array = []
|
|
for p in members: list.append(p.to_dict())
|
|
return {"members": list}
|
|
|
|
static func from_dict(data : Dictionary) -> PokemonTeam:
|
|
var team := PokemonTeam.new()
|
|
for member_data in data.get("members", []):
|
|
var p := PokemonInstance.from_dict(member_data)
|
|
if p: team.add_pokemon(p)
|
|
return team
|