2024-07-23 18:22:05 +02:00
|
|
|
extends CharacterBody2D
|
|
|
|
|
|
|
|
|
|
@export var speed: float = 5
|
2024-07-23 20:07:13 +02:00
|
|
|
@export var strength: int = 1
|
|
|
|
|
|
2024-07-23 20:53:55 +02:00
|
|
|
const _speed_mult: float = 5000
|
2024-07-23 20:07:13 +02:00
|
|
|
|
2024-07-23 20:53:55 +02:00
|
|
|
var _direction: Vector2
|
|
|
|
|
var _velocity: Vector2
|
2024-07-23 20:07:13 +02:00
|
|
|
var deg: float
|
|
|
|
|
|
|
|
|
|
func _ready():
|
2024-07-23 21:22:05 +02:00
|
|
|
var randvalue = _get_rand_direction()
|
|
|
|
|
_direction = Vector2.UP.rotated(deg_to_rad(randvalue))
|
|
|
|
|
|
|
|
|
|
func _get_rand_direction() -> float:
|
|
|
|
|
var value = randf_range(0.0, 360.0)
|
|
|
|
|
if value < 10 \
|
|
|
|
|
or (value >= 80 and value < 100) \
|
|
|
|
|
or (value >= 170 and value < 190) \
|
|
|
|
|
or (value >= 260 and value < 280) \
|
|
|
|
|
or value >= 350:
|
|
|
|
|
return _get_rand_direction()
|
|
|
|
|
return value
|
2024-07-23 18:22:05 +02:00
|
|
|
|
|
|
|
|
func _physics_process(delta):
|
2024-07-23 20:53:55 +02:00
|
|
|
_velocity.x = speed * _speed_mult * _direction.x * delta
|
|
|
|
|
_velocity.y = speed * _speed_mult * _direction.y * delta
|
2024-07-23 18:22:05 +02:00
|
|
|
|
2024-07-23 20:07:13 +02:00
|
|
|
var collide = move_and_collide(_velocity * delta)
|
|
|
|
|
if collide:
|
|
|
|
|
deg = rad_to_deg(collide.get_angle())
|
|
|
|
|
if (deg >= 45 and deg < 135) or (deg >= 225 and deg < 315):
|
|
|
|
|
_invert_x()
|
|
|
|
|
if (deg >= 135 and deg < 225) or deg < 45 or deg >= 315:
|
|
|
|
|
_invert_y()
|
2024-07-23 20:33:27 +02:00
|
|
|
if collide.get_collider().get_parent() is Block:
|
|
|
|
|
collide.get_collider().get_parent().take_damage(strength)
|
2024-07-23 18:22:05 +02:00
|
|
|
|
2024-07-23 20:07:13 +02:00
|
|
|
func _invert_x():
|
2024-07-23 20:53:55 +02:00
|
|
|
_direction.x *= -1
|
2024-07-23 18:22:05 +02:00
|
|
|
|
2024-07-23 20:07:13 +02:00
|
|
|
func _invert_y():
|
2024-07-23 20:53:55 +02:00
|
|
|
_direction.y *= -1
|