Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit b970b8d0 authored by Dan Sandler's avatar Dan Sandler
Browse files

Expire landing flags after an hour.

To do this, factor out the TTL code from Spark into Fuse, a
new class that implements Removable by burning down the
given fuse to 0f.

Fixes: 345812036
Flag: com.android.egg.flags.flag_flag
Test: adb shell am start -n com.android.egg/.landroid.MainActivity
Test: manual instructions: let the screensaver run for a while
Change-Id: Ibb1d2d67d0b9ce577ce41224b9f061577bd5c018
parent c4252906
Loading
Loading
Loading
Loading
+14 −1
Original line number Diff line number Diff line
@@ -20,7 +20,7 @@ import android.util.ArraySet
import kotlin.random.Random

// artificially speed up or slow down the simulation
const val TIME_SCALE = 1f
const val TIME_SCALE = 1f // simulation seconds per wall clock second

// if it's been over 1 real second since our last timestep, don't simulate that elapsed time.
// this allows the simulation to "pause" when, for example, the activity pauses
@@ -36,6 +36,19 @@ interface Entity {
    fun postUpdate(sim: Simulator, dt: Float)
}

interface Removable {
    fun canBeRemoved(): Boolean
}

class Fuse(var lifetime: Float) : Removable {
    fun update(dt: Float) {
        lifetime -= dt
    }
    override fun canBeRemoved(): Boolean {
        return lifetime < 0
    }
}

open class Body(var name: String = "Unknown") : Entity {
    var pos = Vec2.Zero
    var opos = Vec2.Zero
+25 −18
Original line number Diff line number Diff line
@@ -43,11 +43,9 @@ const val CRAFT_SPEED_LIMIT = 5_000f
const val MAIN_ENGINE_ACCEL = 1000f // thrust effect, pixels per second squared
const val LAUNCH_MECO = 2f // how long to suspend gravity when launching

const val SCALED_THRUST = true
const val LANDING_REMOVAL_TIME = 3600f // one hour of simulation time

interface Removable {
    fun canBeRemoved(): Boolean
}
const val SCALED_THRUST = true

open class Planet(
    val orbitCenter: Vec2,
@@ -321,7 +319,7 @@ open class Universe(val namer: Namer, randomSeed: Long) : Simulator(randomSeed)
                        //
                        (1..10).forEach {
                            Spark(
                                    lifetime = rng.nextFloatInRange(0.5f, 2f),
                                    ttl = rng.nextFloatInRange(0.5f, 2f),
                                    style = Spark.Style.DOT,
                                    color = Color.White,
                                    size = 1f
@@ -359,13 +357,22 @@ open class Universe(val namer: Namer, randomSeed: Long) : Simulator(randomSeed)
        entities
            .filterIsInstance<Removable>()
            .filter(predicate = Removable::canBeRemoved)
            .filterIsInstance<Entity>()
            .forEach { remove(it) }
            .forEach { remove(it as Entity) }

        constraints
            .filterIsInstance<Removable>()
            .filter(predicate = Removable::canBeRemoved)
            .forEach { remove(it as Constraint) }
    }
}

class Landing(var ship: Spacecraft?, val planet: Planet, val angle: Float, val text: String = "") :
    Constraint {
class Landing(
    var ship: Spacecraft?,
    val planet: Planet,
    val angle: Float,
    val text: String = "",
    private val fuse: Fuse = Fuse(LANDING_REMOVAL_TIME)
) : Constraint, Removable by fuse {
    override fun solve(sim: Simulator, dt: Float) {
        ship?.let { ship ->
            val landingVector = Vec2.makeWithAngleMag(angle, ship.radius + planet.radius)
@@ -373,17 +380,20 @@ class Landing(var ship: Spacecraft?, val planet: Planet, val angle: Float, val t
            ship.pos = (ship.pos * 0.5f) + (desiredPos * 0.5f) // @@@ FIXME
            ship.angle = angle
        }

        fuse.update(dt)
    }
}

class Spark(
    var lifetime: Float,
    var ttl: Float,
    collides: Boolean = false,
    mass: Float = 0f,
    val style: Style = Style.LINE,
    val color: Color = Color.Gray,
    val size: Float = 2f
) : Removable, Body() {
    val size: Float = 2f,
    val fuse: Fuse = Fuse(ttl)
) : Removable by fuse, Body(name = "Spark") {
    enum class Style {
        LINE,
        LINE_ABSOLUTE,
@@ -398,10 +408,7 @@ class Spark(
    }
    override fun update(sim: Simulator, dt: Float) {
        super.update(sim, dt)
        lifetime -= dt
    }
    override fun canBeRemoved(): Boolean {
        return lifetime < 0
        fuse.update(dt)
    }
}

@@ -486,11 +493,11 @@ class Spacecraft : Body() {
            // exhaust
            sim.add(
                Spark(
                        lifetime = sim.rng.nextFloatInRange(0.5f, 1f),
                        ttl = sim.rng.nextFloatInRange(0.5f, 1f),
                        collides = true,
                        mass = 1f,
                        style = Spark.Style.RING,
                        size = 3f,
                        size = 1f,
                        color = Color(0x40FFFFFF)
                    )
                    .also { spark ->
+10 −2
Original line number Diff line number Diff line
@@ -30,6 +30,7 @@ import androidx.compose.ui.util.lerp
import androidx.core.math.MathUtils.clamp
import com.android.egg.flags.Flags.flagFlag
import java.lang.Float.max
import kotlin.math.exp
import kotlin.math.sqrt

const val DRAW_ORBITS = true
@@ -289,7 +290,8 @@ fun ZoomedDrawScope.drawLanding(landing: Landing) {

fun ZoomedDrawScope.drawSpark(spark: Spark) {
    with(spark) {
        if (lifetime < 0) return
        if (fuse.lifetime < 0) return
        val life = 1f - fuse.lifetime / ttl
        when (style) {
            Spark.Style.LINE ->
                if (opos != Vec2.Zero) drawLine(color, opos, pos, strokeWidth = size)
@@ -297,7 +299,13 @@ fun ZoomedDrawScope.drawSpark(spark: Spark) {
                if (opos != Vec2.Zero) drawLine(color, opos, pos, strokeWidth = size / zoom)
            Spark.Style.DOT -> drawCircle(color, size, pos)
            Spark.Style.DOT_ABSOLUTE -> drawCircle(color, size, pos / zoom)
            Spark.Style.RING -> drawCircle(color, size, pos, style = Stroke(width = 1f / zoom))
            Spark.Style.RING ->
                drawCircle(
                    color = color.copy(alpha = color.alpha * (1f - life)),
                    radius = exp(lerp(size, 3f * size, life)) - 1f,
                    center = pos,
                    style = Stroke(width = 1f / zoom)
                )
        }
    }
}