summaryrefslogtreecommitdiffstats
path: root/examples/sensors/grue/plugin/gruesensorimpl.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'examples/sensors/grue/plugin/gruesensorimpl.cpp')
-rw-r--r--examples/sensors/grue/plugin/gruesensorimpl.cpp17
1 files changed, 11 insertions, 6 deletions
diff --git a/examples/sensors/grue/plugin/gruesensorimpl.cpp b/examples/sensors/grue/plugin/gruesensorimpl.cpp
index d06ab8ee..7ea4aa00 100644
--- a/examples/sensors/grue/plugin/gruesensorimpl.cpp
+++ b/examples/sensors/grue/plugin/gruesensorimpl.cpp
@@ -46,6 +46,7 @@ char const * const gruesensorimpl::id("grue.gruesensor");
gruesensorimpl::gruesensorimpl(QSensor *sensor)
: QSensorBackend(sensor)
+ , lightLevel(QAmbientLightReading::Undefined)
{
// We need a light sensor
lightSensor = new QAmbientLightSensor(this);
@@ -95,13 +96,18 @@ void gruesensorimpl::stop()
void gruesensorimpl::lightChanged()
{
- qreal chance = 0.0;
+ if (lightLevel == lightSensor->reading()->lightLevel())
+ return;
+
+ lightLevel = lightSensor->reading()->lightLevel();
+
+ int chance = 0;
darkTimer->stop();
switch (lightSensor->reading()->lightLevel()) {
case QAmbientLightReading::Dark:
// It is dark. You are likely to be eaten by a grue.
- chance = 0.1;
+ chance = 10;
darkTimer->start();
break;
default:
@@ -112,7 +118,6 @@ void gruesensorimpl::lightChanged()
if (chance != m_reading.chanceOfBeingEaten() || m_reading.timestamp() == 0) {
m_reading.setTimestamp(timer.elapsed());
m_reading.setChanceOfBeingEaten(chance);
-
newReadingAvailable();
}
}
@@ -120,16 +125,16 @@ void gruesensorimpl::lightChanged()
void gruesensorimpl::increaseChance()
{
// The longer you stay in the dark, the higher your chance of being eaten
- qreal chance = m_reading.chanceOfBeingEaten() + 0.1;
+ int chance = m_reading.chanceOfBeingEaten() + 10;
m_reading.setTimestamp(timer.elapsed());
m_reading.setChanceOfBeingEaten(chance);
newReadingAvailable();
- // No point in using the timer anymore if we've hit 1... you can't get more
+ // No point in using the timer anymore if we've hit 100... you can't get more
// likely to be eaten than 100%
- if (chance >= 1.0)
+ if (chance >= 100)
darkTimer->stop();
}