Skip to content

Commit

Permalink
Create attack selector
Browse files Browse the repository at this point in the history
  • Loading branch information
outfrost committed Apr 27, 2021
1 parent 0b4720d commit 4abcf06
Show file tree
Hide file tree
Showing 4 changed files with 254 additions and 31 deletions.
129 changes: 124 additions & 5 deletions character/PlayerCharacter.gd
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,41 @@ export var wind_attack_dmg: float = 0.00
var walk_sound_timer: float = 0.0
var playing_action: bool = false

onready var attack_selector = $CanvasLayer/AttackSelector
onready var attack_icon = $CanvasLayer/AttackSelector/SelectorRect/Icon

const ATTACK_MAP: Dictionary = {
Item.ItemType.NONE: {
basic_attack_dmg = 40.0,
fire_attack_dmg = 0.0,
ice_attack_dmg = 0.0,
wind_attack_dmg = 0.0,
},
Item.ItemType.BOTTLE_RED: {
basic_attack_dmg = 40.0,
fire_attack_dmg = 40.0,
ice_attack_dmg = 0.0,
wind_attack_dmg = 0.0,
},
Item.ItemType.BOTTLE_BLUE: {
basic_attack_dmg = 40.0,
fire_attack_dmg = 0.0,
ice_attack_dmg = 40.0,
wind_attack_dmg = 0.0,
},
Item.ItemType.BOTTLE_YELLOW: {
basic_attack_dmg = 40.0,
fire_attack_dmg = 0.0,
ice_attack_dmg = 0.0,
wind_attack_dmg = 40.0,
},
}

var current_attack_mod = Item.ItemType.NONE

func _ready() -> void:
$AnimatedSprite.playing = true
update_selector()

func _physics_process(delta: float) -> void:
walk_sound_timer += delta
Expand Down Expand Up @@ -125,6 +158,78 @@ func _input(event: InputEvent) -> void:
true)
if collision && collision.collider.has_method("take_damage"):
hit(collision.collider)
elif event.is_action_pressed("next_attack_mod"):
get_tree().set_input_as_handled()
var available_mods = []
for mod in ATTACK_MAP:
if mod == Item.ItemType.NONE:
continue
if inventory.has(mod) && inventory[mod] > 0:
available_mods.append(mod)
if available_mods.size() == 0:
current_attack_mod = Item.ItemType.NONE
else:
match(current_attack_mod):
Item.ItemType.NONE:
current_attack_mod = Item.ItemType.BOTTLE_RED
Item.ItemType.BOTTLE_RED:
current_attack_mod = Item.ItemType.BOTTLE_BLUE
Item.ItemType.BOTTLE_BLUE:
current_attack_mod = Item.ItemType.BOTTLE_YELLOW
Item.ItemType.BOTTLE_YELLOW:
current_attack_mod = Item.ItemType.BOTTLE_RED
while !(current_attack_mod in available_mods):
match(current_attack_mod):
Item.ItemType.NONE:
current_attack_mod = Item.ItemType.BOTTLE_RED
Item.ItemType.BOTTLE_RED:
current_attack_mod = Item.ItemType.BOTTLE_BLUE
Item.ItemType.BOTTLE_BLUE:
current_attack_mod = Item.ItemType.BOTTLE_YELLOW
Item.ItemType.BOTTLE_YELLOW:
current_attack_mod = Item.ItemType.BOTTLE_RED

basic_attack_dmg = ATTACK_MAP[current_attack_mod].basic_attack_dmg
fire_attack_dmg = ATTACK_MAP[current_attack_mod].fire_attack_dmg
ice_attack_dmg = ATTACK_MAP[current_attack_mod].ice_attack_dmg
wind_attack_dmg = ATTACK_MAP[current_attack_mod].wind_attack_dmg
update_selector()
elif event.is_action_pressed("prev_attack_mod"):
get_tree().set_input_as_handled()
var available_mods = []
for mod in ATTACK_MAP:
if mod == Item.ItemType.NONE:
continue
if inventory.has(mod) && inventory[mod] > 0:
available_mods.append(mod)
if available_mods.size() == 0:
current_attack_mod = Item.ItemType.NONE
else:
match(current_attack_mod):
Item.ItemType.NONE:
current_attack_mod = Item.ItemType.BOTTLE_YELLOW
Item.ItemType.BOTTLE_RED:
current_attack_mod = Item.ItemType.BOTTLE_YELLOW
Item.ItemType.BOTTLE_BLUE:
current_attack_mod = Item.ItemType.BOTTLE_RED
Item.ItemType.BOTTLE_YELLOW:
current_attack_mod = Item.ItemType.BOTTLE_BLUE
while !(current_attack_mod in available_mods):
match(current_attack_mod):
Item.ItemType.NONE:
current_attack_mod = Item.ItemType.BOTTLE_YELLOW
Item.ItemType.BOTTLE_RED:
current_attack_mod = Item.ItemType.BOTTLE_YELLOW
Item.ItemType.BOTTLE_BLUE:
current_attack_mod = Item.ItemType.BOTTLE_RED
Item.ItemType.BOTTLE_YELLOW:
current_attack_mod = Item.ItemType.BOTTLE_BLUE

basic_attack_dmg = ATTACK_MAP[current_attack_mod].basic_attack_dmg
fire_attack_dmg = ATTACK_MAP[current_attack_mod].fire_attack_dmg
ice_attack_dmg = ATTACK_MAP[current_attack_mod].ice_attack_dmg
wind_attack_dmg = ATTACK_MAP[current_attack_mod].wind_attack_dmg
update_selector()

func hit(other: CollisionObject2D):
yield(get_tree().create_timer(attack_hit_delay), "timeout")
Expand Down Expand Up @@ -193,11 +298,25 @@ func _process(delta):
DebugLabel.display(self, self.health)

func _update_health_display():
for heart in range($CanvasLayer.get_child_count()):
for heart in range($CanvasLayer/HealthBar.get_child_count()):
if health > heart * 20.0 + 10:
$CanvasLayer.get_child(heart).texture = heart_full
$CanvasLayer/HealthBar.get_child(heart).texture = heart_full
elif health > heart * 20.0:

$CanvasLayer.get_child(heart).texture = heart_half
$CanvasLayer/HealthBar.get_child(heart).texture = heart_half
else:
$CanvasLayer.get_child(heart).texture = heart_empty
$CanvasLayer/HealthBar.get_child(heart).texture = heart_empty

func update_selector():
var has_thing: bool = false
attack_icon.texture = Item.ICONS[Item.ItemType.NONE]
for atk in ATTACK_MAP:
if atk == Item.ItemType.NONE:
continue
if inventory.has(atk) && inventory[atk] > 0:
has_thing = true
if atk == current_attack_mod:
if inventory.has(atk) && inventory[atk] > 0:
attack_icon.texture = Item.ICONS[atk]
else:
current_attack_mod = Item.ItemType.NONE
attack_selector.visible = has_thing
141 changes: 115 additions & 26 deletions character/PlayerCharacter.tscn
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[gd_scene load_steps=20 format=2]
[gd_scene load_steps=22 format=2]

[ext_resource path="res://character/PlayerCharacter.gd" type="Script" id=1]
[ext_resource path="res://character/PlayerCharacter_SpriteFrames.tres" type="SpriteFrames" id=2]
Expand All @@ -18,6 +18,8 @@
[ext_resource path="res://sound/positional/SFX_SwordHit_5.wav" type="AudioStream" id=16]
[ext_resource path="res://sound/positional/SFX_SwordHit_1.wav" type="AudioStream" id=17]
[ext_resource path="res://character/AttackFx_SpriteFrames.tres" type="SpriteFrames" id=18]
[ext_resource path="res://game/ui-theme/oinventoryitembackground.png" type="Texture" id=19]
[ext_resource path="res://game/ui-theme/ui_theme.tres" type="Theme" id=20]

[sub_resource type="CircleShape2D" id=1]
radius = 16.0
Expand Down Expand Up @@ -131,52 +133,139 @@ bus = "Effects"

[node name="CanvasLayer" type="CanvasLayer" parent="."]

[node name="TextureRect" type="TextureRect" parent="CanvasLayer"]
margin_left = 80.0
margin_top = 592.0
margin_right = 144.0
margin_bottom = 656.0
[node name="HealthBar" type="Control" parent="CanvasLayer"]
anchor_right = 1.0
anchor_bottom = 1.0
mouse_filter = 2
__meta__ = {
"_edit_use_anchors_": false
}

[node name="TextureRect" type="TextureRect" parent="CanvasLayer/HealthBar"]
anchor_top = 1.0
anchor_bottom = 1.0
margin_left = 60.0
margin_top = -120.0
margin_right = 124.0
margin_bottom = -56.0
expand = true
__meta__ = {
"_edit_use_anchors_": false
}

[node name="TextureRect2" type="TextureRect" parent="CanvasLayer"]
margin_left = 144.0
margin_top = 592.0
margin_right = 208.0
margin_bottom = 656.0
[node name="TextureRect2" type="TextureRect" parent="CanvasLayer/HealthBar"]
anchor_top = 1.0
anchor_bottom = 1.0
margin_left = 124.0
margin_top = -120.0
margin_right = 188.0
margin_bottom = -56.0
expand = true
__meta__ = {
"_edit_use_anchors_": false
}

[node name="TextureRect3" type="TextureRect" parent="CanvasLayer"]
margin_left = 208.0
margin_top = 592.0
margin_right = 272.0
margin_bottom = 656.0
[node name="TextureRect3" type="TextureRect" parent="CanvasLayer/HealthBar"]
anchor_top = 1.0
anchor_bottom = 1.0
margin_left = 188.0
margin_top = -120.0
margin_right = 252.0
margin_bottom = -56.0
expand = true
__meta__ = {
"_edit_use_anchors_": false
}

[node name="TextureRect4" type="TextureRect" parent="CanvasLayer"]
margin_left = 272.0
margin_top = 592.0
margin_right = 336.0
margin_bottom = 656.0
[node name="TextureRect4" type="TextureRect" parent="CanvasLayer/HealthBar"]
anchor_top = 1.0
anchor_bottom = 1.0
margin_left = 252.0
margin_top = -120.0
margin_right = 316.0
margin_bottom = -56.0
expand = true
__meta__ = {
"_edit_use_anchors_": false
}

[node name="TextureRect5" type="TextureRect" parent="CanvasLayer"]
margin_left = 336.0
margin_top = 592.0
margin_right = 400.0
margin_bottom = 656.0
[node name="TextureRect5" type="TextureRect" parent="CanvasLayer/HealthBar"]
anchor_top = 1.0
anchor_bottom = 1.0
margin_left = 316.0
margin_top = -120.0
margin_right = 380.0
margin_bottom = -56.0
expand = true
__meta__ = {
"_edit_use_anchors_": false
}

[node name="AttackSelector" type="Control" parent="CanvasLayer"]
anchor_right = 1.0
anchor_bottom = 1.0
mouse_filter = 2
theme = ExtResource( 20 )
__meta__ = {
"_edit_use_anchors_": false
}

[node name="Label" type="Label" parent="CanvasLayer/AttackSelector"]
anchor_left = 0.5
anchor_top = 1.0
anchor_right = 0.5
anchor_bottom = 1.0
margin_left = -60.0
margin_top = -100.0
margin_right = -30.0
margin_bottom = -60.0
text = "<"
align = 1
valign = 1
__meta__ = {
"_edit_use_anchors_": false
}

[node name="SelectorRect" type="NinePatchRect" parent="CanvasLayer/AttackSelector"]
anchor_left = 0.5
anchor_top = 1.0
anchor_right = 0.5
anchor_bottom = 1.0
margin_left = -30.0
margin_top = -110.0
margin_right = 30.0
margin_bottom = -50.0
texture = ExtResource( 19 )
patch_margin_left = 12
patch_margin_top = 12
patch_margin_right = 12
patch_margin_bottom = 12
__meta__ = {
"_edit_use_anchors_": false
}

[node name="Icon" type="TextureRect" parent="CanvasLayer/AttackSelector/SelectorRect"]
margin_left = 12.0
margin_top = 12.0
margin_right = 48.0
margin_bottom = 48.0
expand = true
__meta__ = {
"_edit_use_anchors_": false
}

[node name="Label2" type="Label" parent="CanvasLayer/AttackSelector"]
anchor_left = 0.5
anchor_top = 1.0
anchor_right = 0.5
anchor_bottom = 1.0
margin_left = 30.0
margin_top = -100.0
margin_right = 60.0
margin_bottom = -60.0
text = ">"
align = 1
valign = 1
__meta__ = {
"_edit_use_anchors_": false
}
1 change: 1 addition & 0 deletions environment/props/CraftingTable.gd
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ func on_confirm():
if !character.inventory.has(current_recipe):
character.inventory[current_recipe] = 0
character.inventory[current_recipe] += 1
character.update_selector()
update_inventory_view()
done_button.disabled = false

Expand Down
14 changes: 14 additions & 0 deletions project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,20 @@ attack={
, Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"button_mask":0,"position":Vector2( 0, 0 ),"global_position":Vector2( 0, 0 ),"factor":1.0,"button_index":1,"pressed":false,"doubleclick":false,"script":null)
]
}
next_attack_mod={
"deadzone": 0.5,
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":69,"unicode":0,"echo":false,"script":null)
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":93,"unicode":0,"echo":false,"script":null)
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":5,"pressure":0.0,"pressed":false,"script":null)
]
}
prev_attack_mod={
"deadzone": 0.5,
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":81,"unicode":0,"echo":false,"script":null)
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":91,"unicode":0,"echo":false,"script":null)
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":4,"pressure":0.0,"pressed":false,"script":null)
]
}

[physics]

Expand Down

0 comments on commit 4abcf06

Please sign in to comment.