Skip to content

Commit

Permalink
feat: 添加敌机射击策略
Browse files Browse the repository at this point in the history
  • Loading branch information
Jim-shop committed May 10, 2023
1 parent 0d78149 commit 24c38e4
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
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> {
val direction = 1
val bulletY = y + direction * 2
val bulletSpeedY = speedY + direction * 0.1f
return listOf(EnemyBullet(game, x, bulletY, 0f, bulletSpeedY, power))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
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> {
return listOf()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
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> {
val direction = 1
val shootNum = 3
val bulletY = y + direction * 2
val bulletCenterSpeedX = 0f
val bulletCenterSpeedY = speedY + direction * 0.1f
return mutableListOf<EnemyBullet>().apply {
for (i in 0 until shootNum) {
add(
EnemyBullet(
game,
x + (i * 2 - shootNum + 1) * 10,
bulletY,
bulletCenterSpeedX + (i * 2 - shootNum + 1) * 0.01f,
bulletCenterSpeedY,
power
)
)
}
}.toList()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package net.imshit.aircraftwar.element.shoot.enemy

import net.imshit.aircraftwar.element.bullet.EnemyBullet
import net.imshit.aircraftwar.element.shoot.ShootStrategy
import net.imshit.aircraftwar.logic.Games

interface EnemyShootStrategy : ShootStrategy {
override fun shoot(
game: Games,
x: Float,
y: Float,
speedY: Float,
power: Int
): List<EnemyBullet>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package net.imshit.aircraftwar.element.shoot.enemy

import net.imshit.aircraftwar.element.shoot.AbstractShootStrategyFactory

class EnemyShootStrategyFactory : AbstractShootStrategyFactory() {
override fun getStrategy(shootNum: Int): EnemyShootStrategy {
return when (shootNum) {
0 -> EnemyNoShootStrategy()
1 -> EnemyDirectShootStrategy()
3 -> EnemyScatterShootStrategy()
else -> EnemyNoShootStrategy()
}
}
}

0 comments on commit 24c38e4

Please sign in to comment.