alai/godot/characters/enemies/WalkingEnemy.gd

47 lines
1.3 KiB
GDScript3
Raw Normal View History

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