Skip to content

Commit

Permalink
Part 18
Browse files Browse the repository at this point in the history
  • Loading branch information
MateuSai committed Feb 3, 2022
1 parent 626b0ae commit a1aa923
Show file tree
Hide file tree
Showing 9 changed files with 136 additions and 13 deletions.
27 changes: 25 additions & 2 deletions Characters/Player/Player.gd
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,18 @@ enum {UP, DOWN}

var current_weapon: Node2D

signal weapon_switched(prev_index, new_index)
signal weapon_picked_up(weapon_texture)
signal weapon_droped(index)

onready var parent: Node2D = get_parent()
onready var weapons: Node2D = get_node("Weapons")
onready var dust_position: Position2D = get_node("DustPosition")


func _ready() -> void:
emit_signal("weapon_picked_up", weapons.get_child(0).get_texture())

_restore_previous_state()


Expand All @@ -22,8 +28,14 @@ func _restore_previous_state() -> void:
weapon.position = Vector2.ZERO
weapons.add_child(weapon)
weapon.hide()

emit_signal("weapon_picked_up", weapon.get_texture())
emit_signal("weapon_switched", weapons.get_child_count() - 2, weapons.get_child_count() - 1)

current_weapon = weapons.get_child(SavedData.equipped_weapon_index)
current_weapon.show()

emit_signal("weapon_switched", weapons.get_child_count() - 1, SavedData.equipped_weapon_index)


func _process(_delta: float) -> void:
Expand Down Expand Up @@ -60,7 +72,8 @@ func get_input() -> void:


func _switch_weapon(direction: int) -> void:
var index: int = current_weapon.get_index()
var prev_index: int = current_weapon.get_index()
var index: int = prev_index
if direction == UP:
index -= 1
if index < 0:
Expand All @@ -75,22 +88,32 @@ func _switch_weapon(direction: int) -> void:
current_weapon.show()
SavedData.equipped_weapon_index = index

emit_signal("weapon_switched", prev_index, index)


func pick_up_weapon(weapon: Node2D) -> void:
SavedData.weapons.append(weapon.duplicate())
SavedData.equipped_weapon_index = weapons.get_child_count()
var prev_index: int = SavedData.equipped_weapon_index
var new_index: int = weapons.get_child_count()
SavedData.equipped_weapon_index = new_index
weapon.get_parent().call_deferred("remove_child", weapon)
weapons.call_deferred("add_child", weapon)
weapon.set_deferred("owner", weapons)
current_weapon.hide()
current_weapon.cancel_attack()
current_weapon = weapon

emit_signal("weapon_picked_up", weapon.get_texture())
emit_signal("weapon_switched", prev_index, new_index)


func _drop_weapon() -> void:
SavedData.weapons.remove(current_weapon.get_index() - 1)
var weapon_to_drop: Node2D = current_weapon
_switch_weapon(UP)

emit_signal("weapon_droped", weapon_to_drop.get_index())

weapons.call_deferred("remove_child", weapon_to_drop)
get_parent().call_deferred("add_child", weapon_to_drop)
weapon_to_drop.set_owner(get_parent())
Expand Down
33 changes: 32 additions & 1 deletion Game.tscn
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[gd_scene load_steps=7 format=2]
[gd_scene load_steps=8 format=2]

[ext_resource path="res://Characters/Player/Player.tscn" type="PackedScene" id=2]
[ext_resource path="res://Game.gd" type="Script" id=3]
Expand All @@ -7,6 +7,13 @@
[ext_resource path="res://Art/progress bar.png" type="Texture" id=6]
[ext_resource path="res://UI.gd" type="Script" id=7]

[sub_resource type="StyleBoxFlat" id=1]
bg_color = Color( 0.713726, 0.670588, 0.670588, 1 )
border_width_left = 2
border_width_top = 2
border_width_right = 2
border_width_bottom = 2

[node name="Game" type="Node2D"]
script = ExtResource( 3 )

Expand All @@ -28,6 +35,27 @@ __meta__ = {

[node name="Tween" type="Tween" parent="UI/HealthBar"]

[node name="PanelContainer" type="PanelContainer" parent="UI"]
anchor_left = 0.5
anchor_top = 1.0
anchor_right = 0.5
anchor_bottom = 1.0
margin_left = -7.0
margin_top = -30.0
margin_right = 7.0
margin_bottom = -16.0
grow_horizontal = 2
custom_styles/panel = SubResource( 1 )
__meta__ = {
"_edit_use_anchors_": false
}

[node name="Inventory" type="HBoxContainer" parent="UI/PanelContainer"]
margin_left = 2.0
margin_top = 2.0
margin_right = 12.0
margin_bottom = 12.0

[node name="Rooms" type="Navigation2D" parent="."]
script = ExtResource( 4 )

Expand All @@ -38,3 +66,6 @@ max_hp = 4
[node name="Camera2D" type="Camera2D" parent="."]

[connection signal="hp_changed" from="Player" to="UI" method="_on_Player_hp_changed"]
[connection signal="weapon_droped" from="Player" to="UI" method="_on_Player_weapon_droped"]
[connection signal="weapon_picked_up" from="Player" to="UI" method="_on_Player_weapon_picked_up"]
[connection signal="weapon_switched" from="Player" to="UI" method="_on_Player_weapon_switched"]
15 changes: 15 additions & 0 deletions InventoryItem.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
extends TextureRect

onready var border: ReferenceRect = get_node("ReferenceRect")


func initialize(texture: Texture) -> void:
self.texture = texture


func select() -> void:
border.show()


func deselect() -> void:
border.hide()
22 changes: 22 additions & 0 deletions InventoryItem.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[gd_scene load_steps=3 format=2]

[ext_resource path="res://Art/Hammer.png" type="Texture" id=1]
[ext_resource path="res://InventoryItem.gd" type="Script" id=2]

[node name="InventoryItem" type="TextureRect"]
margin_right = 40.0
margin_bottom = 40.0
rect_min_size = Vector2( 16, 16 )
texture = ExtResource( 1 )
stretch_mode = 6
script = ExtResource( 2 )
__meta__ = {
"_edit_use_anchors_": false
}

[node name="ReferenceRect" type="ReferenceRect" parent="."]
anchor_right = 1.0
anchor_bottom = 1.0
border_color = Color( 0.45098, 0.396078, 0.396078, 1 )
border_width = 2.0
editor_only = false
1 change: 1 addition & 0 deletions Items/HealthPotion.gd
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ onready var tween: Tween = get_node("Tween")
func _on_HealthPotion_body_entered(player: KinematicBody2D) -> void:
collision_shape.set_deferred("disabled", true)
player.hp += 1
SavedData.hp += 1
var __ = tween.interpolate_property(self, "modulate", Color(1, 1, 1, 1), Color(1, 1, 1, 0), 0.6, Tween.TRANS_SINE, Tween.EASE_IN)
assert(__)
__ = tween.interpolate_property(self, "position", position, position + Vector2.UP * 16, 0.6, Tween.TRANS_SINE, Tween.EASE_IN)
Expand Down
8 changes: 3 additions & 5 deletions Rooms/SpawnRoom0.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,12 @@ position = Vector2( 112, 128 )
position = Vector2( 112, 48 )
shape = SubResource( 1 )

[node name="CollisionPolygon2D" type="CollisionPolygon2D" parent="." index="9"]

[node name="WarHammer" parent="." index="10" instance=ExtResource( 4 )]
[node name="WarHammer" parent="." index="9" instance=ExtResource( 4 )]
position = Vector2( 208, 88 )
on_floor = true

[node name="Torch" parent="." index="11" instance=ExtResource( 3 )]
[node name="Torch" parent="." index="10" instance=ExtResource( 3 )]
position = Vector2( 72, 24 )

[node name="Torch2" parent="." index="12" instance=ExtResource( 3 )]
[node name="Torch2" parent="." index="11" instance=ExtResource( 3 )]
position = Vector2( 168, 24 )
20 changes: 15 additions & 5 deletions Rooms/TileSet.tres
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[gd_resource type="TileSet" load_steps=40 format=2]
[gd_resource type="TileSet" load_steps=41 format=2]

[ext_resource path="res://Art/v1.1 dungeon crawler 16x16 pixel pack/full tilemap.png" type="Texture" id=1]

Expand Down Expand Up @@ -59,16 +59,19 @@ points = PoolVector2Array( 0, 0, 16, 0, 16, 16, 0, 16 )
points = PoolVector2Array( 0, 0, 16, 0, 16, 16, 0, 16 )

[sub_resource type="ConvexPolygonShape2D" id=18]
points = PoolVector2Array( 0, 0, 4.56078, 0, 4.32822, 16, 0, 16 )
points = PoolVector2Array( 0, 0, 16, 0, 16, 16, 0, 16 )

[sub_resource type="ConvexPolygonShape2D" id=19]
points = PoolVector2Array( 0, 0, 16, 0, 16, 16, 0, 16 )

[sub_resource type="ConvexPolygonShape2D" id=20]
points = PoolVector2Array( 0, 0, 16, 0, 16, 16, 0, 16 )

[sub_resource type="ConvexPolygonShape2D" id=39]
points = PoolVector2Array( 0, 0, 16, 0, 16, 16, 0, 16 )

[sub_resource type="ConvexPolygonShape2D" id=21]
points = PoolVector2Array( 12.5153, 0, 16, 0, 16, 16, 11.8176, 16 )
points = PoolVector2Array( 0, 0, 16, 0, 16, 16, 0, 16 )

[sub_resource type="OccluderPolygon2D" id=22]
polygon = PoolVector2Array( 0, 0, 3.2429, 0, 3.63052, 16, 0, 16 )
Expand Down Expand Up @@ -634,9 +637,16 @@ points = PoolVector2Array( 0, 11.6406, 16, 11.8344, 16, 16, 0, 16 )
28/navigation_offset = Vector2( 0, 0 )
28/shape_offset = Vector2( 0, 0 )
28/shape_transform = Transform2D( 1, 0, 0, 1, 0, 0 )
28/shape = SubResource( 39 )
28/shape_one_way = false
28/shape_one_way_margin = 0.0
28/shapes = [ ]
28/shape_one_way_margin = 1.0
28/shapes = [ {
"autotile_coord": Vector2( 0, 0 ),
"one_way": false,
"one_way_margin": 1.0,
"shape": SubResource( 39 ),
"shape_transform": Transform2D( 1, 0, 0, 1, 0, 0 )
} ]
28/z_index = 0
29/name = "full tilemap.png 29"
29/texture = ExtResource( 1 )
Expand Down
19 changes: 19 additions & 0 deletions UI.gd
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
extends CanvasLayer

const INVENTORY_ITEM_SCENE: PackedScene = preload("res://InventoryItem.tscn")

const MIN_HEALTH: int = 23

var max_hp: int = 4
Expand All @@ -8,6 +10,8 @@ onready var player: KinematicBody2D = get_parent().get_node("Player")
onready var health_bar: TextureProgress = get_node("HealthBar")
onready var health_bar_tween: Tween = get_node("HealthBar/Tween")

onready var inventory: HBoxContainer = get_node("PanelContainer/Inventory")


func _ready() -> void:
max_hp = player.max_hp
Expand All @@ -22,3 +26,18 @@ func _update_health_bar(new_value: int) -> void:
func _on_Player_hp_changed(new_hp: int) -> void:
var new_health: int = int((100 - MIN_HEALTH) * float(new_hp) / max_hp) + MIN_HEALTH
_update_health_bar(new_health)


func _on_Player_weapon_switched(prev_index: int, new_index: int) -> void:
inventory.get_child(prev_index).deselect()
inventory.get_child(new_index).select()


func _on_Player_weapon_picked_up(weapon_texture: Texture) -> void:
var new_inventory_item: TextureRect = INVENTORY_ITEM_SCENE.instance()
inventory.add_child(new_inventory_item)
new_inventory_item.initialize(weapon_texture)


func _on_Player_weapon_droped(index: int) -> void:
inventory.get_child(index).queue_free()
4 changes: 4 additions & 0 deletions Weapons/Weapon.gd
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,7 @@ func show() -> void:
func hide() -> void:
ability_icon.hide()
.hide()


func get_texture() -> Texture:
return get_node("Node2D/Sprite").texture

0 comments on commit a1aa923

Please sign in to comment.