-
Notifications
You must be signed in to change notification settings - Fork 0
/
control.gd
226 lines (164 loc) · 4.8 KB
/
control.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
extends Node2D
var root
var main_loop = true
var ia_active = false
# game
var players_created = false
var next_round = false
var time_left_next_round
var next_player_to_start
# players
var player1
var player2
var player1_config
var player2_config
var player1_points = 0
var player2_points = 0
var limit_points = 12
#TODO check real pos
const PLAYER1_POS = Vector2(100, 200)
const PLAYER2_POS = Vector2(545, 200)
func _ready():
set_fixed_process(true)
func _fixed_process(delta):
# #left-right 0
# #up-down 1
# var _root = get_tree().get_root()
# var root2 = _root.get_child(_root.get_child_count()-1)
# if root2.get_node("kk")!=null:
# #main_loop = false
# root2.get_node("kk").set_text(str(Input.get_joy_axis(0, 0)) + "----" + str(Input.get_joy_axis(0, 1)))
if next_round:
time_left_next_round -= delta
if time_left_next_round <= 0:
next_round = false
control.reset_players_positions()
if next_player_to_start == "player2":
control.player2_start()
else:
control.player1_start()
if ia_active:
IA()
func is_game_in_progress():
if player1.has_the_ball() || player2.has_the_ball():
return false
if next_round:
return false
return true
func end():
next_round = false
set_scene("res://end.tscn")
func get_root():
return root
func enable_ia():
ia_active = true
func set_scene(scene):
root.queue_free()
var s = ResourceLoader.load(scene)
var newroot = s.instance()
get_tree().get_root().add_child(newroot)
func refresh_root():
var _root = get_tree().get_root()
root = _root.get_child(_root.get_child_count()-1)
func init_scene():
refresh_root()
player1 = null
player2 = null
player1_points = 0
player2_points = 0
func get_ball():
return root.get_node("ball")
func get_player(player_name):
return root.get_node(player_name)
func create_get_player1():
if !player1:
var player = load("res://player.tscn")
player1 = player.instance()
root.add_child(player1)
player1.set_player_area("left");
player1.set_player_config(player1_config)
return player1
func create_get_player2():
if !player2:
var player = load("res://player.tscn")
player2 = player.instance()
root.add_child(player2)
var texture = preload("res://ase/player2.png")
player2.get_node("Sprite").set_texture(texture)
player2.set_player_area("right");
player2.set_player_config(player2_config)
return player2
func show_points(player1_points_str, player2_points_str):
var points = load("res://points.tscn")
var poinstaInstance = points.instance()
root.add_child(poinstaInstance)
poinstaInstance.set_pos(Vector2(267, 150))
poinstaInstance.set_points(player1_points_str, player2_points_str)
poinstaInstance.set_z(3)
func set_players_config(new_player1_config, new_player2_config):
player1_config = new_player1_config
player2_config = new_player2_config
func reset_players_positions():
var player1 = create_get_player1()
var player2 = create_get_player2()
player1.set_pos(PLAYER1_POS)
player1.reset_player_orientation()
player2.set_pos(PLAYER2_POS)
player2.reset_player_orientation()
func player1_start():
var ball = get_ball()
ball.reset()
var position = player1.get_player_ball_position()
ball.set_pos(position)
player1.catch()
func player2_start():
var ball = get_ball()
ball.reset()
var position = player2.get_player_ball_position()
ball.set_pos(position)
player2.catch()
func hide_points():
main_loop = true
root.get_node("points").free()
func points(p1_points, p2_points):
if !main_loop:
return
var node_name = ""
player1.stop()
player2.stop()
main_loop = false
player1_points += p1_points
player2_points += p2_points
var player1_points_str = str(player1_points)
var player2_points_str = str(player2_points)
if player2_points_str.length() == 1:
player2_points_str = "0" + player2_points_str
if player1_points_str.length() == 1:
player1_points_str = "0" + player1_points_str
root.get_node("player1_points").set_text(player1_points_str)
root.get_node("player2_points").set_text(player2_points_str)
next_round = true
time_left_next_round = 2
if p1_points > 0:
next_player_to_start = "player2"
elif p2_points > 0:
next_player_to_start = "player1"
if p1_points > 2 || p2_points:
var ball = get_ball()
ball.deactivate()
ball.hide()
show_points(player1_points_str, player2_points_str)
func get_players_poinsts():
return {"player1": player1_points, "player2": player2_points}
func IA():
var shots_types = [Vector2(-1.5, -3), Vector2(-1.5, -1.5), Vector2(-1.5, 1.5), Vector2(-1.5, 3) , Vector2(-1, -1)]
var ball = get_ball()
var ball_pos = ball.get_pos()
if ball_pos.x > 500 && !player2.has_the_ball():
player2.set_pos(ball_pos)
elif player2.has_the_ball():
var time = OS.get_ticks_msec()
var last_catch = player2.get_last_catch_time()
if time - last_catch > 500:
var shot_index = randi() % shots_types.size()
player2.shot(shots_types[shot_index], false)