-
Notifications
You must be signed in to change notification settings - Fork 3
/
EnemyMovement.gd
50 lines (44 loc) · 1.17 KB
/
EnemyMovement.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
50
extends KinematicBody2D
#variables
var speedY = 0
var velocity = Vector2(0,0)
var movementDirection = -1
var deltaTime = 0
#Constants
const SPEEDX = 150
const GRAVITY = 800
func _animate():
if(get_node("Sprite").get_frame()>=3):
get_node("Sprite").set_frame(0)
if(get_node("Sprite").get_frame()<3):
get_node("Sprite").set_frame(get_node("Sprite").get_frame()+1)
deltaTime = 0
pass
func _ready():
set_process(true)
pass
func _process(delta):
if(get_node("CollisionShape2D").is_trigger()):
get_node("Sprite").set_frame(4)
deltaTime = deltaTime + delta
if(deltaTime > 0.1):
_animate()
speedY += GRAVITY * delta
velocity.x = SPEEDX * delta * movementDirection
velocity.y = speedY * delta
var movementRemainder = move(velocity)
if is_colliding():
var normal = get_collision_normal()
var object = get_collider()
var objectParent = object.get_parent()
if(normal == Vector2(0,-1)):
speedY = 0
if(normal == Vector2(1,0)):
movementDirection = 1
get_node("Sprite").set_flip_h(true)
elif( normal == Vector2(-1,0)):
movementDirection = -1
get_node("Sprite").set_flip_h(false)
var finalMovement = normal.slide(movementRemainder)
move(finalMovement)
pass