-
Notifications
You must be signed in to change notification settings - Fork 3
/
rat.gd
127 lines (103 loc) · 3.05 KB
/
rat.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
extends RigidBody2D
onready var sprite = get_node("AnimatedSprite")
onready var healthbar = get_node("TextureProgress")
onready var blood = get_node("BloodPart")
onready var retaliate_timer = get_node("RetaliateTimer")
onready var atk_timer = get_node("AttackTimer")
onready var cd_timer = get_node("CdTimer")
onready var hurtarea = get_node("HurtArea")
onready var lock = get_node("Lock")
onready var player = get_parent().get_tree().get_nodes_in_group("player")[0]
const DAMAGE = 50
var health = 100
var mouse_inside = false
var initial_pos
func get_hit(location):
apply_impulse(Vector2(0, 0), Vector2(4, 4) * (get_global_pos() - location))
var angle = Vector2(4, 4) * (get_global_pos() - location)
var rad = atan2(angle.y, angle.x)
var deg = rad * 180 / 3.14159 + 90
health -= DAMAGE;
if sprite.get_animation() != "atk":
sprite.play("grunt")
blood.set_param(blood.PARAM_DIRECTION, deg)
blood.set_emitting(true)
if retaliate_timer.get_time_left() == 0:
retaliate_timer.start()
pass
func _input(event):
if mouse_inside and event.is_action_pressed("lock"):
if lock.is_visible():
lock.hide()
player.lock_tracking = Vector2(-1, -1)
return
# hide the all of them
for e in get_parent().get_tree().get_nodes_in_group("enemy"):
e.lock.hide()
player.lock_tracking = Vector2(-1, -1)
if !lock.is_visible():
lock.show()
func _ready():
set_process(true)
set_gravity_scale(0)
set_process_input(true)
sprite.play("idle")
initial_pos = get_global_pos()
func move_towards(location):
if health > 0:
if sprite.get_animation() != "atk" and sprite.get_animation() != "grunt":
sprite.play("walk")
apply_impulse(Vector2(0, 0), Vector2(0.1, 0.1) * (location - get_global_pos()))
if (location - get_global_pos()).x > 0:
sprite.set_flip_h(true)
hurtarea.get_node("CollisionPolygon2D").set_pos(Vector2(20, 0))
else:
sprite.set_flip_h(false)
hurtarea.get_node("CollisionPolygon2D").set_pos(Vector2(-20, 0))
func attack():
if health > 0:
if cd_timer.get_time_left() == 0:
sprite.stop()
sprite.play("atk")
atk_timer.start()
cd_timer.start()
func _process(delta):
if health == 100:
healthbar.hide()
else:
healthbar.show()
if lock.is_visible():
player.lock_tracking = get_global_pos()
#die
healthbar.set_value(health)
if health <= 0:
if atk_timer.get_time_left() > 0:
atk_timer.stop()
set_layer_mask(2)
set_collision_mask(2) # not interactable
sprite.play("die")
set_process(false)
healthbar.hide()
lock.hide()
player.lock_tracking = Vector2(-1, -1)
func _on_AnimatedSprite_finished():
if sprite.get_animation() == "grunt":
sprite.stop()
sprite.play("idle")
if sprite.get_animation() == "atk":
sprite.stop()
sprite.play("idle")
func _on_RetaliateTimer_timeout():
pass
func _on_AttackTimer_timeout():
var bodies = hurtarea.get_overlapping_bodies()
for b in bodies:
if b.is_in_group("player"):
print("player")
b.get_hit(get_global_pos(), 8)
else:
print(typeof(b))
func _on_ClickDetect_mouse_enter():
mouse_inside = true
func _on_ClickDetect_mouse_exit():
mouse_inside = false