forked from tralezab/probable-robot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshrimp.gd
More file actions
100 lines (87 loc) · 2.89 KB
/
shrimp.gd
File metadata and controls
100 lines (87 loc) · 2.89 KB
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
extends RigidBody2D
class_name Shrimp, "res://shrimpclass_icon.png"
export var current = false
var speed = 150
var max_health = 100
var health = 100
var attack_cooldown = 50
var attack_timer = 0
var attack_damage = 25
onready var attacksprite = $Attack
onready var attackarea = $Attack_Area
onready var destination = get_parent().get_node("Destination")
var healthbox = null
var healthbar = null
var namepanel = null
var namelabel = null
# Called when the node enters the scene tree for the first time.
func _ready():
rotation = deg2rad(rand_range(-180,180))
mode = RigidBody2D.MODE_STATIC
func setup_vars():
healthbox = get_parent().get_node("bar")
healthbar = healthbox.get_node("health")
healthbox.global_position = global_position - Vector2(0, -40)
healthbar.modulate = Color(0,1,0,1)
namepanel = healthbox.get_node("Panel")
namelabel = namepanel.get_node("Label")
namelabel.set_text(get_parent().player_name)
if current:
$Camera2D._set_current(true)
func _physics_process(delta):
attack_timer -= 1
if !current:
return
var direction_to = destination.global_position - global_position
if destination.visible != false:
rotation = direction_to.angle()
if abs(rad2deg(rotation)) > 90:
$Sprite.flip_v = true
else:
$Sprite.flip_v = false
##REMINDER: SET THE SHRIMP TO SLOWLY LOSE MOMENTUM FOR COLLISIONS
mode = RigidBody2D.MODE_RIGID
else:
mode = RigidBody2D.MODE_STATIC
if direction_to.length() < 10:
linear_velocity = Vector2.ZERO
self.rpc("set_target_position", null)
pass
else:
linear_velocity = direction_to.clamped(speed) * speed * delta
healthbox.global_position = global_position - Vector2(0, -40)
func _process(_delta):
if !current || !is_network_master():
return
if Input.is_action_pressed("rightclick"):
self.rpc("set_target_position", get_global_mouse_position())
if Input.is_action_just_pressed("leftclick") and attack_timer <= 0:
attack_move()
remotesync func set_target_position(set_position):
if set_position != null:
# No out of bounds action
var mouse_pos = get_global_mouse_position()
var world_size = get_viewport().size
destination.global_position = Vector2(clamp(mouse_pos[0],0,world_size[0]),clamp(mouse_pos[1],0,world_size[1]))
destination.visible = true
return
destination.global_position = global_position
destination.visible = false
func attack_move():
attack_timer = attack_cooldown
attacksprite.visible = true
var attacked = attackarea.get_overlapping_bodies()
for shramp in attacked:
if shramp == attackarea.get_parent():
continue
if shramp is RigidBody2D:
shramp.health -= attack_damage
var percentage = float(shramp.health)/float(shramp.MAX_HEALTH)
shramp.healthbar.modulate = Color(1 - percentage,percentage,0,1)
if shramp.health <= 0:
shramp.get_parent().spawn_shrimp()
shramp.queue_free()
yield(get_tree().create_timer(0.5), "timeout")
attacksprite.visible = false
func set_current(boo):
current = boo