extends Node2D
@onready var canvas_modulate = $CanvasModulate
@onready var tileMap : TileMap = $Farmland
@onready var player : Player = $Farmland/player
@onready var grid_helper = $Farmland/GridHelper
const GRID_SIZE = 16
@onready var seed = preload("res://Plant/pumpkin/pumpkin_seed.tscn")
class PlantData :
var is_harvestable : bool = AgeingComponent.harvestable
var planted : Dictionary = {}
func _ready():
grid_helper.visible = false
func _on_inventory_gui_closed():
get_tree().paused = false
func _on_inventory_gui_opened():
get_tree().paused = true
func _physics_process(_delta):
print(AgeingComponent.harvestable)
var playerMapCoord = tileMap.local_to_map(player.position)
var mouse_position = Vector2i(get_global_mouse_position() / GRID_SIZE ) * GRID_SIZE
grid_helper.position = playerMapCoord * GRID_SIZE
var cellLocalCoord = tileMap.local_to_map(grid_helper.position)
var tile : TileData = tileMap.get_cell_tile_data(1, cellLocalCoord)
if tile == null :
grid_helper.visible = false
return
if tile.get_custom_data("HoedLand") :
var plantCoord = tileMap.local_to_map(grid_helper.global_position)
grid_helper.visible = true
if not planted.has(plantCoord) :
_plant_seed(plantCoord)
elif is_harvestable(plantCoord) :
harvest_plant(plantCoord)
func harvest_plant(key) -> void :
var plant_data : PlantData = planted.get(key)
if plant_data.is_harvestable :
plant_data.harvest()
planted.erase(key)
func is_harvestable(key) -> bool :
var plant_data : PlantData = planted.get(key)
return plant_data.is_harvestable if plant_data != null else false
func _plant_seed(coord) -> void :
var plant = seed.instantiate()
get_node("Farmland/Plant").add_child(plant)
var plant_data = PlantData.new()
planted[coord] = plant_data
plant.global_position = tileMap.map_to_local(coord)
this is a main script that control Planting System
extends Node
## Track object age and can replace target scene (usually parent)
## with a new scene after reaching an age_threshold
class_name AgeingComponent
signal age_changed(new_age : float, last_age : float )
signal age_threshold_reached(new_scene : Node2D)
## When set, is the scene that will be replacement with next_scene
## the direct parent will be used
@export var target : Node2D
@export var current_age : float = 0.0 :
set(value) :
if (current_age != value) :
var last_age = current_age
current_age = value
emit_signal("age_changed", current_age, last_age)
if (current_age == age_threshold && _threshold_reached != true) :
var new_scene : Node2D
if (next_scene != null) :
new_scene = _create_next_scene()
emit_signal("age_threshold_reached", new_scene)
_threshold_reached = true
target.queue_free()
elif (current_age > age_threshold) :
harvestable = true
@export var age_threshold : float = 1.0
@export var next_scene : PackedScene
static var group_name = "AgeingComponent"
var _threshold_reached : bool = false
static var harvestable : bool = false
func _ready():
if (target == null) :
target = get_parent()
add_to_group(group_name)
func _create_next_scene() -> Node2D :
var instance = next_scene.instantiate()
target.get_parent().add_child(instance)
instance.global_transform = target.global_transform
return instance
func _harvest() -> void :
queue_free()
this is a Script that control Growing system i watch tutorial on youtube and its not working. I think its because my Growing System is to remove node and create new node. its not a change sprite frame it 1 node. my growing system is working normally. how do i make it can be harvestable
*i watch tutorial on youtube but its not help me solve this problem vey much. *
if anyone know plz guide me and sorry if my grammar is not correct T_T