From 418a97f99ae3abc9417360a5704778c926def7e9 Mon Sep 17 00:00:00 2001 From: Atos1337 Date: Sat, 23 Oct 2021 12:12:19 +0300 Subject: [PATCH] Create Coordinates class instead of Pair and refactor state update --- .../controls/demo/virtual_car/VirtualCar.kt | 62 +++++++++---------- .../demo/virtual_car/VirtualCarController.kt | 2 +- 2 files changed, 32 insertions(+), 32 deletions(-) diff --git a/demo/src/main/kotlin/ru/mipt/npm/controls/demo/virtual_car/VirtualCar.kt b/demo/src/main/kotlin/ru/mipt/npm/controls/demo/virtual_car/VirtualCar.kt index b886d6b..738b3dd 100644 --- a/demo/src/main/kotlin/ru/mipt/npm/controls/demo/virtual_car/VirtualCar.kt +++ b/demo/src/main/kotlin/ru/mipt/npm/controls/demo/virtual_car/VirtualCar.kt @@ -10,22 +10,22 @@ import java.time.Instant import kotlin.time.Duration import kotlin.time.ExperimentalTime +data class Coordinates(val x: Double = 0.0, val y: Double = 0.0) + class VirtualCar : DeviceBySpec(VirtualCar) { - private var _speedState: Pair = Pair(0.0, 0.0) - private val speedState: Pair - get() { + private var speedState: Coordinates = Coordinates() + private fun updateAndGetSpeed(): Coordinates { updateSpeedLocationTime() - return this._speedState + return this.speedState } - private var _locationState: Pair = Pair(0.0, 0.0) - private val locationState: Pair - get() { + private var locationState: Coordinates = Coordinates() + private fun updateAndGetLocation(): Coordinates { updateSpeedLocationTime() - return this._locationState + return this.locationState } - private var accelerationState: Pair = Pair(0.0, 0.0) + private var accelerationState: Coordinates = Coordinates() set(value) { updateSpeedLocationTime() field = value @@ -34,52 +34,52 @@ class VirtualCar : DeviceBySpec(VirtualCar) { private var timeState = Instant.now().toEpochMilli().toDouble() private fun updateSpeedLocationTime() { - val previousSpeed = this._speedState - val previousLocation = this._locationState + val previousSpeed = this.speedState + val previousLocation = this.locationState val currentAcceleration = this.accelerationState val now = Instant.now().toEpochMilli().toDouble() val timeDifference = now - this.timeState this.timeState = now - this._speedState = Pair( - previousSpeed.first + timeDifference * currentAcceleration.first * 1e-3, - previousSpeed.second + timeDifference * currentAcceleration.second * 1e-3 + this.speedState = Coordinates( + previousSpeed.x + timeDifference * currentAcceleration.x * 1e-3, + previousSpeed.y + timeDifference * currentAcceleration.y * 1e-3 ) - val locationDifference = Pair( - timeDifference * 1e-3 * (previousSpeed.first + currentAcceleration.first * timeDifference * 1e-3 / 2), - timeDifference * 1e-3 * (previousSpeed.second + currentAcceleration.second * timeDifference * 1e-3 / 2) + val locationDifference = Coordinates( + timeDifference * 1e-3 * (previousSpeed.x + currentAcceleration.x * timeDifference * 1e-3 / 2), + timeDifference * 1e-3 * (previousSpeed.y + currentAcceleration.y * timeDifference * 1e-3 / 2) ) - this._locationState = Pair( - previousLocation.first + locationDifference.first, - previousLocation.second + locationDifference.second + this.locationState = Coordinates( + previousLocation.x + locationDifference.x, + previousLocation.y + locationDifference.y ) } - object DoublePairMetaConverter : MetaConverter> { - override fun metaToObject(meta: Meta): Pair = Pair( + object CoordinatesMetaConverter : MetaConverter { + override fun metaToObject(meta: Meta): Coordinates = Coordinates( meta["x"].double ?: 0.0, meta["y"].double ?: 0.0 ) - override fun objectToMeta(obj: Pair): Meta = Meta { - "x" put obj.first - "y" put obj.second + override fun objectToMeta(obj: Coordinates): Meta = Meta { + "x" put obj.x + "y" put obj.y } } companion object : DeviceSpec(::VirtualCar) { - val speed by property(DoublePairMetaConverter) { this.speedState } + val speed by property(CoordinatesMetaConverter) { this.updateAndGetSpeed() } - val location by property(DoublePairMetaConverter) { this.locationState } + val location by property(CoordinatesMetaConverter) { this.updateAndGetLocation() } - val acceleration by property(DoublePairMetaConverter, VirtualCar::accelerationState) + val acceleration by property(CoordinatesMetaConverter, VirtualCar::accelerationState) val carProperties by metaProperty { Meta { val time = Instant.now() "time" put time.toEpochMilli() - "speed" put DoublePairMetaConverter.objectToMeta(read(speed)) - "location" put DoublePairMetaConverter.objectToMeta(read(location)) - "acceleration" put DoublePairMetaConverter.objectToMeta(read(acceleration)) + "speed" put CoordinatesMetaConverter.objectToMeta(read(speed)) + "location" put CoordinatesMetaConverter.objectToMeta(read(location)) + "acceleration" put CoordinatesMetaConverter.objectToMeta(read(acceleration)) } } diff --git a/demo/src/main/kotlin/ru/mipt/npm/controls/demo/virtual_car/VirtualCarController.kt b/demo/src/main/kotlin/ru/mipt/npm/controls/demo/virtual_car/VirtualCarController.kt index 314bccb..7be087e 100644 --- a/demo/src/main/kotlin/ru/mipt/npm/controls/demo/virtual_car/VirtualCarController.kt +++ b/demo/src/main/kotlin/ru/mipt/npm/controls/demo/virtual_car/VirtualCarController.kt @@ -97,7 +97,7 @@ class VirtualCarControllerView : View(title = " Virtual car controller remote") action { controller.device?.run { launch { - acceleration.write(Pair(accelerationXProperty.get(), accelerationYProperty.get())) + acceleration.write(Coordinates(accelerationXProperty.get(), accelerationYProperty.get())) } } }