-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenemy1.gd
103 lines (85 loc) · 2.78 KB
/
enemy1.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
extends KinematicBody2D
# Declare member variables here. Examples:
# var a = 2
# var b = "text"
var state = 'walking'
var direction = 'left'
var velocity = Vector2()
var bouncingBack = false
const GRAVITY = 5
const SPEED = 40
const JUMP_POWER = -150
# Called when the node enters the scene tree for the first time.
func _ready():
$AnimatedSprite.play("walk")
# Called every frame. 'delta' is the elapsed time since the previous frame.
#func _process(delta):
# pass
func _physics_process(delta):
if direction == 'left' and $RayCast2D.is_colliding() and !bouncingBack:
$AnimatedSprite.play("walk")
$AnimatedSprite.flip_h = true
velocity.x = -SPEED
if $wallCheckLeft.is_colliding():
$jumpAudio.play()
velocity.y = JUMP_POWER
elif direction == 'right' and $RayCast2D.is_colliding() and !bouncingBack:
$AnimatedSprite.play("walk")
$AnimatedSprite.flip_h = false
velocity.x = SPEED
if $wallCheckRight.is_colliding():
$jumpAudio.play()
velocity.y = JUMP_POWER
if !$RayCast2D.is_colliding():
$AnimatedSprite.play("surprise")
velocity.y += GRAVITY
move_and_slide(velocity)
func connectEnteredSignalFromPlayer(signalToConnect, targetArea, function, target):
targetArea.connect(signalToConnect, self, function, target)
func connectSignalFromPlayer(signalToConnect, target, function):
target.connect(signalToConnect, self, function)
func bounceBack(body, torch):
if body != self or !torch.get_node("Light2D").enabled:
return
bouncingBack = true
$jumpAudio.play()
$bounceBackTimer.start()
if torch.global_position.x > global_position.x:
velocity.x = -SPEED
else:
velocity.x = SPEED
velocity.y = JUMP_POWER/2
if abs(torch.global_position.x - global_position.x) < 20:
velocity.x *= 3
velocity.y *= 3
func getDirection(player_position, plants, torches, _litTorches):
if plants.size() > 0:
var closestPlant = plants[0]
if !is_instance_valid(closestPlant):
getDirectionNoPlants(player_position)
return
var distanceToClosest = abs(closestPlant.global_position.x - global_position.x)
for plant in plants:
var distance = abs(plant.global_position.x - global_position.x)
if distance < distanceToClosest:
distanceToClosest = distance
closestPlant = plant
if distanceToClosest < abs(global_position.x - player_position.x):
if closestPlant.global_position.x > global_position.x:
direction = 'right'
else:
direction = 'left'
else:
getDirectionNoPlants(player_position)
else:
getDirectionNoPlants(player_position)
for area in get_node("Area2D").get_overlapping_areas():
if 'enemyRange' in area.name:
bounceBack(self, area.get_parent())
func getDirectionNoPlants(player_position):
if player_position.x > global_position.x:
direction = 'right'
else:
direction = 'left'
func _on_bounceBackTimer_timeout():
bouncingBack = false