-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasic_enemy.gd
49 lines (38 loc) · 1.11 KB
/
basic_enemy.gd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
extends CharacterBody2D
@export var SPEED = 300.0
@export var JUMP_SPEED = -600.0 # Negative is up
var direction = 1
var hasJumped = true
@export var canJump = false
func _ready():
# Animation
$AnimatedSprite2D.play("moving")
func _physics_process(delta: float) -> void:
# Add the gravity.
if not is_on_floor():
if randi() % 20 == 1 && !hasJumped && canJump:
jump() # chance of jumping in air once
hasJumped = true
else:
velocity += get_gravity() * delta # bandage fix for slopes
else:
hasJumped = false
if (!$Hitbox/DownRay.is_colliding() or $Hitbox/SideRay.is_colliding()) and is_on_floor():
if $Hitbox/SideRay.is_colliding() && (randi() % 2 == 0) && canJump:
jump()
else:
if randi() % 5 != 0 || !canJump:
flip()
else:
jump()
velocity.x = direction * SPEED
move_and_slide()
func jump():
velocity.y = JUMP_SPEED
func flip():
direction = -direction
scale.x *= -1
func _on_killbox_body_shape_entered(body_rid: RID, body: Node2D, body_shape_index: int, local_shape_index: int) -> void:
if body.is_in_group("player") && body.velocity.y > 0:
body.jump()
queue_free()