-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmelee.gd
122 lines (92 loc) · 3.36 KB
/
melee.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
extends Control
const MOUSE_SPEED = 25
const MOUSE_X_MAX = 1920
const MOUSE_X_MIN = 0
const MOUSE_Y_MAX = 1080
const MOUSE_Y_MIN = 0
const MOUSE_MIN_INPUT = 0.4
var players
func activate_players():
for i in range(get_node("/root/player_variables").MAX_NUM_PLAYERS):
if not players[i]["active"] and Input.is_joy_button_pressed(i, JOY_XBOX_A):
players[i]["active"] = true
print("player %d active!" % (i + 1))
get_node("players").get_child(i).hide()
func move_mice():
for i in range(get_node("/root/player_variables").MAX_NUM_PLAYERS):
if players[i]["active"]:
var x_axis = Input.get_joy_axis(i, 0)
var y_axis = Input.get_joy_axis(i, 1)
var mouse = get_node("mice").get_child(i)
var mouse_pos = mouse.get_global_pos()
#print("mouse pos: ", mouse_pos)
if abs(x_axis) < MOUSE_MIN_INPUT:
#print("mouse input not enough")
x_axis = 0
if abs(y_axis) < MOUSE_MIN_INPUT:
#print("mouse input not enough")
y_axis = 0
if mouse_pos.y <= MOUSE_Y_MIN:
y_axis = max (y_axis, 0)
#print("y too small")
elif mouse_pos.y >= MOUSE_Y_MAX:
y_axis = min (y_axis, 0)
#print("y too big")
elif mouse_pos.x <= MOUSE_X_MIN:
x_axis = max (x_axis, 0)
#print("x too small")
elif mouse_pos.x >= MOUSE_X_MAX:
x_axis = min (x_axis, 0)
#print("x too big")
mouse.set_global_pos( mouse_pos + Vector2(x_axis * MOUSE_SPEED, y_axis * MOUSE_SPEED) )
# take a global position vector, if it's in
# a character, return the index, else return -1
func pos_in_avatar(pos):
var i = 0
var no_characters = get_node("characters").get_child_count()
var cur_character
while i < no_characters:
cur_character = get_node("characters").get_child(i)
var min_x = cur_character.get_node("min_point").get_global_pos().x
var min_y = cur_character.get_node("min_point").get_global_pos().y
var max_x = cur_character.get_node("max_point").get_global_pos().x
var max_y = cur_character.get_node("max_point").get_global_pos().y
if pos.x >= min_x and pos.x <= max_x and pos.y >= min_y and pos.y <= max_y:
return i
i += 1
return -1
func update_selected_chars():
for i in range(get_node("/root/player_variables").MAX_NUM_PLAYERS):
if players[i]["active"]:
var mouse = get_node("mice").get_child(i)
var mouse_pos = mouse.get_node("click").get_global_pos()
var selected_char = pos_in_avatar(mouse_pos)
if selected_char != -1:
if Input.is_joy_button_pressed(i, JOY_XBOX_A):
players[i]["character"] = selected_char
func are_we_ready():
var players_active = 0
var players_selected = 0
for i in range(get_node("/root/player_variables").MAX_NUM_PLAYERS):
if players[i]["active"]:
players_active += 1
if players[i]["character"] >= 0:
players_selected += 1
var ready = players_active >= 2 and players_selected == players_active
get_node("ready").set_text("active players: %d\nselected characters: %d\nready: %s" % [players_active, players_selected, ready])
return ready
func _ready():
players = get_node("/root/player_variables").players
set_process(true)
func _process(delta):
activate_players()
move_mice()
update_selected_chars()
var ready = are_we_ready()
if ready:
get_node("start").show()
for i in range(8):
if Input.is_joy_button_pressed(i, JOY_START):
get_node("/root/scene_switcher").goto_scene("res://map_select.tscn")
else:
get_node("start").hide()