major refactor(game-modes): centralize scoring and turn handling
* Move common ball recording, scoring, and turn-switching logic into `BaseMode` * Add hooks for mode-specific ball handling and turn behavior * Simplify 8-ball and 9-ball modes to use `LastBallMode` * Update Snooker to use the new ball-processing hooks * Remove duplicated scoring and turn-management code
This commit is contained in:
@@ -4,93 +4,41 @@ class_name Snooker extends BaseMode
|
||||
# yellow (2 points), green (3), brown (4), blue (5), pink (6), black (7).
|
||||
|
||||
|
||||
var COLORED_BALLS: Array[String] = [
|
||||
"yellow_ball", "green_ball", "brown_ball",
|
||||
"blue_ball", "pink_ball", "black_ball"
|
||||
]
|
||||
|
||||
var COLORED_BALLS_POINTS: Array[int] = [
|
||||
2, 3, 4,
|
||||
5, 6, 7
|
||||
]
|
||||
const COLORED_BALLS := {
|
||||
"yellow_ball": 2, "green_ball": 3, "brown_ball": 4,
|
||||
"blue_ball": 5, "pink_ball": 6, "black_ball": 7,
|
||||
}
|
||||
|
||||
|
||||
func _init() -> void:
|
||||
func _init() -> void:
|
||||
pass
|
||||
|
||||
|
||||
func generate_ball_states() -> Dictionary:
|
||||
var state: Dictionary = {}
|
||||
|
||||
for i in COLORED_BALLS.size():
|
||||
var ball_name = COLORED_BALLS[i]
|
||||
var points = COLORED_BALLS_POINTS[i]
|
||||
|
||||
for ball_name in COLORED_BALLS:
|
||||
state[ball_name] = BALL_STATE.duplicate_deep()
|
||||
state[ball_name]["points"] = points
|
||||
state[ball_name]["points"] = COLORED_BALLS[ball_name]
|
||||
|
||||
# NOTE: Red balls
|
||||
for i: int in range(1, 16):
|
||||
var ball_name = "%02d_ball" % i
|
||||
var ball_name := "%02d_ball" % i
|
||||
state[ball_name] = BALL_STATE.duplicate_deep()
|
||||
state[ball_name]["points"] = 1
|
||||
|
||||
ball_states = state
|
||||
return state
|
||||
|
||||
func is_colored_ball(ball_name: String) -> bool:
|
||||
return ball_name in COLORED_BALLS
|
||||
|
||||
func reap_ball(ball: Node3D) -> void:
|
||||
super(ball)
|
||||
|
||||
if is_colored_ball(ball.name):
|
||||
# TODO: Colored ball must not be be destroyed. Handle rule...
|
||||
func on_ball_reaped(ball: Node3D) -> void:
|
||||
if ball.name in COLORED_BALLS:
|
||||
ball_states[ball.name]["is_foul"] = true
|
||||
|
||||
func process_ball(ball: Node3D) -> void:
|
||||
super(ball)
|
||||
func on_ball_sunk(ball: Node3D) -> void:
|
||||
if ball.name in COLORED_BALLS:
|
||||
# TODO: Check whether a red ball has been sunk first.
|
||||
ball_states[ball.name]["is_foul"] = true
|
||||
|
||||
if is_colored_ball(ball.name):
|
||||
# TODO: Colored ball must not be be processed before red ball sunk.
|
||||
# Handle rule...
|
||||
var red_ball_sunk = true
|
||||
if not red_ball_sunk:
|
||||
ball_states[ball.name]["is_foul"] = true
|
||||
|
||||
func handle_switch_user() -> void:
|
||||
push_warning("Balls reaped: ", balls_reaped.size() )
|
||||
push_warning("Balls sunk: ", balls_sunk.size() )
|
||||
var is_player_1: bool = "Player 1" == get_player()
|
||||
var switch_player: bool = true
|
||||
|
||||
if not balls_reaped.is_empty():
|
||||
for ball_name in balls_reaped:
|
||||
if is_player_1:
|
||||
player_1_points -= ball_states[ball_name]["points"]
|
||||
else:
|
||||
player_2_points -= ball_states[ball_name]["points"]
|
||||
|
||||
balls_reaped.clear()
|
||||
|
||||
if not balls_sunk.is_empty():
|
||||
# NOTE: Handle points, etc, insure color ball not sunk before red ball
|
||||
for ball_name in balls_sunk:
|
||||
if is_player_1:
|
||||
player_1_points += ball_states[ball_name]["points"]
|
||||
else:
|
||||
player_2_points += ball_states[ball_name]["points"]
|
||||
|
||||
switch_player = false
|
||||
balls_sunk.clear()
|
||||
|
||||
if ball_states_finalized():
|
||||
process_win_condition()
|
||||
return
|
||||
|
||||
if switch_player:
|
||||
Globals.game_state.switch_active_player()
|
||||
|
||||
turn_count += 1
|
||||
func score_sunk_balls() -> bool:
|
||||
return true
|
||||
|
||||
func process_win_condition() -> void:
|
||||
pass
|
||||
|
||||
Reference in New Issue
Block a user