Skip to content

Commit

Permalink
refactor: 更改接口
Browse files Browse the repository at this point in the history
  • Loading branch information
Jim-shop committed May 10, 2023
1 parent bf30962 commit 878a833
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ package net.imshit.aircraftwar.element.shoot
import net.imshit.aircraftwar.element.bullet.Bullets
import net.imshit.aircraftwar.logic.Games

interface ShootStrategy {
fun shoot(game: Games, x: Float, y: Float, speedY: Float, power: Int): List<Bullets>
abstract class ShootStrategies(val game: Games) {
abstract fun shoot(x: Float, y: Float, speedY: Float, power: Int): List<Bullets>
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@ package net.imshit.aircraftwar.element.shoot.enemy
import net.imshit.aircraftwar.element.bullet.EnemyBullet
import net.imshit.aircraftwar.logic.Games

class EnemyDirectShootStrategy : EnemyShootStrategy {
override fun shoot(
game: Games, x: Float, y: Float, speedY: Float, power: Int
): List<EnemyBullet> {
class EnemyDirectShootStrategy(game: Games) : EnemyShootStrategies(game = game) {
override fun shoot(x: Float, y: Float, speedY: Float, power: Int): List<EnemyBullet> {
val direction = 1
val bulletY = y + direction * 2
val bulletSpeedY = speedY + direction * 0.1f
return listOf(EnemyBullet(game, x, bulletY, 0f, bulletSpeedY, power))
return listOf(EnemyBullet(this.game, x, bulletY, 0f, bulletSpeedY, power))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ package net.imshit.aircraftwar.element.shoot.enemy
import net.imshit.aircraftwar.element.bullet.EnemyBullet
import net.imshit.aircraftwar.logic.Games

class EnemyNoShootStrategy : EnemyShootStrategy {
override fun shoot(
game: Games, x: Float, y: Float, speedY: Float, power: Int
): List<EnemyBullet> {
class EnemyNoShootStrategy(game: Games) : EnemyShootStrategies(game = game) {
override fun shoot(x: Float, y: Float, speedY: Float, power: Int): List<EnemyBullet> {
return listOf()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ package net.imshit.aircraftwar.element.shoot.enemy
import net.imshit.aircraftwar.element.bullet.EnemyBullet
import net.imshit.aircraftwar.logic.Games

class EnemyScatterShootStrategy : EnemyShootStrategy {
override fun shoot(
game: Games, x: Float, y: Float, speedY: Float, power: Int
): List<EnemyBullet> {
class EnemyScatterShootStrategy(game: Games) : EnemyShootStrategies(game = game) {
override fun shoot(x: Float, y: Float, speedY: Float, power: Int): List<EnemyBullet> {
val direction = 1
val shootNum = 3
val bulletY = y + direction * 2
Expand All @@ -16,7 +14,7 @@ class EnemyScatterShootStrategy : EnemyShootStrategy {
for (i in 0 until shootNum) {
add(
EnemyBullet(
game,
this@EnemyScatterShootStrategy.game,
x + (i * 2 - shootNum + 1) * 10,
bulletY,
bulletCenterSpeedX + (i * 2 - shootNum + 1) * 0.01f,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package net.imshit.aircraftwar.element.shoot.enemy

import net.imshit.aircraftwar.element.shoot.AbstractShootStrategyFactory
import net.imshit.aircraftwar.element.shoot.ShootStrategyFactories
import net.imshit.aircraftwar.logic.Games

class EnemyShootStrategyFactory : AbstractShootStrategyFactory() {
override fun getStrategy(shootNum: Int): EnemyShootStrategy {
class EnemyShootStrategyFactory(game: Games) : ShootStrategyFactories(game = game) {
override fun getStrategy(shootNum: Int): EnemyShootStrategies {
return when (shootNum) {
0 -> EnemyNoShootStrategy()
1 -> EnemyDirectShootStrategy()
3 -> EnemyScatterShootStrategy()
else -> EnemyNoShootStrategy()
0 -> EnemyNoShootStrategy(this.game)
1 -> EnemyDirectShootStrategy(this.game)
3 -> EnemyScatterShootStrategy(this.game)
else -> EnemyNoShootStrategy(this.game)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@ package net.imshit.aircraftwar.element.shoot.hero
import net.imshit.aircraftwar.element.bullet.HeroBullet
import net.imshit.aircraftwar.logic.Games

class HeroDirectShootStrategy : HeroShootStrategy {
override fun shoot(
game: Games, x: Float, y: Float, speedY: Float, power: Int
): List<HeroBullet> {
class HeroDirectShootStrategy(game: Games) : HeroShootStrategies(game = game) {
override fun shoot(x: Float, y: Float, speedY: Float, power: Int): List<HeroBullet> {
val direction = -1
val bulletY = y + direction * 2
val bulletSpeedY = speedY + direction * 0.2f
return listOf(HeroBullet(game, x, bulletY, 0f, bulletSpeedY, power))
return listOf(HeroBullet(this.game, x, bulletY, 0f, bulletSpeedY, power))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package net.imshit.aircraftwar.element.shoot.hero
import net.imshit.aircraftwar.element.bullet.HeroBullet
import net.imshit.aircraftwar.logic.Games

class HeroScatterShootStrategy : HeroShootStrategy {
class HeroScatterShootStrategy(game: Games) : HeroShootStrategies(game = game) {
override fun shoot(
game: Games, x: Float, y: Float, speedY: Float, power: Int
x: Float, y: Float, speedY: Float, power: Int
): List<HeroBullet> {
val direction = -1
val shootNum = 3
Expand All @@ -16,7 +16,7 @@ class HeroScatterShootStrategy : HeroShootStrategy {
for (i in 0 until shootNum) {
add(
HeroBullet(
game,
this@HeroScatterShootStrategy.game,
x + (i * 2 - shootNum + 1) * 10,
bulletY,
bulletCenterSpeedX + (i * 2 - shootNum + 1) * 0.01f,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package net.imshit.aircraftwar.element.shoot.hero

import net.imshit.aircraftwar.element.shoot.AbstractShootStrategyFactory
import net.imshit.aircraftwar.element.shoot.ShootStrategyFactories
import net.imshit.aircraftwar.logic.Games

class HeroShootStrategyFactory : AbstractShootStrategyFactory() {
override fun getStrategy(shootNum: Int): HeroShootStrategy {
class HeroShootStrategyFactory(game: Games) : ShootStrategyFactories(game = game) {
override fun getStrategy(shootNum: Int): HeroShootStrategies {
return when (shootNum) {
1 -> HeroDirectShootStrategy()
3 -> HeroScatterShootStrategy()
else -> HeroDirectShootStrategy()
1 -> HeroDirectShootStrategy(this.game)
3 -> HeroScatterShootStrategy(this.game)
else -> HeroDirectShootStrategy(this.game)
}
}
}

0 comments on commit 878a833

Please sign in to comment.