June 2018
Intermediate to advanced
310 pages
6h 32m
English
First, let's discuss how we could solve this in the Java way.
In Java, we would have created an interface, that abstracts the changes. In our case, what changes is our hero's weapon:
interface Weapon { fun shoot(x: Int, y: Int, direction: Direction): Projectile}
Then all other weapons would implement this interface:
class Peashooter : Weapon { override fun shoot(x: Int, y: Int, direction: Direction) = object : Projectile(x, y, direction) { // Fly straight }}class Pomegranate : Weapon { override fun shoot(x: Int, y: Int, direction: Direction) = object : Projectile(x, y, direction) { // Explode when you hit first enemy }}class Banana : Weapon { override fun shoot(x: Int, y: Int, direction: Direction) = object : Projectile(x ...
Read now
Unlock full access