SlimeoosOdyssey/godot/Villager.gd

81 lines
1.6 KiB
GDScript3
Raw Normal View History

2023-08-20 00:00:17 -06:00
class_name Villager
2023-08-20 15:03:09 -06:00
extends SharedSlime
2023-08-20 01:03:30 -06:00
var color_changes = true
2023-08-20 15:03:09 -06:00
var is_villager = true
2023-08-20 13:31:07 -06:00
var hp = 100
var aggressor
2023-08-20 17:21:02 -06:00
func _ready():
super._ready()
2023-08-20 17:33:37 -06:00
$SoundTimer.wait_time = rng.randi_range(15, 240)
2023-08-20 17:21:02 -06:00
$SoundTimer.start()
2023-08-20 13:31:07 -06:00
func _process(delta):
super._process(delta)
if task == "become_corrupted":
become_corrupted()
func become_corrupted():
2023-08-20 15:03:09 -06:00
if location_xz.distance_to(aggressor.location_xz) <= 1:
print("becoming corrupted")
print(hp)
2023-08-20 13:31:07 -06:00
# Spin for effect and lose hp.
rotate_y(deg_to_rad(-15))
hp -= 0.5
else:
hp = 100
task = "idle"
2023-08-20 15:03:09 -06:00
print("cultist left")
2023-08-20 13:31:07 -06:00
# 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()
2023-08-20 15:03:09 -06:00
get_parent_node_3d().add_child(cultist)
cultist.set_global_position(get_global_position())
2023-08-20 13:31:07 -06:00
# Destroy self.
queue_free()
2023-08-20 01:03:30 -06:00
func get_color_idx() -> int:
return $slime.color_idx
2023-08-20 02:05:40 -06:00
func get_color():
return $slime.get_color()
2023-08-20 01:03:30 -06:00
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)
2023-08-20 13:31:07 -06:00
func _on_collision_detection_body_entered(body):
2023-08-20 15:03:09 -06:00
if "is_cultist" in body:
aggressor = body as Cultist
2023-08-20 13:31:07 -06:00
task = "become_corrupted"
2023-08-20 17:21:02 -06:00
func _on_sound_timer_timeout():
$happyslime.play()
2023-08-20 17:33:37 -06:00
$SoundTimer.wait_time = rng.randi_range(60, 240)
2023-08-20 17:21:02 -06:00
$SoundTimer.start()