working hand

This commit is contained in:
SondreElg
2025-10-05 10:00:07 +02:00
parent c6c873da9d
commit 1315163cd3
5 changed files with 68 additions and 64 deletions

View File

@@ -19,3 +19,13 @@ func activate(world, activator):
for behavior in $Behaviors.get_children():
behavior.activate(world, activator)
func discard(world, activator):
for behavior in $Behaviors.get_children():
behavior.discard(world, activator)
behavior.ammo = behavior.max_ammo
func get_ammo():
for behavior in $Behaviors.get_children():
if behavior.has_method("get_ammo"):
return behavior.get_ammo()
return -1

View File

@@ -2,31 +2,33 @@ extends Control
@export var player: NodePath
@export var world: NodePath
@export var drawpile: Array[PackedScene] = [
preload("res://Cards/FirewallCard.tscn"),
preload("res://Cards/FirewallCard.tscn"),
preload("res://Cards/CursorGunCard.tscn"),
preload("res://Cards/CursorGunCard.tscn"),
preload("res://Cards/DashCard.tscn"),
preload("res://Cards/DashCard.tscn"),
#preload("res://Cards/ShieldCard.tscn"),
#preload("res://Cards/ShieldCard.tscn"),
#preload("res://Cards/OverclockCard.tscn"),
#preload("res://Cards/OverclockCard.tscn"),
@export var hand_size = 3
@export var drawpile: Array = [
preload("res://Cards/FirewallCard.tscn").instantiate(),
preload("res://Cards/FirewallCard.tscn").instantiate(),
preload("res://Cards/CursorGunCard.tscn").instantiate(),
preload("res://Cards/CursorGunCard.tscn").instantiate(),
preload("res://Cards/DashCard.tscn").instantiate(),
preload("res://Cards/DashCard.tscn").instantiate(),
#preload("res://Cards/ShieldCard.tscn").instantiate(),
#preload("res://Cards/ShieldCard.tscn").instantiate(),
#preload("res://Cards/OverclockCard.tscn").instantiate(),
#preload("res://Cards/OverclockCard.tscn").instantiate(),
]
var active_card_index = 0
const discard_pile = [];
var discard_pile = [];
func _ready() -> void:
for card in drawpile:
card.instantiate()
drawpile.shuffle();
#for i in range(get_node(player).hand_size):
#draw_card()
print_debug("Shuffled deck, drawpile size: " + str(drawpile.size()))
print_debug("Empty card slots: " + str(hand_size) + " - " + str(get_cards().size()) + " = " + str(hand_size - get_cards().size()))
# print_debug("Drawing initial hand")
for i in range(hand_size - get_cards().size()):
draw_card()
func _process(delta):
func _process(_delta):
if Input.is_action_pressed("play_card"):
play_card()
if Input.is_action_just_pressed("cycle_card_left"):
@@ -39,35 +41,52 @@ func play_card():
if active_card_index >= 0 and active_card_index < slots.size():
var card = slots[active_card_index].get_node("Card")
card.activate(get_node(world), get_node(player));
if card.ammo <= 0:
discard(false);
print(active_card_index)
func discard():
func discard(do_ability = true):
var cards = get_cards();
if active_card_index >= 0 and active_card_index < cards.size():
var card = cards[active_card_index]
card.discard(get_node(world), get_node(player))
if do_ability:
card.discard(get_node(world), get_node(player))
discard_pile.append(card)
get_card_container().remove_child(card)
card.get_parent().remove_child(card)
#active_card_index = min(active_card_index, hand.size() - 1);
draw_card();
func cycle_card(index_shift):
#if hand.size() > 0:
#active_card_index = (active_card_index + index_shift) % hand.size();
#else:
#draw_card();
pass
if get_cards().size() > 0:
active_card_index = (active_card_index + index_shift) % get_cards().size();
else:
draw_card();
func draw_card():
if drawpile.size() == 0:
print_debug("Drawing card")
var held_card_count = get_cards().size()
if held_card_count == 0:
shuffle_deck();
if drawpile.size() > 0 and get_cards().size() < get_node(player).hand_size:
var card = drawpile.pop_back();
if active_card_index == -1:
active_card_index = 0;
get_card_container().add_child(card)
if drawpile.size() > 0 and held_card_count < hand_size:
for i in range(hand_size - held_card_count):
var card = drawpile.pop_back();
if active_card_index == -1:
active_card_index = 0;
print_debug("Instantiating card from drawpile, remaining size: " + str(drawpile.size()))
var empty_slots = get_card_container().get_children().filter(func(slot): return not slot.has_method("get_node") or slot.get_node_or_null("Card") == null)
if empty_slots.size() > 0:
empty_slots[0].add_child(card)
else:
var first_filled_slot_index = -1
var slots = get_card_container().get_children()
for i in range(slots.size()):
if slots[i].has_method("get_node") and slots[i].get_node_or_null("Card") != null:
first_filled_slot_index = i
break
active_card_index = first_filled_slot_index
# TODO: Reboot mechanics
func shuffle_deck():
@@ -77,7 +96,7 @@ func shuffle_deck():
active_card_index = 0;
func get_cards() -> Array:
return get_card_container().get_children()
return get_card_container().get_children().map(func(c): return c.get_node_or_null("Card")).filter(func(c): return c != null)
func get_card_container():
return $HBoxContainer

View File

@@ -1,8 +1,6 @@
[gd_scene load_steps=4 format=3 uid="uid://co8jnr2dew5ts"]
[gd_scene load_steps=2 format=3 uid="uid://cpp7v4jp8kt74"]
[ext_resource type="Script" uid="uid://bykc0ufv25ij5" path="res://hand.gd" id="1_u4p2i"]
[ext_resource type="PackedScene" uid="uid://cmuig2rqt65f3" path="res://Cards/CursorGunCard.tscn" id="2_bfasp"]
[ext_resource type="PackedScene" uid="uid://dck7bp1b7553t" path="res://Cards/MineCard.tscn" id="2_e75s6"]
[node name="Hand" type="Control"]
layout_mode = 3
@@ -38,17 +36,8 @@ grow_vertical = 0
[node name="Slot1" type="PanelContainer" parent="HBoxContainer"]
layout_mode = 2
[node name="Card" parent="HBoxContainer/Slot1" instance=ExtResource("2_e75s6")]
layout_mode = 2
[node name="Slot2" type="PanelContainer" parent="HBoxContainer"]
layout_mode = 2
[node name="Card" parent="HBoxContainer/Slot2" instance=ExtResource("2_bfasp")]
layout_mode = 2
[node name="Slot3" type="PanelContainer" parent="HBoxContainer"]
layout_mode = 2
[node name="Card" parent="HBoxContainer/Slot3" instance=ExtResource("2_bfasp")]
layout_mode = 2

View File

@@ -4,7 +4,7 @@
[ext_resource type="PackedScene" uid="uid://bsv3h2lpv7h77" path="res://virus.tscn" id="2_oi3di"]
[ext_resource type="PackedScene" uid="uid://bldi3fw0vmlu3" path="res://music.tscn" id="3_oi3di"]
[ext_resource type="TileSet" uid="uid://c20bl25rqyf68" path="res://assets/tiles/new_tile_set.tres" id="4_0b4ue"]
[ext_resource type="PackedScene" uid="uid://co8jnr2dew5ts" path="res://hand.tscn" id="5_f2txt"]
[ext_resource type="PackedScene" uid="uid://cpp7v4jp8kt74" path="res://hand.tscn" id="5_f2txt"]
[node name="Level" type="Node2D"]
@@ -15,7 +15,6 @@ tile_set = ExtResource("4_0b4ue")
[node name="Player" parent="." instance=ExtResource("1_vonw3")]
position = Vector2(-1200, 0)
hand = [null]
[node name="Virus" parent="." instance=ExtResource("2_oi3di")]
@@ -25,3 +24,4 @@ scale = Vector2(2, 2)
[node name="CanvasLayer" type="CanvasLayer" parent="."]
[node name="Hand" parent="CanvasLayer" instance=ExtResource("5_f2txt")]
unique_name_in_owner = true

View File

@@ -6,16 +6,15 @@ enum MoveState {Still, Moving, Dashing, Knockback}
@export var speed = 200 # How fast the player will move (pixels/sec).
@export var dash_cooldown = 0.3
@export var hand_size = 3
@onready var camera: Camera2D = %PlayerCamera
@onready var sprite: AnimatedSprite2D = $PlayerSprite
@onready var hand: Control = %Hand;
var shield_active = true;
var move_direction = Vector2.ZERO;
var target = Vector2.ZERO # The position of the player's cursor.
@export var hand: Control;
var screen_size # Size of the game window.
var action_state = ActionState.None
@@ -65,27 +64,14 @@ func charge():
charge_level = max(charge_level - charge_rate * 2, 0);
func dash():
var card = hand.get_active_card();
if not card or dash_on_cooldown:
if dash_on_cooldown:
return;
hand.discard(self); # Must set move_state at start and end of dash
hand.discard(); # Must set move_state at start and end of dash
dash_on_cooldown = true;
func draw_card():
#if drawpile.size() == 0:
#shuffle_deck();
#if drawpile.size() > 0 and hand.size() < hand_size:
#var card = drawpile.pop_back();
#hand.append(card);
#if active_card_index == -1:
#active_card_index = 0;
pass
func _ready():
screen_size = get_viewport_rect().size
#while (hand.size() < hand_size and drawpile.size() > 0):
#draw_card();
func _process(delta):
update_target_coords();