67 lines
1.8 KiB
GDScript
67 lines
1.8 KiB
GDScript
extends CharacterBody2D
|
|
|
|
@export var move_speed: float
|
|
@export var max_jumps: int = 2
|
|
@export var bullet_velocity: float
|
|
|
|
var bullet_scene = preload('res://bullet.tscn')
|
|
|
|
var jumps_left = 0
|
|
var tgt_position
|
|
|
|
func _ready() -> void:
|
|
Input.mouse_mode = Input.MOUSE_MODE_CONFINED
|
|
|
|
func _input(event: InputEvent) -> void:
|
|
if event is InputEventKey:
|
|
if event.keycode == KEY_ALT:
|
|
if event.pressed:
|
|
Input.mouse_mode = Input.MOUSE_MODE_VISIBLE
|
|
else:
|
|
Input.mouse_mode = Input.MOUSE_MODE_CONFINED
|
|
|
|
func _physics_process(delta: float) -> void:
|
|
if not is_multiplayer_authority():
|
|
if tgt_position:
|
|
position = lerp(position, tgt_position, delta * 20)
|
|
return
|
|
|
|
velocity += Vector2.DOWN * ProjectSettings['physics/2d/default_gravity'] * delta
|
|
|
|
var movement = Vector2.ZERO
|
|
var x_move = Input.get_action_strength('right') - Input.get_action_strength('left')
|
|
movement.x = x_move
|
|
velocity += movement * delta * move_speed
|
|
|
|
if is_on_floor():
|
|
jumps_left = max_jumps
|
|
|
|
if (abs(x_move) < 0.01 and is_on_floor()) or sign(x_move) != sign(velocity.x):
|
|
velocity -= Vector2.RIGHT * velocity * delta * 5.0
|
|
|
|
if abs(velocity.x) < 0.01:
|
|
velocity.x = 0
|
|
|
|
if Input.is_action_just_pressed('jump') and jumps_left > 0:
|
|
velocity.y = -500
|
|
jumps_left -= 1
|
|
|
|
move_and_slide()
|
|
rpc("update_loc", position)
|
|
|
|
if Input.is_action_just_pressed("fire"):
|
|
var pos = get_viewport().get_camera_2d().get_global_mouse_position()
|
|
rpc("fire", pos)
|
|
|
|
@rpc("call_remote")
|
|
func update_loc(pos: Vector2):
|
|
tgt_position = pos
|
|
|
|
@rpc("call_local")
|
|
func fire(pos: Vector2):
|
|
var bullet: RigidBody2D = bullet_scene.instantiate()
|
|
get_parent().add_child(bullet)
|
|
bullet.position = position
|
|
bullet.look_at(pos)
|
|
bullet.apply_central_impulse(bullet.transform.basis_xform(Vector2.RIGHT) * bullet_velocity)
|