-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBaseMob.gd
74 lines (59 loc) · 1.77 KB
/
BaseMob.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
class_name BaseMob extends Enemy
@onready var animation := $AnimatedSprite2D as AnimatedSprite2D
@onready var navigation_agent = $NavigationAgent2D as NavigationAgent2D
var speed = 40
var health = 100
var is_following = false
var target = null
var is_dead = false
var knock_back_time = 0
var pushback_force = Vector2(0,0)
func make_path():
navigation_agent.set_target_position(target.global_position)
func rotate_sprite():
var dir_to_target = position.direction_to(target.global_position)
var hitbox = get_node_or_null("HitBox")
if dir_to_target.x < 0:
if hitbox:
hitbox.scale.x = -1
animation.scale.x = -1
elif dir_to_target.x > 0:
if hitbox:
hitbox.scale.x = 1
animation.scale.x = 1
func take_damage(damage):
var label = preload("res://DamageLabel.tscn").instantiate()
label.global_position = global_position
label.set_damage(damage)
add_child(label)
health -= damage
%ProgressBar.value = health
if health <= 0 && !is_dead:
death()
func _on_detect_area_body_entered(body):
if body is Player:
target = body
on_body_entered(body)
generate_raycasts()
func on_body_entered(_body):
pass
func _on_detect_area_body_exited(body):
if body is Player:
on_body_exited(body)
func on_body_exited(_body):
pass
func _on_navigation_agent_2d_velocity_computed(safe_velocity):
velocity = safe_velocity
func death():
is_dead = true
var coin = preload("res://Coin.tscn").instantiate()
coin.global_position = global_position
await get_parent().call_deferred("add_child",coin)
Events.emit_signal("on_mob_killed")
queue_free()
func handle_knockback(delta):
if knock_back_time > 0:
knock_back_time -= delta*10
var new_pushback_force = lerp(pushback_force, Vector2.ZERO, delta * 10)
pushback_force = new_pushback_force
velocity = pushback_force * delta