Skip to content

Commit

Permalink
[wip] better buiding cost
Browse files Browse the repository at this point in the history
  • Loading branch information
theludovyc committed Jan 11, 2025
1 parent 190751b commit 37e1107
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 35 deletions.
30 changes: 30 additions & 0 deletions theLudovyc/GUI/GUI.gd
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,36 @@ func set_rtl_info_text_money_cost(amount: int):
rtl_info.append_text("[center]" + str(amount) + " ")
rtl_info.add_image(TheBank.money_icon, 20)

func set_rtl_info_buiding_info(building_total_cost:Array):
rtl_info.clear()

if building_total_cost.is_empty():
return

rtl_info.append_text("[center]")

if building_total_cost[0] != 0:
rtl_info.append_text("[color=orange]" + \
Helper.get_string_from_signed_int(building_total_cost[0]) + "[/color] ")

rtl_info.add_image(TheBank.money_icon, 20)
rtl_info.add_text(" / ")

for i in range(building_total_cost[1].size()):
var cost = building_total_cost[1][i]

if i > 0:
rtl_info.add_text(" / ")

var color_str = "green"

if cost[1] < 0:
color_str = "orange"

rtl_info.append_text("[color=" + color_str + "]" + \
Helper.get_string_from_signed_int(cost[1]) + "[/color] ")

rtl_info.add_image(Resources.Icons[cost[0]], 20)

func set_rtl_visibility(b: bool):
rtl_info.visible = b
2 changes: 1 addition & 1 deletion theLudovyc/GUI/GUI.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ content_margin_left = 4.0
content_margin_top = 4.0
content_margin_right = 4.0
content_margin_bottom = 4.0
bg_color = Color(0.718, 0.647, 0.553, 0.75)
bg_color = Color(0.718, 0.647, 0.553, 0.8)
corner_radius_top_left = 10
corner_radius_top_right = 10
corner_radius_bottom_right = 10
Expand Down
4 changes: 2 additions & 2 deletions theLudovyc/GUI/RichTextLabelInfo.gd
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ extends RichTextLabel

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
position = get_global_mouse_position() - Vector2(size.x / 2, size.y * 1.25)
position = get_global_mouse_position() - Vector2(size.x / 2, size.y * 1.5)
pass


func _on_visibility_changed():
# to avoid teleport effect
if visible:
position = get_global_mouse_position() - Vector2(size.x / 2, size.y * 1.25)
position = get_global_mouse_position() - Vector2(size.x / 2, size.y * 1.5)

set_process(visible)
64 changes: 40 additions & 24 deletions theLudovyc/Game2D.gd
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,27 @@ func _ready():

pass # Replace with function body.

# return [money_cost, [[Resources.Types, cost], ...]]
func get_building_total_cost(building_id, trees_to_destroy) -> Array:
var trees_to_destroy_final_cost := 0

if trees_to_destroy > 0:
trees_to_destroy_final_cost = trees_to_destroy * Trees_Destroy_Cost

var building_cost = Buildings.get_building_cost(building_id).duplicate(true)

if building_cost.is_empty():
return [-trees_to_destroy_final_cost, []]

for i in range(building_cost.size()):
var cost = building_cost[i]

cost[1] *= -1

if trees_to_destroy > 0 and (cost[0] == Resources.Types.Wood):
cost[1] += trees_to_destroy

return [-trees_to_destroy_final_cost, building_cost]

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
Expand Down Expand Up @@ -132,35 +153,32 @@ func _process(delta):

if trees_to_destroy > 0:
trees_to_destroy_final_cost = trees_to_destroy * Trees_Destroy_Cost

if trees_to_destroy_final_cost > 0:
gui.set_rtl_info_text_money_cost(trees_to_destroy_final_cost)
gui.set_rtl_visibility(true)
else:
gui.set_rtl_visibility(false)

var building_total_cost = get_building_total_cost(building_id, trees_to_destroy)

gui.set_rtl_info_buiding_info(building_total_cost)

var is_constructible = false

if (
trees_to_destroy >= 0
and (
trees_to_destroy_final_cost == 0
or (
trees_to_destroy_final_cost > 0
and trees_to_destroy_final_cost <= the_bank.money
)
)
and the_storage.has_resources_to_construct_building(building_id)
(building_total_cost[0] >= 0 or
(building_total_cost[0] < 0 and abs(building_total_cost[0]) < the_bank.money))
and the_storage.has_resources_to_construct_building(building_total_cost[1])
):
is_constructible = true

if is_constructible:

gui.set_rtl_visibility(true)

if trees_to_destroy > 0:
cursor_entity.modulate = Color(Color.ORANGE, 0.6)
else:
cursor_entity.modulate = Color(Color.GREEN, 0.6)
else:
# is_constructible = false (already assigned)

cursor_entity.modulate = Color(Color.RED, 0.6)

gui.set_rtl_visibility(false)

if cursor_entity_wait_release and Input.is_action_just_released("alt_command"):
cursor_entity_wait_release = false
Expand All @@ -184,24 +202,22 @@ func _process(delta):
Buildings.get_max_workers(building_id)
)

if trees_to_destroy_final_cost > 0:
gui.set_rtl_visibility(false)

the_bank.money -= trees_to_destroy_final_cost
the_bank.money += building_total_cost[0]

the_storage.conclude_building_construction(building_id)
the_storage.conclude_building_construction(building_total_cost[1])

event_bus.send_building_created.emit(building_id)

tm.build_entityStatic(cursor_entity, tile_pos)

gui.set_rtl_visibility(false)

cursor_entity.modulate = Color.WHITE
cursor_entity.build()
cursor_entity = null

if cursor_entity and Input.is_action_just_pressed("main_command"):
if trees_to_destroy_final_cost > 0:
gui.set_rtl_visibility(false)
gui.set_rtl_visibility(false)

event_bus.send_building_creation_aborted.emit(building_id)

Expand Down
12 changes: 4 additions & 8 deletions theLudovyc/TheStorage.gd
Original file line number Diff line number Diff line change
Expand Up @@ -60,27 +60,23 @@ func update_global_production_rate(resource_type: Resources.Types):
)


func has_resources_to_construct_building(building_id: Buildings.Ids) -> bool:
var building_cost = Buildings.get_building_cost(building_id)

func has_resources_to_construct_building(building_cost:Array) -> bool:
if building_cost.is_empty():
return true

for cost in building_cost:
if cost[1] > storage.get(cost[0], 0):
if abs(cost[1]) > storage.get(cost[0], 0):
return false

return true


func conclude_building_construction(building_id: Buildings.Ids):
var building_cost = Buildings.get_building_cost(building_id)

func conclude_building_construction(building_cost:Array):
if building_cost.is_empty():
return

for cost in building_cost:
add_resource(cost[0], -cost[1])
add_resource(cost[0], cost[1])


func recover_building_construction(building_id: Buildings.Ids):
Expand Down

0 comments on commit 37e1107

Please sign in to comment.