Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Games are often high-performance affairs, in which case you may find that mutable state is just the thing you need.</p> <p>However, in this case, there are a couple of simple solutions:</p> <pre><code>case class MovingTexture(position: VecXY, velocity: VecXY) extends Renders with Advances { def advance(ms: Float) = copy(position = position + ms*velocity def accelerate(acc: Float, ms: Float) = copy(velocity = velocity + ms*acc) ... } </code></pre> <p>That is, instead of having your classes update themselves, have them return new copies of themselves. (You can see how this could get expensive quickly. For Tetris, no big deal. For Crysis? Maybe not so smart.) This seems like it just pushes the problem back one level: now you need a var for the <code>MovingTexture</code>, right? Not at all:</p> <pre><code>Iterator.iterate(MovingTexture(home, defaultSpeed))(_.advance(defaultStep)) </code></pre> <p>This will produce an endless stream of position updates in the same direction. You can do more complicated things to mix in user input or whatnot.</p> <p>Alternatively, you can</p> <pre><code>class Origin extends Renders { // All sorts of expensive stuff goes here } class Transposed(val ori: Origin, val position: VecXY) extends Renders with Advances { // Wrap TextureAtOrigin with inexpensive methods to make it act like it's moved def moving(vel: VecXY, ms: Float) = { Iterator.iterate(this).(tt =&gt; new Transposed(tt.ori, position+ms*vel)) } } </code></pre> <p>That is, have heavyweight things never be updated and have lighter-weight views of them that make them look as though they've changed in the way that you want them changed.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload