SlimeoosOdyssey/godot/Villager.gd

64 lines
1.3 KiB
GDScript

extends SharedSlime
class_name Villager
var color_changes = true
var hp = 100
var aggressor
func _process(delta):
super._process(delta)
if task == "become_corrupted":
become_corrupted()
func become_corrupted():
if location_xz.distance_to(aggressor.location_xy) <= 1:
# Spin for effect and lose hp.
rotate_y(deg_to_rad(-15))
hp -= 0.5
else:
hp = 100
task = "idle"
# Create a new cultist and then destroy self.
if hp <= 0:
# Disable hitbox
$slime_collision.disabled = true
$CollisionDetection/slime_collision.disabled = true
# Load and instantiate a new cultist.
var cultist = load("res://cultist.tscn")
cultist = cultist.instantiate()
get_tree().get_root().add_child(cultist)
cultist.location_xz = location_xz
# Destroy self.
queue_free()
func get_color_idx() -> int:
return $slime.color_idx
func get_color():
return $slime.get_color()
func _on_area_3d_body_entered(body):
if "color_changes" not in body:
return
var other = body.get_color_idx()
var mine = get_color_idx()
if other == mine:
return
var delta = abs(other - mine)
if delta % 2 == 0:
if mine < other:
$slime.change_color(other)
elif mine > other:
$slime.change_color(other)
func _on_collision_detection_body_entered(body):
if body.is_class("Cultist"):
aggressor = body
task = "become_corrupted"