seeding-planets/entities/player/inventory/scripts/items/seed.gd

202 lines
5.5 KiB
GDScript

extends Item
class_name Seed
const MUTATION_PROBABILITY = 0.3
const SHOVEL_ICON = preload("res://common/icons/shovel.svg")
const GROWING_ICON = preload("res://common/icons/chevrons-up.svg")
const SCORE_ICON = preload("res://common/icons/growth.svg")
@export var plant_type: PlantType
@export var plant_mutations: Array[PlantMutation]
func get_item_name() -> String:
return tr("%s_SEED") % plant_type.name
func get_description() -> String:
return tr("PLANT_%s_MUST_BE_USED_IN_DECONTAMINATED_ZONE") % plant_type.name
func get_icon() -> Texture2D:
return plant_type.seed_texture
func get_energy_used() -> int:
return 1
func get_usage_zone_radius() -> int:
return 35
func get_usage_object_affected(i : InspectableEntity) -> bool:
return i is Plant
func _init(
_plant_type : PlantType = null,
_parent_mutation : Array[PlantMutation] = []
):
plant_type = _plant_type
plant_mutations = Seed.mutate(_parent_mutation)
func use_text() -> String:
return tr("PLANT_%s") % plant_type.name
func is_one_time_use():
return true
func can_use(player : Player, zone : Player.ActionZone) -> bool:
if (
player.planet == null
):
return false
var is_there_a_plant_here = false
for area in zone.get_affected_areas():
if area is Plant:
is_there_a_plant_here = true
var is_there_contamination_in_zone = false
for tile in zone.get_tiles():
if not player.planet.decontamination_layer.is_decontamined(tile):
is_there_contamination_in_zone = true
return not is_there_a_plant_here and not is_there_contamination_in_zone
func use(player : Player, zone : Player.ActionZone) -> bool:
if player.planet == null:
return false
AudioManager.play_sfx("Dig")
return player.planet.plant(
plant_type,
zone.get_global_position(),
plant_mutations
)
func card_info() -> CardInfo:
var info = CardInfo.new(
get_item_name()
)
info.texture = icon
info.type_icon = TYPE_ICON
info.stats.append(
CardStatInfo.new(
tr("COST_%d_ENERGY") % energy_usage,
ENERGY_ICON
)
)
if is_one_time_use():
info.stats.append(
CardStatInfo.new(
tr("ONE_TIME_USE"),
ONE_TIME_ICON
)
)
var effect_section = CardSectionInfo.new(
tr("EFFECT"),
get_description()
)
effect_section.title_icon = ACTION_ICON
if energy_usage > 0:
effect_section.title_icon = ENERGY_ICON
info.sections.append(effect_section)
if len(plant_mutations) != 0:
var rarest : int = plant_mutations.map(
func(m : PlantMutation) : return m.get_rarity()
).max()
info.bg_color = PlantMutation.get_rarity_color(rarest)
for m in plant_mutations:
info.sections.append(m.card_section())
return info
func get_particles() -> Array[Particles.Parameters]:
var param : Array[Particles.Parameters] = []
for m in plant_mutations:
param.append(
Particles.Parameters.new(
m.get_icon(),
PlantMutation.get_rarity_color(m.get_rarity())
)
)
return param
static func mutate(parent_mutation : Array[PlantMutation] = []) -> Array[PlantMutation]:
if randf() > MUTATION_PROBABILITY:
return parent_mutation
var possible_mutations : Array[MutationPossibility] = [
AddMutation.new()
]
if (
len(parent_mutation) >= 2
):
possible_mutations = [
UpgradeMutation.new(),
RemoveMutation.new(),
]
elif len(parent_mutation) > 0:
possible_mutations = [
AddMutation.new(),
UpgradeMutation.new(),
RemoveMutation.new(),
]
var chosen_mutation = possible_mutations.pick_random()
return chosen_mutation.mutate(parent_mutation)
class MutationPossibility:
func mutate(_parent_mutation : Array[PlantMutation] = [])-> Array[PlantMutation]:
return []
class DontMutate extends MutationPossibility:
func mutate(parent_mutation : Array[PlantMutation] = [])-> Array[PlantMutation]:
return parent_mutation
class AddMutation extends MutationPossibility:
func mutate(parent_mutation : Array[PlantMutation] = [])-> Array[PlantMutation]:
var new_mutations = parent_mutation.duplicate_deep()
var mut = PlantMutation.random_mutation(parent_mutation)
if mut:
var existing_mut_id = new_mutations.find_custom(func(m:PlantMutation): return m.get_mutation_name() == mut.get_mutation_name())
if existing_mut_id >= 0:
new_mutations[existing_mut_id].level += 1
else :
new_mutations.append(mut)
return new_mutations
class UpgradeMutation extends MutationPossibility:
func mutate(
parent_mutation : Array[PlantMutation] = []
) -> Array[PlantMutation]:
var new_mutations = parent_mutation.duplicate_deep()
new_mutations.pick_random().level += 1
return new_mutations
class RemoveMutation extends MutationPossibility:
func mutate(parent_mutation : Array[PlantMutation] = [])-> Array[PlantMutation]:
var new_mutations :Array[PlantMutation] = parent_mutation.duplicate_deep()
var mut_to_remove = new_mutations.pick_random()
if mut_to_remove.level > 1:
mut_to_remove.level -= 1
else:
new_mutations.remove_at(new_mutations.find(mut_to_remove))
return new_mutations