Skip to content

Commit 282dc9d

Browse files
authored
CharacterRandomizer: Use own random number generator (#2131)
Avoid changing the seed of the base random number generator, which makes the rest of the game predictable. Instead, using its own RNG in this class. Pass the RNG as argument to the CelShadingRecolor and RandomTextureSpriteBehavior, which now accept one, while still falling back to use the base RNG by default. This way, the character is consistent with its character_seed. Luckly, characters randomized this way stay the same, because textures and skin colors are immediately called with the same seed as before. Also: Add a class name and make it inherit from CharacterBody2D. The script is attached to CharacterBody2D nodes in all cases. Fix #2130
1 parent 856660f commit 282dc9d

3 files changed

Lines changed: 16 additions & 8 deletions

File tree

scenes/game_elements/characters/components/character_randomizer.gd

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# SPDX-FileCopyrightText: The Threadbare Authors
22
# SPDX-License-Identifier: MPL-2.0
33
@tool
4-
extends Node2D
4+
class_name CharacterRandomizer
5+
extends CharacterBody2D
56
## @experimental
67
##
78
## Provide a single button to randomize various aspects of a character.
@@ -41,6 +42,8 @@ var random_texture_nodes: Array[RandomTextureSpriteBehavior] = []
4142

4243
var _undoredo: Object # EditorUndoRedoManager
4344

45+
var _random_number_generator := RandomNumberGenerator.new()
46+
4447
var _previous_look_at_side: Enums.LookAtSide = Enums.LookAtSide.UNSPECIFIED
4548

4649

@@ -49,18 +52,18 @@ var _previous_look_at_side: Enums.LookAtSide = Enums.LookAtSide.UNSPECIFIED
4952
## Do it in a consistent way by first seeding the default random number generator
5053
## with the [member character_seed].
5154
func apply_character_randomizations() -> void:
52-
seed(character_seed)
55+
_random_number_generator.seed = character_seed
5356

5457
if skin_recolor_nodes:
5558
var new_skin_medium_color: Color
56-
skin_recolor_nodes[-1].set_random_skin_color()
59+
skin_recolor_nodes[-1].set_random_skin_color(_random_number_generator)
5760
new_skin_medium_color = skin_recolor_nodes[-1].medium_color
5861
for n in skin_recolor_nodes:
5962
n.automatic_shades = true
6063
n.medium_color = new_skin_medium_color
6164

6265
for n in random_texture_nodes:
63-
n.randomize_texture()
66+
n.randomize_texture(_random_number_generator)
6467

6568

6669
## Set a random seed and randomize the character.

scenes/game_elements/components/cel_shading_recolor.gd

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,11 @@ func colorize() -> void:
159159

160160

161161
## Pick a random color from [constant SKIN_COLORS] and automatically set all shades from it.
162-
func set_random_skin_color() -> void:
162+
func set_random_skin_color(rng: RandomNumberGenerator = null) -> void:
163163
automatic_shades = true
164-
medium_color = SKIN_COLORS.values().pick_random()
164+
var random_int: int = rng.randi() if rng else randi()
165+
var index := random_int % SKIN_COLORS.size()
166+
medium_color = SKIN_COLORS.values()[index]
165167

166168

167169
func _get_configuration_warnings() -> PackedStringArray:

scenes/game_logic/sprite_behaviors/random_texture_sprite_behavior.gd

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,11 @@ func _offset_child_sprites(offset: Vector2) -> void:
4545

4646

4747
## Pick a random texture from [member textures] and set it to the sprite.
48-
func randomize_texture() -> void:
49-
var new_texture: Texture2D = textures.pick_random()
48+
func randomize_texture(rng: RandomNumberGenerator = null) -> void:
49+
var random_int: int = rng.randi() if rng else randi()
50+
var index := random_int % textures.size()
51+
var new_texture := textures[index]
52+
5053
var offset := _get_offset_from_texture_filename(new_texture)
5154
_offset_child_sprites(offset)
5255
SpriteFramesHelper.replace_texture(null, new_texture, sprite.sprite_frames)

0 commit comments

Comments
 (0)