interact with server

This commit is contained in:
2020-03-11 14:02:52 +08:00
parent 8aa45d2827
commit 5b24b2cd4a
14 changed files with 226 additions and 105 deletions
+9 -6
View File
@@ -5,18 +5,16 @@ extends Node2D
# var a = 2
# var b = "text"
var tiles = []
var texture
# Called when the node enters the scene tree for the first time.
func _ready():
texture = ImageTexture.new()
texture.load("res://Asset/Tile/DropTileBottom.png")
texture = load("res://Asset/Tile/DropTileBottom.png")
func add_tile(suit, number):
var tile = get_tile_instance(suit, number)
tiles.append(tile)
add_child(tile)
show_tiles()
@@ -26,14 +24,19 @@ func get_tile_instance(suit, number):
tile.centered = false
tile.vframes = 7
tile.hframes = 9
tile.frame_coords = Vector2(suit, number)
tile.frame_coords = Vector2(number-1, suit)
return tile
func show_tiles():
for i in range(tiles.size()):
tiles[i].position = Vector2(i*Constants.Tile.BOTTOM_DROP_WIDTH, 0)
for i in range(get_children().size()):
get_children()[i].position = Vector2(i*Constants.Tile.BOTTOM_DROP_WIDTH, 0)
pass
func clear_tiles():
for tile in get_children():
remove_child(tile)
tile.queue_free()
# Called every frame. 'delta' is the elapsed time since the previous frame.
#func _process(delta):
# pass
+27 -4
View File
@@ -5,6 +5,8 @@ extends HBoxContainer
# var a = 2
# var b = "text"
var new_tile_suit = 0
var new_tile_number = 0
# Called when the node enters the scene tree for the first time.
func _ready():
@@ -12,25 +14,46 @@ func _ready():
func add_tile_by_instance(tile):
add_child(tile)
show_tiles()
# show_tiles()
func remove_tile_by_instance(tile):
remove_child(tile)
show_tiles()
# show_tiles()
func clear_tiles():
for child in get_children():
remove_child(child)
child.queue_free()
func set_new_tile(suit, number):
new_tile_suit = suit
new_tile_number = number
func show_tiles():
var tiles = get_children()
var length = tiles.size()*Constants.Tile.WIDTH
var y = Constants.Screen.HEIGHT - Constants.Table.BOTTOM_HAND_MARGIN_Y - Constants.Tile.HEIGHT
var x = (Constants.Screen.WIDTH - Constants.Table.BOTTOM_LIE_WIDTH - length)/2.0
print_debug(x,", ",y)
var new_tile_flag = true
var new_tile_index = -1
set_position(Vector2(x, y))
for i in range(tiles.size()):
# if new_tile_flag and new_tile_number!=0 and tiles[i].suit==new_tile_number and tiles[i].number==new_tile_number:
# new_tile_flag = false
# new_tile_index = i
# continue
if not tiles[i].is_selected:
tiles[i].position = Vector2(i*Constants.Tile.WIDTH, 0)
else:
tiles[i].position = Vector2(i*Constants.Tile.WIDTH, -Constants.Tile.SELECT_HEIGHT)
print_debug("show_tiles")
# if new_tile_index!=-1:
# var new_tile = tiles[new_tile_index]
# if not tiles[new_tile_index].is_selected:
# new_tile.position = Vector2((length+1)*Constants.Tile.WIDTH, 0)
# else:
# new_tile.position = Vector2((length+1), -Constants.Tile.SELECT_HEIGHT)
func sort_tiles():
pass
+22 -9
View File
@@ -8,10 +8,9 @@ extends Node
var url = "ws://127.0.0.1"
var port = "1114"
enum MsgType {GameMsg = 0, ChatMsg}
enum PlayerAction {Start, Deal, Chow, Pong, Kong, Win}
signal deal_tiles
signal recv_game_msg
signal deal_tiles # 发牌
signal draw_tile # 抓牌
# Our WebSocketClient instance
@@ -27,6 +26,9 @@ func _ready():
# Alternatively, you could check get_peer(1).get_available_packets() in a loop.
_client.connect("data_received", self, "_on_data")
func connect_ws():
# Initiate connection to the given URL.
var err = _client.connect_to_url(url+":"+port)
if err != OK:
@@ -58,14 +60,25 @@ func _on_data():
print("Got data from server: ", data_recv)
var json_recv = JSON.parse(data_recv)
var json = json_recv.result
if json["msg_type"]==MsgType.GameMsg:
if json["available_actions"][0]==PlayerAction.Deal:
print_debug("current tiles: ", json["current_tile"])
print_debug("type: ", typeof(json["current_tile"]))
emit_signal("deal_tiles", json)
if json["msg_type"]==Message.msg_type.GAME_MSG:
emit_signal("recv_game_msg", json)
print_debug("msg_type: ", json_recv.result["msg_type"])
func _process(delta):
# Call this in _process or _physics_process. Data transfer, and signals
# emission will only happen when calling this function.
_client.poll()
func send_start():
var msg = Message.game_msg_dict
msg["action"]=Message.player_action.START
msg["chow_type"] = Message.chow_type.NAC
send_message(to_json(msg))
func send_discard(tile, order):
var msg = Message.game_msg_dict
msg["action"]=Message.player_action.DISCARD
msg["tile"] = tile
msg["table_order"] = order
msg["chow_type"] = Message.chow_type.NAC
send_message(to_json(msg))
+8 -6
View File
@@ -5,18 +5,16 @@ extends Node2D
# var a = 2
# var b = "text"
var tiles = []
var texture
# Called when the node enters the scene tree for the first time.
func _ready():
texture = ImageTexture.new()
texture.load("res://Asset/Tile/DropTileSide.png")
texture = load("res://Asset/Tile/DropTileSide.png")
func add_tile(suit, number):
var tile = get_tile_instance(suit, number)
tiles.append(tile)
add_child(tile)
show_tiles()
@@ -26,16 +24,20 @@ func get_tile_instance(suit, number):
tile.centered = false
tile.vframes = 7
tile.hframes = 9
tile.frame_coords = Vector2(suit, number)
tile.frame_coords = Vector2(number-1, suit)
tile.flip_h = true
return tile
func show_tiles():
for i in range(tiles.size()):
tiles[i].position = Vector2(-i*Constants.Tile.SIDE_DROP_DIFF,
for i in range(get_children().size()):
get_children()[i].position = Vector2(-i*Constants.Tile.SIDE_DROP_DIFF,
i*(Constants.Tile.SIDE_DROP_HEIGHT-Constants.Tile.SIDE_DROP_THICK))
pass
func clear_tiles():
for tile in get_children():
remove_child(tile)
tile.queue_free()
# Called every frame. 'delta' is the elapsed time since the previous frame.
#func _process(delta):
# pass
+1 -3
View File
@@ -26,8 +26,6 @@ func show_tiles(count):
func rearrange_tiles():
print_debug("tiles_count:", tiles.size())
print_debug("hand loc:", self.position)
for i in range(tiles.size()):
tiles[i].position = Vector2(-i*Constants.Tile.SIDE_HAND_DIFF, i*Constants.Tile.SIDE_HAND_WIDTH)
@@ -40,7 +38,7 @@ func get_tile_instance():
# Called when the node enters the scene tree for the first time.
func _ready():
texture = ImageTexture.new()
texture.load("res://Asset/Tile/LeftHand.png")
texture = load("res://Asset/Tile/LeftHand.png")
# Called every frame. 'delta' is the elapsed time since the previous frame.
+25
View File
@@ -0,0 +1,25 @@
extends Node
# Declare member variables here. Examples:
# var a = 2
# var b = "text"
# Called when the node enters the scene tree for the first time.
func _ready():
pass # Replace with function body.
enum msg_type{GAME_MSG}
enum player_action{START,DEAL, CHOW,PONG, EXPOSED_KONG, CONCEALED_KONG, ADDED_KONG, WIN, CANCEL, DISCARD, DRAW}
enum chow_type{LEFT, MID, RIGHT, NAC}
var game_msg_dict = {
"msg_type":msg_type.GAME_MSG,
"table_order":0,
"action":player_action.START,
"tile":{"suit":0,"number":0},
"chow_type":chow_type.NAC,
}
+8 -6
View File
@@ -5,18 +5,16 @@ extends Node2D
# var a = 2
# var b = "text"
var tiles = []
var texture
# Called when the node enters the scene tree for the first time.
func _ready():
texture = ImageTexture.new()
texture.load("res://Asset/Tile/DropTileOppo.png")
texture = load("res://Asset/Tile/DropTileOppo.png")
func add_tile(suit, number):
var tile = get_tile_instance(suit, number)
tiles.append(tile)
add_child(tile)
show_tiles()
@@ -26,14 +24,18 @@ func get_tile_instance(suit, number):
tile.centered = false
tile.vframes = 7
tile.hframes = 9
tile.frame_coords = Vector2(suit, number)
tile.frame_coords = Vector2(number-1, suit)
return tile
func show_tiles():
for i in range(tiles.size()):
tiles[i].position = Vector2(-i*Constants.Tile.OPPOSITE_DROP_WIDTH, 0)
for i in range(get_children().size()):
get_children()[i].position = Vector2(-i*Constants.Tile.OPPOSITE_DROP_WIDTH, 0)
pass
func clear_tiles():
for tile in get_children():
remove_child(tile)
tile.queue_free()
# Called every frame. 'delta' is the elapsed time since the previous frame.
#func _process(delta):
# pass
+1 -3
View File
@@ -26,8 +26,6 @@ func show_tiles(count):
func rearrange_tiles():
print_debug("tiles_count:", tiles.size())
print_debug("hand loc:", self.position)
for i in range(tiles.size()):
tiles[i].position = Vector2(i*Constants.Tile.OPPOSITE_HAND_WIDTH, 0)
var x = (Constants.Screen.WIDTH - get_children().size()*Constants.Tile.OPPOSITE_HAND_WIDTH)/2.0
@@ -43,7 +41,7 @@ func get_tile_instance():
# Called when the node enters the scene tree for the first time.
func _ready():
texture = ImageTexture.new()
texture.load("res://Asset/Tile/OppositeHand.png")
texture = load("res://Asset/Tile/OppositeHand.png")
# Called every frame. 'delta' is the elapsed time since the previous frame.
+9 -6
View File
@@ -5,18 +5,16 @@ extends Node2D
# var a = 2
# var b = "text"
var tiles = []
var texture
# Called when the node enters the scene tree for the first time.
func _ready():
texture = ImageTexture.new()
texture.load("res://Asset/Tile/DropTileSide.png")
texture = load("res://Asset/Tile/DropTileSide.png")
func add_tile(suit, number):
var tile = get_tile_instance(suit, number)
tiles.append(tile)
add_child(tile)
move_child(tile, 0)
show_tiles()
@@ -27,14 +25,19 @@ func get_tile_instance(suit, number):
tile.centered = false
tile.vframes = 7
tile.hframes = 9
tile.frame_coords = Vector2(suit, number)
tile.frame_coords = Vector2(number-1, suit)
return tile
func show_tiles():
for i in range(tiles.size()):
tiles[i].position = Vector2(-i*Constants.Tile.SIDE_DROP_DIFF,
for i in range(get_children().size()):
get_children()[i].position = Vector2(-i*Constants.Tile.SIDE_DROP_DIFF,
-i*(Constants.Tile.SIDE_DROP_HEIGHT-Constants.Tile.SIDE_DROP_THICK))
pass
func clear_tiles():
for tile in get_children():
remove_child(tile)
tile.queue_free()
# Called every frame. 'delta' is the elapsed time since the previous frame.
#func _process(delta):
+1 -3
View File
@@ -26,8 +26,6 @@ func show_tiles(count):
func rearrange_tiles():
print_debug("tiles_count:", tiles.size())
print_debug("hand loc:", self.position)
for i in range(tiles.size()):
tiles[i].position = Vector2(i*Constants.Tile.SIDE_HAND_DIFF, i*Constants.Tile.SIDE_HAND_WIDTH)
@@ -40,7 +38,7 @@ func get_tile_instance():
# Called when the node enters the scene tree for the first time.
func _ready():
texture = ImageTexture.new()
texture.load("res://Asset/Tile/RightHand.png")
texture = load("res://Asset/Tile/RightHand.png")
# Called every frame. 'delta' is the elapsed time since the previous frame.
+80 -18
View File
@@ -11,7 +11,7 @@ var my_table_order = 0
# Called when the node enters the scene tree for the first time.
func _ready():
ConnManager.connect("deal_tiles", self, "deal_tile")
ConnManager.connect("recv_game_msg", self, "handle_game_msg")
# center the timer
pass # Replace with function body.
@@ -22,23 +22,87 @@ func tile_click_handler(tile):
$BottomHand.remove_tile_by_instance(tile)
# TODO:send websocket msg
func handle_game_msg(msg):
set_hand_areas(msg)
set_drop_areas(msg)
set_timer_direction(msg)
set_turn(msg)
# 处理逻辑
func deal_tile(msg):
var tiles = msg["current_tile"]
for i in range(tiles.size()):
set_hand_areas(msg)
set_drop_areas(msg)
set_timer_direction(msg)
set_turn(msg)
func set_hand_areas(msg):
var tiles_count = msg["player_tile"]
$RightHand.show_tiles(tiles_count[(my_table_order+1)%4]["hand_tiles"].size())
$OppositeHand.show_tiles(tiles_count[(my_table_order+2)%4]["hand_tiles"].size())
$LeftHand.show_tiles(tiles_count[(my_table_order+3)%4]["hand_tiles"].size())
var my_tiles = msg["player_tile"][my_table_order]["hand_tiles"]
$BottomHand.clear_tiles()
for i in range(my_tiles.size()):
var t = Tile.instance()
t.set_tile_type(tiles[i]["suit"], tiles[i]["number"])
t.set_tile_type(my_tiles[i]["suit"], my_tiles[i]["number"])
t.connect("clicked", self, "handle_tile_click")
$BottomHand.add_tile_by_instance(t)
pass
var tiles_count = msg["tiles_count"]
$RightHand.show_tiles(tiles_count[(my_table_order+1)%4])
$OppositeHand.show_tiles(tiles_count[(my_table_order+2)%4])
$LeftHand.show_tiles(tiles_count[(my_table_order+3)%4])
$BottomHand.show_tiles()
func set_drop_areas(msg):
var bottom_drop_tiles = msg["player_tile"][my_table_order]["drop_tiles"]
$BottomDropArea.clear_tiles()
if bottom_drop_tiles!=null:
for t in bottom_drop_tiles:
$BottomDropArea.add_tile(t["suit"], t["number"])
var right_drop_tiles = msg["player_tile"][(my_table_order+1)%4]["drop_tiles"]
$RightDropArea.clear_tiles()
if right_drop_tiles!=null:
for t in right_drop_tiles:
$RightDropArea.add_tile(t["suit"], t["number"])
var oppo_drop_tiles = msg["player_tile"][(my_table_order+2)%4]["drop_tiles"]
$OppositeDropArea.clear_tiles()
if oppo_drop_tiles!=null:
for t in oppo_drop_tiles:
$OppositeDropArea.add_tile(t["suit"], t["number"])
var left_drop_tiles = msg["player_tile"][(my_table_order+3)%4]["drop_tiles"]
$LeftDropArea.clear_tiles()
if left_drop_tiles!=null:
for t in left_drop_tiles:
$LeftDropArea.add_tile(t["suit"], t["number"])
func set_timer_direction(msg):
var current_turn = msg["current_turn"]
var direction
if current_turn==my_table_order%4:
direction = Constants.Timer_Table.DIRECTION.BOTTOM
elif current_turn==(my_table_order+1)%4:
direction = Constants.Timer_Table.DIRECTION.RIGHT
elif current_turn==(my_table_order+2)%4:
direction = Constants.Timer_Table.DIRECTION.OPPOSITE
elif current_turn==(my_table_order+3)%4:
direction = Constants.Timer_Table.DIRECTION.LEFT
$Timer.show_timer(direction)
func set_turn(msg):
if msg["current_turn"]==my_table_order:
is_my_turn = true
else:
is_my_turn = false
# Called every frame. 'delta' is the elapsed time since the previous frame.
#func _process(delta):
# pass
func handle_tile_click(suit, number):
if not is_my_turn:
return
ConnManager.send_discard({"suit":suit, "number": number}, my_table_order)
func _on_Button_pressed():
var tile = Tile.instance()
@@ -55,27 +119,25 @@ func _on_Button3_pressed():
func _on_Button4_pressed():
$RightHand.show_tiles(randi()%13)
my_table_order = 0
func _on_Button5_pressed():
$BottomDropArea.add_tile(randi()%3, randi()%9)
pass # Replace with function body.
my_table_order = 1
func _on_Button6_pressed():
$OppositeDropArea.add_tile(randi()%3, randi()%9)
my_table_order = 2
func _on_Button7_pressed():
$RightDropArea.add_tile(randi()%3, randi()%9)
my_table_order = 3
func _on_Button8_pressed():
$LeftDropArea.add_tile(randi()%3, randi()%9)
pass # Replace with function body.
ConnManager.connect_ws()
func _on_Button9_pressed():
ConnManager.send_message($HBoxContainer/TextEdit.text)
ConnManager.send_start()
+29 -35
View File
@@ -122,47 +122,41 @@ margin_right = 281.0
margin_bottom = 40.0
text = "Add to Left"
[node name="Button4" type="Button" parent="HBoxContainer"]
[node name="One" type="Button" parent="HBoxContainer"]
margin_left = 291.0
margin_right = 381.0
margin_right = 346.0
margin_bottom = 40.0
text = "Add to Right"
text = " One "
[node name="Button5" type="Button" parent="HBoxContainer"]
margin_left = 391.0
margin_right = 531.0
[node name="Two" type="Button" parent="HBoxContainer"]
margin_left = 356.0
margin_right = 409.0
margin_bottom = 40.0
text = "AddBoomtDropArea"
text = " Two "
[node name="Button6" type="Button" parent="HBoxContainer"]
margin_left = 541.0
margin_right = 644.0
[node name="Three" type="Button" parent="HBoxContainer"]
margin_left = 419.0
margin_right = 483.0
margin_bottom = 40.0
text = "AddOppoDrop"
text = " Three "
[node name="Button7" type="Button" parent="HBoxContainer"]
margin_left = 654.0
margin_right = 754.0
[node name="Four" type="Button" parent="HBoxContainer"]
margin_left = 493.0
margin_right = 549.0
margin_bottom = 40.0
text = "AddRightDrop"
text = " Four "
[node name="Button8" type="Button" parent="HBoxContainer"]
margin_left = 764.0
margin_right = 856.0
[node name="ConnectButton" type="Button" parent="HBoxContainer"]
margin_left = 559.0
margin_right = 623.0
margin_bottom = 40.0
text = "AddLeftDrop"
text = "Connect"
[node name="Button9" type="Button" parent="HBoxContainer"]
margin_left = 866.0
margin_right = 970.0
[node name="StartButton" type="Button" parent="HBoxContainer"]
margin_left = 633.0
margin_right = 674.0
margin_bottom = 40.0
text = "Send to Server"
[node name="TextEdit" type="TextEdit" parent="HBoxContainer"]
margin_left = 980.0
margin_right = 1030.0
margin_bottom = 40.0
rect_min_size = Vector2( 50, 0 )
text = "Start"
[node name="BottomDropArea" type="Node2D" parent="."]
position = Vector2( 621.315, 779.207 )
@@ -182,9 +176,9 @@ script = ExtResource( 15 )
[connection signal="pressed" from="HBoxContainer/Button" to="." method="_on_Button_pressed"]
[connection signal="pressed" from="HBoxContainer/Button2" to="." method="_on_Button2_pressed"]
[connection signal="pressed" from="HBoxContainer/Button3" to="." method="_on_Button3_pressed"]
[connection signal="pressed" from="HBoxContainer/Button4" to="." method="_on_Button4_pressed"]
[connection signal="pressed" from="HBoxContainer/Button5" to="." method="_on_Button5_pressed"]
[connection signal="pressed" from="HBoxContainer/Button6" to="." method="_on_Button6_pressed"]
[connection signal="pressed" from="HBoxContainer/Button7" to="." method="_on_Button7_pressed"]
[connection signal="pressed" from="HBoxContainer/Button8" to="." method="_on_Button8_pressed"]
[connection signal="pressed" from="HBoxContainer/Button9" to="." method="_on_Button9_pressed"]
[connection signal="pressed" from="HBoxContainer/One" to="." method="_on_Button4_pressed"]
[connection signal="pressed" from="HBoxContainer/Two" to="." method="_on_Button5_pressed"]
[connection signal="pressed" from="HBoxContainer/Three" to="." method="_on_Button6_pressed"]
[connection signal="pressed" from="HBoxContainer/Four" to="." method="_on_Button7_pressed"]
[connection signal="pressed" from="HBoxContainer/ConnectButton" to="." method="_on_Button8_pressed"]
[connection signal="pressed" from="HBoxContainer/StartButton" to="." method="_on_Button9_pressed"]
+4 -6
View File
@@ -29,21 +29,19 @@ func set_tile_type(suit, number):
func _input(event):
if (event is InputEventMouseButton or event is InputEventScreenTouch) and event.pressed :
if get_rect().has_point(to_local(event.position)):
emit_signal("clicked", self)
print_debug("pressed")
emit_signal("clicked", self.suit, self.number)
get_tree().set_input_as_handled()
func mouse_enter_handler():
is_selected = true
position = Vector2(position.x, position.y-Constants.Tile.SELECT_HEIGHT)
print_debug("entered")
pass
func mouse_exit_handler():
is_selected = false
position = Vector2(position.x, position.y+Constants.Tile.SELECT_HEIGHT)
print_debug("leaved")
pass
+2
View File
@@ -16,11 +16,13 @@ _global_script_class_icons={
[application]
config/name="MahjongGame"
run/main_scene="res://Table.tscn"
config/icon="res://icon.png"
[autoload]
Constants="*res://Constants.gd"
Message="*res://Message.gd"
ConnManager="*res://ConnManager.gd"
[display]