Files
godot-demo-projects/2d/platformer/enemy/enemy.gd
2025-10-02 16:51:20 -07:00

51 lines
1.1 KiB
GDScript

extends KinematicBody2D
class_name Enemy
const GRAVITY_VEC = Vector2(0, 900)
const FLOOR_NORMAL = Vector2(0, -1)
const STATE_WALKING = 0
const STATE_KILLED = 1
const WALK_SPEED = 70
var linear_velocity = Vector2()
var direction = -1
var anim = ""
# state machine
var state = STATE_WALKING
onready var DetectFloorLeft = $DetectFloorLeft
onready var DetectWallLeft = $DetectWallLeft
onready var DetectFloorRight = $DetectFloorRight
onready var DetectWallRight = $DetectWallRight
onready var sprite = $Sprite
func _physics_process(delta):
var new_anim = "idle"
if state == STATE_WALKING:
linear_velocity += GRAVITY_VEC * delta
linear_velocity.x = direction * WALK_SPEED
linear_velocity = move_and_slide(linear_velocity, FLOOR_NORMAL)
if not DetectFloorLeft.is_colliding() or DetectWallLeft.is_colliding():
direction = 1.0
if not DetectFloorRight.is_colliding() or DetectWallRight.is_colliding():
direction = -1.0
sprite.scale = Vector2(direction, 1.0)
new_anim = "walk"
else:
new_anim = "explode"
if anim != new_anim:
anim = new_anim
($Anim as AnimationPlayer).play(anim)
func hit_by_bullet():
state = STATE_KILLED