alai/godot/characters/enemies/WalkingEnemy.gd

45 lines
1.4 KiB
GDScript3
Raw Normal View History

2022-07-21 00:05:08 -04:00
extends KinematicBody2D
var velocity = Vector2()
export var direction = -1
export var detect_edges = true
export var speed = 25
export var gravity = 9.8
func _ready() -> void:
2023-02-26 21:45:25 -03:00
if direction == 1:
$AnimatedSprite.flip_h = true
$FloorChecker.position.x = $CollisionShape2D.shape.get_extents().x * direction
$FloorChecker.enabled = detect_edges
Event.connect("level_loaded", self, "_on_level_loaded")
2022-07-21 00:05:08 -04:00
func _physics_process(_delta: float) -> void:
2023-02-26 21:45:25 -03:00
if is_on_wall() or not $FloorChecker.is_colliding() and is_on_floor() and $FloorChecker.enabled:
direction *= -1
$AnimatedSprite.flip_h = not $AnimatedSprite.flip_h
$FloorChecker.position.x = $CollisionShape2D.shape.get_extents().x * direction
2022-07-21 00:05:08 -04:00
2023-02-26 21:45:25 -03:00
velocity.y += gravity
velocity.x = speed * direction
velocity = move_and_slide(velocity, Vector2.UP)
2022-07-21 00:05:08 -04:00
2023-02-26 21:45:25 -03:00
for i in get_slide_count():
var collision = get_slide_collision(i)
if collision.collider.name == "Player":
Event.emit_signal("player_touched", 3)
2022-07-21 00:05:08 -04:00
2023-02-26 21:45:25 -03:00
Event.emit_signal("object_updated", self.get_name(), "Walking", global_position, velocity)
Event.emit_signal("report_object", self.get_name(), "Walking", global_position, velocity)
2022-09-01 11:26:34 -04:00
2022-07-21 00:05:08 -04:00
func squash() -> void:
2023-02-26 21:45:25 -03:00
Event.emit_signal("object_removed", self.get_name())
queue_free()
2022-09-01 11:26:34 -04:00
func _on_level_loaded() -> void:
2023-02-26 21:45:25 -03:00
Event.emit_signal("object_created", self.get_name(), "Walking", global_position, Vector2(0, 0))