godot-webrtc-mplayer-testing/scripts/objects/slime.gd
2022-05-25 10:00:06 -05:00

80 lines
1.9 KiB
GDScript

extends KinematicBody2D
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
var target_player = null
var navigation: Navigation2D = null
var nav_path: PoolVector2Array = []
var target_position = null
remotesync var velocity = Vector2.ZERO
remotesync var acceleration = Vector2.ZERO
enum {
IDLE,
WANDER,
CHASE,
ATTACK
}
var state = IDLE
onready var nav_line = $nav_line
func _ready():
rset_config("position", MultiplayerAPI.RPC_MODE_SLAVE)
func _draw():
pass
func _physics_process(delta):
print("Slime nav %s %s %s" % [navigation, position, target_player.position])
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)
match state:
IDLE:
if target_position != null:
state = CHASE
else:
rset_unreliable("acceleration", Vector2.ZERO)
WANDER:
pass
CHASE:
if target_position == null:
state = IDLE
else:
rset_unreliable("acceleration", (target_position - position).normalized() * speed)
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")