Skip to content

Commit

Permalink
feat: added GAME_SPEED setting w/ reload config
Browse files Browse the repository at this point in the history
  • Loading branch information
akorzunin committed Sep 13, 2024
1 parent 16bd831 commit 8ef5503
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 9 deletions.
5 changes: 5 additions & 0 deletions project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ ui_pause={
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"button_index":6,"pressure":0.0,"pressed":true,"script":null)
]
}
ui_reload={
"deadzone": 0.5,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":82,"key_label":0,"unicode":82,"location":0,"echo":false,"script":null)
]
}

[input_devices]

Expand Down
11 changes: 11 additions & 0 deletions src/components/controls/CommonControls.gd
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,14 @@ func _unhandled_input(event: InputEvent) -> void:
toggle_debug_stats.emit(state)
config._on_debug_stats_state(state)
get_viewport().set_input_as_handled()
if event.is_action_pressed('ui_reload'):
G.reload_settings.emit()

func _notification(what: int):
if what == NOTIFICATION_WM_GO_BACK_REQUEST:
var p := Utils.get_platform()
if p == Utils.Platform.MOBILE:
# Back button on android
InputEmit.new().emit({
action = 'ui_cancel'
})
5 changes: 5 additions & 0 deletions src/components/settings/Config.gd
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ func _init() -> void:
func _ready() -> void:
set_fps_counter_state.connect(_on_fps_counter_state)
set_debug_stats_state.connect(_on_debug_stats_state)
G.reload_settings.connect(_on_reload_settings)

func _on_reload_settings():
var gs = SettingsConfig.load_gs(config)
G.settings = gs

func _on_fps_counter_state(state: bool):
G.settings.FPS_COUNTER_ENABLED = state
Expand Down
3 changes: 2 additions & 1 deletion src/components/settings/DefaultConfig.gd
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const settings = {
SPAWN_MODE=2,
DESPAWNER_MODE=16633,
SCALE_FACTOR=10,
SPAWN_SPEED=10,
GAME_SPEED=10,
ROTATION_SPEED=12,
MAX_LEVEL=0,
},
Expand All @@ -18,6 +20,5 @@ const settings = {
VSYNC_ENABLED=true,
CONTROL_TYPE="FREE_SPIN",
IS_CONTROL_INVERTED=false,
DEFAULT_LEVEL=0,
}
}
12 changes: 7 additions & 5 deletions src/components/timers/LoopTimer.gd
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@ var time_start: int
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
game_state_manager.game_state_changed.connect(_on_game_state_changed)
G.reload_settings.connect(_on_reload_settings)

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
pass
func _on_reload_settings():
await timeout
stop()
start(100. / (G.settings.SPAWN_SPEED * G.settings.GAME_SPEED))

func _on_game_state_changed(old_state: GameStateManager.GameState, new_state: GameStateManager.GameState) -> void:
var gs = GameStateManager.GameState
if new_state == gs.GAME_ACTIVE:
start(10. / G.settings.get("SPAWN_SPEED", 10))
start(100. / (G.settings.SPAWN_SPEED * G.settings.GAME_SPEED))
time_start = Time.get_ticks_msec()
if new_state in [gs.GAME_END, gs.GAME_MENU]:
stop()
Expand All @@ -35,7 +37,7 @@ func get_elapsed_time() -> String:
return "000:000"
return format_time(raw_time)

func format_time(time: int) -> String:
static func format_time(time: int) -> String:
var ms := time % 1000
var sec := float(time) / 1000
return "%d:%03d" % [sec, ms]
6 changes: 4 additions & 2 deletions src/components/timers/ScaleTimer.gd
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ class_name ScaleTimer

@onready var game_state_manager: GameStateManager = %GameStateManager

# 10 ms
const tick_dur := int(10. / 1000. )

# Called when the node enters the scene tree for the first time.
func _ready() -> void:
game_state_manager.game_state_changed.connect(_on_game_state)
autostart = true
# 10 ms
wait_time = 10. / 1000.
wait_time = tick_dur

func _on_game_state(old_state: GameStateManager.GameState, new_state: GameStateManager.GameState):
var gs = GameStateManager.GameState
Expand Down
3 changes: 3 additions & 0 deletions src/globals.gd
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,6 @@ signal font_changed(new_font: FontType)

@warning_ignore("unused_signal")
signal level_changed(new_level: int)

@warning_ignore("unused_signal")
signal reload_settings()
3 changes: 2 additions & 1 deletion src/models/icosahedron/components/Icosahedron.gd
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ func _ready() -> void:

func _on_scale_tick() -> void:
if scaling_enabled:
var sf: float = (G.settings.SCALE_FACTOR + 1000.) / 1000.
var sf: float = 1. + (G.settings.SCALE_FACTOR / 1000. ) \
* (0.5 + (G.settings.GAME_SPEED / (10. + G.settings.GAME_SPEED)))
scale_object_local(Vector3(sf, sf, sf))

func despawn():
Expand Down
48 changes: 48 additions & 0 deletions src/models/icosahedron/test/game_speed_test.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/usr/bin/env -S godot --headless -s --quit
extends SceneTree

var G ={
settings ={
SPAWN_MODE=2,
DESPAWNER_MODE=16633,
ROTATION_SPEED=12,
MAX_LEVEL=0,

SPAWN_SPEED=10,
SCALE_FACTOR=10,
GAME_SPEED=10,
}
}

func gs_calc(x: int):
return 0.5 + (x / (10. + x))

func get_loop():
return 100. / (G.settings.SPAWN_SPEED * G.settings.GAME_SPEED)

func get_scale():
# return 1. + ((G.settings.SCALE_FACTOR / 10.) * gs_calc(G.settings.GAME_SPEED)) / 100.
# return 1. + (
# (G.settings.SCALE_FACTOR / 10.)
# * (0.5 + (G.settings.GAME_SPEED / (10. + G.settings.GAME_SPEED)))
# ) / 100.
return 1. + (G.settings.SCALE_FACTOR / 1000. ) * (0.5 + (G.settings.GAME_SPEED / (10. + G.settings.GAME_SPEED)))

func main():
print_debug(gs_calc(20))

print_debug("base loop: ", 1, " > ", get_loop())
print_debug("base scale: ", 1.01, " > ", get_scale())
G.settings.GAME_SPEED = 5
print_debug("\nL2: gs=", G.settings.GAME_SPEED)
print_debug("loop: ", get_loop())
print_debug("scale: ", get_scale())
G.settings.GAME_SPEED = 20
print_debug("\nL3: gs=", G.settings.GAME_SPEED)
print_debug("loop: ", get_loop())
print_debug("scale: ", get_scale())
pass

func _init():
main()
quit()

0 comments on commit 8ef5503

Please sign in to comment.