Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions project/autoloads/player_inventory.gd
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,49 @@ func remove_item(item: Item, count: int) -> void:
if inventory[item] <= 0:
inventory.erase(item)
Signals.inventory_updated.emit()

func can_drag_item(item: Item) -> bool:
return item in inventory

func can_drop_item(pos: Vector2, item: Item):
var inventory_slots = get_tree().get_nodes_in_group("inventory_slots")

for slot in inventory_slots:
if slot.get_global_rect().has_point(pos):
return false
var weapon_slot = get_tree().get_nodes_in_group("weapon_slot")[0]
if item is Weapon and weapon_slot.get_global_rect().has_point(pos):
return true
if item is Upgrade:
var slot
match item.type:
Upgrade.Type.LEGS:
slot = get_tree().get_nodes_in_group("legs_slot")[0]
Upgrade.Type.ARMS:
slot = get_tree().get_nodes_in_group("arm_slot")[0]
Upgrade.Type.HEAD:
slot = get_tree().get_nodes_in_group("head_slot")[0]
Upgrade.Type.TORSO:
slot = get_tree().get_nodes_in_group("torso_slot")[0]
return slot.get_global_rect().has_point(pos)
return false

func equip_item(item: Item) -> void:
if item is Weapon:
equipped_weapon = item
else:
match item.type:
Upgrade.Type.LEGS:
equipped_legs = item
Upgrade.Type.ARMS:
equipped_arm = item
Upgrade.Type.HEAD:
equipped_head = item
Upgrade.Type.TORSO:
equipped_torso = item
Weapon.Type:
equipped_weapon = item
Signals.inventory_updated.emit()



3 changes: 2 additions & 1 deletion project/modules/enemies/enemy.gd
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ extends CharacterBody2D

# health
@export var max_health := 100
var health := max_health
var health: int

# Wander settings
@export var move_speed := 150.0
Expand Down Expand Up @@ -49,6 +49,7 @@ var state := State.WANDER
## --- built in methods ---

func _ready():
health = max_health
spawn_position = global_position
prev_pos = global_position
pick_new_wander_target()
Expand Down
4 changes: 2 additions & 2 deletions project/modules/hud/weapon_hotbar.gd
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func _ready() -> void:
func populate_hotbar() -> void:
var inventory = PlayerInventory.inventory.keys()
for idx in min(len(inventory), len(weapons_hotbar_list.get_children())):
weapons_hotbar_list.get_child(idx).get_node("SlotImage").texture = inventory[idx].get_2d_texture()
weapons_hotbar_list.get_child(idx).item = inventory[idx]
select_item(0)

# Hightlight the currentlty selected item by showing a frame around it
Expand All @@ -22,4 +22,4 @@ func select_item(idx: int) -> void:

# Change out an item on certain idx
func change_item_on_idx(idx: int, new_weapon: Weapon) -> void:
weapons_hotbar_list.get_child(idx).get_node("SlotImage").texture = new_weapon.icon
weapons_hotbar_list.get_child(idx).item = new_weapon
18 changes: 9 additions & 9 deletions project/modules/hud/weapon_hotbar.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -31,29 +31,29 @@ anchor_bottom = 0.5
grow_horizontal = 2
grow_vertical = 2

[node name="WeaponSlot" parent="HBoxContainer" instance=ExtResource("2_1ysuv")]
[node name="WeaponSlot" parent="HBoxContainer" groups=["inventory_slots"] instance=ExtResource("2_1ysuv")]
layout_mode = 2

[node name="WeaponSlot2" parent="HBoxContainer" instance=ExtResource("2_1ysuv")]
[node name="WeaponSlot2" parent="HBoxContainer" groups=["inventory_slots"] instance=ExtResource("2_1ysuv")]
layout_mode = 2

[node name="WeaponSlot3" parent="HBoxContainer" instance=ExtResource("2_1ysuv")]
[node name="WeaponSlot3" parent="HBoxContainer" groups=["inventory_slots"] instance=ExtResource("2_1ysuv")]
layout_mode = 2

[node name="WeaponSlot4" parent="HBoxContainer" instance=ExtResource("2_1ysuv")]
[node name="WeaponSlot4" parent="HBoxContainer" groups=["inventory_slots"] instance=ExtResource("2_1ysuv")]
layout_mode = 2

[node name="WeaponSlot5" parent="HBoxContainer" instance=ExtResource("2_1ysuv")]
[node name="WeaponSlot5" parent="HBoxContainer" groups=["inventory_slots"] instance=ExtResource("2_1ysuv")]
layout_mode = 2

[node name="WeaponSlot6" parent="HBoxContainer" instance=ExtResource("2_1ysuv")]
[node name="WeaponSlot6" parent="HBoxContainer" groups=["inventory_slots"] instance=ExtResource("2_1ysuv")]
layout_mode = 2

[node name="WeaponSlot7" parent="HBoxContainer" instance=ExtResource("2_1ysuv")]
[node name="WeaponSlot7" parent="HBoxContainer" groups=["inventory_slots"] instance=ExtResource("2_1ysuv")]
layout_mode = 2

[node name="WeaponSlot8" parent="HBoxContainer" instance=ExtResource("2_1ysuv")]
[node name="WeaponSlot8" parent="HBoxContainer" groups=["inventory_slots"] instance=ExtResource("2_1ysuv")]
layout_mode = 2

[node name="WeaponSlot9" parent="HBoxContainer" instance=ExtResource("2_1ysuv")]
[node name="WeaponSlot9" parent="HBoxContainer" groups=["inventory_slots"] instance=ExtResource("2_1ysuv")]
layout_mode = 2
34 changes: 34 additions & 0 deletions project/modules/hud/weapon_slot.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class_name WeaponSlot
extends Control

@onready var WeaponSlotScene = preload("res://modules/hud/weapon_slot.tscn")

var item: Item:
set(new_item):
item = new_item
if is_node_ready():
$SlotImage.texture = item.get_2d_texture()

func _ready() -> void:
if item:
$SlotImage.texture = item.get_2d_texture()

func _get_drag_data(_pos):
if not PlayerInventory.can_drag_item(item):
return
var weapon_slot = WeaponSlotScene.instantiate()
weapon_slot.item = item
weapon_slot.size = size
set_drag_preview(weapon_slot)
return {
"item": item.duplicate(),
"source_item": item,
}

func _can_drop_data(pos, data):
var global_pos = pos + global_position
return PlayerInventory.can_drop_item(global_pos, data["item"])

func _drop_data(_pos, data):
PlayerInventory.equip_item(data["item"])
PlayerInventory.remove_item(data["source_item"], 1)
1 change: 1 addition & 0 deletions project/modules/hud/weapon_slot.gd.uid
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
uid://bwx7gaqe3rnkp
12 changes: 7 additions & 5 deletions project/modules/hud/weapon_slot.tscn
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[gd_scene load_steps=3 format=3 uid="uid://dxom55xj78atn"]
[gd_scene load_steps=4 format=3 uid="uid://dxom55xj78atn"]

[ext_resource type="Script" uid="uid://bwx7gaqe3rnkp" path="res://modules/hud/weapon_slot.gd" id="1_c0gla"]
[ext_resource type="Texture2D" uid="uid://d3ack7vxyxp8c" path="res://assets/ui/inventoryslotempty.png" id="1_i31un"]
[ext_resource type="Texture2D" uid="uid://by414cje0yw6j" path="res://assets/hotbar_slot_frame.png" id="1_s2vfi"]

Expand All @@ -9,6 +10,7 @@ layout_mode = 3
anchors_preset = 0
size_flags_horizontal = 4
size_flags_vertical = 4
script = ExtResource("1_c0gla")

[node name="BaseImage" type="TextureRect" parent="."]
layout_mode = 0
Expand All @@ -17,20 +19,20 @@ offset_bottom = 64.0
texture = ExtResource("1_i31un")
expand_mode = 1

[node name="SlotImage" type="TextureRect" parent="."]
[node name="SlotFrame" type="TextureRect" parent="."]
visible = false
layout_mode = 1
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
texture = ExtResource("1_s2vfi")

[node name="SlotFrame" type="TextureRect" parent="."]
visible = false
[node name="SlotImage" type="TextureRect" parent="."]
layout_mode = 1
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
texture = ExtResource("1_s2vfi")
7 changes: 4 additions & 3 deletions project/modules/items/item.gd
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ func randomize():
current_icon_variant = randi_range(1, icon_variants)

func get_2d_texture() -> AtlasTexture:
var new_icon := icon.duplicate()
var icon_variant = current_icon_variant
if fixed_icon_variant:
icon_variant = fixed_icon_variant
icon.region.position.x = icon.region.size.x * ((icon_variant - 1) % icon_variants_x)
icon.region.position.y = icon.region.size.y * ((icon_variant - 1) % icon_variants_y)
return icon
new_icon.region.position.x = icon.region.size.x * ((icon_variant - 1) % icon_variants_x)
new_icon.region.position.y = icon.region.size.y * ((icon_variant - 1) % icon_variants_y)
return new_icon

10 changes: 5 additions & 5 deletions project/modules/player/player_doll.gd
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ func _input(event: InputEvent) -> void:

func _update_icons() -> void:
if PlayerInventory.equipped_arm:
$Arm.get_node("SlotImage").texture = PlayerInventory.equipped_arm.get_2d_texture()
$Arm.item = PlayerInventory.equipped_arm
if PlayerInventory.equipped_weapon:
$Weapon.get_node("SlotImage").texture = PlayerInventory.equipped_weapon.get_2d_texture()
$Weapon.item = PlayerInventory.equipped_weapon
if PlayerInventory.equipped_head:
$Head.get_node("SlotImage").texture = PlayerInventory.equipped_head.get_2d_texture()
$Head.item = PlayerInventory.equipped_head
if PlayerInventory.equipped_legs:
$Legs.get_node("SlotImage").texture = PlayerInventory.equipped_legs.get_2d_texture()
$Legs.item = PlayerInventory.equipped_legs
if PlayerInventory.equipped_torso:
$Torso.get_node("SlotImage").texture = PlayerInventory.equipped_torso.get_2d_texture()
$Torso.item = PlayerInventory.equipped_torso
10 changes: 5 additions & 5 deletions project/modules/player/player_doll.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -28,31 +28,31 @@ offset_right = 260.0
offset_bottom = 31.0
text = "Generate new android inventory"

[node name="Weapon" parent="." instance=ExtResource("3_qjnmt")]
[node name="Weapon" parent="." groups=["weapon_slot"] instance=ExtResource("3_qjnmt")]
offset_left = 128.0
offset_top = 512.0
offset_right = 192.0
offset_bottom = 576.0

[node name="Legs" parent="." instance=ExtResource("3_qjnmt")]
[node name="Legs" parent="." groups=["legs_slot"] instance=ExtResource("3_qjnmt")]
offset_left = 128.0
offset_top = 640.0
offset_right = 192.0
offset_bottom = 704.0

[node name="Head" parent="." instance=ExtResource("3_qjnmt")]
[node name="Head" parent="." groups=["head_slot"] instance=ExtResource("3_qjnmt")]
offset_left = 128.0
offset_top = 128.0
offset_right = 192.0
offset_bottom = 192.0

[node name="Torso" parent="." instance=ExtResource("3_qjnmt")]
[node name="Torso" parent="." groups=["torso_slot"] instance=ExtResource("3_qjnmt")]
offset_left = 128.0
offset_top = 256.0
offset_right = 192.0
offset_bottom = 320.0

[node name="Arm" parent="." instance=ExtResource("3_qjnmt")]
[node name="Arm" parent="." groups=["arm_slot"] instance=ExtResource("3_qjnmt")]
offset_left = 128.0
offset_top = 384.0
offset_right = 192.0
Expand Down
6 changes: 6 additions & 0 deletions project/project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ enabled=PackedStringArray("res://addons/version_check/plugin.cfg")

enemies="Enemies."
player="Player."
inventory_slots=""
weapon_slot=""
legs_slot=""
head_slot=""
torso_slot=""
arm_slot=""

[gui]

Expand Down