summaryrefslogtreecommitdiff
path: root/src/tools/score_manager/score_manager.gd
blob: 63c78a7c09dfd6540c346bc9d50a4be38411da67 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
extends Node

export(Resource) var TEXT_FONT: Resource

var score: int = 0
var growth: int = 0

var point_text_time: float = 2.0
var _point_text_fmt: String = "+%s"


func _ready():
	Event.connect("food_eaten", self, "_on_food_eaten")


func _on_food_eaten(properties: Dictionary) -> void:
	var points: int = properties["points"]
	var location: Vector2 = properties["global_position"]
	_process_points(points)
	_spawn_added_score_text(points, location)


func _process_points(points: int) -> void:
	var score_to_grow: int = (growth + 1) * Global.POINTS_TO_GROW - score
	var amount_to_grow: int = 0
	var growth_progress: int
	score += points
	if points >= score_to_grow:
		amount_to_grow += 1
		points -= score_to_grow
		# maybe be careful with this
		amount_to_grow += points / Global.POINTS_TO_GROW
		growth += amount_to_grow
		Event.emit_signal("snake_add_new_segment", amount_to_grow)

	growth_progress = Global.POINTS_TO_GROW - ((growth + 1) * Global.POINTS_TO_GROW - score)
	Event.emit_signal("snake_growth_progress", growth_progress)


func _spawn_added_score_text(points: int, location: Vector2) -> void:
	var label: Label = Label.new()
	label.text = _point_text_fmt % points
	label.add_color_override("font_color", Color.red)
	label.add_font_override("font", TEXT_FONT)
	label.set_global_position(location)
	add_child(label)
	yield(get_tree().create_timer(point_text_time), "timeout")
	remove_child(label)