61 lines
1.6 KiB
GDScript
61 lines
1.6 KiB
GDScript
extends CharacterBody2D
|
|
|
|
|
|
const ROTATION = 0.05
|
|
|
|
# Get the gravity from the project settings to be synced with RigidBody nodes.
|
|
var Bullet = preload("res://World/bullet.tscn")
|
|
|
|
var thrust = 6.0
|
|
var gravity := Vector2.ZERO
|
|
var last_shot_utime := 0
|
|
|
|
# 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())
|
|
|
|
gravity = 1000*MapConfig.gravity * Vector2.LEFT.rotated(deg_to_rad(MapConfig.gravityangle))
|
|
|
|
|
|
func shoot():
|
|
if Time.get_ticks_usec() < last_shot_utime + 1000000 * MapConfig.firerepeatrate / MapConfig.framespersecond:
|
|
return
|
|
|
|
# "Muzzle" is a Marker2D placed at the barrel of the gun.
|
|
var b = Bullet.instantiate()
|
|
b.start($"Muzzle main".global_position, velocity, rotation)
|
|
get_tree().root.add_child(b)
|
|
last_shot_utime = Time.get_ticks_usec()
|
|
|
|
|
|
func _physics_process(delta):
|
|
rotation += input.rotate
|
|
|
|
# Add the gravity.
|
|
velocity += gravity * delta
|
|
|
|
if input.thrust:
|
|
velocity += Vector2(thrust, 0).rotated(rotation)
|
|
if input.shoot:
|
|
shoot()
|
|
|
|
var collision = move_and_collide(velocity * delta)
|
|
if collision:
|
|
velocity = (1 - MapConfig.playerwallbouncebrakefactor) * velocity.bounce(collision.get_normal())
|
|
|