172 lines
5.8 KiB
GDScript
172 lines
5.8 KiB
GDScript
extends CanvasLayer
|
|
class_name GameGui
|
|
|
|
const SCORE_ICON : Texture = preload("res://common/icons/growth.svg")
|
|
|
|
var score_by_plant : Dictionary[String, int] = {}
|
|
|
|
var score_mirror :
|
|
set(v):
|
|
score_mirror = v
|
|
score_update(true)
|
|
|
|
func _ready():
|
|
GameInfo.game_data.current_region_data.updated.connect(_on_region_updated)
|
|
GameInfo.game_data.player_data.updated.connect(_on_player_updated)
|
|
GameInfo.game_data.current_region_data.pass_day_ended.connect(_on_region_pass_day_ended)
|
|
|
|
|
|
%ObjectiveProgressBar.visible = not "tutorial" in GameInfo.game_data.current_region_data.flags
|
|
%SimplePlantPointScore.visible = "tutorial" in GameInfo.game_data.current_region_data.flags
|
|
|
|
charge_update(GameInfo.game_data.current_region_data)
|
|
state_update(GameInfo.game_data.current_region_data)
|
|
player_update(GameInfo.game_data.player_data, false)
|
|
%EnergyInfo.reset_size()
|
|
%GiveUpButton.pressed.connect(_on_give_up_pressed)
|
|
|
|
score_mirror = GameInfo.game_data.current_region_data.get_score()
|
|
for p : PlantData in GameInfo.game_data.current_region_data.plants:
|
|
score_by_plant[str(p.random_seed)] = p.get_score()
|
|
|
|
func _on_player_updated(player_data : PlayerData):
|
|
player_update(player_data)
|
|
|
|
func player_update(player_data : PlayerData, with_animation = true):
|
|
%EnergyInfo.update(
|
|
player_data.energy,
|
|
player_data.max_energy,
|
|
with_animation
|
|
)
|
|
|
|
func _on_region_updated(region_data : RegionData):
|
|
await get_tree().create_timer(0.1).timeout
|
|
if score_mirror != region_data.get_score():
|
|
for p in region_data.plants:
|
|
var score = p.get_score()
|
|
if not str(p.random_seed) in score_by_plant:
|
|
score_by_plant[str(p.random_seed)] = 0
|
|
if score > score_by_plant[str(p.random_seed)]:
|
|
plant_changing_score(p, score - score_by_plant[str(p.random_seed)])
|
|
elif score < score_by_plant[str(p.random_seed)]:
|
|
score_mirror -= score_by_plant[str(p.random_seed)] - score
|
|
score_by_plant[str(p.random_seed)] = score
|
|
# Check for removed plants
|
|
for key in score_by_plant:
|
|
if region_data.plants.find_custom(
|
|
func (p): return str(p.random_seed) == key
|
|
) == -1:
|
|
score_mirror -= score_by_plant[key]
|
|
score_by_plant.erase(key)
|
|
|
|
charge_update(region_data)
|
|
state_update(region_data)
|
|
|
|
func _on_give_up_pressed():
|
|
GameInfo.game_data.give_up()
|
|
SceneManager.change_to_scene_id('ASTRA')
|
|
|
|
func charge_update(region_data : RegionData):
|
|
%RechargesLeftLabel.text = tr("%d_CHARGE_LEFT") % (region_data.charges)
|
|
|
|
func score_update(with_animation = true):
|
|
var objective_progression : float
|
|
if GameInfo.game_data.current_region_data.state == RegionData.State.SUCCEEDED:
|
|
objective_progression = 1.
|
|
%ObjectiveProgressBar.text = tr("FULL")
|
|
else:
|
|
var objective = GameInfo.game_data.current_region_data.objective
|
|
objective_progression = (float(score_mirror) / max(float(objective), 1))
|
|
%ObjectiveProgressBar.text = "%d/%d" % [score_mirror, objective]
|
|
|
|
if with_animation:
|
|
get_tree().create_tween().tween_property(
|
|
%ObjectiveProgressBar,
|
|
"progress",
|
|
objective_progression,
|
|
0.5,
|
|
)
|
|
else:
|
|
%ObjectiveProgressBar.set_progress(objective_progression)
|
|
|
|
if score_mirror > 1:
|
|
%SimplePlantPointScoreLabel.text = tr("%d_PLANT_POINTS") % score_mirror
|
|
else :
|
|
%SimplePlantPointScoreLabel.text = tr("%d_PLANT_POINT") % score_mirror
|
|
|
|
func state_update(region_data : RegionData):
|
|
if region_data.state == RegionData.State.SUCCEEDED:
|
|
%Alert.text = "SHIP_IS_READY_TO_TAKE_OFF"
|
|
if "tutorial" in region_data.flags:
|
|
%Alert.text = "TUTORIAL_FINISHED"
|
|
%Alert.modulate = Color("ffa617ff")
|
|
%Alert.appear()
|
|
elif region_data.state == RegionData.State.FAILED:
|
|
%Alert.text = "NO_RECHARGE_LEFT"
|
|
%Alert.modulate = Color("FF006E")
|
|
%Alert.appear()
|
|
else:
|
|
%Alert.disappear()
|
|
%GiveUpButton.visible = region_data.state == RegionData.State.FAILED
|
|
|
|
func plant_changing_score(plant_data: PlantData, amount : int):
|
|
if GameInfo.game_data.current_region_data.in_passing_day_animation:
|
|
await GameInfo.game_data.current_region_data.pass_day_ended
|
|
if amount <= 0:
|
|
score_mirror += amount
|
|
else :
|
|
for i in range(amount):
|
|
var camera = get_viewport().get_camera_2d()
|
|
var screen_size = get_viewport().get_visible_rect().size
|
|
|
|
spawn_score_particle(
|
|
plant_data.position - camera.global_position + screen_size / 2,
|
|
%ObjectiveProgressBar.global_position + %ObjectiveProgressBar.size / 2,
|
|
0.8
|
|
)
|
|
|
|
await get_tree().create_timer(0.5 / max(1.,i)).timeout
|
|
|
|
func spawn_score_particle(
|
|
from_position,
|
|
to_position,
|
|
duration
|
|
):
|
|
var sprite_particle = Sprite2D.new()
|
|
sprite_particle.texture = SCORE_ICON
|
|
%ScoreParticles.add_child(sprite_particle)
|
|
sprite_particle.position = from_position
|
|
|
|
var tween = get_tree().create_tween()
|
|
|
|
tween.set_trans(Tween.TransitionType.TRANS_SPRING)
|
|
|
|
tween.tween_property(
|
|
sprite_particle,
|
|
"position",
|
|
to_position,
|
|
duration
|
|
)
|
|
|
|
|
|
await tween.finished
|
|
score_mirror += 1
|
|
AudioManager.play_sfx("PlantPoint")
|
|
|
|
sprite_particle.queue_free()
|
|
|
|
|
|
func _on_player_action_tried_without_energy():
|
|
$AnimationPlayer.play("no_energy_left")
|
|
|
|
func _on_player_upgraded():
|
|
$EffectAnimation.play("upgrade")
|
|
|
|
func _on_region_pass_day_ended(region:Region):
|
|
if region.data.charges == 1:
|
|
%Announce.announce(
|
|
tr("LAST_RECHARGE"),
|
|
tr("%d_GARDEN_SCORE_LEFT") % [region.data.objective - region.garden.get_score()],
|
|
Announce.RED_COLOR
|
|
)
|