Add slug spawning

This commit is contained in:
Olivier 'reivilibre' 2023-01-09 19:31:21 +00:00
parent be58de4369
commit 21ec8dd5a2
3 changed files with 54 additions and 5 deletions

View File

@ -2,6 +2,7 @@ extends Node2D
var coin = 0
export var postie_times = [0.5]
export var postie_length = .3
# Anything above 10 can't be rendered (for now?)
export var num_days = 5
@ -10,6 +11,11 @@ export var next_level = ""
export var needed_coins = 50
export var slug_o_clock = 0.7
export var slug_chance_per_roll = 0.5
export var slug_roll_rate = 10
export var max_slugs_per_group = 3
func add_coin(amount: int):
coin += amount
$HudInner/Panel/LabelCoin.text = str(coin)
@ -30,14 +36,13 @@ func _win():
var _r = get_tree().change_scene("res://levels/" + next_level + ".tscn")
func remove_hp():
hp -= 1
var hp_indicator = $HudInner/Panel/HpBar.get_child(hp)
hp_indicator.modulate.r = 0
if hp == 0:
# Lose game
self._lose(true)
return
hp -= 1
var hp_indicator = $HudInner/Panel/HpBar.get_child(hp)
hp_indicator.modulate.r = 0
func _ready():
# get a fresh seed for the PRNG
@ -50,6 +55,12 @@ func _ready():
var sch = $HudInner/Panel/Scheduler
sch.postie_times = postie_times
sch.days_on_this_level = num_days
sch.postie_length = postie_length
sch.slug_o_clock = slug_o_clock
sch.slug_chance_per_roll = slug_chance_per_roll
sch.slug_roll_rate = slug_roll_rate
sch.max_slugs_per_group = max_slugs_per_group
$HudInner/Panel/LabelCoinNeed.text = "need: " + str(needed_coins)

View File

@ -29,6 +29,11 @@ var days_on_this_level = 5
var postie_times = [0.1]
var postie_length = .3
var slug_o_clock = 0.7
var slug_chance_per_roll = 0.5
var slug_roll_rate = 10
var max_slugs_per_group = 3
var day_num = 0
var time_of_day = 0.0
@ -44,6 +49,7 @@ func _start_schedule():
for postie_time in postie_times:
# 5s early so the postie has time to drive there.
_postie_in(parent, (postie_time / DAY_PER_SEC) - 5.0, postie_length / DAY_PER_SEC)
_slug_spawner(parent)
$Timer.start(1 / DAY_PER_SEC)
yield($Timer, "timeout")
@ -68,6 +74,39 @@ func _postie_in(parent: Node2D, time: float, idling_time: float):
parent.add_child(new_postie)
func _slug_spawner(parent: Node2D):
var timer = Timer.new()
timer.one_shot = true
$Timer.add_child(timer)
timer.start(slug_o_clock / DAY_PER_SEC); yield(timer, "timeout")
var num_rolls = ceil((1.0 - slug_o_clock) * slug_roll_rate)
print("num rolls = ", num_rolls)
var interval = 1 / (DAY_PER_SEC * slug_roll_rate)
print("interval = ", interval)
for roll_num in range(num_rolls):
# wait for our next roll
timer.start(interval); yield(timer, "timeout")
if rand_range(0.0, 1.0) > slug_chance_per_roll:
print("miss.")
continue
var num_slugs = floor(rand_range(0.0, max_slugs_per_group)) + 1
print("lucky you! ", num_slugs)
for slug_num in num_slugs:
var y_coord = -5
if rand_range(0.0, 1.0) < 0.5:
y_coord = 910
var slug_pos = Vector2(rand_range(0.0, 1600.0), y_coord)
var new_slug = preload("res://characters/pests/Slug.tscn").instance()
new_slug.position = slug_pos
parent.add_child(new_slug)
pass
$Timer.remove_child(timer)
func _process(delta):
time_of_day += delta * DAY_PER_SEC
$Clock/ClockHand.rotation = 2 * PI * fmod(time_of_day, 1.0)

View File

@ -95,7 +95,6 @@ offset = Vector2( -2, -32 )
[node name="PostieRegion7" type="Sprite" parent="Clock/PostieRegions"]
visible = false
rotation = -2.51327
scale = Vector2( 1, 1 )
texture = ExtResource( 6 )
centered = false
offset = Vector2( -2, -32 )