69 lines
1.9 KiB
GDScript
69 lines
1.9 KiB
GDScript
extends CharacterBody2D
|
|
|
|
@export var attack_duration = 0.12 # TODO: finetune
|
|
@export var attack_cooldown_duration = 0.4
|
|
|
|
@onready var attack_area: Area2D = $AttackArea
|
|
@onready var attack_timer: Timer = $AttackTimer
|
|
@onready var attack_cooldown_timer: Timer = $AttackCooldownTimer
|
|
@onready var sprite: AnimatedSprite2D = $AnimatedSprite2D
|
|
|
|
@export var speed = 200
|
|
var damage = 10
|
|
var can_attack = true
|
|
|
|
func _ready() -> void:
|
|
var screen_size = get_viewport_rect().size
|
|
GameManager.init_screen_size(screen_size.x, screen_size.y)
|
|
|
|
attack_area.monitoring = false # no collision until attacking
|
|
attack_timer.wait_time = attack_duration
|
|
attack_cooldown_timer.wait_time = attack_cooldown_duration
|
|
|
|
func _process(delta):
|
|
velocity = Vector2.ZERO
|
|
if Input.is_action_pressed("move_right"):
|
|
velocity.x += 1
|
|
if Input.is_action_pressed("move_left"):
|
|
velocity.x -= 1
|
|
if Input.is_action_pressed("move_down"):
|
|
velocity.y += 1
|
|
if Input.is_action_pressed("move_up"):
|
|
velocity.y -= 1
|
|
if Input.is_action_pressed("try_attack"):
|
|
try_attack()
|
|
|
|
if not velocity.is_zero_approx():
|
|
look_at(velocity * 1000000) # FIXME: ideally we look_at a point at infinity
|
|
# or rotate the player some other way
|
|
move_and_collide(speed * velocity * delta)
|
|
#position += speed * velocity * delta
|
|
position = GameManager.get_boundaried_position(position)
|
|
|
|
func try_attack() -> void:
|
|
if not can_attack:
|
|
return
|
|
attack()
|
|
|
|
func attack() -> void:
|
|
can_attack = false
|
|
attack_area.monitoring = true
|
|
attack_timer.start()
|
|
sprite.play("attacking")
|
|
|
|
func _on_attack_timeout() -> void:
|
|
attack_area.monitoring = false
|
|
attack_cooldown_timer.start()
|
|
|
|
func _on_cooldown_timeout() -> void:
|
|
can_attack = true
|
|
sprite.play("default")
|
|
|
|
func _on_attack_hit(body: Node2D) -> void:
|
|
if body.is_in_group("prey") or body.is_in_group("predators"):
|
|
if body.has_method("handle_damage"):
|
|
body.handle_damage(damage)
|
|
elif body.is_in_group("resources"):
|
|
pass
|
|
# TODO: resource handling logic
|