A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/AlmasB/FXGL/commit/83f006596 below:

RechargeableComponents now maintain the value is not greater th… · AlmasB/FXGL@83f0065 · GitHub

1 +

/*

2 +

* FXGL - JavaFX Game Library. The MIT License (MIT).

3 +

* Copyright (c) AlmasB (almaslvl@gmail.com).

4 +

* See LICENSE for details.

5 +

*/

6 + 7 +

package com.almasb.fxgl.dsl.components

8 + 9 +

import org.hamcrest.CoreMatchers.`is`

10 +

import org.hamcrest.MatcherAssert.assertThat

11 +

import org.junit.jupiter.api.Assertions.assertTrue

12 +

import org.junit.jupiter.api.BeforeEach

13 +

import org.junit.jupiter.api.Test

14 + 15 +

/**

16 +

* @author Almas Baimagambetov (almaslvl@gmail.com)

17 +

*/

18 +

class RechargeableIntComponentTest {

19 + 20 +

private lateinit var hp: HealthIntComponent

21 + 22 +

@BeforeEach

23 +

fun setUp() {

24 +

hp = HealthIntComponent(100)

25 +

}

26 + 27 +

@Test

28 +

fun `Creation`() {

29 +

assertThat(hp.maxValue, `is`(100))

30 +

assertThat(hp.value, `is`(100))

31 +

assertThat(hp.isZero, `is`(false))

32 +

}

33 + 34 +

@Test

35 +

fun `Modification`() {

36 +

hp.value = 100

37 +

assertThat(hp.value, `is`(100))

38 + 39 +

hp.damage(30)

40 +

assertThat(hp.value, `is`(70))

41 + 42 +

hp.damagePercentageCurrent(10.0)

43 +

assertThat(hp.value, `is`(63))

44 + 45 +

hp.damagePercentageMax(50.0)

46 +

assertThat(hp.value, `is`(13))

47 + 48 +

hp.restore(37)

49 +

assertThat(hp.value, `is`(50))

50 + 51 +

hp.restorePercentageCurrent(50.0)

52 +

assertThat(hp.value, `is`(75))

53 + 54 +

hp.restorePercentageMax(15.0)

55 +

assertThat(hp.value, `is`(90))

56 +

assertThat(hp.isZero, `is`(false))

57 + 58 +

hp.damage(100)

59 +

assertThat(hp.value, `is`(0))

60 +

assertThat(hp.isZero, `is`(true))

61 + 62 +

hp.damageFully()

63 +

assertThat(hp.value, `is`(0))

64 +

assertThat(hp.isZero, `is`(true))

65 + 66 +

hp.restoreFully()

67 +

assertThat(hp.value, `is`(100))

68 +

assertThat(hp.isZero, `is`(false))

69 + 70 +

hp.value = 50

71 +

assertThat(hp.value, `is`(50))

72 + 73 +

// From now on, maxValue is 200

74 +

hp.maxValue = 200

75 +

assertThat(hp.maxValue, `is`(200))

76 +

// Value is still 50

77 +

assertThat(hp.value, `is`(50))

78 + 79 +

hp.restoreFully()

80 +

assertThat(hp.value, `is`(200))

81 + 82 +

hp.damagePercentageMax(75.0)

83 +

assertThat(hp.value, `is`(50))

84 + 85 +

hp.restorePercentageMax(50.0)

86 +

assertThat(hp.value, `is`(150))

87 + 88 +

hp.restorePercentageMax(50.0)

89 +

// value cannot be > maxValue

90 +

assertThat(hp.value, `is`(200))

91 +

hp.damagePercentageMax(150.0)

92 +

// value cannot be < 0

93 +

assertThat(hp.value, `is`(0))

94 +

assertThat(hp.isZero, `is`(true))

95 +

}

96 + 97 +

@Test

98 +

fun `Set max below current value`() {

99 +

hp.restoreFully()

100 +

hp.maxValue = 50

101 +

// value cannot be > maxValue

102 +

assertTrue(hp.value <= hp.maxValue)

103 +

}

104 + 105 +

@Test

106 +

fun `Set current value above max`() {

107 +

hp.value = 200

108 +

// value cannot be > maxValue

109 +

assertTrue(hp.value <= hp.maxValue)

110 +

}

111 + 112 +

@Test

113 +

fun `Properties`() {

114 +

var value = 0

115 +

var maxValue = 0

116 +

var zero = false

117 + 118 +

// Add listeners so we can monitor the properties

119 +

hp.valueProperty().addListener { _, _, newValue -> value = newValue as Int }

120 +

hp.maxValueProperty().addListener { _, _, newValue -> maxValue = newValue as Int }

121 +

hp.zeroProperty().addListener { _, _, newValue -> zero = newValue as Boolean }

122 + 123 +

hp.damage(75)

124 +

assertThat(value, `is`(25))

125 +

assertThat(zero, `is`(false))

126 + 127 +

hp.damagePercentageMax(100.0)

128 +

assertThat(value, `is`(0))

129 +

assertThat(zero, `is`(true))

130 + 131 +

hp.restorePercentageCurrent(10.0)

132 +

// Still zero

133 +

assertThat(value, `is`(0))

134 +

assertThat(zero, `is`(true))

135 + 136 +

hp.restorePercentageMax(10.0)

137 +

assertThat(value, `is`(10))

138 +

assertThat(zero, `is`(false))

139 + 140 +

// From now on, maxValue is 200

141 +

hp.maxValue = 200

142 +

assertThat(maxValue, `is`(200))

143 +

assertThat(value, `is`(10))

144 +

assertThat(zero, `is`(false))

145 + 146 +

hp.restorePercentageMax(10.0)

147 +

assertThat(value, `is`(30))

148 +

assertThat(zero, `is`(false))

149 +

}

150 + 151 +

@Test

152 +

fun `value in percent`() {

153 +

hp.value = 10

154 +

hp.maxValue = 100

155 + 156 +

assertThat(hp.valuePercent, `is`(10.0))

157 + 158 +

hp.maxValue = 50

159 + 160 +

assertThat(hp.valuePercent, `is`(20.0))

161 +

assertThat(hp.valuePercentProperty().value, `is`(20.0))

162 +

}

163 +

}


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4