297 lines
8.1 KiB
Kotlin
297 lines
8.1 KiB
Kotlin
import java.util.*
|
|
|
|
class WrongParameterException: Exception()
|
|
|
|
//This class take subjects[0] as the one to be damaged
|
|
class Damage(override vararg val subjects: Character,
|
|
private val decreaseLife: Int) : Effect() {
|
|
|
|
init {
|
|
if (subjects.isEmpty() || decreaseLife <= 0) {
|
|
throw WrongParameterException()
|
|
}
|
|
}
|
|
|
|
private val subject = subjects[0]
|
|
override val reverseEffect: Effect
|
|
get() = Damage(subject.enemy, decreaseLife = decreaseLife)
|
|
|
|
override val id = 1
|
|
|
|
override fun apply() {
|
|
subject.gotDamage(decreaseLife)
|
|
}
|
|
}
|
|
|
|
//This class take subjects[0] as the one the be healed
|
|
class Heal(override vararg val subjects: Character, private val increaseLife: Int) : Effect() {
|
|
|
|
init {
|
|
if (subjects.isEmpty() || increaseLife <= 0) {
|
|
throw WrongParameterException()
|
|
}
|
|
}
|
|
|
|
private val subject = subjects[0]
|
|
override val reverseEffect: Effect
|
|
get() = Heal(subject.enemy, increaseLife = increaseLife)
|
|
override val id = 2
|
|
|
|
override fun apply() {
|
|
subject.heal(increaseLife)
|
|
}
|
|
}
|
|
|
|
//This class take subjects[0] as the one who draws from the deck
|
|
class DrawFromDeck(override vararg val subjects: Character,
|
|
private val numberOfCards: Int): Effect() {
|
|
|
|
init {
|
|
if (subjects.isEmpty() || numberOfCards <= 0) {
|
|
throw WrongParameterException()
|
|
}
|
|
}
|
|
|
|
private val subject = subjects[0]
|
|
override val reverseEffect: Effect
|
|
get() = DrawFromDeck(subject.enemy, numberOfCards = numberOfCards)
|
|
override val id = 3
|
|
|
|
override fun apply() {
|
|
subject.drawFromDeck(numberOfCards)
|
|
}
|
|
}
|
|
|
|
//This class take subjects[0] as the one who draws
|
|
//subjects[1] as the one whose cards is taken
|
|
class ChooseFromCharacterHand(override vararg val subjects: Character): Effect() {
|
|
|
|
init {
|
|
if (subjects.size < 2) {
|
|
throw WrongParameterException()
|
|
}
|
|
}
|
|
|
|
private val taker = subjects[0]
|
|
private val giver = subjects[1]
|
|
override val reverseEffect: Effect
|
|
get() = ChooseFromCharacterHand(giver, taker)
|
|
override val id = 4
|
|
|
|
override fun apply() {
|
|
taker.chooseFromCards(giver.hand)
|
|
}
|
|
}
|
|
|
|
//This class take subjects[0] as the one who loses cards randomly
|
|
class LoseCard(override vararg val subjects: Character, private val num: Int = 1): Effect() {
|
|
|
|
init {
|
|
if (subjects.isEmpty()) {
|
|
throw WrongParameterException()
|
|
}
|
|
}
|
|
|
|
private val subject = subjects[0]
|
|
override val reverseEffect: Effect
|
|
get() = LoseCard(subject.enemy)
|
|
override val id = 5
|
|
|
|
override fun apply() {
|
|
for (i in 1..num) {
|
|
val cards = subject.hand
|
|
val index = Random().nextInt(cards.size)
|
|
cards.toMutableList().removeAt(index)
|
|
}
|
|
}
|
|
}
|
|
|
|
//This class take subject[0] as the one who chooses the card
|
|
class ChooseFromDeck(override vararg val subjects: Character): Effect() {
|
|
init {
|
|
if (subjects.isEmpty()) {
|
|
throw WrongParameterException()
|
|
}
|
|
}
|
|
|
|
private val subject = subjects[0]
|
|
override val reverseEffect: Effect
|
|
get() = ChooseFromDeck(subject.enemy)
|
|
override val id = 6
|
|
|
|
override fun apply() {
|
|
val random = Random()
|
|
val first = random.nextInt(subject.deck.size)
|
|
var second = random.nextInt(subject.deck.size)
|
|
while(second == first) {
|
|
second = random.nextInt(subject.deck.size)
|
|
}
|
|
var third = random.nextInt(subject.deck.size)
|
|
while(third == first || third == second) {
|
|
third = random.nextInt(subject.deck.size)
|
|
}
|
|
val cards = subject.deck.cards
|
|
subject.chooseFromCards(mutableListOf(cards[first], cards[second], cards[third]))
|
|
}
|
|
}
|
|
|
|
//This class take subject[0] as the one who was poisoned
|
|
class SolvePoison(override vararg val subjects: Character): Effect() {
|
|
|
|
init {
|
|
if (subjects.isEmpty()) {
|
|
throw WrongParameterException()
|
|
}
|
|
}
|
|
|
|
private val subject = subjects[0]
|
|
override val reverseEffect: Effect
|
|
get() = SolvePoison(subject.enemy)
|
|
override val id = 7
|
|
|
|
override fun apply() {
|
|
subject.poisoned = false
|
|
}
|
|
}
|
|
|
|
//The class take subject[0] as the one who's going to be poisoned
|
|
class Poison(override vararg val subjects: Character): Effect() {
|
|
|
|
init {
|
|
if (subjects.isEmpty()) {
|
|
throw WrongParameterException()
|
|
}
|
|
}
|
|
|
|
private val subject = subjects[0]
|
|
override val reverseEffect: Effect
|
|
get() = Poison(subject.enemy)
|
|
override val id = 8
|
|
|
|
override fun apply() {
|
|
subject.poisoned = true
|
|
}
|
|
}
|
|
|
|
//This class take subject[0] as the one who needs to defense attack
|
|
class Defense(override vararg val subjects: Character): Effect() {
|
|
|
|
init {
|
|
if (subjects.isEmpty()) {
|
|
throw WrongParameterException()
|
|
}
|
|
}
|
|
|
|
private val subject = subjects[0]
|
|
override val reverseEffect: Effect
|
|
get() = Defense(subject.enemy)
|
|
override val id = 9
|
|
|
|
override fun apply() {
|
|
subject.effectBuffer = emptyArray()
|
|
subject.attackingCard = null
|
|
}
|
|
}
|
|
|
|
//This class take subject[0] as the one who wants to reflect the effect
|
|
class Reverse(override vararg val subjects: Character): Effect() {
|
|
|
|
init {
|
|
if (subjects.isEmpty()) {
|
|
throw WrongParameterException()
|
|
}
|
|
}
|
|
|
|
private val subject = subjects[0]
|
|
override val reverseEffect: Effect
|
|
get() = Reverse(subject.enemy)
|
|
override val id = 10
|
|
|
|
override fun apply() {
|
|
subject.effectBuffer = subject.effectBuffer.map { it.reverseEffect }.toTypedArray()
|
|
subject.effectBuffer.forEach { it.apply() }
|
|
}
|
|
}
|
|
|
|
//This class take subject[0] as the one who wants to get the copied card
|
|
class Copy(override vararg val subjects: Character): Effect() {
|
|
|
|
init {
|
|
if (subjects.isEmpty()) {
|
|
throw WrongParameterException()
|
|
}
|
|
}
|
|
private val subject = subjects[0]
|
|
override val reverseEffect: Effect
|
|
get() = Copy(subject.enemy)
|
|
override val id = 11
|
|
override fun apply() {
|
|
subject.hand.add(subject.attackingCard!!)
|
|
}
|
|
|
|
}
|
|
|
|
//This class take subject[0] as the one to become invincible
|
|
class BecomeInvincible(override vararg val subjects: Character,
|
|
private var turns: Int): Effect() {
|
|
init {
|
|
if(subjects.isEmpty() || turns == 0) {
|
|
throw WrongParameterException()
|
|
}
|
|
}
|
|
private val subject = subjects[0]
|
|
override val reverseEffect: Effect
|
|
get() = BecomeInvincible(subject.enemy, turns = turns)
|
|
override val id = 11
|
|
override fun apply() {
|
|
subject.isInvincible = true
|
|
subject.enemy.nextTurnListeners.add {
|
|
if(turns == 0) {
|
|
subject.isInvincible = false
|
|
true //listener will be removed
|
|
} else {
|
|
turns -= 1
|
|
false //listener will not be removed
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
//This class take subject[0] as the one to change damage addition
|
|
class ChangeDamageAddition(override vararg val subjects: Character,
|
|
private val num: Int,
|
|
private var turns: Int): Effect() {
|
|
private val subject = subjects[0]
|
|
override val reverseEffect: Effect
|
|
get() = ChangeDamageAddition(subject.enemy, num = num, turns = turns)
|
|
override val id = 12
|
|
override fun apply() {
|
|
subject.damageAddition = num
|
|
val listener: NextTurnListener = {
|
|
if (turns == 0) {
|
|
subject.damageAddition = 0
|
|
true //listener will be removed
|
|
} else {
|
|
turns -= 1
|
|
false //listener will not be removed
|
|
}
|
|
}
|
|
subject.nextTurnListeners.add(listener)
|
|
}
|
|
}
|
|
|
|
class UndefendableDamage(override vararg val subjects: Character,
|
|
private val decreaseLife: Int): Effect() {
|
|
private val subject = subjects[0]
|
|
override val reverseEffect: Effect
|
|
get() = UndefendableDamage(subject.enemy, decreaseLife = decreaseLife)
|
|
override val id = 13
|
|
override fun apply() {
|
|
subject.gotDamage(decreaseLife, false)
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|