diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 850b05fe..8ee356ea 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -131,6 +131,7 @@ dependencies { implementation(projects.feature.setting) implementation(projects.feature.carddelete) implementation(projects.feature.webview) + implementation(projects.feature.game) implementation(libs.compose.material.icons.core) implementation(libs.androidx.activity.compose) diff --git a/app/src/main/kotlin/com/gamss/android/app/main/MainScreen.kt b/app/src/main/kotlin/com/gamss/android/app/main/MainScreen.kt index d821328c..e141e2cb 100644 --- a/app/src/main/kotlin/com/gamss/android/app/main/MainScreen.kt +++ b/app/src/main/kotlin/com/gamss/android/app/main/MainScreen.kt @@ -30,6 +30,7 @@ import androidx.compose.ui.graphics.Color import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.res.stringResource import androidx.hilt.navigation.compose.hiltViewModel +import androidx.navigation3.runtime.EntryProviderScope import androidx.navigation3.runtime.NavKey import androidx.navigation3.runtime.entryProvider import androidx.navigation3.scene.Scene @@ -54,6 +55,10 @@ import com.gamss.android.feature.chat.ChatRoomScreen import com.gamss.android.feature.chat.ChattingListScreen import com.gamss.android.feature.chat.navigation.ChatKey import com.gamss.android.feature.chat.navigation.ChatRoomKey +import com.gamss.android.feature.game.GameListScreen +import com.gamss.android.feature.game.clicker.ClickerGameScreen +import com.gamss.android.feature.game.navigation.ClickerGameKey +import com.gamss.android.feature.game.navigation.GameKey import com.gamss.android.feature.home.HomeScreen import com.gamss.android.feature.home.navigation.HomeKey import com.gamss.android.feature.setting.accountinfo.AccountInfoScreen @@ -112,7 +117,7 @@ fun MainScreen( .padding(innerPadding) .statusBarsPadding(), entries = navigationState.toEntries(entryProvider = entries), - // 최상위 탭(홈/보관함/대화) 전환 기본값. 탭은 위계 없는 형제 화면이라 + // 최상위 탭(홈/보관함/대화/오락) 전환 기본값. 탭은 위계 없는 형제 화면이라 // 방향성 있는 슬라이드 대신 fade-through를 쓴다. 상세 화면은 아래 entry의 // metadata(detailTransition)가 이 기본값을 덮어쓴다. transitionSpec = tabFadeThroughSpec, @@ -192,6 +197,7 @@ private fun mainEntryProvider(navigator: Navigator) = entryProvider { entry(metadata = detailSlideTransition) { key -> WebViewScreen(page = key.page, onBackClick = navigator::goBack) } + gameEntries(navigator) entry { ChattingListScreen( onChatClick = { navigator.navigate(ChatRoomKey(it)) }, @@ -211,8 +217,20 @@ private fun mainEntryProvider(navigator: Navigator) = entryProvider { } } +private fun EntryProviderScope.gameEntries(navigator: Navigator) { + entry { + GameListScreen( + onNavigateToSetting = { navigator.navigate(SettingKey) }, + onClickerGameClick = { navigator.navigate(ClickerGameKey) }, + ) + } + entry(metadata = detailSlideTransition) { + ClickerGameScreen(onBackClick = navigator::goBack) + } +} + /** - * 최상위 탭(홈/보관함/대화) 간 전환에 쓰는 기본 트랜지션. + * 최상위 탭(홈/보관함/대화/오락) 간 전환에 쓰는 기본 트랜지션. * 탭은 위계 없는 형제 화면이라 방향성 있는 슬라이드 대신 fade-through를 쓴다. */ private val tabFadeThroughSpec: AnimatedContentTransitionScope>.() -> ContentTransform = { diff --git a/app/src/main/kotlin/com/gamss/android/app/navigation/ScreenView.kt b/app/src/main/kotlin/com/gamss/android/app/navigation/ScreenView.kt index ba025ce5..a9559151 100644 --- a/app/src/main/kotlin/com/gamss/android/app/navigation/ScreenView.kt +++ b/app/src/main/kotlin/com/gamss/android/app/navigation/ScreenView.kt @@ -9,6 +9,8 @@ import com.gamss.android.feature.archive.navigation.ArchiveKey import com.gamss.android.feature.carddelete.navigation.CardDeleteKey import com.gamss.android.feature.chat.navigation.ChatKey import com.gamss.android.feature.chat.navigation.ChatRoomKey +import com.gamss.android.feature.game.navigation.ClickerGameKey +import com.gamss.android.feature.game.navigation.GameKey import com.gamss.android.feature.home.navigation.HomeKey import com.gamss.android.feature.login.navigation.LoginKey import com.gamss.android.feature.onboarding.navigation.OnboardingKey @@ -32,6 +34,8 @@ private val screenNames: Map = mapOf( ArchiveKey to "archive", SettingKey to "setting", AccountInfoKey to "account_info", + GameKey to "game_list", + ClickerGameKey to "clicker_game", ) /** 새 화면 key 를 추가하면 여기에도 이름을 붙여야 집계된다. MainKey 는 탭을 담는 틀이라 세지 않는다. */ diff --git a/app/src/main/kotlin/com/gamss/android/app/navigation/TopLevelDestination.kt b/app/src/main/kotlin/com/gamss/android/app/navigation/TopLevelDestination.kt index 06c467ad..a68767c1 100644 --- a/app/src/main/kotlin/com/gamss/android/app/navigation/TopLevelDestination.kt +++ b/app/src/main/kotlin/com/gamss/android/app/navigation/TopLevelDestination.kt @@ -10,6 +10,7 @@ import com.gamss.android.core.designsystem.component.GamssBottomBarItem import com.gamss.android.core.designsystem.component.GamssIcons import com.gamss.android.feature.archive.navigation.ArchiveKey import com.gamss.android.feature.chat.navigation.ChatKey +import com.gamss.android.feature.game.navigation.GameKey import com.gamss.android.feature.home.navigation.HomeKey data class TopLevelDestination( @@ -22,6 +23,7 @@ fun topLevelDestinations(): List = listOf( TopLevelDestination(key = ArchiveKey, iconRes = GamssIcons.TabArchive, labelRes = R.string.tab_archive), TopLevelDestination(key = HomeKey, iconRes = GamssIcons.TabHome, labelRes = R.string.tab_home), TopLevelDestination(key = ChatKey, iconRes = GamssIcons.TabChat, labelRes = R.string.tab_chat), + TopLevelDestination(key = GameKey, iconRes = GamssIcons.TabGame, labelRes = R.string.tab_game), ) fun List.keys(): Set = map { it.key }.toSet() diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 21b1b45d..52a6e139 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -5,6 +5,7 @@ 보관함 홈 대화 + 오락 뒤로가기를 한 번 더 누르면 종료돼요 diff --git a/app/src/test/kotlin/com/gamss/android/app/navigation/ScreenViewTest.kt b/app/src/test/kotlin/com/gamss/android/app/navigation/ScreenViewTest.kt index 21b89279..be5ddd88 100644 --- a/app/src/test/kotlin/com/gamss/android/app/navigation/ScreenViewTest.kt +++ b/app/src/test/kotlin/com/gamss/android/app/navigation/ScreenViewTest.kt @@ -6,6 +6,8 @@ import com.gamss.android.feature.archive.navigation.ArchiveKey import com.gamss.android.feature.carddelete.navigation.CardDeleteKey import com.gamss.android.feature.chat.navigation.ChatKey import com.gamss.android.feature.chat.navigation.ChatRoomKey +import com.gamss.android.feature.game.navigation.ClickerGameKey +import com.gamss.android.feature.game.navigation.GameKey import com.gamss.android.feature.home.navigation.HomeKey import com.gamss.android.feature.login.navigation.LoginKey import com.gamss.android.feature.onboarding.navigation.OnboardingKey @@ -41,6 +43,8 @@ class ScreenViewTest { AccountInfoKey, NicknameChangeKey("감쓰"), WebViewKey(GamssWebPage.ServiceTerms), + GameKey, + ClickerGameKey, ) keys.forEach { assertNotNull("$it", it.toScreenView()) } } diff --git a/app/src/test/kotlin/com/gamss/android/app/navigation/TopLevelDestinationTest.kt b/app/src/test/kotlin/com/gamss/android/app/navigation/TopLevelDestinationTest.kt index 79239b2c..82a2f582 100644 --- a/app/src/test/kotlin/com/gamss/android/app/navigation/TopLevelDestinationTest.kt +++ b/app/src/test/kotlin/com/gamss/android/app/navigation/TopLevelDestinationTest.kt @@ -2,6 +2,7 @@ package com.gamss.android.app.navigation import com.gamss.android.feature.archive.navigation.ArchiveKey import com.gamss.android.feature.chat.navigation.ChatKey +import com.gamss.android.feature.game.navigation.GameKey import com.gamss.android.feature.home.navigation.HomeKey import org.junit.Assert.assertEquals import org.junit.Test @@ -9,7 +10,7 @@ import org.junit.Test class TopLevelDestinationTest { @Test - fun 탭은_보관함_홈_대화_세_종이다() { - assertEquals(listOf(ArchiveKey, HomeKey, ChatKey), topLevelDestinations().map { it.key }) + fun 탭은_보관함_홈_대화_오락_네_종이다() { + assertEquals(listOf(ArchiveKey, HomeKey, ChatKey, GameKey), topLevelDestinations().map { it.key }) } } diff --git a/core/designsystem/src/main/java/com/gamss/android/core/designsystem/component/GamssBottomBar.kt b/core/designsystem/src/main/java/com/gamss/android/core/designsystem/component/GamssBottomBar.kt index 87d3af55..1d307059 100644 --- a/core/designsystem/src/main/java/com/gamss/android/core/designsystem/component/GamssBottomBar.kt +++ b/core/designsystem/src/main/java/com/gamss/android/core/designsystem/component/GamssBottomBar.kt @@ -25,7 +25,7 @@ import androidx.compose.ui.unit.dp import com.gamss.android.core.designsystem.theme.GamssTheme private val BottomBarHeight = 66.dp -private val ItemRowWidth = 255.dp +private val ItemRowWidth = 312.dp private val ItemRowHeight = 44.dp private val ItemIconSize = 26.dp private val ItemLabelGap = 4.dp diff --git a/core/designsystem/src/main/java/com/gamss/android/core/designsystem/component/GamssIcons.kt b/core/designsystem/src/main/java/com/gamss/android/core/designsystem/component/GamssIcons.kt index 33e03ffc..0218e7ff 100644 --- a/core/designsystem/src/main/java/com/gamss/android/core/designsystem/component/GamssIcons.kt +++ b/core/designsystem/src/main/java/com/gamss/android/core/designsystem/component/GamssIcons.kt @@ -26,6 +26,9 @@ object GamssIcons { @DrawableRes val TabChat: Int = R.drawable.ic_tab_chat + @DrawableRes + val TabGame: Int = R.drawable.ic_tab_game + @DrawableRes val RightChevron: Int = R.drawable.ic_right_chevron diff --git a/core/designsystem/src/main/res/drawable/ic_tab_game.xml b/core/designsystem/src/main/res/drawable/ic_tab_game.xml new file mode 100644 index 00000000..21209e27 --- /dev/null +++ b/core/designsystem/src/main/res/drawable/ic_tab_game.xml @@ -0,0 +1,25 @@ + + + + + + + diff --git a/docs/clicker-game.html b/docs/clicker-game.html new file mode 100644 index 00000000..6165df10 --- /dev/null +++ b/docs/clicker-game.html @@ -0,0 +1,357 @@ + + + + + +눌러눌러 + + + + + + +
+

눌러눌러

+ +
+ +
0.0초
+ +
+ +

키를 누르면 시작합니다

+ +
+
+
+
+
+ + + +
+ + +
+
+ +
+ + +
+

최대한 빨리 게이지를 가득 채우세요. 키보드의 아무 키나 눌러도 됩니다.

+
+ + + + diff --git a/feature/game/build.gradle.kts b/feature/game/build.gradle.kts new file mode 100644 index 00000000..b2d62d10 --- /dev/null +++ b/feature/game/build.gradle.kts @@ -0,0 +1,7 @@ +plugins { + alias(libs.plugins.gamss.android.feature) +} + +android { + namespace = "com.gamss.android.feature.game" +} diff --git a/feature/game/src/main/java/com/gamss/android/feature/game/GameListScreen.kt b/feature/game/src/main/java/com/gamss/android/feature/game/GameListScreen.kt new file mode 100644 index 00000000..a999cce4 --- /dev/null +++ b/feature/game/src/main/java/com/gamss/android/feature/game/GameListScreen.kt @@ -0,0 +1,97 @@ +package com.gamss.android.feature.game + +import androidx.compose.foundation.Image +import androidx.compose.foundation.clickable +import androidx.compose.foundation.layout.Arrangement +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.PaddingValues +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.layout.size +import androidx.compose.foundation.layout.width +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.res.painterResource +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.semantics.Role +import androidx.compose.ui.text.style.TextAlign +import androidx.compose.ui.tooling.preview.Preview +import androidx.compose.ui.unit.dp +import com.gamss.android.core.designsystem.component.GamssIconButton +import com.gamss.android.core.designsystem.component.GamssIcons +import com.gamss.android.core.designsystem.component.GamssLogo +import com.gamss.android.core.designsystem.component.GamssTopBar +import com.gamss.android.core.designsystem.theme.GamssTheme + +@Composable +fun GameListScreen( + onNavigateToSetting: () -> Unit, + onClickerGameClick: () -> Unit, + modifier: Modifier = Modifier, +) { + Column(modifier = modifier.fillMaxSize()) { + GamssTopBar( + contentPadding = PaddingValues(start = TopBarStartPadding, end = TopBarEndPadding), + leading = { GamssLogo(contentDescription = stringResource(R.string.game_logo_description)) }, + trailing = { + GamssIconButton( + iconRes = GamssIcons.Setting, + contentDescription = stringResource(R.string.game_setting_description), + onClick = onNavigateToSetting, + ) + }, + ) + GameCard( + onClick = onClickerGameClick, + modifier = Modifier.padding(start = CardStartPadding, top = CardTopPadding), + ) + } +} + +@Composable +private fun GameCard( + onClick: () -> Unit, + modifier: Modifier = Modifier, +) { + Column( + modifier = modifier + .width(CardWidth) + .clickable(role = Role.Button, onClick = onClick), + horizontalAlignment = Alignment.CenterHorizontally, + verticalArrangement = Arrangement.spacedBy(CardLabelGap), + ) { + Image( + painter = painterResource(R.drawable.game_clicker_thumbnail), + contentDescription = null, + modifier = Modifier.size(width = CardWidth, height = ThumbnailHeight), + ) + Text( + text = stringResource(R.string.game_clicker_title), + style = GamssTheme.typography.body3Medium, + color = GamssTheme.colors.gray950, + textAlign = TextAlign.Center, + ) + } +} + +private val TopBarStartPadding = 20.dp +private val TopBarEndPadding = 8.dp +private val CardStartPadding = 24.dp + +// 시안의 상단바(64dp)보다 GamssTopBar(62dp)가 2dp 낮아 그만큼 더한다. +private val CardTopPadding = 33.dp + +private val CardWidth = 157.dp +private val ThumbnailHeight = 143.67.dp +private val CardLabelGap = 12.dp + +@Preview(showBackground = true, widthDp = 402, heightDp = 874) +@Composable +@Suppress("UnusedPrivateMember") +private fun GameListScreenPreview() { + GamssTheme(darkTheme = false) { + GameListScreen(onNavigateToSetting = {}, onClickerGameClick = {}) + } +} diff --git a/feature/game/src/main/java/com/gamss/android/feature/game/clicker/ClickerGameController.kt b/feature/game/src/main/java/com/gamss/android/feature/game/clicker/ClickerGameController.kt new file mode 100644 index 00000000..85b4f32b --- /dev/null +++ b/feature/game/src/main/java/com/gamss/android/feature/game/clicker/ClickerGameController.kt @@ -0,0 +1,71 @@ +package com.gamss.android.feature.game.clicker + +import androidx.compose.runtime.Composable +import androidx.compose.runtime.Stable +import androidx.compose.runtime.derivedStateOf +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.saveable.listSaver +import androidx.compose.runtime.saveable.rememberSaveable +import androidx.compose.runtime.setValue +import androidx.compose.runtime.withFrameMillis +import kotlin.math.roundToInt + +// 화면이 가려지면 프레임이 멈춘다. 돌아왔을 때 그 공백을 한 프레임으로 몰아 받으면 시간이 +// 한꺼번에 흘러가 버리므로, 한 프레임에 반영하는 시간을 제한해 가려진 동안은 멈춘 것으로 친다. +private const val MAX_FRAME_DELTA_MS = 100L +private const val PERCENT = 100f + +/** [gauge] 는 매 프레임 바뀌므로 그리기 단계에서만 읽는다. */ +@Stable +internal class ClickerGameController(initial: ClickerGameState = ClickerGameState()) { + var state by mutableStateOf(initial) + private set + + val isPlaying by derivedStateOf { state.phase == ClickerGamePhase.Playing } + val record by derivedStateOf { state.record } + + // 접근성 값이 매 프레임 바뀌지 않도록 1% 단위로 끊는다. + val gaugeProgress by derivedStateOf { (state.gauge * PERCENT).roundToInt() / PERCENT } + + val gauge: Float get() = state.gauge + + fun tap(): Boolean { + val next = state.tap() + if (next === state) return false + state = next + return true + } + + fun replay() { + state = ClickerGameState() + } + + suspend fun runClock(): Nothing { + var lastFrameMs = withFrameMillis { it } + while (true) { + val frameMs = withFrameMillis { it } + state = state.advance((frameMs - lastFrameMs).coerceAtMost(MAX_FRAME_DELTA_MS)) + lastFrameMs = frameMs + } + } + + companion object { + val Saver = listSaver( + save = { with(it.state) { listOf(phase.name, gauge, elapsedMs) } }, + restore = { + ClickerGameController( + ClickerGameState( + phase = ClickerGamePhase.valueOf(it[0] as String), + gauge = it[1] as Float, + elapsedMs = it[2] as Long, + ), + ) + }, + ) + } +} + +@Composable +internal fun rememberClickerGameController(): ClickerGameController = + rememberSaveable(saver = ClickerGameController.Saver) { ClickerGameController() } diff --git a/feature/game/src/main/java/com/gamss/android/feature/game/clicker/ClickerGameScreen.kt b/feature/game/src/main/java/com/gamss/android/feature/game/clicker/ClickerGameScreen.kt new file mode 100644 index 00000000..7b14ee52 --- /dev/null +++ b/feature/game/src/main/java/com/gamss/android/feature/game/clicker/ClickerGameScreen.kt @@ -0,0 +1,195 @@ +package com.gamss.android.feature.game.clicker + +import androidx.compose.foundation.layout.Arrangement +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.BoxWithConstraints +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.aspectRatio +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.navigationBarsPadding +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.layout.requiredSize +import androidx.compose.foundation.layout.size +import androidx.compose.foundation.layout.widthIn +import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.graphics.graphicsLayer +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.semantics.ProgressBarRangeInfo +import androidx.compose.ui.semantics.progressBarRangeInfo +import androidx.compose.ui.semantics.semantics +import androidx.compose.ui.tooling.preview.Preview +import androidx.compose.ui.unit.dp +import com.gamss.android.core.designsystem.theme.GamssTheme +import com.gamss.android.core.designsystem.topnavigation.GamssTopNavigation +import com.gamss.android.core.designsystem.topnavigation.GamssTopNavigationContent +import com.gamss.android.feature.game.R +import com.gamss.android.feature.game.clicker.component.ClickerGaugeNote +import com.gamss.android.feature.game.clicker.component.ClickerGaugeNoteSize +import com.gamss.android.feature.game.clicker.component.ClickerKeyCap +import com.gamss.android.feature.game.clicker.component.ClickerRecordBoard +import com.gamss.android.feature.game.clicker.component.rememberClickerKeySound + +private const val KEY_COUNT = 2 + +@Composable +fun ClickerGameScreen( + onBackClick: () -> Unit, + modifier: Modifier = Modifier, +) { + val game = rememberClickerGameController() + val keySound = rememberClickerKeySound() + + LaunchedEffect(game.isPlaying) { + if (game.isPlaying) game.runClock() + } + + ClickerGameContent( + game = game, + onBackClick = onBackClick, + onKeyPress = { if (game.tap()) keySound.play() }, + modifier = modifier, + ) +} + +/** 매 프레임 바뀌는 값은 람다로 넘겨 실제로 쓰는 곳에서만 읽는다. */ +@Composable +private fun ClickerGameContent( + game: ClickerGameController, + onBackClick: () -> Unit, + onKeyPress: () -> Unit, + modifier: Modifier = Modifier, +) { + Column( + modifier = modifier + .fillMaxSize() + .navigationBarsPadding(), + horizontalAlignment = Alignment.CenterHorizontally, + ) { + GamssTopNavigation( + content = GamssTopNavigationContent.None, + backgroundColor = Color.Transparent, + showLeftIcon = true, + leftIconContentDescription = stringResource(R.string.game_clicker_back_description), + onLeftIconClick = onBackClick, + ) + ClickerRecordBoard( + record = { game.record }, + onReplayClick = game::replay, + modifier = Modifier.padding(top = RecordBoardTopPadding), + ) + GaugeArea( + gauge = { game.gauge }, + gaugeProgress = { game.gaugeProgress }, + modifier = Modifier + .fillMaxWidth() + .weight(1f), + ) + KeyRow( + onKeyPress = onKeyPress, + modifier = Modifier.padding(top = KeyRowTopPadding, bottom = KeyRowBottomPadding), + ) + } +} + +/** 시안보다 좁거나 낮은 화면에서는 메모지를 통째로 줄인다. 넓은 화면에서 키우면 손그림이 뭉개진다. */ +@Composable +private fun GaugeArea( + gauge: () -> Float, + gaugeProgress: () -> Float, + modifier: Modifier = Modifier, +) { + val progress = gaugeProgress() + BoxWithConstraints( + modifier = modifier.semantics { + progressBarRangeInfo = ProgressBarRangeInfo(progress, 0f..1f) + }, + contentAlignment = Alignment.BottomCenter, + ) { + val scale = minOf( + 1f, + maxWidth / (ClickerGaugeNoteSize.width + GaugeNoteHorizontalMargin * 2), + maxHeight / ClickerGaugeNoteSize.height, + ) + Box(modifier = Modifier.size(ClickerGaugeNoteSize * scale)) { + ClickerGaugeNote( + gauge = gauge, + modifier = Modifier + .requiredSize(ClickerGaugeNoteSize) + .graphicsLayer { + scaleX = scale + scaleY = scale + }, + ) + } + } +} + +@Composable +private fun KeyRow( + onKeyPress: () -> Unit, + modifier: Modifier = Modifier, +) { + val keyDescription = stringResource(R.string.game_clicker_key_description) + Row( + modifier = modifier + .fillMaxWidth() + .padding(horizontal = KeyRowHorizontalPadding), + horizontalArrangement = Arrangement.spacedBy(KeyGap, Alignment.CenterHorizontally), + ) { + repeat(KEY_COUNT) { + ClickerKeyCap( + onPress = onKeyPress, + contentDescription = keyDescription, + modifier = Modifier + .weight(1f, fill = false) + .widthIn(max = KeyWidth) + .aspectRatio(KEY_ASPECT_RATIO), + ) + } + } +} + +private val RecordBoardTopPadding = 30.dp +private val GaugeNoteHorizontalMargin = 18.dp +private val KeyRowTopPadding = 14.dp +private val KeyRowBottomPadding = 42.dp +private val KeyRowHorizontalPadding = 18.dp +private val KeyWidth = 140.dp +private val KeyGap = 32.dp +private const val KEY_ASPECT_RATIO = 167f / 179f + +@Composable +private fun ClickerGamePreview(state: ClickerGameState) { + GamssTheme(darkTheme = false) { + ClickerGameContent(game = ClickerGameController(state), onBackClick = {}, onKeyPress = {}) + } +} + +@Preview(name = "Ready", showBackground = true, widthDp = 402, heightDp = 874) +@Composable +@Suppress("UnusedPrivateMember") +private fun ClickerGameReadyPreview() { + ClickerGamePreview(ClickerGameState()) +} + +@Preview(name = "Playing", showBackground = true, widthDp = 402, heightDp = 874) +@Composable +@Suppress("UnusedPrivateMember") +private fun ClickerGamePlayingPreview() { + ClickerGamePreview( + ClickerGameState(phase = ClickerGamePhase.Playing, gauge = 0.5f, elapsedMs = 12_300L), + ) +} + +@Preview(name = "Small", showBackground = true, widthDp = 360, heightDp = 640) +@Composable +@Suppress("UnusedPrivateMember") +private fun ClickerGameSmallPreview() { + ClickerGamePreview(ClickerGameState(phase = ClickerGamePhase.Playing, gauge = 0.3f)) +} diff --git a/feature/game/src/main/java/com/gamss/android/feature/game/clicker/ClickerGameState.kt b/feature/game/src/main/java/com/gamss/android/feature/game/clicker/ClickerGameState.kt new file mode 100644 index 00000000..2f99c38c --- /dev/null +++ b/feature/game/src/main/java/com/gamss/android/feature/game/clicker/ClickerGameState.kt @@ -0,0 +1,56 @@ +package com.gamss.android.feature.game.clicker + +internal const val GAME_DURATION_MS = 30_000L +internal const val GAUGE_PER_TAP = 0.02f +internal const val GAUGE_DRAIN_PER_SECOND = 0.1f +private const val MS_PER_SECOND = 1_000f +private const val MS_PER_TENTH = 100L +private const val MS_PER_HUNDREDTH = 10L + +// 0.02f 를 50번 더하면 float 오차로 1.0 에 조금 못 미친다. +private const val GAUGE_FULL_TOLERANCE = 0.0001f + +internal enum class ClickerGamePhase { Ready, Playing, Finished } + +/** 진행 중에는 0.1초 단위로 흐르는 시간을, 끝나면 0.01초 단위 기록이나 시간 초과를 보여 준다. */ +internal sealed interface ClickerRecord { + data class Running(val tenths: Long) : ClickerRecord + data class Cleared(val hundredths: Long) : ClickerRecord + data object TimeOver : ClickerRecord +} + +/** + * 첫 탭부터 시간이 흐르고, 게이지는 진행 중 내내 빠진다. 가득 채우면 걸린 시간이 기록이 된다. + * 빠지는 속도보다 느리게 누르면 끝없이 이어지므로 제한 시간이 지나면 시간 초과로 끝낸다. + */ +internal data class ClickerGameState( + val phase: ClickerGamePhase = ClickerGamePhase.Ready, + val gauge: Float = 0f, + val elapsedMs: Long = 0L, +) { + val record: ClickerRecord + get() = when { + phase != ClickerGamePhase.Finished -> ClickerRecord.Running(elapsedMs / MS_PER_TENTH) + gauge >= 1f -> ClickerRecord.Cleared(elapsedMs / MS_PER_HUNDREDTH) + else -> ClickerRecord.TimeOver + } + + fun tap(): ClickerGameState { + if (phase == ClickerGamePhase.Finished) return this + val isFull = gauge + GAUGE_PER_TAP >= 1f - GAUGE_FULL_TOLERANCE + return copy( + phase = if (isFull) ClickerGamePhase.Finished else ClickerGamePhase.Playing, + gauge = if (isFull) 1f else gauge + GAUGE_PER_TAP, + ) + } + + fun advance(deltaMs: Long): ClickerGameState { + if (phase != ClickerGamePhase.Playing) return this + val nextElapsed = (elapsedMs + deltaMs).coerceAtMost(GAME_DURATION_MS) + return copy( + phase = if (nextElapsed >= GAME_DURATION_MS) ClickerGamePhase.Finished else ClickerGamePhase.Playing, + gauge = (gauge - GAUGE_DRAIN_PER_SECOND * deltaMs / MS_PER_SECOND).coerceAtLeast(0f), + elapsedMs = nextElapsed, + ) + } +} diff --git a/feature/game/src/main/java/com/gamss/android/feature/game/clicker/component/ClickerGaugeNote.kt b/feature/game/src/main/java/com/gamss/android/feature/game/clicker/component/ClickerGaugeNote.kt new file mode 100644 index 00000000..d2513e4c --- /dev/null +++ b/feature/game/src/main/java/com/gamss/android/feature/game/clicker/component/ClickerGaugeNote.kt @@ -0,0 +1,123 @@ +package com.gamss.android.feature.game.clicker.component + +import androidx.compose.foundation.Image +import androidx.compose.foundation.background +import androidx.compose.foundation.border +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.offset +import androidx.compose.foundation.layout.size +import androidx.compose.runtime.Composable +import androidx.compose.ui.Modifier +import androidx.compose.ui.draw.drawWithContent +import androidx.compose.ui.draw.rotate +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.graphics.drawscope.clipRect +import androidx.compose.ui.layout.ContentScale +import androidx.compose.ui.res.painterResource +import androidx.compose.ui.unit.Dp +import androidx.compose.ui.unit.DpSize +import androidx.compose.ui.unit.dp +import com.gamss.android.core.designsystem.theme.GamssTheme +import com.gamss.android.feature.game.R + +/** 시안의 game_main 프레임 크기. 아래 좌표는 모두 이 프레임 기준이다. */ +internal val ClickerGaugeNoteSize = DpSize(366.dp, 347.dp) + +/** + * 시안의 대기 그림 하나로 그린다. 채움은 그림 아래에, 흰 눈·입은 위에 깔고 둘 다 게이지 높이까지만 보인다. + * 그래서 0% 는 대기 시안과 같고, 채움에 덮인 눈·입만 흰색이 된다. + */ +@Composable +internal fun ClickerGaugeNote( + gauge: () -> Float, + modifier: Modifier = Modifier, +) { + Box(modifier = modifier.size(ClickerGaugeNoteSize)) { + NotePaper( + center = CenterOffset(x = 180.11.dp, y = 178.79.dp), + rotation = SHADOW_ROTATION, + modifier = Modifier + .background(NoteShadowColor.copy(alpha = NOTE_SHADOW_ALPHA)), + ) + NotePaper( + center = CenterOffset(x = 182.21.dp, y = 170.76.dp), + rotation = NOTE_ROTATION, + modifier = Modifier + .background(GamssTheme.colors.white) + .border(NoteBorderWidth, GamssTheme.colors.gray950), + ) + Image( + painter = painterResource(R.drawable.game_clicker_character_fill), + contentDescription = null, + contentScale = ContentScale.FillBounds, + modifier = CharacterModifier.clipGauge(gauge), + ) + Image( + painter = painterResource(R.drawable.game_clicker_character), + contentDescription = null, + contentScale = ContentScale.FillBounds, + modifier = CharacterModifier, + ) + Image( + painter = painterResource(R.drawable.game_clicker_character_eyes), + contentDescription = null, + contentScale = ContentScale.FillBounds, + modifier = CharacterModifier.clipGauge(gauge), + ) + Tape(center = CenterOffset(x = 290.27.dp, y = 36.26.dp), rotation = RIGHT_TAPE_ROTATION) + Tape(center = CenterOffset(x = 49.9.dp, y = 54.93.dp), rotation = LEFT_TAPE_ROTATION) + } +} + +private data class CenterOffset(val x: Dp, val y: Dp) + +@Composable +private fun NotePaper( + center: CenterOffset, + rotation: Float, + modifier: Modifier = Modifier, +) { + Box( + modifier = Modifier + .offset(x = center.x - NotePaperSize.width / 2, y = center.y - NotePaperSize.height / 2) + .size(NotePaperSize) + .rotate(rotation) + .then(modifier), + ) +} + +@Composable +private fun Tape(center: CenterOffset, rotation: Float) { + Image( + painter = painterResource(R.drawable.game_clicker_tape), + contentDescription = null, + modifier = Modifier + .offset(x = center.x - TapeSize.width / 2, y = center.y - TapeSize.height / 2) + .size(TapeSize) + .rotate(rotation), + ) +} + +private val NotePaperSize = DpSize(260.dp, 270.dp) +private val NoteBorderWidth = 2.dp +private const val NOTE_ROTATION = -4.97f +private const val SHADOW_ROTATION = -3.55f + +// 시안의 그림자 색이 팔레트에 없는 값이라 그대로 옮긴다. +private val NoteShadowColor = Color(0xFFC4C4C4) +private const val NOTE_SHADOW_ALPHA = 0.6f + +private val TapeSize = DpSize(41.86.dp, 41.83.dp) +private const val RIGHT_TAPE_ROTATION = -15f +private const val LEFT_TAPE_ROTATION = 90f + +// 시안 노드(200x189.84, 83,79.08)보다 내보낸 그림이 선 두께만큼 커서 가운데를 맞춘다. +private val CharacterModifier = Modifier + .offset(x = 81.88.dp, y = 77.53.dp) + .size(width = 202.25.dp, height = 192.93.dp) + +private fun Modifier.clipGauge(gauge: () -> Float): Modifier = drawWithContent { + clipRect(top = size.height * (1f - gauge().coerceIn(0f, 1f))) { + this@drawWithContent.drawContent() + } +} diff --git a/feature/game/src/main/java/com/gamss/android/feature/game/clicker/component/ClickerKeyCap.kt b/feature/game/src/main/java/com/gamss/android/feature/game/clicker/component/ClickerKeyCap.kt new file mode 100644 index 00000000..7ec4bb9d --- /dev/null +++ b/feature/game/src/main/java/com/gamss/android/feature/game/clicker/component/ClickerKeyCap.kt @@ -0,0 +1,96 @@ +package com.gamss.android.feature.game.clicker.component + +import androidx.compose.animation.core.animateFloatAsState +import androidx.compose.animation.core.tween +import androidx.compose.foundation.Image +import androidx.compose.foundation.gestures.awaitEachGesture +import androidx.compose.foundation.gestures.awaitFirstDown +import androidx.compose.foundation.interaction.MutableInteractionSource +import androidx.compose.foundation.interaction.PressInteraction +import androidx.compose.foundation.interaction.collectIsPressedAsState +import androidx.compose.runtime.Composable +import androidx.compose.runtime.getValue +import androidx.compose.runtime.remember +import androidx.compose.runtime.rememberUpdatedState +import androidx.compose.ui.Modifier +import androidx.compose.ui.graphics.graphicsLayer +import androidx.compose.ui.input.pointer.PointerInputScope +import androidx.compose.ui.input.pointer.changedToDown +import androidx.compose.ui.input.pointer.pointerInput +import androidx.compose.ui.layout.ContentScale +import androidx.compose.ui.res.painterResource +import androidx.compose.ui.semantics.Role +import androidx.compose.ui.semantics.contentDescription +import androidx.compose.ui.semantics.onClick +import androidx.compose.ui.semantics.role +import androidx.compose.ui.semantics.semantics +import androidx.compose.ui.util.fastAny +import androidx.compose.ui.util.fastForEach +import com.gamss.android.feature.game.R + +private const val PRESSED_SCALE = 0.94f +private const val PRESS_ANIMATION_MS = 50 + +/** + * 손을 떼는 시점이 아니라 닿는 순간 [onPress]를 부른다. 연타는 누르는 박자로 세야 손맛이 맞는다. + */ +@Composable +internal fun ClickerKeyCap( + onPress: () -> Unit, + contentDescription: String, + modifier: Modifier = Modifier, +) { + val currentOnPress by rememberUpdatedState(onPress) + val interactionSource = remember { MutableInteractionSource() } + val pressed by interactionSource.collectIsPressedAsState() + val scale by animateFloatAsState( + targetValue = if (pressed) PRESSED_SCALE else 1f, + animationSpec = tween(PRESS_ANIMATION_MS), + label = "keyCapScale", + ) + + Image( + painter = painterResource(R.drawable.game_clicker_key), + contentDescription = null, + contentScale = ContentScale.Crop, + modifier = modifier + // 눌려 작아진 모양을 따라 터치 영역까지 줄면 가장자리 연타가 빗나간다. + .pointerInput(interactionSource) { detectEachDown(interactionSource) { currentOnPress() } } + .semantics { + role = Role.Button + this.contentDescription = contentDescription + onClick { + currentOnPress() + true + } + } + .graphicsLayer { + scaleX = scale + scaleY = scale + }, + ) +} + +/** 한 키를 두 손가락으로 번갈아 쳐도 닿을 때마다 센다. 눌린 모양은 손가락이 하나라도 닿아 있는 동안 유지한다. */ +private suspend fun PointerInputScope.detectEachDown( + interactionSource: MutableInteractionSource, + onDown: () -> Unit, +) { + awaitEachGesture { + val firstDown = awaitFirstDown() + val press = PressInteraction.Press(firstDown.position) + interactionSource.tryEmit(press) + firstDown.consume() + onDown() + do { + val event = awaitPointerEvent() + event.changes.fastForEach { + if (it.changedToDown()) { + it.consume() + onDown() + } + } + } while (event.changes.fastAny { it.pressed }) + interactionSource.tryEmit(PressInteraction.Release(press)) + } +} diff --git a/feature/game/src/main/java/com/gamss/android/feature/game/clicker/component/ClickerKeySound.kt b/feature/game/src/main/java/com/gamss/android/feature/game/clicker/component/ClickerKeySound.kt new file mode 100644 index 00000000..8ae0fc1d --- /dev/null +++ b/feature/game/src/main/java/com/gamss/android/feature/game/clicker/component/ClickerKeySound.kt @@ -0,0 +1,48 @@ +package com.gamss.android.feature.game.clicker.component + +import android.content.Context +import android.media.AudioAttributes +import android.media.SoundPool +import androidx.compose.runtime.Composable +import androidx.compose.runtime.DisposableEffect +import androidx.compose.runtime.remember +import androidx.compose.ui.platform.LocalContext +import com.gamss.android.feature.game.R +import kotlin.random.Random + +private const val MAX_STREAMS = 6 +private const val MIN_RATE = 0.94f +private const val RATE_RANGE = 0.12f + +@Composable +internal fun rememberClickerKeySound(): ClickerKeySound { + val context = LocalContext.current + val sound = remember(context) { ClickerKeySound(context) } + DisposableEffect(sound) { + onDispose { sound.release() } + } + return sound +} + +/** 게임 소리로 미디어 볼륨만 따른다. 무음 모드여도 미디어 볼륨이 켜져 있으면 난다. */ +internal class ClickerKeySound(context: Context) { + private val pool = SoundPool.Builder() + .setMaxStreams(MAX_STREAMS) + .setAudioAttributes( + AudioAttributes.Builder() + .setUsage(AudioAttributes.USAGE_GAME) + .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) + .build(), + ) + .build() + private val soundId = pool.load(context, R.raw.game_clicker_key_sound, 1) + + // 같은 소리가 똑같이 반복되지 않게 재생 속도를 조금씩 바꾼다. + fun play() { + pool.play(soundId, 1f, 1f, 0, 0, MIN_RATE + Random.nextFloat() * RATE_RANGE) + } + + fun release() { + pool.release() + } +} diff --git a/feature/game/src/main/java/com/gamss/android/feature/game/clicker/component/ClickerRecordBoard.kt b/feature/game/src/main/java/com/gamss/android/feature/game/clicker/component/ClickerRecordBoard.kt new file mode 100644 index 00000000..b509d546 --- /dev/null +++ b/feature/game/src/main/java/com/gamss/android/feature/game/clicker/component/ClickerRecordBoard.kt @@ -0,0 +1,107 @@ +package com.gamss.android.feature.game.clicker.component + +import androidx.compose.foundation.background +import androidx.compose.foundation.border +import androidx.compose.foundation.clickable +import androidx.compose.foundation.layout.Arrangement +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.Spacer +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.layout.size +import androidx.compose.foundation.layout.width +import androidx.compose.foundation.layout.widthIn +import androidx.compose.material3.Icon +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.res.painterResource +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.semantics.Role +import androidx.compose.ui.text.style.TextAlign +import androidx.compose.ui.unit.dp +import com.gamss.android.core.designsystem.theme.GamssTheme +import com.gamss.android.feature.game.R +import com.gamss.android.feature.game.clicker.ClickerRecord + +private const val TENTHS_PER_SECOND = 10 +private const val HUNDREDTHS_PER_SECOND = 100 + +/** 가운데 칸이 시안처럼 화면 가운데 오도록 다시하기와 같은 폭을 왼쪽에 비워 둔다. */ +@Composable +internal fun ClickerRecordBoard( + record: () -> ClickerRecord, + onReplayClick: () -> Unit, + modifier: Modifier = Modifier, +) { + Row( + modifier = modifier, + horizontalArrangement = Arrangement.spacedBy(ItemGap), + verticalAlignment = Alignment.CenterVertically, + ) { + Spacer(modifier = Modifier.width(SideItemWidth)) + RecordBox(record = record) + ReplayButton(onClick = onReplayClick) + } +} + +/** 0.1초마다 바뀌므로 이 칸만 다시 그려지도록 따로 둔다. */ +@Composable +private fun RecordBox(record: () -> ClickerRecord) { + val text = when (val value = record()) { + is ClickerRecord.Running -> stringResource( + R.string.game_clicker_elapsed_time, + value.tenths / TENTHS_PER_SECOND, + value.tenths % TENTHS_PER_SECOND, + ) + is ClickerRecord.Cleared -> stringResource( + R.string.game_clicker_record, + value.hundredths / HUNDREDTHS_PER_SECOND, + value.hundredths % HUNDREDTHS_PER_SECOND, + ) + ClickerRecord.TimeOver -> stringResource(R.string.game_clicker_time_over) + } + Text( + text = text, + modifier = Modifier + .widthIn(min = RecordBoxWidth) + .background(GamssTheme.colors.white) + .border(RecordBoxBorderWidth, GamssTheme.colors.gray950) + .padding(horizontal = 16.dp, vertical = 15.dp), + style = GamssTheme.typography.title2, + color = GamssTheme.colors.gray900, + textAlign = TextAlign.Center, + maxLines = 1, + ) +} + +@Composable +private fun ReplayButton(onClick: () -> Unit) { + Column( + modifier = Modifier + .widthIn(min = SideItemWidth) + .clickable(role = Role.Button, onClick = onClick), + horizontalAlignment = Alignment.CenterHorizontally, + verticalArrangement = Arrangement.spacedBy(2.dp), + ) { + Icon( + painter = painterResource(R.drawable.game_ic_replay), + contentDescription = null, + modifier = Modifier.size(24.dp), + tint = GamssTheme.colors.gray900, + ) + Text( + text = stringResource(R.string.game_clicker_replay), + style = GamssTheme.typography.subtitle3, + color = GamssTheme.colors.gray900, + textAlign = TextAlign.Center, + maxLines = 1, + ) + } +} + +private val ItemGap = 36.dp +private val SideItemWidth = 62.dp +private val RecordBoxWidth = 142.dp +private val RecordBoxBorderWidth = 1.5.dp diff --git a/feature/game/src/main/java/com/gamss/android/feature/game/navigation/ClickerGameKey.kt b/feature/game/src/main/java/com/gamss/android/feature/game/navigation/ClickerGameKey.kt new file mode 100644 index 00000000..bc8658ce --- /dev/null +++ b/feature/game/src/main/java/com/gamss/android/feature/game/navigation/ClickerGameKey.kt @@ -0,0 +1,7 @@ +package com.gamss.android.feature.game.navigation + +import androidx.navigation3.runtime.NavKey +import kotlinx.serialization.Serializable + +@Serializable +data object ClickerGameKey : NavKey diff --git a/feature/game/src/main/java/com/gamss/android/feature/game/navigation/GameKey.kt b/feature/game/src/main/java/com/gamss/android/feature/game/navigation/GameKey.kt new file mode 100644 index 00000000..bf644157 --- /dev/null +++ b/feature/game/src/main/java/com/gamss/android/feature/game/navigation/GameKey.kt @@ -0,0 +1,7 @@ +package com.gamss.android.feature.game.navigation + +import androidx.navigation3.runtime.NavKey +import kotlinx.serialization.Serializable + +@Serializable +data object GameKey : NavKey diff --git a/feature/game/src/main/res/drawable-xxhdpi/game_clicker_character.png b/feature/game/src/main/res/drawable-xxhdpi/game_clicker_character.png new file mode 100644 index 00000000..3e631ab9 Binary files /dev/null and b/feature/game/src/main/res/drawable-xxhdpi/game_clicker_character.png differ diff --git a/feature/game/src/main/res/drawable-xxhdpi/game_clicker_character_eyes.png b/feature/game/src/main/res/drawable-xxhdpi/game_clicker_character_eyes.png new file mode 100644 index 00000000..41ebe12e Binary files /dev/null and b/feature/game/src/main/res/drawable-xxhdpi/game_clicker_character_eyes.png differ diff --git a/feature/game/src/main/res/drawable-xxhdpi/game_clicker_character_fill.png b/feature/game/src/main/res/drawable-xxhdpi/game_clicker_character_fill.png new file mode 100644 index 00000000..670c66ff Binary files /dev/null and b/feature/game/src/main/res/drawable-xxhdpi/game_clicker_character_fill.png differ diff --git a/feature/game/src/main/res/drawable-xxhdpi/game_clicker_key.png b/feature/game/src/main/res/drawable-xxhdpi/game_clicker_key.png new file mode 100644 index 00000000..f208e47d Binary files /dev/null and b/feature/game/src/main/res/drawable-xxhdpi/game_clicker_key.png differ diff --git a/feature/game/src/main/res/drawable-xxhdpi/game_clicker_thumbnail.png b/feature/game/src/main/res/drawable-xxhdpi/game_clicker_thumbnail.png new file mode 100644 index 00000000..a865bea8 Binary files /dev/null and b/feature/game/src/main/res/drawable-xxhdpi/game_clicker_thumbnail.png differ diff --git a/feature/game/src/main/res/drawable/game_clicker_tape.xml b/feature/game/src/main/res/drawable/game_clicker_tape.xml new file mode 100644 index 00000000..f7f8a03b --- /dev/null +++ b/feature/game/src/main/res/drawable/game_clicker_tape.xml @@ -0,0 +1,10 @@ + + + diff --git a/feature/game/src/main/res/drawable/game_ic_replay.xml b/feature/game/src/main/res/drawable/game_ic_replay.xml new file mode 100644 index 00000000..f03e94a9 --- /dev/null +++ b/feature/game/src/main/res/drawable/game_ic_replay.xml @@ -0,0 +1,22 @@ + + + + + + diff --git a/feature/game/src/main/res/raw/game_clicker_key_sound.wav b/feature/game/src/main/res/raw/game_clicker_key_sound.wav new file mode 100644 index 00000000..416218aa Binary files /dev/null and b/feature/game/src/main/res/raw/game_clicker_key_sound.wav differ diff --git a/feature/game/src/main/res/values/strings.xml b/feature/game/src/main/res/values/strings.xml new file mode 100644 index 00000000..22098277 --- /dev/null +++ b/feature/game/src/main/res/values/strings.xml @@ -0,0 +1,13 @@ + + + 감쓰 + 설정 + 눌러눌러 + + 뒤로 가기 + %1$d.%2$d초 + %1$d.%2$02d초 + 시간 초과 + 다시하기 + 누르기 + diff --git a/feature/game/src/test/java/com/gamss/android/feature/game/clicker/ClickerGameControllerTest.kt b/feature/game/src/test/java/com/gamss/android/feature/game/clicker/ClickerGameControllerTest.kt new file mode 100644 index 00000000..c049593b --- /dev/null +++ b/feature/game/src/test/java/com/gamss/android/feature/game/clicker/ClickerGameControllerTest.kt @@ -0,0 +1,43 @@ +package com.gamss.android.feature.game.clicker + +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertTrue +import org.junit.Test + +class ClickerGameControllerTest { + + @Test + fun `반영된 입력만 true 를 돌려준다`() { + val game = ClickerGameController() + + assertTrue(game.tap()) + assertEquals(GAUGE_PER_TAP, game.gauge, 0.0001f) + assertTrue(game.isPlaying) + } + + @Test + fun `끝난 뒤의 입력은 false 를 돌려주고 상태를 바꾸지 않는다`() { + val finished = ClickerGameState(phase = ClickerGamePhase.Finished, gauge = 1f) + val game = ClickerGameController(finished) + + assertFalse(game.tap()) + assertEquals(finished, game.state) + } + + @Test + fun `다시하기는 대기 상태로 되돌린다`() { + val game = ClickerGameController(ClickerGameState(phase = ClickerGamePhase.Playing, gauge = 0.3f)) + + game.replay() + + assertEquals(ClickerGameState(), game.state) + } + + @Test + fun `접근성 게이지 값은 1퍼센트 단위로 반올림한다`() { + val game = ClickerGameController(ClickerGameState(phase = ClickerGamePhase.Playing, gauge = 0.346f)) + + assertEquals(0.35f, game.gaugeProgress, 0.0001f) + } +} diff --git a/feature/game/src/test/java/com/gamss/android/feature/game/clicker/ClickerGameStateTest.kt b/feature/game/src/test/java/com/gamss/android/feature/game/clicker/ClickerGameStateTest.kt new file mode 100644 index 00000000..a8f84f30 --- /dev/null +++ b/feature/game/src/test/java/com/gamss/android/feature/game/clicker/ClickerGameStateTest.kt @@ -0,0 +1,87 @@ +package com.gamss.android.feature.game.clicker + +import org.junit.Assert.assertEquals +import org.junit.Assert.assertSame +import org.junit.Test + +class ClickerGameStateTest { + + @Test + fun `대기 중에는 시간이 흐르지 않는다`() { + val state = ClickerGameState().advance(1_000L) + + assertEquals(ClickerGameState(), state) + assertEquals(ClickerRecord.Running(0L), state.record) + } + + @Test + fun `첫 탭에 게임이 시작되고 게이지가 오른다`() { + val state = ClickerGameState().tap() + + assertEquals(ClickerGamePhase.Playing, state.phase) + assertEquals(GAUGE_PER_TAP, state.gauge, DELTA) + } + + @Test + fun `진행 중에는 시간에 따라 게이지가 줄고 0 아래로 내려가지 않는다`() { + val tapped = ClickerGameState().tap() + + val drained = tapped.advance(100L) + assertEquals(GAUGE_PER_TAP - GAUGE_DRAIN_PER_SECOND * 0.1f, drained.gauge, DELTA) + assertEquals(100L, drained.elapsedMs) + + assertEquals(0f, tapped.advance(5_000L).gauge, DELTA) + } + + @Test + fun `진행 중 기록은 흐른 시간을 0점1초 단위로 내림한다`() { + val state = ClickerGameState(phase = ClickerGamePhase.Playing, elapsedMs = 12_399L) + + assertEquals(ClickerRecord.Running(123L), state.record) + } + + @Test + fun `가득 채우면 걸린 시간을 0점01초 단위 기록으로 남긴다`() { + val state = ClickerGameState( + phase = ClickerGamePhase.Playing, + gauge = 1f - GAUGE_PER_TAP, + elapsedMs = 12_345L, + ).tap() + + assertEquals(ClickerGamePhase.Finished, state.phase) + assertEquals(1f, state.gauge, DELTA) + assertEquals(ClickerRecord.Cleared(1_234L), state.record) + } + + @Test + fun `시간이 흐르지 않으면 50번 만에 가득 찬다`() { + var state = ClickerGameState() + repeat(49) { state = state.tap() } + assertEquals(ClickerGamePhase.Playing, state.phase) + + state = state.tap() + assertEquals(ClickerGamePhase.Finished, state.phase) + assertEquals(1f, state.gauge, DELTA) + } + + @Test + fun `제한 시간이 지나면 시간 초과로 끝난다`() { + val state = ClickerGameState().tap().advance(GAME_DURATION_MS + 500L) + + assertEquals(ClickerGamePhase.Finished, state.phase) + assertEquals(GAME_DURATION_MS, state.elapsedMs) + assertEquals(ClickerRecord.TimeOver, state.record) + } + + @Test + fun `끝난 뒤에는 탭도 시간도 반영하지 않는다`() { + val finished = ClickerGameState(phase = ClickerGamePhase.Finished, gauge = 0.4f, elapsedMs = GAME_DURATION_MS) + + assertSame(finished, finished.tap()) + assertSame(finished, finished.advance(100L)) + } + + private companion object { + const val DELTA = 0.0001f + } +} diff --git a/settings.gradle.kts b/settings.gradle.kts index 82fa52a6..834fefdd 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -44,3 +44,4 @@ include(":feature:carddelete") include(":feature:webview") include(":core:designsystem") include(":feature:onboarding") +include(":feature:game")