Skip to content

Commit

Permalink
refactor: better oob detection
Browse files Browse the repository at this point in the history
  • Loading branch information
florianvazelle committed Feb 11, 2024
1 parent bbe67e0 commit 02b5944
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 14 deletions.
19 changes: 11 additions & 8 deletions scripts/gui/ranking.gd
Original file line number Diff line number Diff line change
Expand Up @@ -42,24 +42,27 @@ func more_checkpoint(a: Participant, b: Participant) -> bool:
var b_marble = b.get_marble()

var out: bool
if (
a_marble.has_finish() and b_marble.has_finish()
or a_marble.get_checkpoint_count() == b_marble.get_checkpoint_count()
):
if a_marble.has_finish() and b_marble.has_finish():
out = a.get_rank() < b.get_rank()
elif a_marble.has_finish():
out = true
elif b_marble.has_finish():
out = false
else:
if a_marble.has_explode() and b_marble.has_explode():
if (
(a_marble.has_explode() or a_marble.has_oob())
and (b_marble.has_explode() or b_marble.has_oob())
):
out = a.get_rank() < b.get_rank()
elif a_marble.has_explode():
elif a_marble.has_explode() or a_marble.has_oob():
out = false
elif b_marble.has_explode():
elif b_marble.has_explode() or b_marble.has_oob():
out = true
else:
out = a_marble.get_checkpoint_count() > b_marble.get_checkpoint_count()
if a_marble.get_checkpoint_count() == b_marble.get_checkpoint_count():
out = a.get_rank() < b.get_rank()
else:
out = a_marble.get_checkpoint_count() > b_marble.get_checkpoint_count()
return out


Expand Down
46 changes: 43 additions & 3 deletions scripts/main.gd
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ var _current_marble_index := 0
var _time := 0.0
var _explosion_enabled := false
var _race_has_started := false
var _lower_boundary = null

# Variables used in explosion mode to check
# if we need to generate another chunk of the race
Expand Down Expand Up @@ -105,6 +106,7 @@ func _unhandled_input(event):
KEY_R:
if _mode == State.MODE_MARBLE:
_race.generate_race(!_explosion_enabled)
_lower_boundary = get_lowest_piece(_race, true).global_transform.origin.y

KEY_SPACE:
for marble in _marbles:
Expand Down Expand Up @@ -157,13 +159,39 @@ func get_highest_piece() -> Piece:
var pieces = _race.get_children()
if len(pieces) == 0:
return null
var highest_piece = pieces[0]
var highest_piece = null
for piece in pieces:
if piece.position.y > highest_piece.position.y:
if not piece is Piece:
continue
if highest_piece == null:
highest_piece = piece
elif piece.position.y > highest_piece.position.y:
highest_piece = piece
return highest_piece


func get_lowest_piece(piece_or_race, recursive: bool = false) -> Piece:
var pieces = piece_or_race.get_children()
if len(pieces) == 0:
return null

# Find the lower piece from children of the current node3d
var lowest_piece = null
for piece in pieces:
if not piece is Piece:
continue
if lowest_piece == null:
lowest_piece = piece
elif piece.position.y < lowest_piece.position.y:
lowest_piece = piece

if recursive:
var child_lowest_piece = get_lowest_piece(lowest_piece)
if child_lowest_piece != null:
lowest_piece = child_lowest_piece
return lowest_piece


# Replace cameras with a new one
func replace_camera(new_camera, old_cameras) -> void:
# Ensure old cameras are removed from the current scene
Expand Down Expand Up @@ -212,11 +240,12 @@ func set_mode(mode):
# If no marbles exist
if start_a_new_race:
_podium.hide()

await Fade.fade_out(1, Color.BLACK, "Diamond", false, false).finished

_explosion_enabled = SettingsManager.get_value(&"marbles", &"explosion_enabled") as bool
_race.generate_race(!_explosion_enabled)
_lower_boundary = get_lowest_piece(_race, true).global_transform.origin.y

_overlay.reset()
reset_position()
Expand Down Expand Up @@ -316,6 +345,7 @@ func _process(delta):
if lap_count > _old_lap_count:
# Generate a chunk
_race.generate_chunk()
_lower_boundary = get_lowest_piece(_race, true).global_transform.origin.y
_old_lap_count = lap_count
else:
_panel_timer.hide()
Expand Down Expand Up @@ -344,6 +374,16 @@ func _process(delta):
_podium.set_second(_ranking._second_marble)
_podium.set_third(_ranking._third_marble)

# Check if some marbles are out of bound
if _race_has_started and _lower_boundary != null:
for marble in _marbles:
if (
marble.visible
and marble._state == Marble.State.ROLL
and marble.global_transform.origin.y + 100 < _lower_boundary
):
marble.out_of_bound()


# Handle victory conditions on explosion mode
func explosion_victory(_last_marble: Marble) -> bool:
Expand Down
3 changes: 0 additions & 3 deletions scripts/marble.gd
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,6 @@ func _process(_delta: float) -> void:
_name.global_transform.origin = offset
_score.global_transform.origin = offset + Vector3(-0.4, -0.2, 0)

if global_transform.origin.y < -10000:
out_of_bound()


func reset() -> void:
_start()
Expand Down

0 comments on commit 02b5944

Please sign in to comment.