Skip to content
Permalink
Browse files

feat: #7 - 🆕 speed of fences in preferences

- Default speed and factors in app config
- Choice of speed in preferences

Signed-off-by: Pierre-Yves Lapersonne (Orange) <pierreyves.lapersonne@orange.com>
  • Loading branch information...
pylapp committed Sep 11, 2019
1 parent 4902b4b commit fb5d284c2e87632b3fd94370aaeaf8ad37635d13
@@ -217,13 +217,17 @@ class InstrumentedTestPropertiesReader {

val defaultFencesNumber = appContext!!.loadProperties().getProperty(PropertiesKeys.GAME_SHEEP_DEFAULT_FENCES_NUMBER.key).toInt()
val defaultMaxNumber = appContext!!.loadProperties().getProperty(PropertiesKeys.GAME_SHEEP_MAX_FENCES_NUMBER.key).toInt()
val defaultSpeed = appContext!!.loadProperties().getProperty(PropertiesKeys.GAME_SHEEP_DEFAULT_SPEED_VALUE.key)
val defaultSpeed = appContext!!.loadProperties().getProperty(PropertiesKeys.GAME_SHEEP_DEFAULT_SPEED_VALUE.key).toLong()
val speedFactors = appContext!!.loadProperties().getProperty(PropertiesKeys.GAME_SHEEP_DEFAULT_SPEED_FACTOR.key).split(";")

val sheepDefaultGameConfiguration = appContext!!.readSheepDefaultConfiguration()

assertTrue(defaultFencesNumber == sheepDefaultGameConfiguration.defaultFencesCount)
assertTrue(defaultMaxNumber == sheepDefaultGameConfiguration.defaultMaxFencesCount)
assertTrue(defaultSpeed == sheepDefaultGameConfiguration.defaultSpeed)
assertTrue(sheepDefaultGameConfiguration.speedFactors.first == speedFactors[0].toFloat())
assertTrue(sheepDefaultGameConfiguration.speedFactors.second == speedFactors[1].toFloat())
assertTrue(sheepDefaultGameConfiguration.speedFactors.third == speedFactors[2].toFloat())

}

@@ -71,6 +71,10 @@ game_sheep_fences_max_number = 10
# Default speed value for the fences (in ms), on which a factor should be applied (difficulty_factor_values)
game_sheep_fences_default_speed = 20000

# Factor applied to default speed of fences
# low, medium, high
game_sheep_fences_default_speed_factor = 1;0.6;0.3

# #######
# Sensors
# #######
@@ -24,6 +24,7 @@ game_sheep_introduction_animation_period = [0-9]+
game_sheep_fences_default_number = [0-9]+
game_sheep_fences_max_number = [0-9]+
game_sheep_fences_default_speed = [0-9]+
game_sheep_fences_default_speed_factor = [0-9]+\.[0-9]+;[0-9]+\.[0-9]+;[0-9]+\.[0-9]+

sensor_series_queue_size = [0-9]+
sensor_series_interval_for_update = [0-9]+
@@ -496,7 +496,7 @@ class GameSheepFragment : AbstractGameFragment() {

fencesAnimator = ObjectAnimator.ofFloat(fence, "translationX",
metrics.widthPixels.toFloat(), metrics.widthPixels * -1f)
fencesAnimator.duration = defaultGameConfiguration.defaultSpeed.toLong()
fencesAnimator.duration = computeFencesSpeed()
fencesAnimator.repeatCount = totalNumberOfFences - 1 // For k fences, repeat k-1 times
fencesAnimator.repeatMode = ValueAnimator.RESTART

@@ -550,4 +550,22 @@ class GameSheepFragment : AbstractGameFragment() {
}
}

/**
* Reads from preferences the speed of fences.
* Then get from app config the values to apply to compute this speed, i.e. the default speed
* and the speed factor
*
* @return Long The speed for the fences
*/
private fun computeFencesSpeed(): Long {
val speedPreference = PreferenceManager.getDefaultSharedPreferences(activity).getInt("pref_key_settings_game_sheep_fences_speed", 0)
val speedFactor = when(speedPreference) {
1 -> defaultGameConfiguration.speedFactors.second
2 -> defaultGameConfiguration.speedFactors.third
else /* including 0 */ -> defaultGameConfiguration.speedFactors.first
}
return (defaultGameConfiguration.defaultSpeed * speedFactor).toLong()

}

}
@@ -112,7 +112,8 @@ class SettingsActivity : AppCompatActivity() {
prepareVersionPreference()
prepare3rdPartyPreference()
prepareDifficultyFactorPreference()
prepareSheepGamePreference()
prepareSheepGameFencesPreference()
prepareSheepGameSpeedPreference()
}

/**
@@ -170,7 +171,7 @@ class SettingsActivity : AppCompatActivity() {
/**
* Defines minimum, maximum and defaults values for the slider
*/
private fun prepareSheepGamePreference() {
private fun prepareSheepGameFencesPreference() {
val sheepGameDefaultConfiguration = activity!!.readSheepDefaultConfiguration()
val numberOfFencesPreferences = findPreference("pref_key_settings_game_sheep_fences_number") as SeekBarPreference
// TODO If never defined use default value from properties
@@ -186,6 +187,20 @@ class SettingsActivity : AppCompatActivity() {
numberOfFencesPreferences.setDefaultValue(sheepGameDefaultConfiguration.defaultFencesCount)
}

/**
* Defines minimum, maximum and defaults values for the slider
*/
private fun prepareSheepGameSpeedPreference() {
val speedPreference = findPreference("pref_key_settings_game_sheep_fences_speed") as SeekBarPreference
speedPreference.setOnPreferenceChangeListener { _, newValue ->
val preferences = PreferenceManager.getDefaultSharedPreferences(context)
val editor = preferences.edit()
editor.putInt("pref_key_settings_game_sheep_fences_speed", newValue as Int)
editor.apply()
true
}
}

} // End of class MySettingsFragment

} // End of class SettingsActivity : AppCompatActivity()
@@ -110,6 +110,10 @@ enum class PropertiesKeys(val key: String) {
* Defines the default speed for the sheep / fences, i.e. the speed for which the floor with fences move
*/
GAME_SHEEP_DEFAULT_SPEED_VALUE("game_sheep_fences_default_speed"),
/**
* The numeric values for the factor to apply to the default speed (low, medium and high).
*/
GAME_SHEEP_DEFAULT_SPEED_FACTOR("game_sheep_fences_default_speed_factor"),
/**
* Defines the default number of fences to jump over
*/
@@ -193,8 +193,11 @@ fun Context.readSheepDefaultConfiguration(): SheepGameDefaultConfiguration {
val properties = loadProperties()
val defaultFencesNumber = properties.getProperty(PropertiesKeys.GAME_SHEEP_DEFAULT_FENCES_NUMBER.key).toInt()
val defaultFencesMaxNumber = properties.getProperty(PropertiesKeys.GAME_SHEEP_MAX_FENCES_NUMBER.key).toInt()
val defaultSpeed = properties.getProperty(PropertiesKeys.GAME_SHEEP_DEFAULT_SPEED_VALUE.key)
return SheepGameDefaultConfiguration(defaultFencesNumber, defaultFencesMaxNumber, defaultSpeed)
val defaultSpeed = properties.getProperty(PropertiesKeys.GAME_SHEEP_DEFAULT_SPEED_VALUE.key).toLong()
val speedFactors = properties.getProperty(PropertiesKeys.GAME_SHEEP_DEFAULT_SPEED_FACTOR.key).split(DELIMITER)
return SheepGameDefaultConfiguration(defaultFencesNumber, defaultFencesMaxNumber, defaultSpeed,
Triple(speedFactors[0].toFloat(), speedFactors[1].toFloat(), speedFactors[2].toFloat())
)
}

/**
@@ -303,9 +306,11 @@ data class SheepGameConfiguration(val moveOffset: Int, val walkAnimationPeriod:
* Models a bundle of sheep game default configuration values..
* @param defaultFencesCount The default number of fences to display
* @param defaultMaxFencesCount The highest number of fences to display
* @param defaultSpeed The default speed for the floor and fences
* @param defaultSpeed The default speed for the floor and fences (in ms)
* @param speedFactors A triple with factors to apply to default speed (low, medium and high)
*/
data class SheepGameDefaultConfiguration(val defaultFencesCount: Int, val defaultMaxFencesCount: Int, val defaultSpeed: String)
data class SheepGameDefaultConfiguration(val defaultFencesCount: Int, val defaultMaxFencesCount: Int,
val defaultSpeed: Long, val speedFactors: Triple<Float, Float, Float>)

/**
* Models a bundle of configuration details for sensor data series
@@ -43,6 +43,8 @@
<string name="preferences_game_sheep_title">Saute mouton</string>
<string name="preferences_game_sheep_fences_number_title">Barrières à franchir</string>
<string name="preferences_game_sheep_fences_number_summary">Combien ?</string>
<string name="preferences_game_sheep_fences_speed_title">Vitesse des barrières</string>
<string name="preferences_game_sheep_fences_speed_summary">Lente, moyenne, forte</string>

<string name="selector_connect_hand_title">Choisi une boîte à utiliser</string>

@@ -43,6 +43,8 @@
<string name="preferences_game_sheep_title">Sheep game</string>
<string name="preferences_game_sheep_fences_number_title">Fences to jump over</string>
<string name="preferences_game_sheep_fences_number_summary">How many?</string>
<string name="preferences_game_sheep_fences_speed_title">Speed of fences</string>
<string name="preferences_game_sheep_fences_speed_summary">Slow, medium, high</string>

<string name="selector_connect_hand_title">Select a box to connected to</string>

@@ -63,6 +63,14 @@

</SeekBarPreference>

<SeekBarPreference
android:title="@string/preferences_game_sheep_fences_speed_title"
android:summary="@string/preferences_game_sheep_fences_speed_summary"
android:key="pref_key_settings_game_sheep_fences_speed"
android:max="2">

</SeekBarPreference>

</PreferenceCategory>

<!-- ************* -->
@@ -65,6 +65,7 @@ class UnitTestPropertiesKeys {
assertEquals(PropertiesKeys.GAME_SHEEP_MOVE_DURATION.key, "game_sheep_move_duration")
assertEquals(PropertiesKeys.GAME_SHEEP_INTRODUCTION_ANIMATION_PERIOD.key, "game_sheep_introduction_animation_period")
assertEquals(PropertiesKeys.GAME_SHEEP_DEFAULT_SPEED_VALUE.key, "game_sheep_fences_default_speed")
assertEquals(PropertiesKeys.GAME_SHEEP_DEFAULT_SPEED_FACTOR.key, "game_sheep_fences_default_speed_factor")
assertEquals(PropertiesKeys.GAME_SHEEP_DEFAULT_FENCES_NUMBER.key, "game_sheep_fences_default_number")
assertEquals(PropertiesKeys.GAME_SHEEP_MAX_FENCES_NUMBER.key, "game_sheep_fences_max_number")

0 comments on commit fb5d284

Please sign in to comment.
You can’t perform that action at this time.