Skip to content

Commit

Permalink
shop fix
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelcastrocouto committed May 4, 2022
1 parent 03e493d commit ab85e7c
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 71 deletions.
4 changes: 2 additions & 2 deletions prototype/controls/orders/unit_orders.gd
Original file line number Diff line number Diff line change
Expand Up @@ -342,9 +342,9 @@ func set_taxes(tax, team):

func update_taxes():
for leader in game.player_leaders:
game.ui.inventories.leaders[leader.name].extra_tax_gold = tax_gold[player_tax]
game.ui.inventories.player_leaders_inv[leader.name].extra_tax_gold = tax_gold[player_tax]
for leader in game.enemy_leaders:
game.ui.inventories.leaders[leader.name].extra_tax_gold = tax_gold[enemy_tax]
game.ui.inventories.enemy_leaders_inv[leader.name].extra_tax_gold = tax_gold[enemy_tax]


# RETREAT
Expand Down
10 changes: 5 additions & 5 deletions prototype/items/button/item_button.gd
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func _ready():


func setup(new_item):
sell_button.hide()
sell_button.visible = false

if new_item == null:
self.item = null
Expand All @@ -26,8 +26,8 @@ func setup(new_item):
self.icon = null
self.hint_tooltip = "Buy items in the Blacksmith"
self.disabled = true
name_label.hide()
price_label.hide()
name_label.visible = false
price_label.visible = false

else:
self.item = new_item
Expand Down Expand Up @@ -55,8 +55,8 @@ func on_button_down():
game.ui.inventories.add_delivery(leader, item)
game.ui.shop.disable_all()

else: # inventory item
# USE ITEM
else:
# use inventory item
for key in item.attributes.keys():
if key in leader:
leader[key] += item.attributes[key]
Expand Down
134 changes: 75 additions & 59 deletions prototype/items/inventories.gd
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ const consumable_items_max = 1
const delivery_time = 2

# Dictionary of all leaders inventories
var leaders = {}
var deliveries = {}
var player_leaders_inv = {}
var player_deliveries = {}

var enemy_leaders_inv = {}
var enemy_deliveries = {}


var item_button_preload = preload("res://items/button/item_button.tscn")
Expand All @@ -35,7 +38,6 @@ func clear():


func new_inventory(leader):

var extra_skill_gold = 0
if leader.display_name in game.unit.skills.leader:
var leader_skills = game.unit.skills.leader[leader.display_name]
Expand Down Expand Up @@ -72,6 +74,40 @@ func build_leaders():
gold_update_cycle()


func get_leader_inventory(leader):
if leader.type == 'leader':
var inv = player_leaders_inv
if not leader.team == game.player_team:
inv = enemy_leaders_inv
if leader.name in inv: return inv[leader.name]


func set_leader_inventory(leader, inventory):
if leader.type == 'leader':
var inv = player_leaders_inv
if not leader.team == game.player_team:
inv = enemy_leaders_inv

inventory.leader = leader
inv[leader.name] = inventory


func get_leader_delivery(leader):
if leader.type == 'leader':
var deliv = player_deliveries
if not leader.team == game.player_team:
deliv = enemy_deliveries
if leader.name in deliv: return deliv[leader.name]


func set_leader_delivery(leader, delivery):
if leader.type == 'leader':
var deliv = player_deliveries
if not leader.team == game.player_team:
deliv = enemy_deliveries
deliv[leader.name] = delivery


func gold_update_cycle():
if not game.paused:
game.ui.shop.update_buttons()
Expand All @@ -84,8 +120,7 @@ func add_inventory(leader):
# Setup GUI for inventory
var inventory = new_inventory(leader)
add_child(inventory.container)
leaders[leader.name] = inventory
inventory.leader = leader
set_leader_inventory(leader, inventory)
gold_timer_timeout(leader)
var counter = 0
var item_button
Expand All @@ -109,11 +144,12 @@ func add_inventory(leader):

func gold_timer_timeout(unit):
if not game.paused:
var inventory = get_leader_inventory(unit)
var gold_per_sec = 1
if unit.name in leaders:
gold_per_sec += leaders[unit.name].extra_skill_gold
gold_per_sec += leaders[unit.name].extra_tax_gold
gold_per_sec += leaders[unit.name].extra_mine_gold
if inventory:
gold_per_sec += inventory.extra_skill_gold
gold_per_sec += inventory.extra_tax_gold
gold_per_sec += inventory.extra_mine_gold
unit.gold += gold_per_sec
# Updates gold label
if unit == game.selected_unit: game.ui.stats.update()
Expand All @@ -122,17 +158,17 @@ func gold_timer_timeout(unit):



func equip_items_has_slots(leader_name):
var inventory = leaders[leader_name]
func equip_items_has_slots(leader):
var inventory = get_leader_inventory(leader)
for item in inventory.equip_items:
if item == null:
return true
return false



func consumable_items_has_slots(leader_name):
var inventory = leaders[leader_name]
func consumable_items_has_slots(leader):
var inventory = get_leader_inventory(leader)
for item in inventory.consumable_items:
if item == null:
return true
Expand All @@ -149,25 +185,23 @@ func add_delivery(leader, item):
"label": null,
"button": null
}
deliveries[leader.name] = new_delivery

var inventory = leaders[leader.name]
set_leader_delivery(leader, new_delivery)
var inventory = get_leader_inventory(leader)

if item.type == "equip":
for index in range(equip_items_max):
if inventory.equip_items[index] == null:
new_delivery.index = index
new_delivery.button = inventory.equip_item_buttons[index]
new_delivery.label = new_delivery.button.price_label
break
elif item.type == "consumable":
for index in range(consumable_items_max):
if inventory.consumable_items[index] == null:
new_delivery.index = index
new_delivery.button = inventory.consumable_item_buttons[index]
new_delivery.label = new_delivery.button.price_label
break

new_delivery.label = new_delivery.button.price_label
delivery_timer(new_delivery)


Expand All @@ -177,38 +211,36 @@ func delivery_timer(delivery):
delivery.time -= 1
if delivery.time > 0:
delivery.label.text = "0:0"+str(delivery.time)
yield(get_tree().create_timer(1), "timeout")
delivery_timer(delivery)
else:
match delivery.item.type:
"consumable": give_item(delivery)
"equip":
if game.ui.shop.close_to_blacksmith(delivery.leader):
give_item(delivery)
else:
var inventory = leaders[delivery.leader.name]
else:
var inventory = get_leader_inventory(delivery.leader)
var index = delivery.index
inventory.equip_items[index] = delivery.item
delivery.item.ready = true
delivery.label.text = "ready"
else:
yield(get_tree().create_timer(1), "timeout")
delivery_timer(delivery)

yield(get_tree().create_timer(1), "timeout")
if delivery.time > 0: delivery_timer(delivery)


func is_delivering(leader):
if leader.name in game.ui.inventories.deliveries:
var delivery = game.ui.inventories.deliveries[leader.name]
return (delivery.time > 0)
else: return false
var delivery = get_leader_delivery(leader)
if delivery: return (delivery.time > 0)
return false


func give_item(delivery):
var leader = delivery.leader
var inventory = leaders[leader.name]
var inventory = get_leader_inventory(leader)
var item = delivery.item
var index = delivery.index

deliveries.erase(leader.name)
get_leader_delivery(leader).erase(leader.name)

match item.type:
"equip":
Expand All @@ -228,45 +260,30 @@ func give_item(delivery):


func remove_item(leader, index):
var leader_items = leaders[leader.name].equip_items + leaders[leader.name].consumable_items
var inventory = get_leader_inventory(leader)

var leader_items = inventory.equip_items + inventory.consumable_items
var item = leader_items[index]

if item.type == "equip":
# Remove attributes that were added when purchasing an item
for key in item.attributes.keys():
game.unit.modifiers.remove(leader, key, item.name, item.attributes[key])

leaders[leader.name].equip_items[index] = null
inventory.equip_items[index] = null

elif item.type == "consumable":
leaders[leader.name].consumable_items[index - equip_items_max] = null
inventory.consumable_items[index - equip_items_max] = null

leader.get_node("hud").update_hpbar(leader)


return item



func setup_items(leader_name):
var counter = 0
var inventory = leaders[leader_name]
for item in inventory.equip_items:
inventory.equip_item_buttons[counter].setup(item)
counter += 1
counter = 0
for item in inventory.consumable_items:
inventory.consumable_item_buttons[counter].setup(item)
counter += 1

update_buttons()
game.ui.stats.update()



# Disable potion if full heath
func update_consumables(leader):
var inventory = leaders[leader.name]
var inventory = get_leader_inventory(leader)
var counter = 0
for item in inventory.consumable_items:
var item_button = inventory.consumable_item_buttons[counter]
Expand All @@ -276,27 +293,26 @@ func update_consumables(leader):


func update_buttons():
for leader in game.player_leaders:
var inventory = leaders[leader.name]
for leader in game.player_leaders + game.enemy_leaders:
var inventory = get_leader_inventory(leader)
var close_to_blacksmith = game.ui.shop.close_to_blacksmith(leader)
inventory.container.hide()
# deliver ready items
if close_to_blacksmith:
for item in inventory.equip_items:
if item and item.ready and not item.delivered:
var ready_delivery = deliveries[leader.name]
give_item(ready_delivery)
var delivery = get_leader_delivery(leader)
give_item(delivery)

if game.selected_leader and game.selected_leader.name in leaders:
if game.selected_leader:
var inventory = get_leader_inventory(game.selected_leader)
var leader = game.selected_leader
var inventory = leaders[leader.name]
var close_to_blacksmith = game.ui.shop.close_to_blacksmith(leader)

show()
inventory.container.show()
update_consumables(leader)


# toggle sell buttons
if game.ui.shop.visible and close_to_blacksmith:
var counter = 0
Expand Down
9 changes: 5 additions & 4 deletions prototype/items/shop.gd
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ func update_buttons():
return

# enable/disable all buttons on which leader don't have enough golds
if leader and leader.name in game.ui.inventories.leaders:
var inventory = game.ui.inventories.get_leader_inventory(leader)
if leader and inventory:
for item_button in equip_items.get_children() + consumable_items.get_children():
var item_price = item_button.item.price
item_button.disabled = (leader.gold < item_price)
Expand All @@ -128,11 +129,11 @@ func update_buttons():
disable_equip()

# disable buttons if leader don't have empty slots for item
if leader and leader.name in game.ui.inventories.leaders:
if !game.ui.inventories.equip_items_has_slots(leader.name):
if leader and inventory:
if !game.ui.inventories.equip_items_has_slots(leader):
for item_button in equip_items.get_children():
item_button.disabled = true
if !game.ui.inventories.consumable_items_has_slots(leader.name):
if !game.ui.inventories.consumable_items_has_slots(leader):
for item_button in consumable_items.get_children():
item_button.disabled = true

1 change: 1 addition & 0 deletions prototype/ui/ui.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ __meta__ = {
}

[node name="main" parent="background" instance=ExtResource( 17 )]
pause_mode = 2
position = Vector2( -528, -300 )
scale = Vector2( 1.666, 1.666 )

Expand Down
2 changes: 1 addition & 1 deletion prototype/utils/test.gd
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func spawn_unit():
dummy.set_behavior("stand")
dummy.hp = 10000
dummy.current_hp = 10000
#game.map.create(s.infantry, "mid", "red", "Vector2", Vector2(1000,900))
game.map.create(s.takoda, "mid", "red", "Vector2", Vector2(1000,900))
# leader.hp = 100
# leader.current_hp = 100

Expand Down

0 comments on commit ab85e7c

Please sign in to comment.