Implemented player controller in client
This commit is contained in:
		
							
								
								
									
										38
									
								
								client/scripts/entities/Player.gd
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										38
									
								
								client/scripts/entities/Player.gd
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,38 @@
 | 
			
		||||
extends KinematicBody2D
 | 
			
		||||
 | 
			
		||||
signal finished_moving
 | 
			
		||||
 | 
			
		||||
export var base_movement_speed = 100
 | 
			
		||||
export(NodePath) var world
 | 
			
		||||
 | 
			
		||||
onready var navigation : Navigation2D = get_node(world)
 | 
			
		||||
var path : PoolVector2Array
 | 
			
		||||
var move : bool = false
 | 
			
		||||
 | 
			
		||||
func _process(delta):
 | 
			
		||||
	
 | 
			
		||||
	# Click for movement target
 | 
			
		||||
	if Input.is_action_just_pressed("move_to_cursor"):
 | 
			
		||||
		path = navigation.get_simple_path(global_position, get_global_mouse_position())
 | 
			
		||||
		move = true
 | 
			
		||||
	
 | 
			
		||||
	if move:
 | 
			
		||||
		move_along_path(base_movement_speed * delta)
 | 
			
		||||
 | 
			
		||||
func move_along_path(distance : float):
 | 
			
		||||
	var start_point := global_position
 | 
			
		||||
	for i in range(path.size()):
 | 
			
		||||
		var distance_to_next := start_point.distance_to(path[0])
 | 
			
		||||
		if distance <= distance_to_next and distance >= 0.0:
 | 
			
		||||
			global_position = start_point.linear_interpolate(path[0], distance/distance_to_next)
 | 
			
		||||
			break
 | 
			
		||||
		elif distance < 0.0:
 | 
			
		||||
			global_position = path[0]
 | 
			
		||||
			emit_signal("finished_moving")
 | 
			
		||||
			print("DONE")
 | 
			
		||||
			move = false
 | 
			
		||||
			break
 | 
			
		||||
		distance -= distance_to_next
 | 
			
		||||
		start_point = path[0]
 | 
			
		||||
		path.remove(0)
 | 
			
		||||
 | 
			
		||||
@ -1,6 +1,9 @@
 | 
			
		||||
extends Node
 | 
			
		||||
 | 
			
		||||
signal tile_update(tile_data)
 | 
			
		||||
signal player_joined(user_id)
 | 
			
		||||
signal player_left(user_id)
 | 
			
		||||
signal player_pos_update(user_id, pos)
 | 
			
		||||
 | 
			
		||||
const KEY := "defaultkey"
 | 
			
		||||
const SERVER_ENDPOINT := "nakama.cloudsumu.com"
 | 
			
		||||
@ -9,9 +12,11 @@ var _session : NakamaSession
 | 
			
		||||
var _client : NakamaClient = Nakama.create_client(KEY, SERVER_ENDPOINT, 7350, "http")
 | 
			
		||||
var _socket : NakamaSocket
 | 
			
		||||
var _precenses : Dictionary = {}
 | 
			
		||||
var _world_id
 | 
			
		||||
 | 
			
		||||
enum OPCODE {
 | 
			
		||||
	tile_update = 1
 | 
			
		||||
	tile_update = 1,
 | 
			
		||||
	update_position = 2
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func authenticate_async(email : String, password : String) -> NakamaException:
 | 
			
		||||
@ -43,6 +48,7 @@ func connect_to_server_async() -> NakamaException:
 | 
			
		||||
	var result : NakamaAsyncResult = yield(_socket.connect_async(_session), "completed")
 | 
			
		||||
	if not result.is_exception():
 | 
			
		||||
		_socket.connect("received_match_state", self, "_on_socket_received_match_state")
 | 
			
		||||
		_socket.connect("received_match_presence", self, "_on_received_match_presence")
 | 
			
		||||
		_socket.connect("closed", self, "_on_socket_closed")
 | 
			
		||||
		return null
 | 
			
		||||
	return result.exception
 | 
			
		||||
@ -57,9 +63,12 @@ func join_world_async() -> Dictionary:
 | 
			
		||||
	if match_join_result.is_exception():
 | 
			
		||||
		print("Join match error: %s - %s" % [match_join_result.exception.status_code, match_join_result.exception.message])
 | 
			
		||||
		return {}
 | 
			
		||||
		
 | 
			
		||||
	_world_id = world.payload
 | 
			
		||||
	
 | 
			
		||||
	for precense in match_join_result.presences:
 | 
			
		||||
		_precenses[precense.user_id] = precense
 | 
			
		||||
		emit_signal("player_joined", precense.user_id)
 | 
			
		||||
		
 | 
			
		||||
	print("Joined matched with %s other players!" % _precenses.size())
 | 
			
		||||
 | 
			
		||||
@ -67,9 +76,26 @@ func join_world_async() -> Dictionary:
 | 
			
		||||
 | 
			
		||||
func _on_socket_closed():
 | 
			
		||||
	_socket = null
 | 
			
		||||
	
 | 
			
		||||
 | 
			
		||||
func _on_received_match_presence(match_precense : NakamaRTAPI.MatchPresenceEvent):
 | 
			
		||||
	for precense in match_precense.joins:
 | 
			
		||||
		print("%s joined the game!" % precense.username)
 | 
			
		||||
		_precenses[precense.user_id] = precense
 | 
			
		||||
		emit_signal("player_joined", precense.user_id)
 | 
			
		||||
		
 | 
			
		||||
	for precense in match_precense.leaves:
 | 
			
		||||
		print("%s left the game!" % precense.username)
 | 
			
		||||
		_precenses.erase(precense.user_id)
 | 
			
		||||
		emit_signal("player_left", precense.user_id)
 | 
			
		||||
 | 
			
		||||
func _on_socket_received_match_state(match_state: NakamaRTAPI.MatchData):
 | 
			
		||||
	match match_state.op_code:
 | 
			
		||||
		OPCODE.tile_update:
 | 
			
		||||
			emit_signal("tile_update", JSON.parse(match_state.data).result)
 | 
			
		||||
		OPCODE.update_position:
 | 
			
		||||
			var pos_data = JSON.parse(match_state.data).result
 | 
			
		||||
			emit_signal("player_pos_update", pos_data["player"], Vector2(float(pos_data["x"]), float(pos_data["y"])))
 | 
			
		||||
 | 
			
		||||
func send_player_position(position : Vector2) -> void:
 | 
			
		||||
	_socket.send_match_state_async(_world_id, OPCODE.update_position, JSON.print({X = str(position.x), Y = str(position.y)}))
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										31
									
								
								client/scripts/systems/PlayerPuppeteer.gd
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										31
									
								
								client/scripts/systems/PlayerPuppeteer.gd
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,31 @@
 | 
			
		||||
extends Node2D
 | 
			
		||||
 | 
			
		||||
export(NodePath) var puppet_parent
 | 
			
		||||
export(Resource) var puppet_template
 | 
			
		||||
 | 
			
		||||
onready var puppet_parent_node : Node = get_node(puppet_parent)
 | 
			
		||||
 | 
			
		||||
var puppets : Dictionary = {}
 | 
			
		||||
 | 
			
		||||
func _ready():
 | 
			
		||||
	ServerConnection.connect("player_joined", self, "on_player_join")
 | 
			
		||||
	ServerConnection.connect("player_left", self, "on_player_leave")
 | 
			
		||||
	ServerConnection.connect("player_pos_update", self, "on_player_pos_update")
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
func on_player_join(user_id):
 | 
			
		||||
	if user_id != ServerConnection._session.user_id:
 | 
			
		||||
		var new_puppet : Node = puppet_template.instance()
 | 
			
		||||
		new_puppet.name = "Player: " + user_id
 | 
			
		||||
		puppet_parent_node.add_child(new_puppet)
 | 
			
		||||
		puppets[user_id] = new_puppet
 | 
			
		||||
 | 
			
		||||
func on_player_leave(user_id):
 | 
			
		||||
	if user_id != ServerConnection._session.user_id:
 | 
			
		||||
		var player_puppet : Node = puppets[user_id]
 | 
			
		||||
		player_puppet.queue_free()
 | 
			
		||||
 | 
			
		||||
func on_player_pos_update(user_id, pos):
 | 
			
		||||
	if user_id != ServerConnection._session.user_id:
 | 
			
		||||
		var player_puppet : Node2D = puppets[user_id]
 | 
			
		||||
		player_puppet.global_position = pos
 | 
			
		||||
		Reference in New Issue
	
	Block a user