Skip to content

Commit

Permalink
COTR-105 Implemented shop functionality (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
disusen authored May 15, 2023
1 parent daefb47 commit afba046
Show file tree
Hide file tree
Showing 21 changed files with 721 additions and 190 deletions.
230 changes: 115 additions & 115 deletions Scenes/Actors/Player/Player.tscn

Large diffs are not rendered by default.

41 changes: 22 additions & 19 deletions Scenes/Actors/Player/WeaponsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ public partial class WeaponsManager : Node2D

public override void _Ready()
{
CurrentWeapon = GetNode<Weapon>("LongSword");

int weaponAmmount = 2;
int weaponAmmount = 3;
weapons = GetChildren()
.OfType<Weapon>()
.Take(weaponAmmount)
Expand All @@ -34,8 +32,6 @@ public override void _Ready()
return weapon;
})
.ToArray();

CurrentWeapon.Show();
}

/// <summary>
Expand All @@ -59,17 +55,28 @@ public void Initialize(Team.Teams setTeam, Weapon weapon)
/// <summary>
/// Add weapon to currently carrying weapon list
/// </summary>
/// <param name="index">Index to put in to</param>
public void AddWeapon(Weapon weapon, int index)
/// <param name="weapon">Weapon to be added</param>
/// <param name="index">Index to which weapon should be added to</param>
/// <returns>True if weapon added succesfully, false otherwise</returns>
public bool AddWeapon(Weapon weapon, int index)
{
if (this.GetChildCount() > 2)
{
return false;
}
if (index >= weapons.Length)
{
Array.Resize(ref weapons, index + 1);
}
if (weapons[index] != null)
{
weapons[index].QueueFree();

}
AddChild(weapon);
weapons[index] = weapon;
weapon.Hide();

EmitSignal(nameof(WeaponChanged), index, weapons[index]);
return true;
}

/// <summary>
Expand All @@ -84,18 +91,14 @@ public Weapon GetCurrentWeapon()
/// <summary>
/// Change the current weapon the user is holding
/// </summary>
public void ChangeWeapon()
/// <param name="indexToChangeTo">Index of the weapon to switch to</param>
public void ChangeWeapon(int indexToChangeTo)
{
int index = System.Array.IndexOf(weapons, CurrentWeapon);
for (int i = index; i < weapons.Length - 1; i++)
if (weapons[indexToChangeTo] != null)
{
if (weapons[i + 1] != null)
{
SwitchWeapon(weapons[i + 1]);
return;
}
SwitchWeapon(weapons[indexToChangeTo]);
return;
}
SwitchWeapon(weapons[0]);
}

/// <summary>
Expand All @@ -108,8 +111,8 @@ public void SwitchWeapon(Weapon weapon)
// Should cancel weapon attack
CurrentWeapon.Hide();
weapon.Show();
weapon.Idle();
CurrentWeapon = weapon;
EmitSignal(nameof(WeaponChanged), CurrentWeapon);
}

/// <summary>
Expand Down
10 changes: 8 additions & 2 deletions Scenes/Actors/Troops/Infantry/Weapon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,19 @@ public partial class Weapon : Node2D
/// Amount of damage weapon can inflict
/// </summary>
/// <value>float damage value</value>
[Export] protected float damage { get; set; }
[Export] public float Damage { get; set; }

/// <summary>
/// Amount of knockback weapon can inflict
/// </summary>
/// <value>float knockback value</value>
[Export] protected float knockback { get; set; }
[Export] public float Knockback { get; set; }

/// <summary>
/// The price of the weapon in the shop
/// </summary>
/// <value>float knockback value</value>
[Export] public float Price { get; set; }

/// <summary>
/// Current Weapon icon
Expand Down
8 changes: 4 additions & 4 deletions Scenes/Maps/Main/Main.tscn
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
[gd_scene load_steps=32 format=3 uid="uid://cumnukgfcos74"]

[ext_resource type="Script" path="res://Scenes/Maps/Main/Map.cs" id="1_urlge"]
[ext_resource type="Texture2D" uid="uid://8m5j2nlj5q7l" path="res://Sprites/Maps/Environment/Tilesets/Outside2.png" id="3_rmuyw"]
[ext_resource type="Texture2D" uid="uid://tlqq7sc36uo6" path="res://Sprites/Maps/Environment/Tilesets/Outside1.png" id="4_iae4g"]
[ext_resource type="Texture2D" uid="uid://bayf80a1slbrn" path="res://Sprites/Maps/Environment/Tilesets/Outside4.png" id="5_0sqop"]
[ext_resource type="Texture2D" uid="uid://c8fp0mtc7qga8" path="res://Sprites/Maps/Environment/Tilesets/Outside2.png" id="3_rmuyw"]
[ext_resource type="Texture2D" uid="uid://cf25ejek351mo" path="res://Sprites/Maps/Environment/Tilesets/Outside1.png" id="4_iae4g"]
[ext_resource type="Texture2D" uid="uid://dksp53cdhxgq3" path="res://Sprites/Maps/Environment/Tilesets/Outside4.png" id="5_0sqop"]
[ext_resource type="PackedScene" uid="uid://cxrn3bt2jbpg0" path="res://Scenes/UI/HUD/HUD.tscn" id="5_5ce41"]
[ext_resource type="Texture2D" uid="uid://b76hwjggkir27" path="res://Sprites/Maps/Environment/Tilesets/LiquidMerged.png" id="5_ipjvp"]
[ext_resource type="Texture2D" uid="uid://df254l3brmsks" path="res://Sprites/Maps/Environment/Tilesets/LiquidMerged.png" id="5_ipjvp"]
[ext_resource type="PackedScene" uid="uid://7efuq2vnsuig" path="res://Scenes/Utility/EnemySpawner.tscn" id="7_i7fgq"]
[ext_resource type="PackedScene" uid="uid://b4ilybs3djksi" path="res://Scenes/Utility/Loot/LootManager.tscn" id="7_tdyjp"]
[ext_resource type="AudioStream" uid="uid://id8036uqggyh" path="res://Sounds/Music/InActionMusic.mp3" id="8_60mcr"]
Expand Down
14 changes: 12 additions & 2 deletions Scenes/UI/HUD/GUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace ChampionsOfTheRealm;
/// </summary>
public partial class GUI : Control
{
private Player player;
private ProgressBar healthBar;
private ProgressBar healthBarUnder;
private Label currencyLabel;
Expand Down Expand Up @@ -80,6 +81,7 @@ public void Initialize(Player player)
{
ChangeItem(i, weaponsArray[i]);
}
this.player = player;
}

/// <summary>
Expand Down Expand Up @@ -186,13 +188,21 @@ private void ChangeItem(int index, Weapon weapon = null)
{
itemArray[index].Icon = null;
}

itemArray[index].Disabled = false;
itemArray[index].FocusMode = Godot.Control.FocusModeEnum.All;
itemArray[index].Icon = weapon.Icon;
}

/// <summary>
/// Switch item in index
/// </summary>
/// <param name="index">Index to switch from</param>
private void SwitchItem(int index) => currentItemIndex = index;
private void SwitchItem(int index)
{
if (index < player.WeaponsManager.GetChildCount())
{
currentItemIndex = index;
player.WeaponsManager.ChangeWeapon(currentItemIndex);
}
}
}
14 changes: 12 additions & 2 deletions Scenes/UI/HUD/HUD.tscn
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[gd_scene load_steps=9 format=3 uid="uid://cxrn3bt2jbpg0"]

[ext_resource type="Script" path="res://Scenes/UI/HUD/GUI.cs" id="1_ujh52"]
[ext_resource type="FontFile" uid="uid://dueesb3m2y77e" path="res://Fonts/Arvo-Regular.ttf" id="2_0m84m"]
[ext_resource type="Texture2D" uid="uid://dswbo25622gh1" path="res://Sprites/Loot/Coins/GoldenCoin.png" id="2_s8hb3"]
[ext_resource type="FontFile" uid="uid://n3rgq5lbx0xe" path="res://Fonts/Arvo-Regular.ttf" id="2_0m84m"]
[ext_resource type="Texture2D" uid="uid://02lxn08mvhbb" path="res://Sprites/Loot/Coins/GoldenCoin.png" id="2_s8hb3"]
[ext_resource type="PackedScene" uid="uid://dj2jplbqn401k" path="res://Scenes/UI/HUD/IntentoryItems.tscn" id="2_yhl6b"]

[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_not3w"]
Expand Down Expand Up @@ -183,20 +183,26 @@ theme_override_constants/separation = 25
[node name="IntentoryItems" parent="HUD/MarginContainer/Rows/BottomRow/InventoryContainer/MarginContainer/VBoxContainer" instance=ExtResource("2_yhl6b")]
custom_minimum_size = Vector2(50, 50)
layout_mode = 2
focus_mode = 0
disabled = true
keep_pressed_outside = true
icon = null
expand_icon = true

[node name="IntentoryItems2" parent="HUD/MarginContainer/Rows/BottomRow/InventoryContainer/MarginContainer/VBoxContainer" instance=ExtResource("2_yhl6b")]
custom_minimum_size = Vector2(50, 50)
layout_mode = 2
focus_mode = 0
disabled = true
keep_pressed_outside = true
icon = null
expand_icon = true

[node name="IntentoryItems3" parent="HUD/MarginContainer/Rows/BottomRow/InventoryContainer/MarginContainer/VBoxContainer" instance=ExtResource("2_yhl6b")]
custom_minimum_size = Vector2(50, 50)
layout_mode = 2
focus_mode = 0
disabled = true
keep_pressed_outside = true
icon = null
expand_icon = true
Expand Down Expand Up @@ -226,3 +232,7 @@ theme_override_colors/font_shadow_color = Color(0, 0, 0, 1)
theme_override_fonts/font = ExtResource("2_0m84m")
theme_override_font_sizes/font_size = 25
text = "30"

[connection signal="pressed" from="HUD/MarginContainer/Rows/BottomRow/InventoryContainer/MarginContainer/VBoxContainer/IntentoryItems" to="." method="SwitchItem" binds= [0]]
[connection signal="pressed" from="HUD/MarginContainer/Rows/BottomRow/InventoryContainer/MarginContainer/VBoxContainer/IntentoryItems2" to="." method="SwitchItem" binds= [1]]
[connection signal="pressed" from="HUD/MarginContainer/Rows/BottomRow/InventoryContainer/MarginContainer/VBoxContainer/IntentoryItems3" to="." method="SwitchItem" binds= [2]]
2 changes: 1 addition & 1 deletion Scenes/UI/HUD/IntentoryItems.tscn
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[gd_scene load_steps=2 format=3 uid="uid://dj2jplbqn401k"]

[ext_resource type="Texture2D" uid="uid://03dlj425qsyb" path="res://Sprites/Weapons/DefaultSword/Icon/DefaultSwordIcon.png" id="1_kg3kc"]
[ext_resource type="Texture2D" uid="uid://ce5e6mtjqyn8q" path="res://Sprites/Weapons/DefaultSword/Icon/DefaultSwordIcon.png" id="1_kg3kc"]

[node name="IntentoryItems" type="Button"]
icon = ExtResource("1_kg3kc")
Expand Down
6 changes: 3 additions & 3 deletions Scenes/UI/Menus/Main/MainMenu.tscn
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
[gd_scene load_steps=6 format=3 uid="uid://iq8ol3bmxqrn"]

[ext_resource type="Script" path="res://Scenes/UI/Menus/Main/MainMenu.cs" id="1_dfesn"]
[ext_resource type="Texture2D" uid="uid://cjdya5edatej1" path="res://Sprites/UI/MainMenu/Menus_Detailed.png" id="2_3hnfr"]
[ext_resource type="FontFile" uid="uid://dv08u1jfpleu1" path="res://Fonts/BreeSerif-Regular.ttf" id="3_4go22"]
[ext_resource type="Texture2D" uid="uid://bv1xeghiers11" path="res://Sprites/UI/MainMenu/Menus_Detailed.png" id="2_3hnfr"]
[ext_resource type="FontFile" uid="uid://t4g5nmqihapq" path="res://Fonts/BreeSerif-Regular.ttf" id="3_4go22"]
[ext_resource type="AudioStream" uid="uid://6h5peo4tccka" path="res://Sounds/Music/MainMenuMusic.mp3" id="3_dqi8g"]
[ext_resource type="FontFile" uid="uid://djd1dtiawjj4s" path="res://Fonts/Arvo-Bold.ttf" id="3_m611j"]
[ext_resource type="FontFile" uid="uid://brryd65wgxriy" path="res://Fonts/Arvo-Bold.ttf" id="3_m611j"]

[node name="MainMenu" type="Control"]
process_mode = 3
Expand Down
4 changes: 2 additions & 2 deletions Scenes/UI/Menus/Settings/Settings.tscn
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[gd_scene load_steps=4 format=3 uid="uid://chq6j643dyo63"]

[ext_resource type="Script" path="res://Scenes/UI/Menus/Settings/Settings.cs" id="1_xaj1n"]
[ext_resource type="FontFile" uid="uid://djd1dtiawjj4s" path="res://Fonts/Arvo-Bold.ttf" id="2_4ylps"]
[ext_resource type="FontFile" uid="uid://dueesb3m2y77e" path="res://Fonts/Arvo-Regular.ttf" id="3_f1rxe"]
[ext_resource type="FontFile" uid="uid://brryd65wgxriy" path="res://Fonts/Arvo-Bold.ttf" id="2_4ylps"]
[ext_resource type="FontFile" uid="uid://n3rgq5lbx0xe" path="res://Fonts/Arvo-Regular.ttf" id="3_f1rxe"]

[node name="Settings" type="Control"]
layout_mode = 3
Expand Down
20 changes: 14 additions & 6 deletions Scenes/UI/Menus/Shop/Shop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,30 @@ public partial class Shop : CanvasLayer
[Export] private Godot.Collections.Array<PackedScene> weapons; // Weapons array
private Globals globals; // global variables and functionality
private Player player; // Player character
private TabBar weaponTab; // Weapon type tab
private HBoxContainer weaponTab; // Weapon type tab
private Label currencyLabel; // currency label
private Label errorLabel; // shows errors to the user related to purchasing weapons.
private PackedScene weaponSlot = ResourceLoader.Load<PackedScene>("res://Scenes/UI/Menus/Shop/WeaponSlot.tscn"); // Weapon slot resource

public override void _Ready()
{
globals = GetNode<Globals>("/root/Globals");
weaponTab = GetNode<TabBar>("CenterContainer/PanelContainer/MarginContainer/VBoxContainer/TabContainer/Melee"); // loading melee weapons tab
InitializeWeapons(); // loading weapons into their slots.
weaponTab = GetNode<HBoxContainer>("CenterContainer/PanelContainer/MarginContainer/VBoxContainer/TabContainer/Melee/HBoxContainer"); // loading melee weapons tab
currencyLabel = GetNode<Label>("CenterContainer/PanelContainer/MarginContainer/VBoxContainer/CurrenctContainer/CurrencyLabel"); // loading currency label
errorLabel = GetNode<Label>("CenterContainer/PanelContainer/MarginContainer/VBoxContainer/ErrorLabel");
GetTree().Paused = true;
}

/// <summary>
/// Initializes the shop with the specified player
/// </summary>
/// <param name="p">The player character</param>
public void Initialize(Player p) => this.player = p;
/// <param name="player">Player</param>
public void Initialize(Player player)
{
this.player = player;
currencyLabel.Text = player.Stats.Gold.ToString();
InitializeWeapons();
}

/// <summary>
/// Initializes the weapons in the shop by loading them into slots
Expand All @@ -33,7 +41,7 @@ public void InitializeWeapons()
{
Weapon weapon = weaponScene.Instantiate<Weapon>();
WeaponSlot slot = weaponSlot.Instantiate<WeaponSlot>();
slot.Initialize(weapon);
slot.Initialize(weapon, player, currencyLabel, errorLabel);
weaponTab.AddChild(slot);
}
}
Expand Down
32 changes: 25 additions & 7 deletions Scenes/UI/Menus/Shop/Shop.tscn
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
[gd_scene load_steps=8 format=3 uid="uid://syfmsjeuejk6"]
[gd_scene load_steps=10 format=3 uid="uid://cugjv62038ee8"]

[ext_resource type="Script" path="res://Scenes/UI/Menus/Shop/Shop.cs" id="1_6wyba"]
[ext_resource type="PackedScene" uid="uid://n7p6gc4n6gwd" path="res://Scenes/Weapons/Melee/Melee.tscn" id="2_i1utb"]
[ext_resource type="Texture2D" uid="uid://dswbo25622gh1" path="res://Sprites/Loot/Coins/GoldenCoin.png" id="2_xuusi"]
[ext_resource type="FontFile" uid="uid://djd1dtiawjj4s" path="res://Fonts/Arvo-Bold.ttf" id="3_yjlo8"]
[ext_resource type="FontFile" uid="uid://dueesb3m2y77e" path="res://Fonts/Arvo-Regular.ttf" id="5_cjsy3"]
[ext_resource type="PackedScene" uid="uid://hlqb6s3l2ahs" path="res://Scenes/Weapons/Melee/LongSword2.tscn" id="2_um57g"]
[ext_resource type="Texture2D" uid="uid://02lxn08mvhbb" path="res://Sprites/Loot/Coins/GoldenCoin.png" id="2_xuusi"]
[ext_resource type="FontFile" uid="uid://brryd65wgxriy" path="res://Fonts/Arvo-Bold.ttf" id="3_yjlo8"]
[ext_resource type="PackedScene" uid="uid://c3d0ejgm6sa8p" path="res://Scenes/Weapons/Melee/LongSword4.tscn" id="4_24ya0"]
[ext_resource type="PackedScene" uid="uid://j6njggmbp8bb" path="res://Scenes/Weapons/Melee/LongSword3.tscn" id="4_wenwh"]
[ext_resource type="FontFile" uid="uid://n3rgq5lbx0xe" path="res://Fonts/Arvo-Regular.ttf" id="5_cjsy3"]

[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_d3ob0"]
bg_color = Color(0, 0, 0, 0.796078)
Expand All @@ -15,7 +17,7 @@ content_margin_left = 20.0
[node name="Shop" type="CanvasLayer"]
process_mode = 3
script = ExtResource("1_6wyba")
weapons = Array[PackedScene]([ExtResource("2_i1utb")])
weapons = Array[PackedScene]([ExtResource("2_um57g"), ExtResource("4_wenwh"), ExtResource("4_24ya0")])

[node name="CenterContainer" type="CenterContainer" parent="."]
anchors_preset = 15
Expand All @@ -25,7 +27,7 @@ grow_horizontal = 2
grow_vertical = 2

[node name="PanelContainer" type="PanelContainer" parent="CenterContainer"]
custom_minimum_size = Vector2(1024, 640)
custom_minimum_size = Vector2(640, 360)
layout_mode = 2
theme_override_styles/panel = SubResource("StyleBoxFlat_d3ob0")

Expand Down Expand Up @@ -89,6 +91,22 @@ process_mode = 3
layout_mode = 2
theme_override_fonts/font = ExtResource("5_cjsy3")

[node name="HBoxContainer" type="HBoxContainer" parent="CenterContainer/PanelContainer/MarginContainer/VBoxContainer/TabContainer/Melee"]
layout_mode = 1
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2

[node name="ErrorLabel" type="Label" parent="CenterContainer/PanelContainer/MarginContainer/VBoxContainer"]
visible = false
layout_mode = 2
size_flags_horizontal = 4
theme_override_colors/font_color = Color(1, 0, 0, 1)
theme_override_fonts/font = ExtResource("3_yjlo8")
text = "error"

[node name="ButtonsContainer" type="HBoxContainer" parent="CenterContainer/PanelContainer/MarginContainer/VBoxContainer"]
layout_mode = 2
size_flags_horizontal = 4
Expand Down
Loading

0 comments on commit afba046

Please sign in to comment.