Cow_Life_Sim_RPG/Objects/SkillSpecific/Gardening/PlantingSpot.gd

299 lines
7.6 KiB
GDScript

extends Node2D
class_name PlantingSpot
enum PlantState {Empty, Planted, GrownTree}
@export var plantZoneName = "plant"
var currentState = PlantState.Empty
var currentItem
var queuedStates = []
var currentAction = ""
var currentActionProgress = 0
var currentActionSpeed = 30
var plantLives = 0
var timeBoosted = 0
var harvestCombo = 0
var rng = RandomNumberGenerator.new()
func _ready():
setup()
check_for_state_updates()
func _process(delta):
check_for_state_updates()
if currentAction == "Digging":
if GameVariables.player.cowState != "Digging":
cancel_digging()
else:
currentActionProgress += delta * currentActionSpeed
%DiggingBar.value = currentActionProgress
if currentActionProgress >= 100:
dig_up_complete()
elif currentAction == "Harvesting":
if GameVariables.player.cowState != "Harvesting":
cancel_harvesting()
else:
currentActionProgress += delta * currentActionSpeed
%HarvestingBar.value = currentActionProgress
if currentActionProgress >= 100:
currentActionProgress = 0
harvest_item()
if !harvest_life_check():
plantLives -= 1
if plantLives <= 0:
finish_harvesting()
func setup():
var plantDetails = PlantManager.get_plant_spot_details(plantZoneName)
if plantDetails != null:
plantLives = plantDetails.plantLives
timeBoosted = plantDetails.timeBoosted
queuedStates = plantDetails.queuedStates
set_state(plantDetails.state, plantDetails.item)
else:
set_state(PlantState.Empty, null)
func open_planting_menu():
if currentState == PlantState.Empty:
$PlantingWindow.popup_centered()
$PlantingWindow.opened()
func open_fertilization_menu():
if currentState == PlantState.Planted:
$FertilizingWindow.popup_centered()
$FertilizingWindow.opened()
func dig_up():
if currentState != PlantState.Planted and currentState != PlantState.GrownTree:
return
if currentAction == "Harvesting":
cancel_harvesting()
currentAction = "Digging"
GameVariables.player.change_state("Digging")
currentActionProgress = 0
%DiggingBar.value = 0
%DiggingBar.visible = true
func dig_up_complete():
InventoryManager.add_item_to_inventory(currentItem.duplicate())
MessageManager.item_popup(currentItem.duplicate())
queuedStates = []
set_state(PlantState.Empty, null)
cancel_digging()
func cancel_digging():
currentAction = ""
%DiggingBar.visible = false
GameVariables.player.change_state("Idle")
func harvest():
if currentState != PlantState.GrownTree:
return
if currentAction == "Digging":
cancel_digging()
currentAction = "Harvesting"
GameVariables.player.change_state("Harvesting")
currentActionProgress = 0
harvestCombo = 0
%HarvestingBar.value = 0
%HarvestingBar.visible = true
func harvest_item():
InventoryManager.add_item_to_inventory(currentItem.duplicate())
MessageManager.item_popup(currentItem.duplicate())
harvestCombo += 1
if harvestCombo >= 10:
if currentItem.get_name(false) == "Sugar":
AchievementManager.complete_achievement("Bountiful Harvest")
var itemXP = 2
itemXP += floori(currentItem.get_value() * 1.7)
if itemXP <= 0:
itemXP = 1
LevelManager.add_XP("gardening", itemXP)
LevelManager.get_skill("appreciating").experience_item(currentItem, "harvested")
func cancel_harvesting():
currentAction = ""
%HarvestingBar.visible = false
GameVariables.player.change_state("Idle")
func finish_harvesting():
currentAction = ""
%HarvestingBar.visible = false
queuedStates = []
set_state(PlantState.Empty, null)
GameVariables.player.change_state("Idle")
func harvest_life_check():
var successChance = 35
successChance -= currentItem.get_value() * 1.5
successChance += currentItem.get_edibility() * 0.5
successChance += LevelManager.get_skill("gardening").currentLevel * 1
var minChance = LevelManager.get_skill("gardening").currentLevel * 0.25
if currentItem.get_edibility() > 0:
minChance += currentItem.get_edibility() * 0.05
if successChance < minChance:
successChance = minChance
if successChance > 88:
successChance = 88
var result = rng.randf_range(1, 100)
if result <= successChance:
return true
return false
func check_for_state_updates():
var currentTime = Time.get_unix_time_from_system()
var latestReadyState = null
var statesToRemove = []
for state in queuedStates:
if currentTime + timeBoosted >= state.time:
if latestReadyState == null or latestReadyState.time < state.time:
latestReadyState = state
statesToRemove.append(state)
for state in statesToRemove:
queuedStates.erase(state)
if latestReadyState != null:
set_state(latestReadyState.state, latestReadyState.item)
func get_time_to_next_state_change():
var currentTime = Time.get_unix_time_from_system()
var lowestTime = null
for state in queuedStates:
var timeToState = state.time - (currentTime + timeBoosted)
if lowestTime == null or timeToState < lowestTime:
lowestTime = timeToState
return lowestTime
func set_state(state, item):
if $FertilizingWindow.visible:
$FertilizingWindow.hide()
get_tree().paused = false
if currentAction == "Digging":
if GameVariables.player.cowState == "Digging":
GameVariables.player.change_state("Idle")
cancel_digging()
elif currentAction == "Harvesting":
if GameVariables.player.cowState == "Harvesting":
GameVariables.player.change_state("Idle")
cancel_harvesting()
%HarvestingTree.visible = false
if state == PlantState.Empty:
currentItem = null
%PlantedItem.texture = null
%GrownPanel.visible = false
plantLives = 1
timeBoosted = 0
elif state == PlantState.Planted:
currentItem = item
%PlantedItem.texture = item.get_sprite()
%GrownPanel.visible = false
elif state == PlantState.GrownTree:
currentItem = item
%PlantedItem.texture = null
%HarvestingTree.visible = true
%HarvestableSprite1.texture = item.get_sprite()
%HarvestableSprite2.texture = item.get_sprite()
%HarvestableSprite3.texture = item.get_sprite()
%GrownPanel.visible = true
currentState = state
save_details()
func add_plant_lives(amount):
plantLives += amount
save_details()
func add_time_boosted(amountToBoost):
timeBoosted += amountToBoost
save_details()
func save_details():
PlantManager.store_plant_spot_details(plantZoneName, self)
func plant_item(item):
queuedStates = []
var growthTime = get_growth_time(item)
queuedStates.append(create_queued_state(PlantState.GrownTree, item, growthTime))
check_for_queued_state_override(item)
plantLives = get_starting_lives(item)
if plantZoneName == "pigeonGrovePlant" and item.get_name(false).contains("Evidence"):
SaveManager.set_save_value("jonaldPigeonEvidencePlanted", true)
set_state(PlantState.Planted, item)
func check_for_queued_state_override(item):
if item.get_name(false) == "Sunflower Seed":
queuedStates[0].item = preload("res://Items/Plants/Sunflower.gd").new()
if item.get_name(false) == "Mustard Seed":
queuedStates[0].item = preload("res://Items/Foods/Condiments/Mustard.gd").new()
if item.get_name(false) == "Grow Your Own Mushroom Kit":
queuedStates[0].item = MushroomGenerator.get_a_mushroom()
func get_starting_lives(item):
return 1
func get_growth_time(item:Item):
var growthTime = 45
growthTime += item.get_value() * 8
growthTime += (100 - item.get_edibility()) * 6
if Item.modifications.Wet in item.itemModifications:
growthTime = growthTime * 0.6
growthTime = floori(growthTime)
if growthTime < 1:
growthTime = 1
return growthTime
func create_queued_state(state, plant, timeToPass):
var currentTime = Time.get_unix_time_from_system()
var newQueuedState = {}
newQueuedState.state = state
newQueuedState.item = plant
newQueuedState.time = currentTime + timeToPass
return newQueuedState