-
Notifications
You must be signed in to change notification settings - Fork 1
/
CapturableBase.gd
119 lines (84 loc) · 2.91 KB
/
CapturableBase.gd
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
extends Area2D
class_name CapturableBase
signal base_captured(new_team)
export (Color) var neutral_color = Color(1, 1, 1)
export (Color) var player_color = Color(0.192157, 0.486275, 0.207843)
export (Color) var enemy_color = Color(0.337255, 0.360784, 0.643137)
var player_unit_count: int = 0
var enemy_unit_count: int = 0
var team_to_capture: int = Team.TeamName.NEUTRAL
onready var collision_shape = $CollisionShape2D
onready var team = $Team
onready var capture_timer = $CaptureTimer
onready var sprite = $Sprite
onready var player_label = $PlayerLabel
onready var enemy_label = $EnemyLabel
func _ready() -> void:
player_label.text = "0"
enemy_label.text = "0"
func get_random_position_within_capture_radius() -> Vector2:
var extents = collision_shape.shape.extents
var top_left = collision_shape.global_position - (extents / 2)
var x = rand_range(top_left.x, top_left.x + extents.x)
var y = rand_range(top_left.y, top_left.y + extents.y)
return Vector2(x, y)
func _on_CapturableBase_body_entered(body: Node) -> void:
if body.has_method("get_team"):
var body_team = body.get_team()
if body_team == Team.TeamName.ENEMY:
enemy_unit_count += 1
change_enemy_label()
elif body_team == Team.TeamName.PLAYER:
player_unit_count += 1
change_player_label()
check_whether_base_can_be_captured()
func _on_CapturableBase_body_exited(body: Node) -> void:
if body.has_method("get_team"):
var body_team = body.get_team()
if body_team == Team.TeamName.ENEMY:
enemy_unit_count -= 1
change_enemy_label()
elif body_team == Team.TeamName.PLAYER:
player_unit_count -= 1
change_player_label()
check_whether_base_can_be_captured()
func change_enemy_label():
enemy_label.text = str(enemy_unit_count)
func change_player_label():
player_label.text = str(player_unit_count)
func check_whether_base_can_be_captured():
var majority_team = get_team_with_majority()
if majority_team == Team.TeamName.NEUTRAL:
print("No majority in base, stopping capture clock")
team_to_capture = Team.TeamName.NEUTRAL
capture_timer.stop()
elif majority_team == team.team:
print("Owning team regained majority, stopping capture clock")
team_to_capture = Team.TeamName.NEUTRAL
capture_timer.stop()
else:
print("New team has majority in base, starting capture clock")
team_to_capture = majority_team
capture_timer.start()
func get_team_with_majority() -> int:
if enemy_unit_count == player_unit_count:
return Team.TeamName.NEUTRAL
elif enemy_unit_count > player_unit_count:
return Team.TeamName.ENEMY
else:
return Team.TeamName.PLAYER
func set_team(new_team: int):
team.team = new_team
emit_signal("base_captured", new_team)
match new_team:
Team.TeamName.NEUTRAL:
sprite.modulate = neutral_color
return
Team.TeamName.PLAYER:
sprite.modulate = player_color
return
Team.TeamName.ENEMY:
sprite.modulate = enemy_color
return
func _on_CaptureTimer_timeout() -> void:
set_team(team_to_capture)