Skip to content
Snippets Groups Projects
Commit 545618d5 authored by Alan's avatar Alan
Browse files

Seperated game logic and state from the shop to the game script

parent 4ea31651
No related branches found
No related tags found
No related merge requests found
ColorRect
Label
TextureRect
HBoxContainer
......@@ -9,6 +10,5 @@ Node2D
Node
Control
AnimationPlayer
ColorRect
CanvasLayer
Sprite2D
......@@ -10,7 +10,7 @@ dock_split_2=0
dock_split_3=0
dock_hsplit_1=0
<<<<<<<HEADdock_hsplit_2=498
dock_hsplit_3=-436
dock_hsplit_3=-318
dock_hsplit_2=306
dock_hsplit_4=0
dock_filesystem_h_split_offset=240
......@@ -27,16 +27,16 @@ dock_5="Inspector,Node,History"
[EditorNode]
open_scenes=PackedStringArray("res://scenes/game.tscn", "res://main.tscn", "res://scenes/Shop.tscn", "res://scenes/ShopItem.tscn")
current_scene="res://scenes/ShopItem.tscn"
center_split_offset=-314
current_scene="res://scenes/game.tscn"
center_split_offset=-336
selected_default_debugger_tab_idx=0
selected_main_editor_idx=0
selected_main_editor_idx=2
selected_bottom_panel_item=0
[ScriptEditor]
open_scripts=["res://scripts/game.gd", "res://README.md", "res://scripts/scene_manager.gd", "res://scripts/Shop.gd"]
selected_script="res://scripts/Shop.gd"
selected_script="res://scripts/game.gd"
open_help=[]
script_split_offset=70
list_split_offset=0
......
......@@ -11,3 +11,12 @@ res://scenes/Shop.tscn
res://scripts/Shop.gd
res://scenes/ShopItem.tscn
res://scripts/Piece.gd
res://scripts/Pawn.gd
res://scripts/Rook.gd
res://scripts/Bishop.gd
res://scripts/Queen.gd
res://scripts/King.gd
res://scripts/Knight.gd
res://scripts/Assassin.gd
res://scripts/Mage.gd
res://scripts/Wizard.gd
......@@ -175,4 +175,4 @@ Anim={
"zfar": 4000.01,
"znear": 0.05
}
selected_nodes=Array[NodePath]([NodePath("/root/@EditorNode@16886/@Panel@13/@VBoxContainer@14/DockHSplitLeftL/DockHSplitLeftR/DockHSplitMain/@VBoxContainer@25/DockVSplitCenter/@VSplitContainer@52/@VBoxContainer@53/@PanelContainer@98/MainScreen/@CanvasItemEditor@9272/@VSplitContainer@9094/@HSplitContainer@9096/@HSplitContainer@9098/@Control@9099/@SubViewportContainer@9100/@SubViewport@9101/ShopItem/MarginContainer/VBoxContainer/ItemSprite")])
selected_nodes=Array[NodePath]([NodePath("/root/@EditorNode@16886/@Panel@13/@VBoxContainer@14/DockHSplitLeftL/DockHSplitLeftR/DockHSplitMain/@VBoxContainer@25/DockVSplitCenter/@VSplitContainer@52/@VBoxContainer@53/@PanelContainer@98/MainScreen/@CanvasItemEditor@9272/@VSplitContainer@9094/@HSplitContainer@9096/@HSplitContainer@9098/@Control@9099/@SubViewportContainer@9100/@SubViewport@9101/Game/GoldDisplay")])
[folding]
node_unfolds=[NodePath("."), PackedStringArray("opponent_shader", "Editor Description"), NodePath("ShadowContainer"), PackedStringArray("Ordering"), NodePath("TileContainer"), PackedStringArray("Transform", "Ordering"), NodePath("PieceContainer"), PackedStringArray("Ordering", "Process", "Texture"), NodePath("Background"), PackedStringArray("Layout", "material", "Ordering", "Layout/Transform", "Layout/Grow Direction", "Layout/Container Sizing", "Layout/Anchor Points", "Layout/Anchor Offsets", "Texture"), NodePath("Explosion"), PackedStringArray("Ordering", "Transform", "Scale", "Time"), NodePath("PlayerPieces"), PackedStringArray("Transform"), NodePath("OpponentPieces"), PackedStringArray("Visibility", "Transform")]
node_unfolds=[NodePath("."), PackedStringArray("opponent_shader", "Editor Description"), NodePath("ShadowContainer"), PackedStringArray("Ordering"), NodePath("TileContainer"), PackedStringArray("Transform", "Ordering"), NodePath("PieceContainer"), PackedStringArray("Ordering", "Process", "Texture"), NodePath("Background"), PackedStringArray("Layout", "material", "Ordering", "Layout/Transform", "Layout/Grow Direction", "Layout/Container Sizing", "Layout/Anchor Points", "Layout/Anchor Offsets", "Texture"), NodePath("Explosion"), PackedStringArray("Ordering", "Transform", "Scale", "Time"), NodePath("PlayerPieces"), PackedStringArray("Transform"), NodePath("OpponentPieces"), PackedStringArray("Visibility", "Transform"), NodePath("GoldDisplay"), PackedStringArray("Theme Overrides", "Theme Overrides/fonts", "Theme Overrides/font_sizes")]
resource_unfolds=["res://scenes/game.tscn::ShaderMaterial_gdbwp", PackedStringArray("Shader Parameters", "Resource")]
nodes_folded=[]
......@@ -175,4 +175,4 @@ Anim={
"zfar": 4000.01,
"znear": 0.05
}
selected_nodes=Array[NodePath]([NodePath("/root/@EditorNode@16886/@Panel@13/@VBoxContainer@14/DockHSplitLeftL/DockHSplitLeftR/DockHSplitMain/@VBoxContainer@25/DockVSplitCenter/@VSplitContainer@52/@VBoxContainer@53/@PanelContainer@98/MainScreen/@CanvasItemEditor@9272/@VSplitContainer@9094/@HSplitContainer@9096/@HSplitContainer@9098/@Control@9099/@SubViewportContainer@9100/@SubViewport@9101/ShopItem/MarginContainer/VBoxContainer/ItemSprite")])
selected_nodes=Array[NodePath]([NodePath("/root/@EditorNode@16886/@Panel@13/@VBoxContainer@14/DockHSplitLeftL/DockHSplitLeftR/DockHSplitMain/@VBoxContainer@25/DockVSplitCenter/@VSplitContainer@52/@VBoxContainer@53/@PanelContainer@98/MainScreen/@CanvasItemEditor@9272/@VSplitContainer@9094/@HSplitContainer@9096/@HSplitContainer@9098/@Control@9099/@SubViewportContainer@9100/@SubViewport@9101/Game/GoldDisplay")])
......@@ -21,4 +21,4 @@ last_selected_language="GDScript"
[color_picker]
picker_shape=3
recent_presets=PackedColorArray(0.408717, 0.672092, 0.458479, 1)
recent_presets=PackedColorArray(0, 0, 0, 1, 0, 0, 0, 0.313726, 0, 0, 0.137255, 0.313726, 4.81308e-08, 9.62615e-08, 0.540356, 0.313726, 0, 0, 1, 0.313726, 0, 0, 1, 0.156863, 0, 1, 1, 0.156863, 0, 1, 0, 0.156863, 1, 1, 0, 0.156863)
......@@ -17,11 +17,11 @@ state={
state={
"bookmarks": PackedInt32Array(),
"breakpoints": PackedInt32Array(),
"column": 16,
"column": 32,
"folded_lines": Array[int]([]),
"h_scroll_position": 0,
"row": 47,
"scroll_position": 38.0,
"row": 551,
"scroll_position": 534.0,
"selection": false,
"syntax_highlighter": "GDScript"
}
......@@ -31,11 +31,11 @@ state={
state={
"bookmarks": PackedInt32Array(),
"breakpoints": PackedInt32Array(),
"column": 33,
"column": 0,
"folded_lines": Array[int]([]),
"h_scroll_position": 0,
"row": 72,
"scroll_position": 60.0,
"row": 74,
"scroll_position": 55.0,
"selection": false,
"syntax_highlighter": "GDScript"
}
......
[gd_scene load_steps=3 format=3 uid="uid://ct878s7wjsq77"]
[gd_scene load_steps=2 format=3 uid="uid://ct878s7wjsq77"]
[ext_resource type="Script" path="res://scripts/Shop.gd" id="1_8nmap"]
[sub_resource type="PlaceholderTexture2D" id="PlaceholderTexture2D_rcoi1"]
size = Vector2(16, 16)
[node name="Shop" type="Node2D"]
script = ExtResource("1_8nmap")
[node name="Background" type="TextureRect" parent="."]
visible = false
offset_right = 131.0
offset_bottom = 101.0
[node name="Item1" type="Button" parent="."]
visible = false
anchors_preset = 8
anchor_left = 0.5
anchor_top = 0.5
anchor_right = 0.5
anchor_bottom = 0.5
offset_left = -59.0
offset_top = 30.0
offset_right = -2.0
offset_bottom = 61.0
grow_horizontal = 2
grow_vertical = 2
text = "Item 1"
[node name="Item2" type="Button" parent="."]
visible = false
offset_left = 2.0
offset_top = 30.0
offset_right = 59.0
offset_bottom = 61.0
text = "Item 2"
[node name="MainContainer" type="MarginContainer" parent="."]
offset_right = 40.0
offset_bottom = 40.0
offset_right = 180.0
offset_bottom = 246.0
theme_override_constants/margin_left = 20
theme_override_constants/margin_top = 20
theme_override_constants/margin_right = 20
theme_override_constants/margin_bottom = 20
[node name="GoldDisplay" type="MarginContainer" parent="MainContainer"]
layout_mode = 2
size_flags_horizontal = 8
size_flags_vertical = 0
theme_override_constants/margin_top = 20
theme_override_constants/margin_right = 20
[node name="HBoxContainer" type="HBoxContainer" parent="MainContainer/GoldDisplay"]
layout_mode = 2
theme_override_constants/separation = 10
[node name="GoldIcon" type="TextureRect" parent="MainContainer/GoldDisplay/HBoxContainer"]
layout_mode = 2
texture = SubResource("PlaceholderTexture2D_rcoi1")
stretch_mode = 2
[node name="GoldAmount" type="Label" parent="MainContainer/GoldDisplay/HBoxContainer"]
[node name="Background" type="ColorRect" parent="MainContainer"]
layout_mode = 2
theme_override_font_sizes/font_size = 24
text = "0"
color = Color(1, 1, 0, 0.156863)
[node name="RowsContainer" type="VBoxContainer" parent="MainContainer"]
layout_mode = 2
......@@ -96,7 +48,4 @@ size_flags_horizontal = 8
text = "Next Round
"
[connection signal="item_purchased" from="." to="." method="_on_item_purchased"]
[connection signal="pressed" from="Item1" to="." method="_on_item_1_pressed"]
[connection signal="pressed" from="Item2" to="." method="_on_item_2_pressed"]
[connection signal="pressed" from="MainContainer/RowsContainer/BottomSection/NextRoundButton" to="." method="_on_next_round_button_pressed"]
......@@ -85,5 +85,6 @@ offset_top = 36.0
offset_right = -2.0
offset_bottom = 103.0
grow_horizontal = 0
theme_override_font_sizes/font_size = 26
[connection signal="pressed" from="ShopButton" to="." method="_on_shop_button_pressed"]
# Shop.gd
extends Node2D
signal item_purchased(item_name: String) # Define the signal
signal gold_spent(amount: int)
signal purchase_attempted(item_data: Dictionary)
signal next_round_requested
@onready var gold_display: Label = $GoldDisplay
@onready var shop_item_scene = preload("res://scenes/ShopItem.tscn")
@onready var gold_amount: Label = $MainContainer/GoldDisplay/HBoxContainer/GoldAmount
@onready var gold_icon: TextureRect = $MainContainer/GoldDisplay/HBoxContainer/GoldIcon
var player_gold: int = 0
var shop_items = {
"pieces": [
{"name": "Pawn", "price": 1, "texture": "res://assets/Resized Chess Piece Assets/White_Pawn.png"},
{"name": "Rook", "price": 3, "texture": "res://assets/Resized Chess Piece Assets/White_Rook.png"},
{"name": "Bishop", "price": 3, "texture": "res://assets/Resized Chess Piece Assets/White_Bishop.png"},
{"name": "Knight", "price": 3, "texture": "res://assets/Resized Chess Piece Assets/White_Knight.png"},
{"name": "Queen", "price": 5, "texture": "res://assets/Resized Chess Piece Assets/White_Queen.png"}
{"name": "Pawn", "price": 1, "texture": "res://assets/Resized Chess Piece Assets/White_Pawn.png", "purchased": false},
{"name": "Rook", "price": 3, "texture": "res://assets/Resized Chess Piece Assets/White_Rook.png", "purchased": false},
{"name": "Bishop", "price": 3, "texture": "res://assets/Resized Chess Piece Assets/White_Bishop.png", "purchased": false},
{"name": "Knight", "price": 3, "texture": "res://assets/Resized Chess Piece Assets/White_Knight.png", "purchased": false},
{"name": "Queen", "price": 5, "texture": "res://assets/Resized Chess Piece Assets/White_Queen.png", "purchased": false}
],
"upgrades": [
{"name": "Upgrade 1", "price": 3, "texture": "res://assets/Resized Chess Piece Assets/Black_Pawn.png"},
{"name": "Upgrade 2", "price": 5, "texture": "res://assets/Resized Chess Piece Assets/Black_Rook.png"},
{"name": "Upgrade 3", "price": 5, "texture": "res://assets/Resized Chess Piece Assets/Black_Bishop.png"},
{"name": "Upgrade 4", "price": 5, "texture": "res://assets/Resized Chess Piece Assets/Black_Knight.png"},
{"name": "Upgrade 5", "price": 5, "texture": "res://assets/Resized Chess Piece Assets/Black_Queen.png"}
{"name": "Upgrade 1", "price": 3, "texture": "res://assets/Resized Chess Piece Assets/Black_Pawn.png", "purchased": false},
{"name": "Upgrade 2", "price": 5, "texture": "res://assets/Resized Chess Piece Assets/Black_Rook.png", "purchased": false},
{"name": "Upgrade 3", "price": 5, "texture": "res://assets/Resized Chess Piece Assets/Black_Bishop.png", "purchased": false},
{"name": "Upgrade 4", "price": 5, "texture": "res://assets/Resized Chess Piece Assets/Black_Knight.png", "purchased": false},
{"name": "Upgrade 5", "price": 5, "texture": "res://assets/Resized Chess Piece Assets/Black_Queen.png", "purchased": false}
]
}
......@@ -33,26 +28,28 @@ func _ready():
self.visible = false # Start hidden
print("Shop scene loaded!") # Check if this prints
populate_rows() # Populate the shop rows
update_gold_display(player_gold) # Initialize gold display
func set_visibility(visible: bool):
self.visible = visible
func update_gold_display(gold: int):
player_gold = gold # Store gold locally for shop logic
gold_amount.text = str(gold) # Update the label text
func _on_back_button_pressed():
get_tree().change_scene_to_file("res://Game.tscn") # Return to game
func populate_rows():
for item in shop_items["pieces"]:
add_item_to_row(item, $MainContainer/RowsContainer/PiecesRow)
# Create a copy to avoid modifying the original data
var item_copy = item.duplicate()
add_item_to_row(item_copy, $MainContainer/RowsContainer/PiecesRow)
for item in shop_items["upgrades"]:
add_item_to_row(item, $MainContainer/RowsContainer/UpgradesRow)
var item_copy = item.duplicate()
add_item_to_row(item_copy, $MainContainer/RowsContainer/UpgradesRow)
func add_item_to_row(item_data: Dictionary, row: HBoxContainer):
if item_data["purchased"]:
return # Skip purchased items
var shop_item = shop_item_scene.instantiate()
shop_item.get_node("MarginContainer/VBoxContainer/ItemName").text = item_data["name"]
......@@ -63,21 +60,18 @@ func add_item_to_row(item_data: Dictionary, row: HBoxContainer):
var buy_button = shop_item.get_node("MarginContainer/VBoxContainer/BuyButton")
# Connect the button to a handler, passing the item_data
buy_button.connect("pressed", Callable(self, "_on_item_purchased").bind(item_data))
buy_button.connect("pressed", Callable(self, "_on_buy_button_pressed").bind(item_data))
row.add_child(shop_item)
func _on_item_purchased(item_data: Dictionary):
if player_gold >= item_data["price"]:
player_gold -= item_data["price"]
update_gold_display(player_gold)
emit_signal("gold_spent", item_data["price"]) # Emit the signal
emit_signal("item_purchased", item_data["name"]) # Notify the main game
print("Purchased: ", item_data["name"])
func _on_buy_button_pressed(item_data: Dictionary):
if !item_data["purchased"]:
print("Attempting to Purchase: ", item_data["name"])
# Emit signals to let Game.gd handle the purchase
emit_signal("purchase_attempted", item_data) # Emit the full item data
# TODO: Add a sound effect / Animation for purchasing items
else:
# TODO: Add a sound effect / warning for not enough gold
print("Not enough gold!")
print("Item already purchased!")
......
......@@ -4,6 +4,8 @@ class_name game
var player_gold: int = 10
var shop_scene = preload("res://scenes/Shop.tscn")
var shop: Node2D
var purchased_pieces: Array = []
var purchased_upgrades: Array = []
@export var tile_size = 64
@export var dark = Color(0,0,0,1)
......@@ -48,8 +50,9 @@ func _ready() -> void:
add_child(shop)
shop.position = Vector2(200, 10)
shop.visible = false # Start hidden
shop.gold_spent.connect(_on_gold_spent)
update_gold_display() # Initialize gold display
set_gold_display_position() # Set the anchors and margins for the gold display
shop.purchase_attempted.connect(_on_purchase_attempted)
shop.next_round_requested.connect(_on_next_round_requested)
DisplayServer.window_set_min_size(Vector2(1152, 648)) # Set minimum window size
......@@ -500,34 +503,32 @@ func shoot_projectile(x: int, y: int):
# Shop Stuff
func _on_shop_button_pressed():
print("Attempting to open shop...")
shop.visible = !shop.visible
shop.update_gold_display(player_gold) # Pass current gold to shop
update_gold_display() # Pass current gold to shop
piece_container.visible = !piece_container.visible
shadow_container.visible = !shadow_container.visible
tile_container.visible = !tile_container.visible
print("Shop added to scene tree!") # Check if this prints
# Signal handler method
func _on_item_purchased(item_name: String):
print("Signal received: ", item_name)
match item_name:
"item1":
player_gold -= 1
print("Purchased Item 1! Remaining gold: ", player_gold)
# Add logic to apply the item effect
"item2":
player_gold -= 2
print("Purchased Item 2! Remaining gold: ", player_gold)
func _on_shop_item_purchased(item_name: String) -> void:
print("Signal received: ", item_name)
match item_name:
"item1":
player_gold -= 10
print("Purchased Item 1! Remaining gold: ", player_gold)
func _on_purchase_attempted(item_data: Dictionary):
# Check gold here where we have access to the real value
if player_gold >= item_data["price"]:
# Deduct gold and mark the item as purchased
player_gold -= item_data["price"]
item_data["purchased"] = true
# Track purchased items
if item_data in shop.shop_items["pieces"]:
purchased_pieces.append(item_data)
elif item_data in shop.shop_items["upgrades"]:
purchased_upgrades.append(item_data)
# Update UI and print debug info
update_gold_display()
print("Purchased: ", item_data["name"])
print("Remaining gold: ", player_gold)
else:
# TODO: Play sound / animation for not enough gold
print("Not enough gold!")
func _on_gold_spent(amount: int):
player_gold -= amount
......@@ -543,3 +544,10 @@ func _on_next_round_requested():
piece_container.visible = true # Show the board
shadow_container.visible = true
tile_container.visible = true
func set_gold_display_position():
# Set anchors to a percentage of the parent container's size
gold_display.anchor_left = 0.2
gold_display.anchor_top = 0.0
gold_display.anchor_right = 1.0
gold_display.anchor_bottom = 0.1
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment