Skip to content

Commit

Permalink
A new way to change phase (#124)
Browse files Browse the repository at this point in the history
* Change phase enum

* Rewrite PhaseManager
  • Loading branch information
Turtyo authored Aug 17, 2024
1 parent ae7d779 commit ac66d38
Show file tree
Hide file tree
Showing 12 changed files with 76 additions and 45 deletions.
2 changes: 1 addition & 1 deletion #Scenes/Events/mob/0.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
[ext_resource type="Texture2D" uid="uid://d4muqvs3etnr8" path="res://Art/Card_layout/discard_pile.png" id="12_0nlrw"]
[ext_resource type="Script" path="res://UI/DiscardPileUISetter.gd" id="13_8nsar"]
[ext_resource type="PackedScene" uid="uid://bam77cwf4emyr" path="res://#Scenes/TopBarOverlay.tscn" id="14_os5i4"]
[ext_resource type="PackedScene" path="res://#Scenes/xp_bar.tscn" id="15_r7cr8"]
[ext_resource type="PackedScene" uid="uid://ruwidm3egyrx" path="res://#Scenes/xp_bar.tscn" id="15_r7cr8"]

[node name="TestingScene" type="Node2D"]
script = ExtResource("1_nmgwp")
Expand Down
10 changes: 5 additions & 5 deletions Cards/CardContainer.gd
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ var _cards_queued_for_discard: Array[CardWorld] = []
var _discard_timer: SceneTreeTimer = null

func _ready() -> void:
PhaseManager.on_phase_changed.connect(_on_phase_changed)
PhaseManager.on_combat_phase_changed.connect(_on_phase_changed)
CardManager.on_deck_initialized.connect(_init_default_draw_pile)
CardManager.set_card_container(self)
CardManager.on_card_action_finished.connect(finish_active_card_action.unbind(1))
Expand Down Expand Up @@ -294,14 +294,14 @@ func _handle_discard_queue() -> void:

if _cards_queued_for_discard.size() > 0:
_handle_discard_queue()
elif PhaseManager.current_phase == GlobalEnums.Phase.PLAYER_FINISHING:
elif PhaseManager.current_combat_phase == GlobalEnums.CombatPhase.PLAYER_FINISHING:
on_finished_discarding_hand.emit()


func _on_phase_changed(new_phase: GlobalEnums.Phase, _old_phase: GlobalEnums.Phase) -> void:
if new_phase == GlobalEnums.Phase.PLAYER_ATTACKING:
func _on_phase_changed(new_phase: GlobalEnums.CombatPhase, _old_phase: GlobalEnums.CombatPhase) -> void:
if new_phase == GlobalEnums.CombatPhase.PLAYER_ATTACKING:
deal_to_starting_hand_size()
if new_phase == GlobalEnums.Phase.PLAYER_FINISHING:
if new_phase == GlobalEnums.CombatPhase.PLAYER_FINISHING:
if (cards_in_hand.size() == 0):
on_finished_discarding_hand.emit()
else:
Expand Down
6 changes: 3 additions & 3 deletions Cards/CardWorld.gd
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var card_data: CardBase = null
var card_cast_type : GlobalEnums.CardCastType

func _ready() -> void:
PhaseManager.on_phase_changed.connect(_on_phase_changed)
PhaseManager.on_combat_phase_changed.connect(_on_phase_changed)


func init_card(in_card_data: CardBase) -> void:
Expand All @@ -25,9 +25,9 @@ func init_card(in_card_data: CardBase) -> void:
break


func _on_phase_changed(new_phase: GlobalEnums.Phase, _old_phase: GlobalEnums.Phase) -> void:
func _on_phase_changed(new_phase: GlobalEnums.CombatPhase, _old_phase: GlobalEnums.CombatPhase) -> void:
# enable clicks on card only if player is in attack phase
get_click_handler().set_interactable(new_phase == GlobalEnums.Phase.PLAYER_ATTACKING)
get_click_handler().set_interactable(new_phase == GlobalEnums.CombatPhase.PLAYER_ATTACKING)


func get_card_movement_component() -> CardMovementComponent:
Expand Down
2 changes: 1 addition & 1 deletion Cards/Resource/Card_Poison.tres
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[gd_resource type="Resource" script_class="CardBase" load_steps=17 format=3 uid="uid://ctx8jsvac84so"]

[ext_resource type="Script" path="res://Cards/Effects/EffectApplyStatus.gd" id="1_5lji7"]
[ext_resource type="PackedScene" path="res://Cards/Animation/Scene/Anim_Poison.tscn" id="1_q5mb6"]
[ext_resource type="PackedScene" uid="uid://c13302ahcfpuy" path="res://Cards/Animation/Scene/Anim_Poison.tscn" id="1_q5mb6"]
[ext_resource type="Script" path="res://Cards/CardBase.gd" id="1_u6k7h"]
[ext_resource type="Script" path="res://Status/Debuffs/Debuff_Poison.gd" id="2_6vdl1"]
[ext_resource type="Script" path="res://Cards/Effects/EffectData.gd" id="2_omhae"]
Expand Down
15 changes: 8 additions & 7 deletions Core/Battler.gd
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ func _ready() -> void:
PlayerManager.on_player_initialized.connect(_on_player_initialized)
else:
_on_player_initialized()

PhaseManager.on_phase_changed.connect(_on_phase_changed)

PhaseManager.start_combat()
PhaseManager.on_combat_phase_changed.connect(_on_phase_changed)
CardManager.on_card_container_initialized.connect(_on_card_container_initialized)
CardManager.on_card_action_finished.connect(_handle_deaths.unbind(1))

Expand All @@ -49,11 +50,11 @@ func _on_player_initialized() -> void:
player_status_comp.add_status(buff, PlayerManager.player)


func _on_phase_changed(new_phase: GlobalEnums.Phase, _old_phase: GlobalEnums.Phase) -> void:
if new_phase == GlobalEnums.Phase.PLAYER_ATTACKING:
func _on_phase_changed(new_phase: GlobalEnums.CombatPhase, _old_phase: GlobalEnums.CombatPhase) -> void:
if new_phase == GlobalEnums.CombatPhase.PLAYER_ATTACKING:
_on_player_start_turn()

if new_phase == GlobalEnums.Phase.ENEMY_ATTACKING:
if new_phase == GlobalEnums.CombatPhase.ENEMY_ATTACKING:
_on_enemy_start_turn()


Expand All @@ -63,7 +64,7 @@ func _on_card_container_initialized() -> void:


func _on_player_hand_discarded() -> void:
PhaseManager.set_phase(GlobalEnums.Phase.ENEMY_ATTACKING)
PhaseManager.advance_to_next_combat_phase()


## player start phase: apply status
Expand Down Expand Up @@ -123,7 +124,7 @@ func _try_finish_enemy_attacks() -> void:
if _enemy_action_list.size() > 0:
_handle_enemy_attack_queue()
else:
PhaseManager.set_phase(GlobalEnums.Phase.PLAYER_ATTACKING)
PhaseManager.advance_to_next_combat_phase()


## when player clicks themselves (eg: healing card)
Expand Down
6 changes: 3 additions & 3 deletions Entity/Components/HealthComponent.gd
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,12 @@ func _set_block(new_block: int) -> void:
current_block = new_block
on_block_changed.emit(current_block)

func reset_block_on_round_start(new_phase: GlobalEnums.Phase, _old_phase: GlobalEnums.Phase) -> void:
func reset_block_on_round_start(new_phase: GlobalEnums.CombatPhase, _old_phase: GlobalEnums.CombatPhase) -> void:
if(team == GlobalEnums.Team.FRIENDLY):
if(new_phase == GlobalEnums.Phase.PLAYER_ATTACKING):
if(new_phase == GlobalEnums.CombatPhase.PLAYER_ATTACKING):
reset_block()
if(team == GlobalEnums.Team.ENEMY):
if(new_phase == GlobalEnums.Phase.ENEMY_ATTACKING):
if(new_phase == GlobalEnums.CombatPhase.ENEMY_ATTACKING):
reset_block()

## Sets block to 0 [br]
Expand Down
6 changes: 3 additions & 3 deletions Entity/Enemy/Enemy.gd
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ class_name Enemy
## Connect the enemy to the [PhaseManager] to make it attack when it's its turn.
func _ready() -> void:
super()
PhaseManager.on_phase_changed.connect(_on_phase_changed)
PhaseManager.on_combat_phase_changed.connect(_on_phase_changed)
get_stress_component().init_entity_component(self)


## Only allow the player to interact with enemies when it's the player turn
func _on_phase_changed(new_phase: GlobalEnums.Phase, _old_phase: GlobalEnums.Phase) -> void:
get_click_handler().set_interactable(new_phase == GlobalEnums.Phase.PLAYER_ATTACKING)
func _on_phase_changed(new_phase: GlobalEnums.CombatPhase, _old_phase: GlobalEnums.CombatPhase) -> void:
get_click_handler().set_interactable(new_phase == GlobalEnums.CombatPhase.PLAYER_ATTACKING)

## Used to get the enemy AI [br]
## Not in [Entity] as this is specific to enemies
Expand Down
1 change: 1 addition & 0 deletions Event/EventBase.gd
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func get_event_name() -> String:
## What to do once the event starts
func on_event_started() -> void:
PlayerManager.is_map_movement_allowed = false
PhaseManager.set_global_phase(GlobalEnums.GlobalPhase.SCENE_STARTED)

## What to do once the event ends
func on_event_ended() -> void:
Expand Down
12 changes: 8 additions & 4 deletions Global/Global_Enums.gd
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@ extends Node
class_name GlobalEnums
## Global enums list

## Where are we in the fight
enum Phase
{
## For phases that are common between multiple events
enum GlobalPhase {
NONE, ## Hello Youston, we are nowhere
GAME_STARTING, ## The game is starting
SCENE_STARTED, ## A new scene started
SCENE_END, ## The current scene ended
}

## For phases that are combat specific
enum CombatPhase {
PLAYER_ATTACKING, ## The player is attacking
PLAYER_FINISHING, ## The player finished its turn (card discard and other stuff starts here)
ENEMY_ATTACKING, ## Enemy turn
SCENE_END, ## The current scene ended
}

## In which team is an entity
Expand Down
51 changes: 38 additions & 13 deletions Managers/PhaseManager.gd
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,70 @@ extends Node


signal on_game_start
signal on_phase_changed(new_phase: GlobalEnums.Phase, old_phase: GlobalEnums.Phase)
signal on_combat_phase_changed(new_phase: GlobalEnums.CombatPhase, old_phase: GlobalEnums.CombatPhase)
signal on_global_phase_changed(new_phase: GlobalEnums.GlobalPhase, old_phase: GlobalEnums.GlobalPhase)
## When a player wins the event
signal on_event_win
## When the player is dead (reduced to 0 health)
signal on_defeat

## This is a signal is a temporary solution to block removal while #118 and #120 are done
## should be removed with those issues
signal temp_before_phase_changed(new_phase: GlobalEnums.Phase, old_phase: GlobalEnums.Phase)

var current_phase: GlobalEnums.Phase = GlobalEnums.Phase.NONE
signal temp_before_phase_changed(new_phase: GlobalEnums.CombatPhase, old_phase: GlobalEnums.CombatPhase)

var current_combat_phase: GlobalEnums.CombatPhase = GlobalEnums.CombatPhase.PLAYER_ATTACKING
var current_combat_phase_index: int = 0
var current_global_phase: GlobalEnums.GlobalPhase = GlobalEnums.GlobalPhase.NONE

func _ready() -> void:
# initialize_game()
return


## Init phase
func initialize_game() -> void:
set_phase(GlobalEnums.Phase.GAME_STARTING)
set_global_phase(GlobalEnums.GlobalPhase.GAME_STARTING)

# TODO give all objects some time to initialize. Kinda hacky
await get_tree().create_timer(.1).timeout
_start_game()


## Start the game
func _start_game() -> void:
set_phase(GlobalEnums.Phase.PLAYER_ATTACKING)
_set_combat_phase(GlobalEnums.CombatPhase.PLAYER_ATTACKING)
on_game_start.emit()


## Change phases in the game (mainly used in combat for now)
func set_phase(phase: GlobalEnums.Phase) -> void:
if (current_phase == phase):
return

var old_phase: GlobalEnums.Phase = current_phase
func _set_combat_phase(phase: GlobalEnums.CombatPhase) -> void:
# allow old phase being same as new phase
# this is if you finish a fight on player turn, you start the next also on player turn
var old_phase: GlobalEnums.CombatPhase = current_combat_phase

temp_before_phase_changed.emit(phase, old_phase)

current_phase = phase
on_phase_changed.emit(current_phase, old_phase)
current_combat_phase = phase
on_combat_phase_changed.emit(current_combat_phase, old_phase)

## Call to setup combat phase [br]
## Note: This might be used later to also properly remove block from previous fights
func start_combat() -> void:
current_combat_phase_index = 0
_set_combat_phase(GlobalEnums.CombatPhase.values()[0])

## Go to the next phase of the combat
func advance_to_next_combat_phase() -> void:
var combat_phase: Array = GlobalEnums.CombatPhase.values()
# advance the index by 1, going back to 0 if the index is bigger than the total number of possible phase
current_combat_phase_index = (current_combat_phase_index + 1) % (combat_phase.size())
_set_combat_phase(GlobalEnums.CombatPhase.values()[current_combat_phase_index])

## Used to set global game phase
func set_global_phase(phase: GlobalEnums.GlobalPhase) -> void:
if (current_global_phase == phase):
return
var old_phase: GlobalEnums.GlobalPhase = current_global_phase
current_global_phase = phase
on_global_phase_changed.emit(current_global_phase, old_phase)
2 changes: 1 addition & 1 deletion Tests/test_health.gd
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func test_reset_block_on_round_start() -> void:
var caster: Entity = _player

_player_health_component.add_block(block_amount, caster)
PhaseManager.temp_before_phase_changed.emit(GlobalEnums.Phase.PLAYER_ATTACKING, GlobalEnums.Phase.ENEMY_ATTACKING)
PhaseManager.temp_before_phase_changed.emit(GlobalEnums.CombatPhase.PLAYER_ATTACKING, GlobalEnums.CombatPhase.ENEMY_ATTACKING)
assert_eq(_player_health_component.current_block, 0)

func test_take_lots_of_damage() -> void:
Expand Down
8 changes: 4 additions & 4 deletions UI/EndTurnButton.gd
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ extends Button

func _process(_delta: float) -> void:
# disable button during enemy attack phase
disabled = GlobalEnums.Phase.ENEMY_ATTACKING == PhaseManager.current_phase\
or PhaseManager.current_phase == GlobalEnums.Phase.PLAYER_FINISHING\
or (PhaseManager.current_phase == GlobalEnums.Phase.PLAYER_ATTACKING\
disabled = GlobalEnums.CombatPhase.ENEMY_ATTACKING == PhaseManager.current_combat_phase\
or PhaseManager.current_combat_phase == GlobalEnums.CombatPhase.PLAYER_FINISHING\
or (PhaseManager.current_combat_phase == GlobalEnums.CombatPhase.PLAYER_ATTACKING\
and (CardManager.card_container.are_cards_dealing()\
or CardManager.card_container.is_card_queued()\
or CardManager.card_container.are_cards_active()))


func _on_pressed() -> void:
PhaseManager.set_phase(GlobalEnums.Phase.PLAYER_FINISHING)
PhaseManager.advance_to_next_combat_phase()

0 comments on commit ac66d38

Please sign in to comment.