Skip to content

Commit a3391b7

Browse files
committed
Easier explosions with Explosion
Wraps up an Exploder in a Target, so we can just hand off to it and not worry about anything else.
1 parent fc7d979 commit a3391b7

File tree

5 files changed

+105
-52
lines changed

5 files changed

+105
-52
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package uk.co.jezuk.swoop.craft
2+
3+
import android.graphics.Canvas
4+
import android.graphics.Paint
5+
import uk.co.jezuk.swoop.Game
6+
import uk.co.jezuk.swoop.geometry.Point
7+
import uk.co.jezuk.swoop.geometry.Rotation
8+
import uk.co.jezuk.swoop.geometry.Vector
9+
import uk.co.jezuk.swoop.wave.Wave
10+
11+
class Explosion(
12+
private val wave: Wave,
13+
pos: Point,
14+
private val velocity: Vector,
15+
initialOrientation: Rotation,
16+
private val rotation: Double,
17+
shape: FloatArray,
18+
brush: Paint,
19+
length: Int,
20+
useMid: Boolean = true,
21+
expansion: Float = 5f
22+
) : Target {
23+
private val exploder = Exploder(
24+
{ wave.removeTarget(this) },
25+
shape,
26+
brush,
27+
length,
28+
useMid,
29+
expansion
30+
)
31+
override val position = pos.copy()
32+
override val killDist = 0f
33+
private val orientation = initialOrientation.clone()
34+
35+
init {
36+
wave.addTarget(this)
37+
}
38+
39+
override fun update(frameRateScale: Float) {
40+
position.drift(velocity, frameRateScale)
41+
exploder.update(frameRateScale)
42+
} // update
43+
44+
override fun draw(canvas: Canvas) {
45+
canvas.save()
46+
47+
position.translate(canvas)
48+
orientation.rotate(canvas)
49+
50+
exploder.draw(canvas)
51+
52+
canvas.restore()
53+
} // draw
54+
55+
override fun shipCollision(ship: Ship) { }
56+
override fun shot(): Target.Impact = Target.Impact.NONE
57+
override fun explode() {}
58+
}

app/src/main/java/uk/co/jezuk/swoop/craft/Saucer.kt

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,10 @@ class Saucer(
2424
private val velocity = Vector(Random.nextDouble(3.0, 3.0 + aggressiveness), position.angleTo(traverse[1]))
2525
private val bounciness = Random.nextDouble(1.0, (aggressiveness * 2.0)) * Math.PI
2626
private val swinginess = Random.nextDouble(100.0, 100.0 + (50.0 * aggressiveness))
27-
private var bounceOffset = 0.0
2827
private val traverseLength = startPosition.distance(traverse[1])
2928
private val skew = Rotation.random()
3029
private val firedStep = if (wave.ship != null) Random.nextDouble(0.4 - (0.07 * aggressiveness), 0.4) else 1000.0
3130
private var fired = firedStep
32-
private var exploder: Exploder? = null
33-
34-
private val alive get() = (exploder == null)
3531

3632
init {
3733
game.sound(R.raw.sauceralarm, position)
@@ -42,8 +38,7 @@ class Saucer(
4238

4339
override fun update(frameRateScale: Float) {
4440
val distance = basePosition.distance(startPosition) / traverseLength
45-
if (alive)
46-
bounceOffset = sin(distance * bounciness) * swinginess
41+
val bounceOffset = sin(distance * bounciness) * swinginess
4742

4843
position.moveTo(basePosition)
4944
position.move(Vector(bounceOffset, skew), 1f, game.extent, killDist)
@@ -52,13 +47,9 @@ class Saucer(
5247
destroyed();
5348

5449
fireIfReady(distance)
55-
56-
if (!alive)
57-
exploder?.update(frameRateScale)
5850
} // update
5951

6052
private fun fireIfReady(distance: Float) {
61-
if (!alive) return
6253
if (fired > distance) return
6354

6455
fired += firedStep
@@ -74,34 +65,30 @@ class Saucer(
7465
canvas.save()
7566

7667
position.translate(canvas)
77-
if (alive)
78-
canvas.drawLines(shape, shipBrush)
79-
else
80-
exploder?.draw(canvas)
68+
69+
canvas.drawLines(shape, shipBrush)
8170

8271
canvas.restore()
8372
} // draw
8473

8574
override fun shot(): Target.Impact {
86-
if (alive) {
87-
game.scored(1500)
88-
explode()
89-
return Target.Impact.HARD
90-
}
91-
return Target.Impact.NONE
75+
game.scored(1500)
76+
explode()
77+
return Target.Impact.HARD
9278
} // shot
9379

9480
override fun explode() {
95-
exploder = Exploder(::destroyed, shape, shipBrush, 70)
9681
game.sound(R.raw.saucerexplosion, position)
9782
BigPuff(wave, position)
83+
Explosion(
84+
wave, position, velocity, Rotation.none(), 0.0,
85+
shape, shipBrush, 70
86+
)
87+
destroyed()
9888
} // explode
9989

10090
override fun shipCollision(ship: Ship) = ship.hit()
101-
102-
private fun destroyed() {
103-
wave.removeTarget(this)
104-
}
91+
private fun destroyed() = wave.removeTarget(this)
10592

10693
companion object {
10794
val shape = floatArrayOf(

app/src/main/java/uk/co/jezuk/swoop/craft/Tumbler.kt

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,63 +20,54 @@ class Tumbler(
2020
override val position = Point(traverse[0])
2121
private val velocity = Vector(Random.nextDouble(2.0, 5.0), position.angleTo(traverse[1]))
2222
private val orientation = Rotation.random()
23-
private var rotation = Random.nextDouble(1.0, 2.5)
24-
private var shooter = Repeat(Random.nextInt(120, 180), { fire() })
25-
private var exploder: Exploder? = null
26-
27-
private val alive get() = (exploder == null)
23+
private val rotation = initialRotation()
24+
private val shooter = Repeat(Random.nextInt(120, 180), { fire() })
2825

2926
init {
3027
game.sound(R.raw.sauceralarm, position)
3128
wave.addTarget(this)
32-
33-
if (Random.nextDouble() < 0.5)
34-
rotation = -rotation
3529
} // init
3630

3731
override val killDist get() = 50f
3832

3933
override fun update(frameRateScale: Float) {
4034
position.move(velocity, frameRateScale, game.extent, killDist)
4135
orientation += rotation * frameRateScale
42-
if (alive)
43-
shooter.tick(frameRateScale)
44-
else
45-
exploder?.update(frameRateScale)
36+
shooter.tick(frameRateScale)
4637
} // update
4738

4839
override fun draw(canvas: Canvas) {
4940
canvas.save()
5041

5142
position.translate(canvas)
5243
orientation.rotate(canvas)
53-
if (alive)
54-
canvas.drawLines(tumblerShape, shipBrush)
55-
else
56-
exploder?.draw(canvas)
44+
45+
canvas.drawLines(tumblerShape, shipBrush)
5746

5847
canvas.restore()
5948
} // draw
6049

6150
override fun shot(): Target.Impact {
62-
if (alive) {
63-
game.scored(1000)
64-
explode()
65-
return Target.Impact.HARD
66-
}
67-
return Target.Impact.NONE
51+
game.scored(1000)
52+
explode()
53+
return Target.Impact.HARD
6854
} // shot
6955

7056
override fun explode() {
71-
exploder = Exploder({ wave.removeTarget(this) }, outerShape, shipBrush, 70)
7257
game.sound(R.raw.saucerexplosion, position)
7358
BigPuff(wave, position)
59+
Explosion(
60+
wave, position, velocity, orientation, rotation,
61+
outerShape, shipBrush, 70
62+
)
63+
Explosion(
64+
wave, position, velocity, orientation, rotation,
65+
cutOuts, shipBrush, 50, expansion = 3f
66+
)
67+
wave.removeTarget(this)
7468
} // explode
7569

76-
override fun shipCollision(ship: Ship) {
77-
if (alive)
78-
ship.hit()
79-
} // shipCollision
70+
override fun shipCollision(ship: Ship) = ship.hit()
8071

8172
private fun fire() {
8273
game.sound(R.raw.saucerfire, position)
@@ -91,6 +82,13 @@ class Tumbler(
9182
} // fire
9283

9384
companion object {
85+
private fun initialRotation(): Double {
86+
var rotation = Random.nextDouble(1.0, 2.5)
87+
if (Random.nextDouble() < 0.5)
88+
rotation = -rotation
89+
return rotation
90+
} // initialRotation
91+
9492
private val outerShape = floatArrayOf(
9593
60f, -25f, 60f, 25f,
9694
60f, 25f, 25f, 60f,

app/src/main/java/uk/co/jezuk/swoop/geometry/Point.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,15 @@ data class Point(
4646
y = target.y
4747
} // moveTo
4848

49+
fun drift(
50+
vec: Vector,
51+
frameScaleRate: Float
52+
) {
53+
val (deltaX, deltaY) = vec.offset(frameScaleRate)
54+
x += deltaX.toFloat()
55+
y += deltaY.toFloat()
56+
}
57+
4958
fun distance(pos: Point): Float {
5059
val offsetX = distanceBetween(x, pos.x)
5160
val offsetY = distanceBetween(y, pos.y)

app/src/main/java/uk/co/jezuk/swoop/geometry/Rotation.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,6 @@ class Rotation(
3737

3838
companion object {
3939
fun random() = Rotation(Random.nextDouble(360.0))
40+
fun none() = Rotation(0.0)
4041
} // companion object
4142
} // Rotation

0 commit comments

Comments
 (0)