Skip to content

Commit

Permalink
[ench] recover wood when build on tree
Browse files Browse the repository at this point in the history
When build destroy tree, lost money but recover wood
Improve the HUD to see the cost of the building to build
  • Loading branch information
theludovyc committed Jan 11, 2025
1 parent 190751b commit 8976d26
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 71 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)
125 changes: 65 additions & 60 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 @@ -126,82 +147,66 @@ func _process(delta):

var building_id = cursor_entity.building_id

# -1 can not build, 0 yes and 0 tree, 1+ yes and 1+ tree to destroy
var trees_to_destroy = tm.is_entityStatic_constructible(cursor_entity, tile_pos)

var trees_to_destroy_final_cost := 0

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:

if (trees_to_destroy < 0):
cursor_entity.modulate = Color(Color.RED, 0.6)

gui.set_rtl_visibility(false)

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)
):
is_constructible = true

if is_constructible:
if trees_to_destroy > 0:
cursor_entity.modulate = Color(Color.ORANGE, 0.6)
else:
cursor_entity.modulate = Color(Color.GREEN, 0.6)
else:
cursor_entity.modulate = Color(Color.RED, 0.6)
var building_total_cost = get_building_total_cost(building_id, trees_to_destroy)

if (
(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])
):
gui.set_rtl_info_buiding_info(building_total_cost)
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)

if cursor_entity_wait_release and Input.is_action_just_released("alt_command"):
cursor_entity_wait_release = false
if (
not cursor_entity_wait_release
and Input.is_action_just_pressed("alt_command")
):
match Buildings.get_building_type(building_id):
Buildings.Types.Residential:
var amount := Buildings.get_max_workers(building_id)

if (
not cursor_entity_wait_release
and is_constructible
and Input.is_action_just_pressed("alt_command")
):
match Buildings.get_building_type(building_id):
Buildings.Types.Residential:
var amount := Buildings.get_max_workers(building_id)
population += amount

population += amount
the_factory.population_increase(amount)

the_factory.population_increase(amount)
Buildings.Types.Producing:
the_factory.add_workers(
Buildings.get_produce_resource(building_id),
Buildings.get_max_workers(building_id)
)

Buildings.Types.Producing:
the_factory.add_workers(
Buildings.get_produce_resource(building_id),
Buildings.get_max_workers(building_id)
)
the_bank.money += building_total_cost[0]

if trees_to_destroy_final_cost > 0:
gui.set_rtl_visibility(false)
the_storage.conclude_building_construction(building_total_cost[1])

the_bank.money -= trees_to_destroy_final_cost
event_bus.send_building_created.emit(building_id)

the_storage.conclude_building_construction(building_id)
tm.build_entityStatic(cursor_entity, tile_pos)

event_bus.send_building_created.emit(building_id)
gui.set_rtl_visibility(false)

tm.build_entityStatic(cursor_entity, tile_pos)
cursor_entity.modulate = Color.WHITE
cursor_entity.build()
cursor_entity = null

cursor_entity.modulate = Color.WHITE
cursor_entity.build()
cursor_entity = null
if cursor_entity_wait_release and Input.is_action_just_released("alt_command"):
cursor_entity_wait_release = false

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 8976d26

Please sign in to comment.