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
9 changes: 9 additions & 0 deletions entities/game/card_collider/scripts/CardCollider.gd
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,19 @@ var _shape: CollisionShape2D = null

var _mouse_inside: bool = false
var _active_state: bool = false
var _card_id: int = -1

func _ready() -> void:
_shape = get_node("%CollisionShape")

func set_card_id(id: int) -> void:
if _card_id != -1:
return
_card_id = id

func get_card_id() -> int:
return _card_id

func set_size(size: Vector2) -> void:
var shape: RectangleShape2D = RectangleShape2D.new()
shape.size = size
Expand Down
3 changes: 0 additions & 3 deletions entities/game/card_template/scripts/CardTemplate.gd
Original file line number Diff line number Diff line change
Expand Up @@ -281,9 +281,6 @@ func play_sound(audio: AudioStream) -> void:
return
GlobalSoundManager.play_sound_effect(audio)

func player_changed(ai_player: bool) -> void:
input_allowed(!ai_player)

func input_allowed(allowed: bool) -> void:
input_active.emit(allowed)

Expand Down
30 changes: 10 additions & 20 deletions entities/game/memory_game/scenes/MemoryGame.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
[ext_resource type="Script" uid="uid://6fa28680utd5" path="res://shared/entities/popup_manager/scripts/PopupSystem.gd" id="21_dv4ao"]
[ext_resource type="PackedScene" uid="uid://d15qeu4s4bgjk" path="res://entities/game/tutorial_window/scenes/TutorialWindow.tscn" id="22_ubsrg"]
[ext_resource type="Texture2D" uid="uid://vk4fpd7xo01j" path="res://assets/sprites/Axuree/wood_medium.png" id="23_a3528"]
[ext_resource type="Script" uid="uid://btm576yjkyt4j" path="res://entities/game/memory_game/scripts/CardSpawner.gd" id="26_a3528"]
[ext_resource type="Script" uid="uid://cf8loyigibdp6" path="res://entities/game/memory_game/scripts/CardInteractionField.gd" id="27_7lv0s"]
[ext_resource type="PackedScene" uid="uid://0m4c62oqj4ih" path="res://entities/game/card_collider/scene/CardCollider.tscn" id="28_7ou1n"]
[ext_resource type="Script" uid="uid://drr3p4wba2ys3" path="res://entities/game/memory_game/scripts/systems/NetworkHandler.gd" id="29_kihnp"]
Expand Down Expand Up @@ -120,11 +119,6 @@ script = ExtResource("29_kihnp")
finished_game_template = ExtResource("4_6f670")
metadata/_custom_type_script = "uid://drr3p4wba2ys3"

[node name="CardSpawnerSystem" type="Node" parent="Systems" unique_id=1452917331]
script = ExtResource("26_a3528")
card_template = ExtResource("3_ihyl6")
metadata/_custom_type_script = "uid://btm576yjkyt4j"

[node name="UiInformationSystem" type="Node" parent="Systems" unique_id=1812178848]
unique_name_in_owner = true
script = ExtResource("36_m766l")
Expand All @@ -146,6 +140,7 @@ script = ExtResource("19_ploi3")
state_machine = NodePath("../../Systems/GameStateSystem")
matching_card_sound_effect = ExtResource("6_a3528")
visual_card_node = NodePath("CardVisuals")
card_template = ExtResource("3_ihyl6")

[node name="CardInteractionField" type="Node2D" parent="World/CardBoard" unique_id=1435215390]
z_index = 10
Expand Down Expand Up @@ -239,9 +234,9 @@ mouse_filter = 2
memory_game = NodePath("../..")
debug_functions = Array[ExtResource("34_4hsit")]([SubResource("Resource_8rpam")])

[connection signal="announce_deck" from="." to="World/CardBoard" method="set_deck"]
[connection signal="game_paused" from="." to="Systems/PlayerInputSystem" method="game_paused"]
[connection signal="game_paused" from="." to="World/Camera2D" method="game_paused"]
[connection signal="load_game" from="." to="Systems/CardSpawnerSystem" method="place_cards_from_deck"]
[connection signal="load_game" from="." to="World/CardBoard/CardInteractionField" method="set_board_information"]
[connection signal="request_popup" from="." to="Systems/PopupSystem" method="add_and_show_popup"]
[connection signal="force_close_popup" from="Systems/GameStateSystem" to="Systems/PopupSystem" method="force_close_popup_with_id"]
Expand Down Expand Up @@ -282,19 +277,6 @@ debug_functions = Array[ExtResource("34_4hsit")]([SubResource("Resource_8rpam")]
[connection signal="player_has_changed" from="Systems/NetworkHandlerSystem" to="Systems/PlayerSystem" method="set_player_by_id"]
[connection signal="player_has_scored" from="Systems/NetworkHandlerSystem" to="Systems/PlayerSystem" method="set_player_score"]
[connection signal="remove_card" from="Systems/NetworkHandlerSystem" to="World/CardBoard" method="remove_card_from_board"]
[connection signal="announce_field_size" from="Systems/CardSpawnerSystem" to="World/Camera2D" method="adjust_zoom_and_position_to_play_area"]
[connection signal="card_placed" from="Systems/CardSpawnerSystem" to="." method="card_was_placed"]
[connection signal="card_placed" from="Systems/CardSpawnerSystem" to="World/CardBoard" method="card_was_placed"]
[connection signal="card_placed" from="Systems/CardSpawnerSystem" to="World/CardBoard/CardInteractionField" method="card_was_added"]
[connection signal="card_placing_done" from="Systems/CardSpawnerSystem" to="." method="all_cards_placed"]
[connection signal="card_placing_done" from="Systems/CardSpawnerSystem" to="Systems/GameStateSystem" method="game_field_ready"]
[connection signal="card_placing_done" from="Systems/CardSpawnerSystem" to="World/CardBoard" method="card_loading_done"]
[connection signal="card_placing_done" from="Systems/CardSpawnerSystem" to="UI/PauseMenuButton" method="show_button_if_valid"]
[connection signal="card_placing_done" from="Systems/CardSpawnerSystem" to="UI/PlayersOverlay" method="show"]
[connection signal="card_placing_done" from="Systems/CardSpawnerSystem" to="UI/ZoomButtons/ZoomIn" method="show_button_if_valid"]
[connection signal="card_placing_done" from="Systems/CardSpawnerSystem" to="UI/ZoomButtons/ZoomOut" method="show_button_if_valid"]
[connection signal="field_constructed" from="Systems/CardSpawnerSystem" to="World/CardBoard" method="receive_field_size"]
[connection signal="field_constructed" from="Systems/CardSpawnerSystem" to="World/CardBoard/CardInteractionField" method="build_field"]
[connection signal="all_matching_cards_removed" from="World/CardBoard" to="." method="all_cards_removed"]
[connection signal="all_matching_cards_removed" from="World/CardBoard" to="Systems/GameStateSystem" method="matches_found"]
[connection signal="all_matching_cards_removed" from="World/CardBoard" to="Systems/TutorialStateSystem" method="matching_card_found"]
Expand All @@ -307,7 +289,15 @@ debug_functions = Array[ExtResource("34_4hsit")]([SubResource("Resource_8rpam")]
[connection signal="card_triggered" from="World/CardBoard" to="Systems/AiAgentSystem" method="card_was_triggered"]
[connection signal="identical_cards" from="World/CardBoard" to="Systems/AiAgentSystem" method="card_was_identically"]
[connection signal="no_matches_found" from="World/CardBoard" to="Systems/GameStateSystem" method="no_matches"]
[connection signal="board_area" from="World/CardBoard/CardInteractionField" to="World/Camera2D" method="adjust_zoom_and_position_to_play_area"]
[connection signal="board_build" from="World/CardBoard/CardInteractionField" to="." method="all_cards_placed"]
[connection signal="board_build" from="World/CardBoard/CardInteractionField" to="Systems/GameStateSystem" method="game_field_ready"]
[connection signal="board_build" from="World/CardBoard/CardInteractionField" to="UI/PauseMenuButton" method="show_button_if_valid"]
[connection signal="board_build" from="World/CardBoard/CardInteractionField" to="UI/PlayersOverlay" method="show"]
[connection signal="board_build" from="World/CardBoard/CardInteractionField" to="UI/ZoomButtons/ZoomIn" method="show_button_if_valid"]
[connection signal="board_build" from="World/CardBoard/CardInteractionField" to="UI/ZoomButtons/ZoomOut" method="show_button_if_valid"]
[connection signal="clicked" from="World/CardBoard/CardInteractionField" to="World/CardBoard" method="trigger_card_at_position"]
[connection signal="mouse_enter" from="World/CardBoard/CardInteractionField" to="World/CardBoard" method="select_card_at_position"]
[connection signal="place_card" from="World/CardBoard/CardInteractionField" to="World/CardBoard" method="place_card"]
[connection signal="touch_used" from="World/Camera2D" to="World/CardBoard" method="disable_card_effects"]
[connection signal="pressed" from="UI/PauseMenuButton" to="." method="show_game_menu"]
101 changes: 84 additions & 17 deletions entities/game/memory_game/scripts/CardInteractionField.gd
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ signal mouse_enter(grid: Vector2i)
signal mouse_left(grid: Vector2i)
signal clicked(grid: Vector2i)

signal place_card(card: MemoryCardResource, grid_position: Vector2i, world_position: Vector2)
signal board_area(area: Rect2)
signal board_build()

enum Axis {X, Y}

@export var default_texture_size: Vector2 = Vector2(499, 550)
Expand All @@ -19,29 +23,71 @@ var _is_ai_player: bool = false
var _selected_grid_position: Vector2i = -Vector2i.ONE
var _controller_input_was_made: bool = false

var _placed_cards: Dictionary[Vector2i, CardCollider]
var _current_deck: MemoryDeckResource = null

var possible_movements: Array[Vector2] = [
Vector2.RIGHT,
Vector2.DOWN,
Vector2.LEFT,
Vector2.UP
]


func _reset_grid_position() -> void:
_selected_grid_position = -Vector2i.ONE

func set_board_information(deck_to_use: MemoryDeckResource, card_separation: int, field_offset: Vector2i) -> void:
_current_deck = deck_to_use

var back_image: Texture2D = deck_to_use.get_back_image()
_card_size = default_texture_size
if back_image != null:
_card_size = deck_to_use.get_back_image().get_size()
_separation = card_separation
_offset = field_offset

func build_field(cards_on_x: int, cards_on_y: int) -> void:
_field_size = Vector2i(cards_on_x, cards_on_y)
for x: int in cards_on_x:
for y: int in cards_on_y:
await _create_field()
var area: Rect2 = _calculate_field_area()
board_area.emit(area)
board_build.emit()

func _create_field() -> void:
var play_cards: Array[MemoryCardResource] = _create_card_set()
_field_size = _calculate_field_size(play_cards.size())

build_field(play_cards)
await RenderingServer.frame_post_draw

func _calculate_field_size(card_count: int) -> Vector2i:
if card_count <= 0:
return Vector2i.ZERO

var side_length: int = floori(sqrt(card_count))

var row_count: int = side_length
var column_count: int = side_length

while row_count * column_count < card_count:
column_count = column_count + 1

return Vector2i(column_count, row_count)

func _create_card_set() -> Array[MemoryCardResource]:
var return_data: Array[MemoryCardResource] = []
for card: MemoryCardResource in _current_deck.cards:
return_data.append(card)
return_data.append(card)

return_data.shuffle()
return return_data

func build_field(cards: Array[MemoryCardResource]) -> void:
for x: int in _field_size.x:
for y: int in _field_size.y:
if cards.size() == 0:
return
var card: MemoryCardResource = cards.pick_random()
_remove_card_from_cards_stack(card.id, cards)
var x_addition: int = _separation * x
x_addition += int(_card_size.x) * x
var y_addition: int = _separation * y
Expand All @@ -50,10 +96,24 @@ func build_field(cards_on_x: int, cards_on_y: int) -> void:
template.set_size(_card_size)
template.position = _offset + additional_offset + Vector2i(x_addition, y_addition)
template.set_grid_coordinate(Vector2i(x, y))
template.set_card_id(card.id)
_connect_card_interaction(template)

_placed_cards.set(Vector2i(x, y), template)
add_child(template)
template.disable_collider()
place_card.emit(card, Vector2i(x, y), Vector2i(x_addition, y_addition) + _offset)

func _calculate_field_area() -> Rect2:
var field_width: float = _card_size.x * _field_size.x
var field_height: float = _card_size.y * _field_size.y

var center_x: float = field_width / 2
var center_y: float = field_height / 2
return Rect2(center_x, center_y, field_width, field_height)

func _remove_card_from_cards_stack(id: int, cards: Array[MemoryCardResource]) -> void:
var index: int = cards.find_custom((func(card: MemoryCardResource) -> bool: return card.id == id))
cards.remove_at(index)

func get_field_size() -> Vector2i:
return _field_size
Expand All @@ -62,28 +122,30 @@ func card_was_added(card: CardTemplate) -> void:
card.remove_requested.connect(remove_card.bind(card.grid_position))

func remove_card(grid_position: Vector2i) -> void:
for child: CardCollider in get_children():
if child.get_grid_coordinate() == grid_position:
child.queue_free()
if _placed_cards.has(grid_position):
var child: CardCollider = _placed_cards.get(grid_position)
child.queue_free()
_placed_cards.erase(grid_position)

func player_changed(current_player: PlayerResource) -> void:
_is_ai_player = current_player.is_ai()

func game_state_changed(new_state: GameEnum.State) -> void:
var _new_collider_enabled_state: bool = false
if new_state == GameEnum.State.TURN_START and not _is_ai_player:
if new_state == GameEnum.State.TURN_START:
_new_collider_enabled_state = true

if new_state == GameEnum.State.TURN_FREEZE:
_new_collider_enabled_state = false

_change_interaction_state(_new_collider_enabled_state)

func _change_interaction_state(new_state: bool) -> void:
for child: CardCollider in get_children():
for child: CardCollider in _placed_cards.values():
if new_state:
child.enable_collider()
child.reset()
if not _is_ai_player:
child.enable_collider()
else:
child.disable_collider()

Expand Down Expand Up @@ -145,32 +207,37 @@ func _get_closest_card_position(movement: Vector2) -> Vector2i:

return return_position

func get_card_id_from_position(grid_position: Vector2i) -> int:
if _placed_cards.has(grid_position):
return _placed_cards.get(grid_position).get_card_id()
return -1

func get_all_card_positions(get_turned: bool = false) -> Array[Vector2i]:
var card_positions: Array[Vector2i] = []
for card_collider: CardCollider in get_children():
for card_collider: CardCollider in _placed_cards.values():
if card_collider != null and not card_collider.is_queued_for_deletion():
if get_turned or card_collider.is_active():
card_positions.append(card_collider.get_grid_coordinate())
return card_positions

func get_all_disabled_cards() -> Array[Vector2i]:
var card_positions: Array[Vector2i] = []
for card_collider: CardCollider in get_children():
for card_collider: CardCollider in _placed_cards.values():
if card_collider != null and not card_collider.is_queued_for_deletion():
if not card_collider.is_active():
card_positions.append(card_collider.get_grid_coordinate())
return card_positions

func is_there_a_card_on_position(grid_position: Vector2i) -> bool:
for card_collider: CardCollider in get_children():
for card_collider: CardCollider in _placed_cards.values():
if card_collider.get_grid_coordinate() == grid_position:
var active: bool = card_collider.is_active()
return active
return false

func _get_all_relevant_available_cards(current_pos: Vector2i, go_negative: bool, axis: Axis) -> Array[Vector2i]:
var valid_cards: Array[Vector2i] = []
for card_collider: Node in get_children():
for card_collider: Node in _placed_cards.values():
if card_collider != null and not card_collider.is_queued_for_deletion() and card_collider is CardCollider:
if _check_if_valid_card(current_pos, go_negative, axis, card_collider):
valid_cards.append(card_collider.get_grid_coordinate())
Expand Down
Loading
Loading