UDP movement
This commit is contained in:
		@ -1,8 +1,10 @@
 | 
				
			|||||||
extends KinematicBody2D
 | 
					extends KinematicBody2D
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const BASE_SPEED : float = 50.0
 | 
				
			||||||
export var user : String = ""
 | 
					export var user : String = ""
 | 
				
			||||||
var main : bool = false
 | 
					var main : bool = false
 | 
				
			||||||
var input_locks = 0
 | 
					var input_locks = 0
 | 
				
			||||||
 | 
					const SPEED_VAL = 75
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func set_main():
 | 
					func set_main():
 | 
				
			||||||
	main = true
 | 
						main = true
 | 
				
			||||||
@ -19,23 +21,31 @@ func _ready():
 | 
				
			|||||||
func set_username(username):
 | 
					func set_username(username):
 | 
				
			||||||
	user = username
 | 
						user = username
 | 
				
			||||||
	$Label.text = user
 | 
						$Label.text = user
 | 
				
			||||||
 | 
						
 | 
				
			||||||
 | 
					func update_position(pos : Vector2):
 | 
				
			||||||
 | 
						if not main or abs(abs(pos.x) - abs(position.x)) > SPEED_VAL or abs(abs(pos.y) - abs(position.y)) > SPEED_VAL:
 | 
				
			||||||
 | 
							position = pos
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var velocity : Vector2 = Vector2(0,0)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func _process(delta):
 | 
					func _process(delta):
 | 
				
			||||||
	if main and input_locks <= 0:
 | 
						if main and input_locks <= 0:
 | 
				
			||||||
		var movePos : Vector2 = Vector2(0,0)
 | 
							var relative_movement : Vector2 = Vector2(0,0)
 | 
				
			||||||
		
 | 
							
 | 
				
			||||||
		
 | 
							if(Input.is_action_pressed("ui_up")):
 | 
				
			||||||
		if(Input.is_action_just_pressed("ui_up")):
 | 
								relative_movement.y = relative_movement.y - 1
 | 
				
			||||||
			movePos.y = movePos.y - 1
 | 
							if(Input.is_action_pressed("ui_down")):
 | 
				
			||||||
		if(Input.is_action_just_pressed("ui_down")):
 | 
								relative_movement.y = relative_movement.y + 1
 | 
				
			||||||
			movePos.y = movePos.y + 1
 | 
							if(Input.is_action_pressed("ui_right")):
 | 
				
			||||||
		if(Input.is_action_just_pressed("ui_right")):
 | 
								relative_movement.x = relative_movement.x + 1
 | 
				
			||||||
			movePos.x = movePos.x + 1
 | 
							if(Input.is_action_pressed("ui_left")):
 | 
				
			||||||
		if(Input.is_action_just_pressed("ui_left")):
 | 
								relative_movement.x = relative_movement.x - 1
 | 
				
			||||||
			movePos.x = movePos.x - 1
 | 
					
 | 
				
			||||||
	
 | 
							if relative_movement != Vector2.ZERO:
 | 
				
			||||||
		if movePos != Vector2.ZERO:
 | 
								move_and_collide(relative_movement * delta * BASE_SPEED)
 | 
				
			||||||
			$"/root/NetworkManager".move_player(movePos.x, movePos.y)
 | 
								var dest = $"/root/ImportantEntities".tile_map.world_to_map(self.position)
 | 
				
			||||||
 | 
								dest = dest + (position - (dest * 16))/16
 | 
				
			||||||
 | 
								$"/root/NetworkManager".move_player(dest.x, dest.y)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func lock_input():
 | 
					func lock_input():
 | 
				
			||||||
	input_locks = input_locks + 1
 | 
						input_locks = input_locks + 1
 | 
				
			||||||
 | 
				
			|||||||
@ -107,19 +107,19 @@ func send_chat_message(msg : String):
 | 
				
			|||||||
	var pckt : PoolByteArray = (msg + '\n').to_ascii()
 | 
						var pckt : PoolByteArray = (msg + '\n').to_ascii()
 | 
				
			||||||
	send_packet(pckt, 1)
 | 
						send_packet(pckt, 1)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func send_packet(packet : PoolByteArray, channel = 0):
 | 
					func send_packet(packet : PoolByteArray, channel = 0, pck_type = GDNetMessage.RELIABLE):
 | 
				
			||||||
	packetQueue.append({'channel':channel, 'packet' : packet})
 | 
						packetQueue.append({'channel':channel, 'packet' : packet, 'type' : pck_type})
 | 
				
			||||||
			
 | 
								
 | 
				
			||||||
func move_player(x,y):
 | 
					func move_player(x,y):
 | 
				
			||||||
	var pckt : PoolByteArray = ("3|" + str(x) + "," + str(y)).to_ascii()
 | 
						var pckt : PoolByteArray = ("3|" + str(x) + "," + str(y)).to_ascii()
 | 
				
			||||||
	
 | 
						
 | 
				
			||||||
	send_packet(pckt)
 | 
						send_packet(pckt, 0, GDNetMessage.SEQUENCED)
 | 
				
			||||||
	
 | 
						
 | 
				
			||||||
func _process(delta):
 | 
					func _process(delta):
 | 
				
			||||||
	process_events()
 | 
						process_events()
 | 
				
			||||||
	
 | 
						
 | 
				
			||||||
	if len(packetQueue) > 0 and connected:
 | 
						if len(packetQueue) > 0 and connected:
 | 
				
			||||||
		peer.send_packet(packetQueue[0]['packet'], packetQueue[0]['channel'], GDNetMessage.RELIABLE)
 | 
							peer.send_packet(packetQueue[0]['packet'], packetQueue[0]['channel'], packetQueue[0]['type'])
 | 
				
			||||||
		packetQueue.remove(0)
 | 
							packetQueue.remove(0)
 | 
				
			||||||
		
 | 
							
 | 
				
			||||||
	
 | 
						
 | 
				
			||||||
 | 
				
			|||||||
@ -1,3 +1,4 @@
 | 
				
			|||||||
extends Node
 | 
					extends Node
 | 
				
			||||||
 | 
					
 | 
				
			||||||
var main_player : KinematicBody2D = null
 | 
					var main_player : KinematicBody2D = null
 | 
				
			||||||
 | 
					var tile_map : TileMap = null
 | 
				
			||||||
 | 
				
			|||||||
@ -2,6 +2,7 @@ extends Node
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func _ready():
 | 
					func _ready():
 | 
				
			||||||
 | 
						$"/root/ImportantEntities".tile_map = $Tiles
 | 
				
			||||||
	$Tiles.clear()
 | 
						$Tiles.clear()
 | 
				
			||||||
	$"/root/NetworkManager".connect("world_data_recieved", self, "_on_world_update")
 | 
						$"/root/NetworkManager".connect("world_data_recieved", self, "_on_world_update")
 | 
				
			||||||
	$"/root/NetworkManager".request_world_map()
 | 
						$"/root/NetworkManager".request_world_map()
 | 
				
			||||||
@ -16,7 +17,10 @@ func _on_world_update():
 | 
				
			|||||||
			elif ',' in tileUpdate:
 | 
								elif ',' in tileUpdate:
 | 
				
			||||||
				var tile_data = tileUpdate.split(',')
 | 
									var tile_data = tileUpdate.split(',')
 | 
				
			||||||
				if ':' in tile_data[2]:
 | 
									if ':' in tile_data[2]:
 | 
				
			||||||
					var pos : Vector2 = $Tiles.map_to_world(Vector2(int(tile_data[0]), int(tile_data[1])))
 | 
										var pos : Vector2 = $Tiles.map_to_world(Vector2(float(tile_data[0]), float(tile_data[1]))) 
 | 
				
			||||||
 | 
										pos = Vector2(pos.x + get_decimals(float(tile_data[0])), pos.y + get_decimals(float(tile_data[1])))
 | 
				
			||||||
 | 
										
 | 
				
			||||||
 | 
										#pos = Vector2(pos.x + decs.x, pos.y + decs.y)
 | 
				
			||||||
					var entity_data = tile_data[2].split(':')
 | 
										var entity_data = tile_data[2].split(':')
 | 
				
			||||||
					update_entity(entity_data[1], pos, entity_data[0])
 | 
										update_entity(entity_data[1], pos, entity_data[0])
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -25,11 +29,15 @@ func _on_world_update():
 | 
				
			|||||||
	if get_node_or_null("Loading") != null:
 | 
						if get_node_or_null("Loading") != null:
 | 
				
			||||||
		$Loading.queue_free()
 | 
							$Loading.queue_free()
 | 
				
			||||||
		
 | 
							
 | 
				
			||||||
 | 
					func get_decimals(number : float):
 | 
				
			||||||
 | 
						var temp : float = int(number)
 | 
				
			||||||
 | 
						return (number - temp) * 16
 | 
				
			||||||
 | 
							
 | 
				
			||||||
func display_error(error):
 | 
					func display_error(error):
 | 
				
			||||||
	print("Error " + error)
 | 
						print("Error " + error)
 | 
				
			||||||
		
 | 
							
 | 
				
			||||||
func update_entity(entity_id : String, pos : Vector2, type : String):
 | 
					func update_entity(entity_id : String, pos : Vector2, type : String):
 | 
				
			||||||
	var entity : Node2D = get_node_or_null( str(type + "-" + entity_id))	
 | 
						var entity : Node2D = get_node_or_null( str(type + "-" + entity_id))
 | 
				
			||||||
	if not entity:
 | 
						if not entity:
 | 
				
			||||||
		var entity_location = "res://nodes/entities/" + type + ".tscn"
 | 
							var entity_location = "res://nodes/entities/" + type + ".tscn"
 | 
				
			||||||
		if File.new().file_exists(entity_location):
 | 
							if File.new().file_exists(entity_location):
 | 
				
			||||||
@ -43,7 +51,10 @@ func update_entity(entity_id : String, pos : Vector2, type : String):
 | 
				
			|||||||
		else:
 | 
							else:
 | 
				
			||||||
			display_error("Trying to load entity of type: " + type + ", but failed.")
 | 
								display_error("Trying to load entity of type: " + type + ", but failed.")
 | 
				
			||||||
	if entity:
 | 
						if entity:
 | 
				
			||||||
		entity.position = pos
 | 
							if entity.has_method("update_position"):
 | 
				
			||||||
 | 
								entity.update_position(pos)
 | 
				
			||||||
 | 
							else:
 | 
				
			||||||
 | 
								entity.position = pos
 | 
				
			||||||
		if entity.has_method("set_username"):
 | 
							if entity.has_method("set_username"):
 | 
				
			||||||
			entity.set_username(entity_id)
 | 
								entity.set_username(entity_id)
 | 
				
			||||||
		
 | 
							
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user