From 35c9bae69fcc24c395d1f4350355f5e766fdb266 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Alexis=20Laferri=C3=A8re?= Date: Tue, 11 Aug 2015 15:58:05 -0400 Subject: [PATCH] examples/moles: use a timeout to prevent changing state too fast MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Alexis Laferrière --- examples/mnit_moles/src/moles.nit | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/examples/mnit_moles/src/moles.nit b/examples/mnit_moles/src/moles.nit index 873bf5c..27ca3ad 100644 --- a/examples/mnit_moles/src/moles.nit +++ b/examples/mnit_moles/src/moles.nit @@ -23,6 +23,7 @@ module moles is end import mnit +import realtime import drawing @@ -45,21 +46,35 @@ class Hole # Content (and state) of this hole var content: nullable HoleContent = null + # Time at witch this hole can act again + var timeout_until: Float = app.clock.total.to_f is lazy + + # Timeout for 1/2 sec or less with a higher score + private fun reset_timeout do + timeout_until = app.clock.total.to_f + + 0.5 - 0.4 * (game.points.to_f / 1000.0).min(1.0) + end + fun do_turn do + if timeout_until > app.clock.total.to_f then return + var content = content if content != null then if content == game.down then if (20.0*game.speed_modifier).to_i.rand == 0 then # dead / hide self.content = null + reset_timeout end else if (80.0*game.speed_modifier).to_i.rand == 0 then # hide self.content = null + reset_timeout end else if (100.0*game.speed_modifier).to_i.rand == 0 then self.content = to_pop + reset_timeout end end @@ -290,6 +305,9 @@ redef class App # Numbers to display the score var numbers = new NumberImages(assets.n) + # Elapsed time since program launch + var clock = new Clock + redef fun on_start do super -- 1.7.9.5