summaryrefslogtreecommitdiff
path: root/Towers/Components/TowerComponent.gd
blob: 9a1684588aa5ac0369640dfd4b3f95ac3c56e837 (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
class_name TowerComponent
extends Node2D


var current_tower: Tower:
	get():
		return get_parent()

enum ComponentType {
	Range,
	Attack,
	Speed,
	Frost,
	Burn,
	Poison,
}

@export var type: ComponentType
var sprite: TextureRect:
	get():
		@warning_ignore("shadowed_variable")
		var sprite := $Sprite.duplicate()
		sprite.name = get_type_name()
		
		return sprite


var id: String:
	get():
		return get_type_name()

signal level_changed
var level: int = 1:
	set(value):
		level = value
		level_changed.emit()


func set_sprite(texture: Texture2D):
	sprite = get_node("Sprite")
	sprite.texture = texture
	sprite.name = get_type_name()


func get_type_name(component_type: ComponentType = type):
	return ComponentType.keys()[component_type]


func on_add(tower: Tower):
	current_tower = tower


class NetworkData extends Resource:
	var type: ComponentType
	var level: int

func to_network_data() -> NetworkData:
	var data = NetworkData.new()
	
	data.type = type
	data.level = level
	
	return data

func update_with_network_data(data: NetworkData):
	level = data.level

static func from_network_data(data: NetworkData) -> TowerComponent:
	var component = load(
		"res://Towers/Components/%sComponent.tscn" % [ComponentType.keys()[data.type]]
	).instantiate()
	
	component.type = data.type
	component.level = data.level
	
	return component