52 lines
1.4 KiB
GDScript
52 lines
1.4 KiB
GDScript
extends CharacterBody2D
|
|
|
|
|
|
const ROTATION = 0.05
|
|
|
|
# Get the gravity from the project settings to be synced with RigidBody nodes.
|
|
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
|
|
var Bullet = preload("res://World/bullet.tscn")
|
|
|
|
var thrust = 6.0
|
|
var collision_damping = 0.1
|
|
|
|
# Set by the authority, synchronized on spawn.
|
|
@export var player := 1 :
|
|
set(id):
|
|
player = id
|
|
# Give authority over the player input to the appropriate peer.
|
|
$PlayerInput.set_multiplayer_authority(id)
|
|
|
|
# Player synchronized input.
|
|
@onready var input = $PlayerInput
|
|
|
|
|
|
func _ready():
|
|
# Set the camera as current if we are this player.
|
|
if player == multiplayer.get_unique_id():
|
|
$Camera2D.make_current()
|
|
# Only process on server.
|
|
# EDIT: Left the client simulate player movement too to compesate network latency.
|
|
# set_physics_process(multiplayer.is_server())
|
|
pass
|
|
|
|
func shoot():
|
|
# "Muzzle" is a Marker2D placed at the barrel of the gun.
|
|
var b = Bullet.instantiate()
|
|
b.start($"Muzzle main".global_position, rotation)
|
|
get_tree().root.add_child(b)
|
|
|
|
func _physics_process(delta):
|
|
rotation += input.rotate
|
|
|
|
# Add the gravity.
|
|
velocity.y += gravity * delta
|
|
|
|
if input.thrust:
|
|
velocity += Vector2(thrust, 0).rotated(rotation)
|
|
|
|
var collision = move_and_collide(velocity * delta)
|
|
if collision:
|
|
velocity = (1 - collision_damping) * velocity.bounce(collision.get_normal())
|
|
|