start Multiplayer

This commit is contained in:
2023-01-15 23:59:12 +01:00
parent 5cfc0839f7
commit 1ef5d54d6c
11 changed files with 209 additions and 255 deletions

25
Game.gd
View File

@@ -1,33 +1,29 @@
extends Node3D
class_name Game
@onready var label : Label = $CanvasLayer/Label
@onready var player : CharacterBody3D = $PlayerQ3
signal game_loaded
var init_config
func _ready():
func load_map():
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
var config = Runtimeloader.loadConfig("init.cfg") as ConfigFile
init_config = Runtimeloader.loadConfig("init.cfg") as ConfigFile
print("Loading PCK...")
for map_list in config.get_section_keys("MAPS"):
for map in config.get_value("MAPS", map_list):
for map_list in init_config.get_section_keys("MAPS"):
for map in init_config.get_value("MAPS", map_list):
Runtimeloader.loadPCK(map)
for weapons_list in config.get_section_keys("WEAPONS"):
for weapon in config.get_value("WEAPONS", weapons_list):
for weapons_list in init_config.get_section_keys("WEAPONS"):
for weapon in init_config.get_value("WEAPONS", weapons_list):
Runtimeloader.loadPCK(weapon)
print("Loading Resources Done.")
Runtimeloader.loadScene("maps/test_map.tscn", self)
#Runtimeloader.get_all_entities(node)
func _process(_delta):
label.text = "H Velocity: %3.2f" % [Vector2(player.velocity.x, player.velocity.z).length()]
label.text += "\nV Velocity: %3.2f" % [player.velocity.y]
label.text += "\nOn floor: %s" % player.is_on_floor()
game_loaded.emit()
func _input(event):
@@ -39,3 +35,4 @@ func _input(event):
DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_WINDOWED)
elif event.keycode == KEY_ESCAPE:
get_tree().quit()

View File

@@ -1,6 +1,9 @@
shader_type spatial;
render_mode cull_back;
uniform float z_offset : hint_range(-1, 1);
void fragment() {
// Place fragment code here.
// write the final depth value to the depth buffer
DEPTH = 0.9;
}

View File

@@ -1,12 +1,19 @@
[gd_scene load_steps=6 format=3 uid="uid://bl7jynld7s25o"]
[gd_scene load_steps=10 format=3 uid="uid://bl7jynld7s25o"]
[ext_resource type="Script" path="res://scripts/player/PlayerQ3.gd" id="1"]
[ext_resource type="Script" path="res://scripts/player/Weapons.gd" id="2_dl1i1"]
[ext_resource type="Texture2D" uid="uid://17b1nu8spjfi" path="res://textures/icon.png" id="2_mxb5i"]
[ext_resource type="Shader" path="res://assets/shader/crosshair.gdshader" id="3_3vnqi"]
[sub_resource type="CylinderShape3D" id="1"]
[sub_resource type="CylinderShape3D" id="CylinderShape3D_resyc"]
[sub_resource type="ShaderMaterial" id="ShaderMaterial_mqdp4"]
[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_d6dov"]
albedo_texture = ExtResource("2_mxb5i")
uv1_scale = Vector3(3, 2, 1)
[sub_resource type="CylinderMesh" id="CylinderMesh_j6ixa"]
[sub_resource type="ShaderMaterial" id="ShaderMaterial_qn642"]
shader = ExtResource("3_3vnqi")
shader_parameter/center_enabled = null
shader_parameter/legs_enabled = null
@@ -21,23 +28,39 @@ shader_parameter/len = null
shader_parameter/spacing = null
shader_parameter/spread = null
[sub_resource type="SceneReplicationConfig" id="SceneReplicationConfig_36ekt"]
properties/0/path = NodePath(".:position")
properties/0/spawn = true
properties/0/sync = true
properties/1/path = NodePath("Body:rotation")
properties/1/spawn = true
properties/1/sync = true
properties/2/path = NodePath("Body/Head:rotation")
properties/2/spawn = true
properties/2/sync = true
[node name="PlayerQ3" type="CharacterBody3D"]
script = ExtResource("1")
[node name="CollisionShape3D" type="CollisionShape3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0)
shape = SubResource("1")
shape = SubResource("CylinderShape3D_resyc")
[node name="MeshInstance3D" type="MeshInstance3D" parent="CollisionShape3D"]
material_override = SubResource("StandardMaterial3D_d6dov")
mesh = SubResource("CylinderMesh_j6ixa")
[node name="Body" type="Node3D" parent="."]
[node name="Head" type="Node3D" parent="Body"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.75, 0)
[node name="Hand" type="Node3D" parent="Body/Head"]
[node name="Hand" type="Node3D" parent="Body/Head" node_paths=PackedStringArray("player_root")]
script = ExtResource("2_dl1i1")
player_root = NodePath("../../..")
[node name="Crosshair" type="ColorRect" parent="Body/Head/Hand"]
material = SubResource("ShaderMaterial_mqdp4")
material = SubResource("ShaderMaterial_qn642")
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
@@ -51,3 +74,16 @@ current = true
[node name="AimCast" type="RayCast3D" parent="Body/Head/Camera3D"]
target_position = Vector3(0, 0, -1)
collision_mask = 512
[node name="CanvasLayer" type="CanvasLayer" parent="."]
[node name="Label" type="Label" parent="CanvasLayer"]
offset_right = 40.0
offset_bottom = 14.0
metadata/_edit_use_anchors_ = true
[node name="Networking" type="Node" parent="."]
[node name="MultiplayerSynchronizer" type="MultiplayerSynchronizer" parent="Networking"]
root_path = NodePath("../..")
replication_config = SubResource("SceneReplicationConfig_36ekt")

View File

@@ -9,6 +9,11 @@
config_version=5
_global_script_classes=[{
"base": "Node3D",
"class": &"Game",
"language": &"GDScript",
"path": "res://Game.gd"
}, {
"base": "Node",
"class": &"Hitable",
"language": &"GDScript",
@@ -25,6 +30,7 @@ _global_script_classes=[{
"path": "res://scripts/player/Weapon.gd"
}]
_global_script_class_icons={
"Game": "",
"Hitable": "",
"TBPlugin": "",
"Weapon": ""
@@ -40,6 +46,7 @@ config/icon="res://textures/icon.png"
[autoload]
Runtimeloader="*res://scripts/utils/runtimeloader.gd"
Network="*res://scripts/utils/network.gd"
[display]

View File

@@ -1,4 +1,8 @@
[gd_scene format=3 uid="uid://dsnlwjofq6psf"]
[gd_scene load_steps=4 format=3 uid="uid://dsnlwjofq6psf"]
[ext_resource type="Script" path="res://scenes/GUI/MainMenu.gd" id="1_23sw7"]
[ext_resource type="PackedScene" uid="uid://3o430r2kys4k" path="res://scenes/GUI/MAIN.tscn" id="1_sojs8"]
[ext_resource type="PackedScene" uid="uid://d0kkhtesg83g" path="res://scenes/GUI/START_GAME.tscn" id="2_ipoll"]
[node name="Main Menu" type="Control"]
layout_mode = 3
@@ -9,61 +13,14 @@ anchor_right = 0.5
anchor_bottom = 0.5
grow_horizontal = 2
grow_vertical = 2
script = ExtResource("1_23sw7")
metadata/_edit_use_anchors_ = true
[node name="VBoxContainer" type="VBoxContainer" parent="."]
anchors_preset = 8
anchor_left = 0.5
anchor_top = 0.5
anchor_right = 0.5
anchor_bottom = 0.5
offset_left = -198.0
offset_top = -104.0
offset_right = 199.0
offset_bottom = 64.0
grow_horizontal = 2
grow_vertical = 2
[node name="MAIN" parent="." instance=ExtResource("1_sojs8")]
[node name="HBoxContainer" type="HBoxContainer" parent="VBoxContainer"]
offset_left = 102.0
offset_right = 295.0
offset_bottom = 31.0
size_flags_horizontal = 4
size_flags_vertical = 2
[node name="START GAME" parent="." instance=ExtResource("2_ipoll")]
visible = false
[node name="Start" type="Button" parent="VBoxContainer/HBoxContainer"]
offset_right = 96.0
offset_bottom = 31.0
text = "Start Game"
[node name="ModsMenu" type="MenuButton" parent="VBoxContainer/HBoxContainer"]
offset_left = 100.0
offset_right = 193.0
offset_bottom = 31.0
text = "Select Mod"
flat = false
item_count = 2
popup/item_0/text = "test1"
popup/item_0/id = 0
popup/item_1/text = "test2"
popup/item_1/id = 1
[node name="VBoxContainer2" type="VBoxContainer" parent="VBoxContainer"]
offset_left = 138.0
offset_top = 102.0
offset_right = 258.0
offset_bottom = 168.0
size_flags_horizontal = 4
size_flags_vertical = 10
[node name="SettingsButton" type="Button" parent="VBoxContainer/VBoxContainer2"]
offset_right = 120.0
offset_bottom = 31.0
text = "Game Settings"
icon_alignment = 1
[node name="ExitButton" type="Button" parent="VBoxContainer/VBoxContainer2"]
offset_top = 35.0
offset_right = 120.0
offset_bottom = 66.0
text = "Exit"
[connection signal="start_game" from="MAIN" to="." method="_on_main_start_game"]
[connection signal="back" from="START GAME" to="." method="_on_start_game_back"]
[connection signal="start_game" from="START GAME" to="." method="_on_start_game_start_game"]

View File

@@ -1,8 +1,7 @@
[gd_scene load_steps=7 format=3 uid="uid://8g7w4uv2xsck"]
[gd_scene load_steps=6 format=3 uid="uid://8g7w4uv2xsck"]
[ext_resource type="Script" path="res://Game.gd" id="1_71dcv"]
[ext_resource type="PackedScene" uid="uid://bp5ekvan8qsmc" path="res://entities/enemy/dummy.tscn" id="2_13e86"]
[ext_resource type="PackedScene" uid="uid://bl7jynld7s25o" path="res://entities/Player.tscn" id="3_ngdfc"]
[ext_resource type="PackedScene" uid="uid://dsnlwjofq6psf" path="res://scenes/GUI/Main Menu.tscn" id="2_448f8"]
[sub_resource type="PhysicalSkyMaterial" id="PhysicalSkyMaterial_2n8qo"]
@@ -31,15 +30,10 @@ shadow_enabled = true
[node name="WorldEnvironment" type="WorldEnvironment" parent="."]
environment = SubResource("Environment_oqaam")
[node name="CanvasLayer" type="CanvasLayer" parent="."]
[node name="Main Menu" parent="." instance=ExtResource("2_448f8")]
[node name="Label" type="Label" parent="CanvasLayer"]
offset_right = 40.0
offset_bottom = 14.0
metadata/_edit_use_anchors_ = true
[node name="Networking" type="Node" parent="."]
[node name="Enemy" parent="." instance=ExtResource("2_13e86")]
transform = Transform3D(-1, 0, -8.74228e-08, 0, 1, 0, 8.74228e-08, 0, -1, 23.8087, 0.346554, -28.3203)
[node name="PlayerQ3" parent="." instance=ExtResource("3_ngdfc")]
transform = Transform3D(-1, 0, -8.74228e-08, 0, 1, 0, 8.74228e-08, 0, -1, 3.52828, 2.64918, -22.0849)
[node name="MultiplayerSpawner" type="MultiplayerSpawner" parent="Networking"]
_spawnable_scenes = PackedStringArray("res://entities/Player.tscn")
spawn_path = NodePath("../..")

File diff suppressed because one or more lines are too long

View File

@@ -2,6 +2,9 @@ extends CharacterBody3D
const DIST_FLOOR_SNAP := Vector3.DOWN * 0.2
@onready var label : Label = $CanvasLayer/Label
@onready var synchroniser = $Networking/MultiplayerSynchronizer
@onready var body : Node3D = $Body
@onready var head : Node3D = $Body/Head
@onready var cam : Camera3D = $Body/Head/Camera3D
@@ -26,9 +29,21 @@ const DIST_FLOOR_SNAP := Vector3.DOWN * 0.2
var floor_snap := Vector3.ZERO
func _ready():
synchroniser.set_multiplayer_authority(str(name).to_int())
cam.current = is_local_authority()
func is_local_authority():
return synchroniser.get_multiplayer_authority() == multiplayer.get_unique_id()
func _process(_delta):
if not is_local_authority():
return
label.text = "H Velocity: %3.2f" % [Vector2(velocity.x, velocity.z).length()]
label.text += "\nV Velocity: %3.2f" % [velocity.y]
label.text += "\nOn floor: %s" % is_on_floor()
func get_move_direction() -> Vector3:
var input_dir := Vector2(
Input.get_action_strength("move_backward") - Input.get_action_strength("move_forward"),
Input.get_action_strength("move_right") - Input.get_action_strength("move_left")
@@ -36,18 +51,21 @@ func get_move_direction() -> Vector3:
return input_dir.x * body.global_transform.basis.z + input_dir.y * body.global_transform.basis.x
func _input(event):
if is_local_authority():
if Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED and event is InputEventMouseMotion:
rotate_look(event.relative * 0.001 * mouse_sensitivity)
func rotate_look(amount : Vector2) -> void:
body.rotation.y -= amount.x
head.rotation.x = clamp(head.rotation.x - amount.y, -PI * 0.5, PI * 0.5)
func _physics_process(delta):
if !is_local_authority():
move_and_slide()
return
var was_on_floor : bool = is_on_floor()
var h_target_dir : Vector3 = get_move_direction()
@@ -79,7 +97,6 @@ func _physics_process(delta):
elif was_on_floor and !is_on_floor():
floor_snap = Vector3.ZERO
func accelerate(delta : float, p_target_dir : Vector3, p_target_speed : float, p_accel : float):
var current_speed : float = velocity.dot(p_target_dir)
var add_speed : float = p_target_speed - current_speed
@@ -87,7 +104,6 @@ func accelerate(delta : float, p_target_dir : Vector3, p_target_speed : float, p
var accel_speed : float = min(add_speed, p_accel * delta * p_target_speed)
velocity += p_target_dir * accel_speed
func apply_friction(delta : float):
#var vec : Vector3 = velocity

View File

@@ -1,22 +1,22 @@
extends Node3D
const CONFIG_PATH = "weapons.cfg"
signal shoot
var state = 0
@onready var player_root = get_node("../../../../PlayerQ3") as CharacterBody3D
@export var player_root : CharacterBody3D
@onready var raycast = get_node("../Camera3D/AimCast") as RayCast3D
#@onready var crosshair = $Crosshair
var ALL_WEAPONS = {}
var weapons = []
var current_weapon = 0
var current_weapon_index = 0
var current_weapon = null
var mouse_mov : Vector3
var sway_lerp = 5
func _ready():
get_tree().get_current_scene().ready.connect(init)
get_tree().get_current_scene().game_loaded.connect(init)
func init():
var config = Runtimeloader.loadConfig(CONFIG_PATH) as ConfigFile
@@ -24,7 +24,98 @@ func init():
return
Init_Config(config)
ChangeWeapon(0)
var init_config = get_tree().get_current_scene().init_config
for w_name in init_config.get_value("PLAYER", "starting_weapons"):
GiveWeapon(w_name)
func GiveWeapon(weapon_name):
if weapon_name in ALL_WEAPONS and not ALL_WEAPONS[weapon_name] in weapons:
weapons.append(ALL_WEAPONS[weapon_name])
ChangeWeapon(posmod(current_weapon_index, weapons.size()))
func TakeWeapon(weapon_name):
if weapon_name in ALL_WEAPONS and ALL_WEAPONS[weapon_name] in weapons:
weapons.erase(ALL_WEAPONS[weapon_name])
ChangeWeapon(posmod(current_weapon_index, weapons.size()))
func _input(event):
if event is InputEventMouseMotion:
mouse_mov.y = clampf(-event.relative.x, -0.1, 0.1)
mouse_mov.x = clampf(event.relative.y, -0.1, 0.1)
func _unhandled_input(_event):
if(weapons.size() == 0):
return
if weapons.size() > 1 && Input.is_action_just_released("wheel_up"):
ChangeWeapon(posmod((current_weapon_index + 1),weapons.size()))
if weapons.size() > 1 && Input.is_action_just_released("wheel_down"):
ChangeWeapon(posmod(current_weapon_index - 1,weapons.size()))
if Input.is_action_just_pressed("reload"):
current_weapon.Reload()
func ChangeWeapon(i):
if(current_weapon):
current_weapon.hide()
if(weapons.size() == 0):
current_weapon = null
return
current_weapon_index = i
current_weapon = weapons[current_weapon_index]
current_weapon.show()
current_weapon.init()
raycast.target_position.z = -current_weapon.RAY_LEN
var time = 0
func _process(delta):
if(current_weapon == null):
return
if Input.is_action_pressed("shoot") && current_weapon.Shoot():
if(current_weapon.HITSCAN):
HitScan()
else:
Projectile()
if Input.is_action_just_released("shoot"):
current_weapon.Release()
#SWAY
if mouse_mov != null && mouse_mov.length() > 0:
current_weapon.MODEL.rotation = current_weapon.MODEL.rotation.lerp(mouse_mov, sway_lerp * delta)
mouse_mov = Vector3.ZERO
else:
current_weapon.MODEL.rotation = current_weapon.MODEL.rotation.lerp(Vector3.ZERO, sway_lerp * delta)
#BOB
if player_root.velocity.length() > 0:
time += delta
var bobOscillate = sin(time * 4 * (2 * PI)) * 0.01
position.y = bobOscillate
bobOscillate = sin(time * 4 * (PI) + PI/4) * 0.015
position.x = bobOscillate
func HitScan():
if(raycast.is_colliding()):
var target = raycast.get_collider()
print_debug("Hit target ", target.name, " with ", current_weapon.name, " ",current_weapon_index, ". Dealt damage ", current_weapon.DAMAGE)
if(raycast.get_collision_mask_value(10)):
if(target is Hitable):
target.Hit(current_weapon.DAMAGE)
func Projectile():
pass
func Init_Config(config):
for weapon in config.get_sections():
@@ -71,71 +162,4 @@ func Init_Config(config):
root.RELOAD_ANGULAR_MOMENTUM = config.get_value(weapon, "RELOAD_ANGULAR_MOMENTUM")
root.RELOAD_TIME = config.get_value(weapon, "RELOAD_TIME")
weapons.append(root)
func _input(event):
if event is InputEventMouseMotion:
mouse_mov.y = clampf(-event.relative.x, -0.1, 0.1)
mouse_mov.x = clampf(event.relative.y, -0.1, 0.1)
func _unhandled_input(_event):
if weapons.size() > 1 && Input.is_action_just_released("wheel_up"):
ChangeWeapon((current_weapon + 1) % weapons.size())
if weapons.size() > 1 && Input.is_action_just_released("wheel_down"):
ChangeWeapon((current_weapon - 1) % weapons.size())
if Input.is_action_just_pressed("reload"):
weapons[current_weapon].Reload()
func ChangeWeapon(i):
weapons[current_weapon].hide()
current_weapon = i
weapons[current_weapon].show()
weapons[current_weapon].init()
raycast.target_position.z = -weapons[current_weapon].RAY_LEN
var time = 0
func _process(delta):
if Input.is_action_pressed("shoot") && weapons[current_weapon].Shoot():
shoot.emit(0.20, 0.15)
if(weapons[current_weapon].HITSCAN):
HitScan()
else:
Projectile()
if Input.is_action_just_released("shoot"):
weapons[current_weapon].Release()
#SWAY
if mouse_mov != null && mouse_mov.length() > 0:
weapons[current_weapon].MODEL.rotation = weapons[current_weapon].MODEL.rotation.lerp(mouse_mov, sway_lerp * delta)
mouse_mov = Vector3.ZERO
else:
weapons[current_weapon].MODEL.rotation = weapons[current_weapon].MODEL.rotation.lerp(Vector3.ZERO, sway_lerp * delta)
#BOB
if player_root.velocity.length() > 0:
time += delta
var bobOscillate = sin(time * 4 * (2 * PI)) * 0.01
position.y = bobOscillate
bobOscillate = sin(time * 4 * (PI) + PI/4) * 0.015
position.x = bobOscillate
func HitScan():
if(raycast.is_colliding()):
var target = raycast.get_collider()
print_debug("Hit target ", target.name, " with ", weapons[current_weapon].MODEL.name, " ",current_weapon, ". Dealt damage ", weapons[current_weapon].DAMAGE)
if(raycast.get_collision_mask_value(10)):
if(target is Hitable):
target.Hit(weapons[current_weapon].DAMAGE)
func Projectile():
pass
ALL_WEAPONS[root.name] = root

View File

@@ -7,10 +7,6 @@ func _ready():
if(not OS.has_feature("editor")):
EXTERNAL_PATH = OS.get_executable_path().get_base_dir() + "/scoom/"
func _input(event):
if event is InputEventKey and event.is_pressed():
if event.keycode == KEY_T:
load_gltf("models/runtime_loader_test.glb")
func get_all_children(in_node, arr = []):
arr.push_back(in_node)