start Multiplayer
This commit is contained in:
@@ -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 Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED and event is InputEventMouseMotion:
|
||||
rotate_look(event.relative * 0.001 * mouse_sensitivity)
|
||||
|
||||
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
|
||||
|
||||
@@ -47,7 +47,7 @@ func init():
|
||||
can_shoot = true
|
||||
InitialPos(DRAW_TIME, DRAW_TIME)
|
||||
|
||||
|
||||
|
||||
|
||||
func InitialPos(draw_time, ready_time):
|
||||
tween = create_tween()
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user