Skip to content

Commit

Permalink
Merge pull request #290 from Poobslag/utils-split
Browse files Browse the repository at this point in the history
Split Utils from Global.
  • Loading branch information
Poobslag authored Jun 3, 2020
2 parents 54fe958 + bc4f0e3 commit 18d3d4f
Show file tree
Hide file tree
Showing 22 changed files with 145 additions and 138 deletions.
1 change: 1 addition & 0 deletions project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,7 @@ PuzzleScore="*res://src/main/puzzle/puzzle-score.gd"
ResourceCache="*res://src/main/ResourceCache.tscn"
Scenario="*res://src/main/puzzle/scenario/scenario.gd"
StringUtils="*res://src/main/string-utils.gd"
Utils="*res://src/main/utils.gd"

[display]

Expand Down
6 changes: 3 additions & 3 deletions src/demo/chat/chat-frame-demo.gd
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ func _ready() -> void:


func _input(event: InputEvent) -> void:
match Global.key_scancode(event):
match Utils.key_scancode(event):
KEY_0, KEY_1, KEY_2, KEY_3, KEY_4, KEY_5, KEY_6, KEY_7, KEY_8, KEY_9:
if Input.is_key_pressed(KEY_SHIFT):
_who_index = Global.key_num(event)
_who_index = Utils.key_num(event)
else:
_text_index = Global.key_num(event)
_text_index = Utils.key_num(event)
_play_chat_event()
KEY_A:
if $ChatFrame.chat_window_showing():
Expand Down
6 changes: 3 additions & 3 deletions src/demo/chat/chat-ui-demo.gd
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ func _ready() -> void:


func _input(event: InputEvent) -> void:
match Global.key_scancode(event):
match Utils.key_scancode(event):
KEY_0, KEY_1, KEY_2, KEY_3, KEY_4, KEY_5, KEY_6, KEY_7, KEY_8, KEY_9:
if Input.is_key_pressed(KEY_CONTROL):
_choice_override = CHOICES[Global.key_num(event)]
_choice_override = CHOICES[Utils.key_num(event)]
else:
_text_override = SENTENCES[Global.key_num(event)]
_text_override = SENTENCES[Utils.key_num(event)]
_play_chat_tree()
KEY_Q:
_text_override = ""
Expand Down
2 changes: 1 addition & 1 deletion src/demo/chat/mood-demo.gd
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Keys:
"""

func _input(event: InputEvent) -> void:
match(Global.key_scancode(event)):
match(Utils.key_scancode(event)):
KEY_1: $Creature.play_mood(ChatEvent.Mood.DEFAULT)
KEY_Q: $Creature.play_mood(ChatEvent.Mood.SMILE0)
KEY_W: $Creature.play_mood(ChatEvent.Mood.SMILE1)
Expand Down
4 changes: 2 additions & 2 deletions src/demo/creature-view-demo.gd
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ func _ready() -> void:


func _input(event: InputEvent) -> void:
match Global.key_scancode(event):
match Utils.key_scancode(event):
KEY_F: $CreatureView/SceneClip/CreatureSwitcher/Scene.feed()
KEY_D: $CreatureView/SceneClip/CreatureSwitcher/Scene.play_door_chime(0)
KEY_V: $CreatureView.play_goodbye_voice()
KEY_0, KEY_1, KEY_2, KEY_3, KEY_4, KEY_5, KEY_6, KEY_7, KEY_8, KEY_9:
$CreatureView.set_fatness(FATNESS_KEYS[Global.key_num(event)])
$CreatureView.set_fatness(FATNESS_KEYS[Utils.key_num(event)])
KEY_Q: $CreatureView.set_current_creature_index(0)
KEY_W: $CreatureView.set_current_creature_index(1)
KEY_E: $CreatureView.set_current_creature_index(2)
Expand Down
2 changes: 1 addition & 1 deletion src/demo/music-player-demo.gd
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Keys:
"""

func _input(event: InputEvent) -> void:
match(Global.key_scancode(event)):
match(Utils.key_scancode(event)):
KEY_C: MusicPlayer.play_chill_bgm()
KEY_EQUAL: MusicPlayer.fade_in()
KEY_U: MusicPlayer.play_upbeat_bgm()
Expand Down
2 changes: 1 addition & 1 deletion src/demo/overworld-grid-map-demo.gd
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func _ready() -> void:


func _input(event: InputEvent) -> void:
match Global.key_scancode(event):
match Utils.key_scancode(event):
KEY_D: $GridMap.regenerate_dramatic_world()
KEY_E: $GridMap.regenerate_empty_world()
KEY_P: regenerate_path_world()
Expand Down
4 changes: 2 additions & 2 deletions src/demo/playfield-demo.gd
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ var _line_clear_count := 1
var _box_int := 0

func _input(event: InputEvent) -> void:
match(Global.key_scancode(event)):
KEY_1, KEY_2, KEY_3: _line_clear_count = Global.key_num(event)
match(Utils.key_scancode(event)):
KEY_1, KEY_2, KEY_3: _line_clear_count = Utils.key_num(event)

# make boxes
KEY_Q: _make_box(3)
Expand Down
1 change: 1 addition & 0 deletions src/demo/rank-calculation-demo.gd
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func _ready() -> void:

# calculate target lines and score for a fake scenario which resembles the game's scenario
Scenario.settings = _scenario_settings(data_key, start_level, seconds)
Scenario.launched_scenario_name = Scenario.settings.name
var target_score := _target_score(target_rank)
var target_lines := _target_lines(target_rank)

Expand Down
4 changes: 2 additions & 2 deletions src/demo/restaurant-scene-demo.gd
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ func _ready() -> void:


func _input(event: InputEvent) -> void:
match Global.key_scancode(event):
match Utils.key_scancode(event):
KEY_F: $RestaurantScene.feed()
KEY_0, KEY_1, KEY_2, KEY_3, KEY_4, KEY_5, KEY_6, KEY_7, KEY_8, KEY_9:
$RestaurantScene.set_fatness(FATNESS_KEYS[Global.key_num(event)])
$RestaurantScene.set_fatness(FATNESS_KEYS[Utils.key_num(event)])
KEY_BRACELEFT:
if _current_color_index == -1:
_current_color_index = 0
Expand Down
2 changes: 1 addition & 1 deletion src/demo/results-hud-demo.gd
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ func _ready() -> void:


func _input(event: InputEvent) -> void:
match(Global.key_scancode(event)):
match(Utils.key_scancode(event)):
KEY_S: $ResultsHud.show_results_message(_rank_result, _creature_scores, _finish_condition_type)
KEY_H: $ResultsHud.hide_results_message()
4 changes: 2 additions & 2 deletions src/demo/tutorial-hud-demo.gd
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ func _ready() -> void:


func _input(event: InputEvent) -> void:
match Global.key_scancode(event):
match Utils.key_scancode(event):
KEY_0, KEY_1, KEY_2, KEY_3, KEY_4, KEY_5, KEY_6, KEY_7, KEY_8, KEY_9:
_tutorial_hud.append_message(TEXTS[Global.key_num(event)])
_tutorial_hud.append_message(TEXTS[Utils.key_num(event)])
KEY_O:
_tutorial_hud.append_big_message("O/H/,/// M/Y/!/!/!")
KEY_H:
Expand Down
92 changes: 1 addition & 91 deletions src/main/global.gd
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
extends Node
"""
Contains global utilities, as well as variables for preserving state when loading different scenes.
Contains variables for preserving state when loading different scenes.
"""

const NUM_SCANCODES: Dictionary = {
KEY_0: 0, KEY_1: 1, KEY_2: 2, KEY_3: 3, KEY_4: 4,
KEY_5: 5, KEY_6: 6, KEY_7: 7, KEY_8: 8, KEY_9: 9,
}

# Target number of creature greetings (hello, goodbye) per minute
const GREETINGS_PER_MINUTE := 3.0

Expand Down Expand Up @@ -58,88 +53,3 @@ func should_chat() -> bool:
else:
should_chat = false
return should_chat


"""
Returns the scancode for a keypress event, or -1 if the event is not a keypress event.
"""
func key_scancode(event: InputEvent) -> int:
var scancode := -1
if event is InputEventKey and event.is_pressed() and not event.is_echo():
scancode = event.scancode
return scancode


"""
Returns [0-9] for a number key event, or -1 if the event is not a number key event.
"""
func key_num(event: InputEvent) -> int:
return NUM_SCANCODES.get(key_scancode(event), -1)


"""
Returns a vector corresponding to the direction the user is pressing.
Parameters:
'event': (Optional) The input event to be evaluated. If null, this method will evaluate all current inputs.
"""
func ui_pressed_dir(event: InputEvent = null) -> Vector2:
var ui_dir := Vector2.ZERO
if event:
if event.is_action_pressed("ui_up"):
ui_dir += Vector2.UP
if event.is_action_pressed("ui_down"):
ui_dir += Vector2.DOWN
if event.is_action_pressed("ui_left"):
ui_dir += Vector2.LEFT
if event.is_action_pressed("ui_right"):
ui_dir += Vector2.RIGHT
else:
if Input.is_action_pressed("ui_up"):
ui_dir += Vector2.UP
if Input.is_action_pressed("ui_down"):
ui_dir += Vector2.DOWN
if Input.is_action_pressed("ui_left"):
ui_dir += Vector2.LEFT
if Input.is_action_pressed("ui_right"):
ui_dir += Vector2.RIGHT
return ui_dir


"""
Returns 'true' if the player just released a direction key.
Parameters:
'event': (Optional) The input event to be evaluated. If null, this method will evaluate all current inputs.
"""
func ui_released_dir(event: InputEvent = null) -> bool:
var ui_dir := Vector2.ZERO
if event:
if event.is_action_released("ui_up"):
ui_dir += Vector2.UP
if event.is_action_released("ui_down"):
ui_dir += Vector2.DOWN
if event.is_action_released("ui_left"):
ui_dir += Vector2.LEFT
if event.is_action_released("ui_right"):
ui_dir += Vector2.RIGHT
else:
if Input.is_action_just_released("ui_up"):
ui_dir += Vector2.UP
if Input.is_action_just_released("ui_down"):
ui_dir += Vector2.DOWN
if Input.is_action_just_released("ui_left"):
ui_dir += Vector2.LEFT
if Input.is_action_just_released("ui_right"):
ui_dir += Vector2.RIGHT
return ui_dir.length() > 0


"""
Returns a transparent version of the specified color.
Tweening from forest green to 'Color.transparent' results in some strange in-between frames which are grey or white.
It's better to tween to a transparent forest green.
"""
static func to_transparent(color: Color, alpha := 0.0) -> Color:
return Color(color.r, color.g, color.b, alpha)
2 changes: 1 addition & 1 deletion src/main/puzzle/frosting-glob.gd
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func initialize(color: Color, new_position: Vector2) -> void:

modulate = color
$Tween.remove_all()
$Tween.interpolate_property(self, "modulate", color, Global.to_transparent(color), \
$Tween.interpolate_property(self, "modulate", color, Utils.to_transparent(color), \
DURATION, Tween.TRANS_CIRC, Tween.EASE_IN)
$Tween.start()

Expand Down
2 changes: 1 addition & 1 deletion src/main/puzzle/milestone-hud.gd
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func update_milebar_color() -> void:
level_color = LEVEL_COLOR_1
else:
level_color = LEVEL_COLOR_0
$ProgressBar.get("custom_styles/fg").set_bg_color(Global.to_transparent(level_color, 0.333))
$ProgressBar.get("custom_styles/fg").set_bg_color(Utils.to_transparent(level_color, 0.333))


"""
Expand Down
9 changes: 6 additions & 3 deletions src/main/puzzle/piece/piece-manager.gd
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,9 @@ Writes the current piece to the playfield, checking whether it makes any boxes o
Returns true if the newly written piece results in a line clear.
"""
func write_piece_to_playfield() -> bool:
var caused_line_clear := _playfield.write_piece(piece.pos, piece.orientation, piece.type)
func write_piece_to_playfield() -> void:
_playfield.write_piece(piece.pos, piece.orientation, piece.type)
clear_piece()
return caused_line_clear


"""
Expand Down Expand Up @@ -435,6 +434,10 @@ func apply_lock() -> void:
piece.lock = 0


func is_playfield_clearing_lines() -> bool:
return _playfield.remaining_line_clear_frames > 0


func _is_cell_blocked(pos: Vector2) -> bool:
var blocked := false
if pos.x < 0 or pos.x >= Playfield.COL_COUNT: blocked = true
Expand Down
3 changes: 2 additions & 1 deletion src/main/puzzle/piece/states/prelock.gd
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ func update(piece_manager: PieceManager) -> String:
if piece_manager.apply_player_input():
new_state_name = "MovePiece"
elif frames >= PieceSpeeds.current_speed.post_lock_delay:
piece_manager.write_piece_to_playfield()
var spawn_delay: float
if piece_manager.write_piece_to_playfield():
if piece_manager.is_playfield_clearing_lines():
# line was cleared; different appearance delay
spawn_delay = PieceSpeeds.current_speed.line_appearance_delay
else:
Expand Down
6 changes: 3 additions & 3 deletions src/main/puzzle/playfield-fx.gd
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,9 @@ func _calculate_brightness(combo: int) -> void:
Adjust the modulation for the lights.
"""
func _remodulate_lights(combo: int) -> void:
$LightMap.modulate = Global.to_transparent(_color)
$GlowMap.modulate = Global.to_transparent(_color)
$BgStrobe.color = Global.to_transparent(_color)
$LightMap.modulate = Utils.to_transparent(_color)
$GlowMap.modulate = Utils.to_transparent(_color)
$BgStrobe.color = Utils.to_transparent(_color)

_refresh_tilemaps(combo)

Expand Down
30 changes: 14 additions & 16 deletions src/main/puzzle/playfield.gd
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ var lines_being_cleared := []
var _cleared_line_index := 0

# Stores timing values to ensure lines are erased one at a time with consistent timing.
# Lines are erased when '_remaining_line_clear_frames' falls below the values in this array.
# Lines are erased when 'remaining_line_clear_frames' falls below the values in this array.
var _remaining_line_clear_timings := []

# remaining frames to wait for clearing the current lines
var _remaining_line_clear_frames := 0
var remaining_line_clear_frames := 0

# remaining frames to wait for making the current box
var _remaining_box_build_frames := 0
Expand All @@ -64,19 +64,19 @@ func _physics_process(delta: float) -> void:
if _remaining_box_build_frames > 0:
_remaining_box_build_frames -= 1
if _remaining_box_build_frames <= 0:
if _remaining_line_clear_frames > 0:
if remaining_line_clear_frames > 0:
_schedule_full_row_line_clears()
else:
emit_signal("after_piece_written")
elif _remaining_line_clear_frames > 0:
elif remaining_line_clear_frames > 0:
if _cleared_line_index < lines_being_cleared.size() \
and _remaining_line_clear_frames <= _remaining_line_clear_timings[_cleared_line_index]:
and remaining_line_clear_frames <= _remaining_line_clear_timings[_cleared_line_index]:
clear_line(lines_being_cleared[_cleared_line_index], lines_being_cleared.size(),
lines_being_cleared.size() - _cleared_line_index - 1)
_cleared_line_index += 1

_remaining_line_clear_frames -= 1
if _remaining_line_clear_frames <= 0:
remaining_line_clear_frames -= 1
if remaining_line_clear_frames <= 0:
_cleared_line_index = 0
_delete_rows()
emit_signal("after_piece_written")
Expand Down Expand Up @@ -110,7 +110,7 @@ Returns false the playfield is paused for an of animation or delay which should
"""
func ready_for_new_piece() -> bool:
return _remaining_box_build_frames <= 0 \
and _remaining_line_clear_frames <= 0 \
and remaining_line_clear_frames <= 0 \
and _remaining_misc_delay_frames <= 0


Expand All @@ -119,9 +119,9 @@ Writes a piece to the playfield, checking whether it makes any boxes or clears a
Returns true if the written piece results in a line clear.
"""
func write_piece(pos: Vector2, orientation: int, type: PieceType, death_piece := false) -> bool:
func write_piece(pos: Vector2, orientation: int, type: PieceType, death_piece := false) -> void:
_remaining_box_build_frames = 0
_remaining_line_clear_frames = 0
remaining_line_clear_frames = 0

$TileMapClip/TileMap.save_state()

Expand All @@ -134,12 +134,10 @@ func write_piece(pos: Vector2, orientation: int, type: PieceType, death_piece :=
_process_boxes()
_schedule_full_row_line_clears()

if _remaining_box_build_frames == 0 and _remaining_line_clear_frames == 0:
if _remaining_box_build_frames == 0 and remaining_line_clear_frames == 0:
# If any boxes are being made or lines are being cleared, we emit the
# signal later. Otherwise we emit it now.
emit_signal("after_piece_written")

return _remaining_line_clear_frames > 0


"""
Expand Down Expand Up @@ -327,12 +325,12 @@ func schedule_line_clears(lines_to_clear: Array, line_clear_delay: int, award_po

# Calculate the timing values when lines will be cleared. Set at least line
# clear frame; processing occurs when the frame goes from 1 -> 0
_remaining_line_clear_frames = max(1, line_clear_delay)
remaining_line_clear_frames = max(1, line_clear_delay)
_remaining_line_clear_timings.clear()
var _line_erase_timing_window := LINE_ERASE_TIMING_PCT * _remaining_line_clear_frames
var _line_erase_timing_window := LINE_ERASE_TIMING_PCT * remaining_line_clear_frames
var _per_line_frame_delay := floor(_line_erase_timing_window / max(1, lines_being_cleared.size() - 1))
for i in range(lines_being_cleared.size()):
_remaining_line_clear_timings.append(_remaining_line_clear_frames - i * _per_line_frame_delay)
_remaining_line_clear_timings.append(remaining_line_clear_frames - i * _per_line_frame_delay)


"""
Expand Down
Loading

0 comments on commit 18d3d4f

Please sign in to comment.