| … | … | @@ -16,6 +16,7 @@ const LRU = preload('res://src/utils/LRU.gd') |
| 16 | 16 | # Broadcast self when seeing a new remotePublicKey |
| 17 | 17 | |
| 18 | 18 | export var archiveRoot = 'hyper://blog.mauve.moe/' |
| 19 | #export var archiveRoot = 'hyper://70338a94d7415990ab4a3160cf98a5d940243d7fa24dd297352e66b52d3ff841/' | |
| 19 | 20 | export var extensionName = 'hyperchat-example-1' |
| 20 | 21 | |
| 21 | 22 | var extensionsFolder = archiveRoot + '$/extensions/' |
| … | … | @@ -87,5 +87,5 @@ __meta__ = { |
| 87 | 87 | [connection signal="started_gateway" from="HyperGateway" to="StatusLabel" method="setGatewayLoaded"] |
| 88 | 88 | [connection signal="starting_gateway" from="HyperGateway" to="StatusLabel" method="setGatewayLoading"] |
| 89 | 89 | [connection signal="loading_data" from="HyperGateway/HyperRequest" to="StatusLabel" method="setLoading"] |
| 90 | [connection signal="request_completed" from="HyperGateway/HyperRequest" to="ScrollContainer/Output" method="_on_HyperLoad_request_completed"] | |
| 91 | 90 | [connection signal="request_completed" from="HyperGateway/HyperRequest" to="StatusLabel" method="_on_HyperLoad_request_completed"] |
| 91 | [connection signal="request_completed" from="HyperGateway/HyperRequest" to="ScrollContainer/Output" method="_on_HyperLoad_request_completed"] |
| … | … | @@ -1,5 +1,6 @@ |
| 1 | 1 | extends Area2D |
| 2 | 2 | |
| 3 | var created_by = 'me' | |
| 3 | 4 | var initialSpeed = 600 |
| 4 | 5 | var velocity = Vector2(initialSpeed, 0) |
| 5 | 6 |
| … | … | @@ -1,14 +1,17 @@ |
| 1 | 1 | extends Node2D |
| 2 | 2 | |
| 3 | var PlayerSprite = preload("res://src/MultiplayerDemo/PlayerSprite.tscn") | |
| 3 | var RemotePlayer = preload("res://src/MultiplayerDemo/RemotePlayer.tscn") | |
| 4 | var RemoteBullet = preload("res://src/MultiplayerDemo/RemoteBullet.tscn") | |
| 4 | 5 | |
| 5 | export var url: String = "hyper://blog.mauve.moe/" | |
| 6 | #export var url: String = "hyper://blog.mauve.moe/" | |
| 7 | export var url: String = "hyper://70338a94d7415990ab4a3160cf98a5d940243d7fa24dd297352e66b52d3ff841/" | |
| 6 | 8 | |
| 7 | 9 | onready var gossip = $HyperGossip |
| 8 | 10 | onready var player = $Player |
| 9 | 11 | onready var gateway = $HyperGateway |
| 10 | 12 | |
| 11 | 13 | const POSITION = 'position' |
| 14 | const SHOOT = 'shoot' | |
| 12 | 15 | |
| 13 | 16 | const knownPlayers = {} |
| 14 | 17 | |
| … | … | @@ -18,40 +21,79 @@ func _ready(): |
| 18 | 21 | func get_player_object(id): |
| 19 | 22 | if knownPlayers.has(id): return knownPlayers[id] |
| 20 | 23 | |
| 21 | var playerSprite = PlayerSprite.instance() | |
| 22 | knownPlayers[id] = playerSprite | |
| 24 | var remotePlayer = RemotePlayer.instance() | |
| 25 | knownPlayers[id] = remotePlayer | |
| 23 | 26 | |
| 27 | add_child(remotePlayer) | |
| 24 | 28 | _broadcast_player() |
| 25 | 29 | |
| 26 | return playerSprite | |
| 30 | return remotePlayer | |
| 27 | 31 | |
| 28 | 32 | func _broadcast_player(): |
| 29 | 33 | var position = player.position |
| 30 | 34 | var rotation = player.rotation_degrees |
| 35 | var velocity = player.velocity | |
| 31 | 36 | var data = { |
| 32 | 37 | "position": { |
| 33 | 38 | "x": position.x, |
| 34 | 39 | "y": position.y |
| 35 | 40 | }, |
| 41 | "velocity": { | |
| 42 | "x": velocity.x, | |
| 43 | "y": velocity.y | |
| 44 | }, | |
| 36 | 45 | "rotation": rotation |
| 37 | 46 | } |
| 47 | print('Broadcasting', data) | |
| 38 | 48 | gossip.broadcast_event(POSITION, data) |
| 39 | 49 | |
| 50 | func _broadcast_shoot(direction,location): | |
| 51 | var data = { | |
| 52 | "direction": direction, | |
| 53 | "position": { | |
| 54 | "x": location.x, | |
| 55 | "y": location.y | |
| 56 | } | |
| 57 | } | |
| 58 | ||
| 59 | gossip.broadcast_event(SHOOT, data) | |
| 60 | ||
| 40 | 61 | func _on_remote_player_moved(positionData, id): |
| 62 | # print('Moving player', id, " ", positionData) | |
| 41 | 63 | var remotePlayer = get_player_object(id) |
| 42 | 64 | var rotation_degrees = positionData.rotation |
| 43 | 65 | var raw_position = positionData.position |
| 44 | remotePlayer.rotation_degrees = rotation_degrees | |
| 45 | remotePlayer.position.x = raw_position.x | |
| 46 | remotePlayer.position.y = raw_position.y | |
| 66 | var raw_velocity = positionData.velocity | |
| 67 | ||
| 68 | remotePlayer.update_position( | |
| 69 | Vector2(raw_position.x, raw_position.y), | |
| 70 | rotation_degrees, | |
| 71 | Vector2(raw_velocity.x, raw_velocity.y) | |
| 72 | ) | |
| 73 | ||
| 74 | func _on_remote_player_shoot(positionData, id): | |
| 75 | var direction = positionData.direction | |
| 76 | var raw_position = positionData.position | |
| 77 | var new_position = Vector2(raw_position.x, raw_position.y) | |
| 78 | ||
| 79 | var bullet = RemoteBullet.instance() | |
| 80 | bullet.rotation = direction | |
| 81 | bullet.position = new_position | |
| 82 | bullet.velocity = bullet.velocity.rotated(direction) | |
| 83 | bullet.created_by = id | |
| 84 | ||
| 85 | add_child(bullet) | |
| 47 | 86 | |
| 48 | 87 | func _on_Player_shoot(Bullet, direction, location): |
| 49 | 88 | var bullet = Bullet.instance() |
| 50 | add_child(bullet) | |
| 51 | 89 | bullet.rotation = direction |
| 52 | 90 | bullet.position = location |
| 53 | 91 | bullet.velocity = bullet.velocity.rotated(direction) |
| 54 | 92 | |
| 93 | add_child(bullet) | |
| 94 | ||
| 95 | _broadcast_shoot(direction, location) | |
| 96 | ||
| 55 | 97 | func _on_HyperGateway_started_gateway(_pid): |
| 56 | 98 | gossip.url = url |
| 57 | 99 | gossip.start_listening() |
| … | … | @@ -62,8 +104,11 @@ func _on_HyperGossip_listening(_extension_name): |
| 62 | 104 | _broadcast_player() |
| 63 | 105 | |
| 64 | 106 | func _on_HyperGossip_event(type, data, from): |
| 107 | # print("Event ", type, " ", from) | |
| 65 | 108 | if type == POSITION: |
| 66 | 109 | _on_remote_player_moved(data, from) |
| 110 | if type == SHOOT: | |
| 111 | _on_remote_player_shoot(data, from) | |
| 67 | 112 | |
| 68 | 113 | func _on_Player_moved(_position, _rotation, _velocity): |
| 69 | 114 | # TODO: Don't broadcast too often to avoid flooding the system |
| … | … | @@ -8,28 +8,27 @@ signal shoot(bullet, direction, location) |
| 8 | 8 | signal moved(position, rotation, velocity) |
| 9 | 9 | |
| 10 | 10 | var velocity = Vector2() |
| 11 | var old_velocity = Vector2() | |
| 11 | 12 | |
| 12 | 13 | func proccess_input(): |
| 13 | var hasMoved = false | |
| 14 | 14 | look_at(get_global_mouse_position()) |
| 15 | old_velocity = velocity | |
| 15 | 16 | velocity = Vector2() |
| 16 | 17 | if Input.is_action_pressed("right"): |
| 17 | hasMoved = true | |
| 18 | 18 | velocity.x += 1 |
| 19 | 19 | if Input.is_action_pressed("left"): |
| 20 | hasMoved = true | |
| 21 | 20 | velocity.x -= 1 |
| 22 | 21 | if Input.is_action_pressed("down"): |
| 23 | hasMoved = true | |
| 24 | 22 | velocity.y += 1 |
| 25 | 23 | if Input.is_action_pressed("up"): |
| 26 | hasMoved = true | |
| 27 | 24 | velocity.y -= 1 |
| 28 | 25 | |
| 29 | 26 | velocity = velocity.normalized() * speed |
| 30 | 27 | |
| 31 | 28 | if Input.is_action_just_pressed("click"): |
| 32 | 29 | emit_signal("shoot", Bullet, rotation, position) |
| 30 | ||
| 31 | var hasMoved = !velocity.is_equal_approx(old_velocity) | |
| 33 | 32 | |
| 34 | 33 | return hasMoved |
| 35 | 34 |
| … | … | @@ -0,0 +1,5 @@ |
| 1 | [gd_scene load_steps=2 format=2] | |
| 2 | ||
| 3 | [ext_resource path="res://src/MultiplayerDemo/Bullet.tscn" type="PackedScene" id=1] | |
| 4 | ||
| 5 | [node name="Bullet" instance=ExtResource( 1 )] |
| … | … | @@ -0,0 +1,11 @@ |
| 1 | extends "res://src/MultiplayerDemo/PlayerSprite.gd" | |
| 2 | ||
| 3 | var velocity = Vector2() | |
| 4 | ||
| 5 | func update_position(new_position, new_rotation, new_velocity): | |
| 6 | position = new_position | |
| 7 | rotation_degrees = new_rotation | |
| 8 | velocity = new_velocity | |
| 9 | ||
| 10 | func _physics_process(_delta): | |
| 11 | velocity = move_and_slide(velocity) |
| … | … | @@ -0,0 +1,7 @@ |
| 1 | [gd_scene load_steps=3 format=2] | |
| 2 | ||
| 3 | [ext_resource path="res://src/MultiplayerDemo/PlayerSprite.tscn" type="PackedScene" id=1] | |
| 4 | [ext_resource path="res://src/MultiplayerDemo/RemotePlayer.gd" type="Script" id=2] | |
| 5 | ||
| 6 | [node name="PlayerSprite" instance=ExtResource( 1 )] | |
| 7 | script = ExtResource( 2 ) |
| … | … | @@ -26,7 +26,7 @@ func _ready(): |
| 26 | 26 | add_child(broadcastRequest) |
| 27 | 27 | broadcastRequest.connect("request_completed", self, "_on_broadcast_completed") |
| 28 | 28 | |
| 29 | eventSource.connect("event", self, "_on_extesnion_event") | |
| 29 | eventSource.connect("event", self, "_on_event") | |
| 30 | 30 | eventSource.connect("response_started", self, "_on_events_started") |
| 31 | 31 | |
| 32 | 32 | func start_listening(): |