2024-03-03 20:22:50 +01:00
|
|
|
extends CharacterBody2D
|
|
|
|
|
|
|
|
signal interact
|
|
|
|
signal interact2
|
|
|
|
const SPEED = 300.0
|
2024-03-06 22:05:35 +01:00
|
|
|
@onready var gun = $gun
|
|
|
|
var has_gun = false
|
2024-03-03 20:22:50 +01:00
|
|
|
@export var moving = true
|
2024-03-06 22:05:35 +01:00
|
|
|
var bullets = 0
|
|
|
|
var can_shoot = true
|
2024-03-03 20:22:50 +01:00
|
|
|
# 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
|
2024-03-06 22:05:35 +01:00
|
|
|
$gun.z_index = -1
|
2024-03-03 20:22:50 +01:00
|
|
|
$character/body/face.visible = false
|
|
|
|
elif round(direction.y) == 1:
|
|
|
|
$character/body/tail.z_index = 0
|
2024-03-06 22:05:35 +01:00
|
|
|
$gun.z_index = 0
|
2024-03-03 20:22:50 +01:00
|
|
|
$character/body/face.visible = true
|
|
|
|
|
2024-03-06 22:05:35 +01:00
|
|
|
func _ready():
|
|
|
|
if not has_gun:
|
|
|
|
$gun.visible = false
|
2024-03-03 20:22:50 +01:00
|
|
|
|
|
|
|
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")
|
2024-03-06 22:05:35 +01:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2024-03-03 20:22:50 +01:00
|
|
|
if direction == Vector2(0,0):
|
|
|
|
$AnimationPlayer.current_animation = "idle"
|
|
|
|
else:
|
|
|
|
handle_layer(direction)
|
2024-03-06 22:05:35 +01:00
|
|
|
|
|
|
|
|
2024-03-03 20:22:50 +01:00
|
|
|
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()
|
2024-03-06 22:05:35 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func _on_cooldown_timeout():
|
|
|
|
can_shoot = true
|