clowder/scripts/plr.gd
2024-03-06 22:05:35 +01:00

72 lines
1.8 KiB
GDScript

extends CharacterBody2D
signal interact
signal interact2
const SPEED = 300.0
@onready var gun = $gun
var has_gun = false
@export var moving = true
var bullets = 0
var can_shoot = true
# Get the gravity from the project settings to be synced with RigidBody nodes.
func handle_layer(direction):
if round(direction.y) == -1:
$character/body/tail.z_index = 1
$gun.z_index = -1
$character/body/face.visible = false
elif round(direction.y) == 1:
$character/body/tail.z_index = 0
$gun.z_index = 0
$character/body/face.visible = true
func _ready():
if not has_gun:
$gun.visible = false
func _physics_process(_delta):
if Input.is_action_just_pressed("interact"):
interact.emit()
if Input.is_action_just_pressed("interact 2"):
interact2.emit()
if not moving: return
# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gawameplay actions.
var direction = Input.get_vector("left","right","up","down")
if Input.is_action_pressed("shoot") and can_shoot and has_gun:
var bullet = preload("res://tscn/bullet.tscn").instantiate()
bullet.position = gun.global_position
bullet.z_index = gun.z_index
var dir = (get_global_mouse_position() - gun.global_position).normalized()
bullet.global_rotation = dir.angle() + PI / 2.0
bullet.direction = dir
get_tree().current_scene.add_child(bullet)
$cooldown.start()
can_shoot = false
if direction == Vector2(0,0):
$AnimationPlayer.current_animation = "idle"
else:
handle_layer(direction)
if not direction.x == 0:
$character.scale.x = round(direction.x)
$AnimationPlayer.current_animation = "walk"
else:
$AnimationPlayer.current_animation = "vmove"
velocity = direction * SPEED
move_and_slide()
func _on_cooldown_timeout():
can_shoot = true