142 lines
3.4 KiB
GDScript
142 lines
3.4 KiB
GDScript
extends EntityData
|
|
class_name PlantData
|
|
|
|
signal updated(p: PlantData)
|
|
signal disappeared(p: PlantData)
|
|
signal nearby_plant_updated()
|
|
|
|
enum State {PLANTED, GROWING, MATURE, DEAD}
|
|
|
|
@export var archetype: PlantArchetype
|
|
@export var plant_name: String
|
|
@export var mutations: Array[PlantMutation]
|
|
@export var day: int:
|
|
set(v):
|
|
day = v
|
|
updated.emit(self )
|
|
@export var random_seed: int
|
|
|
|
@export var leafs = 0 # +1 score
|
|
@export var roots = 0 # +1 lifetime
|
|
|
|
# var texture_builder: TextureBuilder = preload("res://entities/plants/scripts/texture_builder/texture_builder.tres")
|
|
|
|
var nearby_plants : Array[PlantData]
|
|
|
|
func _init(
|
|
_position: Vector2 = Vector2.ZERO,
|
|
_archetype: PlantArchetype = PlantArchetype.get_random(),
|
|
_plant_name: String = Random.generate_random_word(),
|
|
_mutations: Array[PlantMutation] = [],
|
|
_day: int = 0,
|
|
_random_seed = randi()
|
|
):
|
|
position = _position
|
|
archetype = _archetype
|
|
plant_name = _plant_name
|
|
mutations = _mutations
|
|
day = _day
|
|
random_seed = _random_seed
|
|
|
|
for m in mutations:
|
|
m.mutate_plant_data(self )
|
|
|
|
static func generate_from_seed(plant_seed: Seed, plant_position: Vector2) -> PlantData:
|
|
return PlantData.new(
|
|
plant_position,
|
|
plant_seed.plant_archetype,
|
|
plant_seed.plant_name,
|
|
plant_seed.plant_mutations
|
|
)
|
|
|
|
func load_entity() -> Entity:
|
|
var plant = Plant.new(
|
|
self
|
|
)
|
|
return plant
|
|
|
|
func get_lifetime() -> int:
|
|
var lifetime = archetype.lifetime + roots
|
|
|
|
for m in mutations:
|
|
lifetime = m.mutate_lifetime(self , lifetime)
|
|
|
|
for pd in nearby_plants:
|
|
lifetime += pd.get_lifetime_buff()
|
|
|
|
return lifetime
|
|
|
|
func get_growing_time() -> int:
|
|
var growing_time = archetype.growing_time
|
|
|
|
for m in mutations:
|
|
growing_time = m.mutate_growing_time(self , growing_time)
|
|
|
|
return growing_time
|
|
|
|
func get_score(state: State = get_state()) -> int:
|
|
var score = archetype.base_score + leafs if state == State.MATURE else 0
|
|
|
|
var mult := 1
|
|
|
|
for m in mutations:
|
|
score = m.mutate_score(self , score)
|
|
mult = m.mutate_score_multiplier(self , mult)
|
|
|
|
return score * mult
|
|
|
|
func get_state() -> State:
|
|
if day >= get_lifetime():
|
|
return State.DEAD
|
|
elif day == 0:
|
|
return State.PLANTED
|
|
elif day < get_growing_time():
|
|
return State.GROWING
|
|
return State.MATURE
|
|
|
|
func is_mature() -> bool:
|
|
return get_state() == State.MATURE
|
|
|
|
func get_seed_number(state = get_state()):
|
|
var seed_number = archetype.seed_number if (state == State.MATURE or state == State.DEAD) else 0
|
|
|
|
for m in mutations:
|
|
seed_number = m.mutate_seed_number(self , seed_number)
|
|
|
|
for pd in nearby_plants:
|
|
seed_number += pd.get_seed_buff()
|
|
|
|
return seed_number
|
|
|
|
func get_seed_random_loose():
|
|
var seed_random_loose = archetype.seed_random_loose
|
|
for m in mutations:
|
|
seed_random_loose = m.mutate_seed_random_loose(self , seed_random_loose)
|
|
|
|
return seed_random_loose
|
|
|
|
func get_random_seed_income():
|
|
return max(
|
|
get_seed_number() - randi_range(0, get_seed_random_loose()),
|
|
0
|
|
)
|
|
|
|
func get_lifetime_buff() -> int:
|
|
var buff = 0
|
|
|
|
for m in mutations:
|
|
buff += m.mutate_lifetime_buff(self)
|
|
|
|
return buff
|
|
|
|
func get_seed_buff() -> int:
|
|
var buff = 0
|
|
|
|
for m in mutations:
|
|
buff += m.mutate_seed_buff(self)
|
|
|
|
return buff
|
|
|
|
func disappear():
|
|
disappeared.emit(self )
|