100 lines
2.6 KiB
GDScript
100 lines
2.6 KiB
GDScript
extends InspectableEntity
|
|
class_name Plant
|
|
|
|
const PLANT_AREA_WIDTH = 20
|
|
const HARVESTED_SEED_DISPLACEMENT_FACTOR = 100
|
|
|
|
const RANDOM_MAX_GROW_INTERVAL = Planet.PASS_DAY_ANIMATION_TIME/2. - 0.1
|
|
|
|
const SPRITE_SCENE : PackedScene = preload("res://entities/plants/plant_sprite.tscn")
|
|
|
|
enum State {PLANTED, GROWING, MATURE}
|
|
|
|
@export var plant_type: PlantType
|
|
var planet: Planet # mis à jour par la classe Planet
|
|
|
|
var state: State = State.PLANTED: set = change_state
|
|
@export var day: int = 0 : set = set_day
|
|
|
|
@onready var plant_sprite: PlantSprite = generate_sprite()
|
|
@onready var collision_shape: CollisionShape2D = generate_collision_shape()
|
|
|
|
func _init(_plant_type = null, _planet = null):
|
|
plant_type = _plant_type
|
|
planet = _planet
|
|
|
|
func pointer_text():
|
|
var state_text = "Growing"
|
|
if state == State.MATURE: state_text = "Mature"
|
|
return state_text + " " + plant_type.name
|
|
|
|
func inspector_info() -> Inspector.Info:
|
|
return Inspector.Info.new(
|
|
pointer_text(),
|
|
plant_type.description,
|
|
plant_type.mature_texture
|
|
)
|
|
|
|
func generate_sprite() -> PlantSprite:
|
|
var spriteObject : PlantSprite = SPRITE_SCENE.instantiate()
|
|
|
|
add_child(spriteObject)
|
|
spriteObject.update_plant_sprite(self)
|
|
|
|
return spriteObject
|
|
|
|
func generate_collision_shape() -> CollisionShape2D:
|
|
var collision = CollisionShape2D.new()
|
|
var shape = CircleShape2D.new()
|
|
shape.radius = PLANT_AREA_WIDTH
|
|
|
|
collision.shape = shape
|
|
add_child(collision)
|
|
|
|
return collision
|
|
|
|
# Méthode déclenchée par la classe planet
|
|
func _pass_day():
|
|
await get_tree().create_timer(randf_range(0., RANDOM_MAX_GROW_INTERVAL)).timeout
|
|
|
|
if state == State.MATURE and plant_type.cyclic_effect:
|
|
plant_type.cyclic_effect.effect(self)
|
|
|
|
day += 1
|
|
|
|
func set_day(d):
|
|
day = d
|
|
if day == 0:
|
|
change_state(State.PLANTED)
|
|
if day > plant_type.growing_time:
|
|
if state != State.MATURE:
|
|
change_state(State.MATURE)
|
|
else:
|
|
if state != State.GROWING:
|
|
change_state(State.GROWING)
|
|
|
|
func change_state(_state: State):
|
|
state = _state
|
|
|
|
if state == State.MATURE and plant_type.mature_effect:
|
|
plant_type.mature_effect.effect(self)
|
|
|
|
plant_sprite.update_plant_sprite(self, true)
|
|
|
|
func harvest():
|
|
if state == State.MATURE:
|
|
var seed_number = plant_type.harvest_number.pick_random()
|
|
for i in range(seed_number):
|
|
var seed_plant_type : PlantType = plant_type
|
|
if len(plant_type.harvest_types_path):
|
|
seed_plant_type = load(plant_type.harvest_types_path.pick_random())
|
|
planet.drop_item(
|
|
Seed.new(seed_plant_type),
|
|
global_position,
|
|
HARVESTED_SEED_DISPLACEMENT_FACTOR,
|
|
)
|
|
|
|
plant_sprite.start_harvest_animation()
|
|
await plant_sprite.harvest_animation_finished
|
|
queue_free()
|