Skip to content

Commit

Permalink
feat: moved FreeSpit control to separate module
Browse files Browse the repository at this point in the history
  • Loading branch information
akorzunin committed Aug 23, 2024
1 parent e37ac78 commit 2f4e69a
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 43 deletions.
3 changes: 1 addition & 2 deletions src/components/controls/FaceLock.gd
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,9 @@ static func handle_rot_right(is_alt: bool) -> Quaternion:
return left_q.normalized().inverse()
return (alt_q * down_q.normalized().inverse()).inverse()

static func handle_face_lock_input(controlledNode: MeshIcosahedron):
static func handle_face_lock_input(controlledNode: MeshIcosahedron, is_inverted := false):
if controlledNode.is_rotating:
return
var is_inverted = G.settings.IS_CONTROL_INVERTED
var q: Quaternion
if Input.is_action_just_pressed("ui_up"):
if not controlledNode.is_alt:
Expand Down
19 changes: 19 additions & 0 deletions src/components/controls/FreeSpin.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
extends RefCounted
class_name FreeSpin

static func handle_free_spin_input(controlledNode: MeshInstance3D, rot_speed: float, is_inverted := false):
if not controlledNode:
return
var rotation: Quaternion

if Input.is_action_pressed("ui_up"):
rotation = rotation * Quaternion(0, 0, -rot_speed, 1, )
if Input.is_action_pressed("ui_down"):
rotation = rotation * Quaternion(0, 0, rot_speed, 1, )
if Op.xor(is_inverted, Input.is_action_pressed("ui_right")):
rotation = rotation * Quaternion(0, rot_speed, 0, 1, )
if Op.xor(is_inverted, Input.is_action_pressed("ui_left")):
rotation = rotation * Quaternion(0, -rot_speed, 0, 1, )
if rotation:
var t = rotation.normalized() * controlledNode.quaternion
controlledNode.transform.basis = Basis(t).orthonormalized()
26 changes: 4 additions & 22 deletions src/components/controls/LoopControls.gd
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ class_name LoopControls

@export var figureRoot: FigureRoot
@export var controlledNode: MeshIcosahedron
@export var ROTATION_SPEED: float
@onready var game_progress: GameProgress = %GameProgress
@onready var game_state_manager: GameStateManager = %GameStateManager
@onready var sfx_player: SfxPlayer = $"/root/MainScene/SfxPlayer"
Expand All @@ -13,7 +12,6 @@ enum ControlType {FREE_SPIN, FACE_LOCK}
# 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)
ROTATION_SPEED = G.settings.ROTATION_SPEED

func _on_game_state(old_state: GameStateManager.GameState, new_state: GameStateManager.GameState):
var gs := GameStateManager.GameState
Expand Down Expand Up @@ -99,25 +97,9 @@ func _process(delta: float) -> void:
return
if game_state_manager.game_state == GameStateManager.GameState.GAME_END:
return
var is_inverted = G.settings.IS_CONTROL_INVERTED
if G.settings.CONTROL_TYPE == "FACE_LOCK":
FaceLock.handle_face_lock_input(controlledNode)
FaceLock.handle_face_lock_input(controlledNode, is_inverted)
else:
handle_free_spin_input(delta)

# TODO make statc
func handle_free_spin_input(delta: float):
var rotation: Quaternion
var rs := ROTATION_SPEED / 10. * delta
var is_inverted = G.settings.IS_CONTROL_INVERTED

if Input.is_action_pressed("ui_up"):
rotation = rotation * Quaternion(0, 0, -rs, 1, )
if Input.is_action_pressed("ui_down"):
rotation = rotation * Quaternion(0, 0, rs, 1, )
if Op.xor(is_inverted, Input.is_action_pressed("ui_right")):
rotation = rotation * Quaternion(0, rs, 0, 1, )
if Op.xor(is_inverted, Input.is_action_pressed("ui_left")):
rotation = rotation * Quaternion(0, -rs, 0, 1, )
if controlledNode and rotation:
var t = rotation.normalized() * controlledNode.quaternion
controlledNode.transform.basis = Basis(t).orthonormalized()
var rot_speed: float = G.settings.ROTATION_SPEED / 10. * delta
FreeSpin.handle_free_spin_input(controlledNode, rot_speed, is_inverted)
21 changes: 2 additions & 19 deletions src/models/icosahedron/test/variant_test.gd
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ extends Node3D
const IcosahedronScene = preload('res://src/models/icosahedron/Icosahedron.tscn')
var controlledNode
# TODO get from defaults
var ROTATION_SPEED := 12
@export var ROTATION_SPEED: float = G.settings.ROTATION_SPEED
@export var show_face_numbers: bool = false
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
Expand All @@ -18,22 +18,5 @@ func _process(delta: float) -> void:
return
if Input.is_action_just_pressed('ui_accept'):
controlledNode.transform.basis = Basis(Quaternion()).orthonormalized()
handle_free_spin_input(delta)
pass

func handle_free_spin_input(delta: float):
var _rotation: Quaternion
var rs := ROTATION_SPEED / 10. * delta
var is_inverted = false

if Input.is_action_pressed("ui_up"):
_rotation = _rotation * Quaternion(0, 0, -rs, 1, )
if Input.is_action_pressed("ui_down"):
_rotation = _rotation * Quaternion(0, 0, rs, 1, )
if Op.xor(is_inverted, Input.is_action_pressed("ui_right")):
_rotation = _rotation * Quaternion(0, rs, 0, 1, )
if Op.xor(is_inverted, Input.is_action_pressed("ui_left")):
_rotation = _rotation * Quaternion(0, -rs, 0, 1, )
if controlledNode and _rotation:
var t = _rotation.normalized() * controlledNode.quaternion
controlledNode.transform.basis = Basis(t).orthonormalized()
FreeSpin.handle_free_spin_input(controlledNode, rs)

0 comments on commit 2f4e69a

Please sign in to comment.