-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathshrimp.gd
More file actions
110 lines (95 loc) · 3.21 KB
/
shrimp.gd
File metadata and controls
110 lines (95 loc) · 3.21 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
101
102
103
104
105
106
107
108
109
110
extends RigidBody2D
class_name Shrimp, "res://shrimpclass_icon.png"
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 infoholder = null
var healthbar = null
var namelabel = null
puppet var puppet_pos = Vector2()
puppet var puppet_motion = Vector2()
puppet var puppet_rotation = 0
# 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():
infoholder = get_parent().get_node("bar")
healthbar = infoholder.get_node("health")
healthbar.modulate = Color(0,1,0,1)
namelabel = infoholder.get_node("name")
func _physics_process(delta):
if is_network_master():
attack_timer -= 1
var direction_to = destination.global_position - global_position
if destination.visible != false:
rotation = direction_to.angle()
##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
set_target_position(null)
else:
linear_velocity = direction_to.clamped(speed) * speed * delta
infoholder.position = position
set_puppet_vars()
else:
position = puppet_pos
infoholder.position = position
linear_velocity = puppet_motion
rotation = puppet_rotation
if abs(rad2deg(rotation)) > 90:
$Sprite.flip_v = true
else:
$Sprite.flip_v = false
# master makes sure no idiot calls this on the wrong shrimp.
master func set_puppet_vars():
rset("puppet_motion", linear_velocity)
rset("puppet_pos", position)
rset("puppet_rotation", rotation)
func _process(_delta):
if!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 is_network_master():
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
rpc("display_attacksprite", 0.5)
var attacked = attackarea.get_overlapping_bodies()
for shramp in attacked:
if shramp == attackarea.get_parent():
continue
# Can't reference your own class. At least this won't crash
if shramp.has_method("adjust_health"):
shramp.rpc("adjust_health",-attack_damage)
remotesync func adjust_health(amount):
health += min(amount, max_health)
var percentage = float(health)/float(max_health)
healthbar.modulate = Color(1 - percentage,percentage,0,1)
if health <= 0:
get_parent().on_shrimp_death()
remotesync func display_attacksprite(time):
attacksprite.visible = true
yield(get_tree().create_timer(time), "timeout")
attacksprite.visible = false