2021-12-17 17:10:56 -06:00
|
|
|
extends KinematicBody2D
|
|
|
|
|
2021-12-21 16:40:58 -06:00
|
|
|
class_name Slime
|
|
|
|
|
|
|
|
export var weight := 8
|
|
|
|
export var max_speed = 48 * 5 # ~3 tiles/sec at top speed
|
|
|
|
export var friction = 2000
|
|
|
|
export var speed = 80
|
2021-12-17 17:10:56 -06:00
|
|
|
|
|
|
|
var target_player = null
|
2021-12-21 16:40:58 -06:00
|
|
|
var navigation: Navigation2D = null
|
|
|
|
var nav_path: PoolVector2Array = []
|
|
|
|
var target_position = null
|
2021-12-17 17:10:56 -06:00
|
|
|
remotesync var velocity = Vector2.ZERO
|
|
|
|
remotesync var acceleration = Vector2.ZERO
|
|
|
|
|
|
|
|
enum {
|
|
|
|
IDLE,
|
|
|
|
WANDER,
|
|
|
|
CHASE,
|
|
|
|
ATTACK
|
|
|
|
}
|
|
|
|
|
|
|
|
var state = IDLE
|
2021-12-21 16:40:58 -06:00
|
|
|
onready var nav_line = $nav_line
|
2021-12-17 17:10:56 -06:00
|
|
|
|
|
|
|
func _ready():
|
|
|
|
rset_config("position", MultiplayerAPI.RPC_MODE_SLAVE)
|
2021-12-21 16:40:58 -06:00
|
|
|
|
|
|
|
func _draw():
|
|
|
|
pass
|
|
|
|
|
2021-12-17 17:10:56 -06:00
|
|
|
func _physics_process(delta):
|
2022-05-25 10:00:06 -05:00
|
|
|
print("Slime nav %s %s %s" % [navigation, position, target_player.position])
|
2021-12-21 16:40:58 -06:00
|
|
|
nav_path = navigation.get_simple_path(position, target_player.position)
|
|
|
|
if not nav_path.empty() and (position - target_player.position).length() > (128 + 64):
|
|
|
|
if (nav_path[0] - position).length() < 5: target_position = nav_path[1]
|
|
|
|
else: target_position = nav_path[0]
|
|
|
|
else: target_position = null
|
|
|
|
nav_line.points = []
|
|
|
|
for p in nav_path:
|
|
|
|
nav_line.points.append(p - position)
|
|
|
|
# print(position, path, target_position, target_player)
|
2021-12-17 17:10:56 -06:00
|
|
|
match state:
|
|
|
|
IDLE:
|
2021-12-21 16:40:58 -06:00
|
|
|
if target_position != null:
|
2021-12-17 17:10:56 -06:00
|
|
|
state = CHASE
|
|
|
|
else:
|
|
|
|
rset_unreliable("acceleration", Vector2.ZERO)
|
|
|
|
WANDER:
|
|
|
|
pass
|
|
|
|
CHASE:
|
2021-12-21 16:40:58 -06:00
|
|
|
if target_position == null:
|
2021-12-17 17:10:56 -06:00
|
|
|
state = IDLE
|
|
|
|
else:
|
2021-12-21 16:40:58 -06:00
|
|
|
rset_unreliable("acceleration", (target_position - position).normalized() * speed)
|
2021-12-17 17:10:56 -06:00
|
|
|
ATTACK:
|
|
|
|
pass
|
|
|
|
rset("velocity", move_and_slide((velocity + acceleration).clamped(max_speed), Vector2.ZERO, false, 4, 0.785398, false).move_toward(Vector2.ZERO, friction * delta))
|
|
|
|
if velocity.x < 1:
|
|
|
|
$sprite.flip_h = true
|
|
|
|
elif velocity.x > 1:
|
|
|
|
$sprite.flip_h = false
|
|
|
|
|
|
|
|
if is_network_master():
|
|
|
|
rset_unreliable("position", position)
|
|
|
|
|
|
|
|
func seek_player():
|
|
|
|
pass
|
|
|
|
|
|
|
|
func _on_hurtbox_die(_a, _b):
|
|
|
|
print("slime die")
|
|
|
|
rpc("die")
|
|
|
|
|
|
|
|
remotesync func die():
|
|
|
|
queue_free()
|
|
|
|
|
|
|
|
func _on_hurtbox_hit():
|
|
|
|
print("slime hit")
|