brackeys-12/main.gd
2024-09-23 20:36:55 -04:00

106 lines
2.9 KiB
GDScript

extends Node2D
@onready var building_levels = [
$Node2D/Level2/BuildingLevel2,
$Node2D/Level3/BuildingLevel3,
$Node2D/Level4/BuildingLevel4,
$Node2D/Level5/BuildingLevel5,
$Node2D/Node2D/BuildingLevel6
]
@onready var night: ColorRect = $CanvasLayer/Night
var night_tween
var timer = 0
var timer2 = 0
func _ready() -> void:
Triggerer.listen("game_started", _on_game_started)
Triggerer.listen("destroy", _on_destroy)
func _on_game_started(_data):
if night_tween:
night_tween.kill()
night_tween = create_tween()
night_tween.set_loops()
night_tween.tween_property(night, "modulate", Color.TRANSPARENT, 5)
night_tween.tween_interval(15)
night_tween.tween_property(night, "modulate", Color.WHITE, 5)
func _process(delta: float) -> void:
if Persister.get_value("win") or Persister.get_value("lose"):
return
if Persister.get_value("population") <= 0:
Persister.persist_data("lose", true)
if Input.is_action_just_pressed("mclick"):
for build_level in building_levels:
build_level.pick_block()
if Input.is_action_just_released("lclick"):
if Persister.get_value("drag_mode") and Persister.get_value("building_mode"):
for build_level in building_levels:
build_level.place_building()
Persister.persist_data("building_mode", false)
var building_mode = Persister.get_value("building_mode")
if Input.is_action_just_pressed("lclick") and not Persister.get_value("drag_mode"):
if building_mode:
for build_level in building_levels:
build_level.place_building()
else:
for build_level in building_levels:
build_level.show_info_building()
Persister.persist_data("building_mode", false)
if Input.is_action_just_pressed("rclick"):
Persister.persist_data("building_mode", false)
if Persister.get_value("game_started"):
timer += delta
timer2 += delta
if timer >= 5:
var pop = Persister.get_value("population")
var pop_scale = ceil(pop / 5.0)
if Persister.get_value("food") > pop_scale and Persister.get_value("water") > pop_scale:
Persister.change_value("food", -pop_scale)
Persister.change_value("water", -pop_scale)
if Persister.get_value("population") < Persister.get_value("max_population"):
Persister.change_value("population", 1)
Persister.change_value("avail_population", 1)
elif Persister.get_value("population") > Persister.get_value("max_population"):
Persister.change_value("population", -1)
if Persister.get_value("avail_population") > 0:
Persister.change_value("avail_population", -1)
else:
_kill_citizen()
else:
Persister.change_value("population", -1)
if Persister.get_value("avail_population") > 0:
Persister.change_value("avail_population", -1)
else:
_kill_citizen()
timer -= 5
func _on_destroy(data):
var tile = data.tile
for build_level in building_levels:
build_level.destroy(tile)
func _kill_citizen():
for build_level in building_levels:
var val = build_level.kill_citizen()
if val:
return