@@ -11,9 +11,7 @@ import androidx.compose.foundation.shape.CircleShape
1111import androidx.compose.runtime.Composable
1212import androidx.compose.runtime.collectAsState
1313import androidx.compose.runtime.getValue
14- import androidx.compose.runtime.mutableStateOf
1514import androidx.compose.runtime.remember
16- import androidx.compose.runtime.setValue
1715import androidx.compose.ui.Alignment
1816import androidx.compose.ui.Modifier
1917import androidx.compose.ui.draw.clip
@@ -37,8 +35,10 @@ import com.imsproject.watch.viewmodel.FlourMillViewModel
3735import com.imsproject.watch.viewmodel.GameViewModel
3836import androidx.compose.ui.input.pointer.PointerEventType
3937import com.imsproject.watch.BRIGHT_CYAN_COLOR
38+ import com.imsproject.watch.SILVER_COLOR
4039import com.imsproject.watch.TOUCH_CIRCLE_RADIUS
4140import com.imsproject.watch.utils.Angle
41+ import com.imsproject.watch.viewmodel.FlourMillViewModel.AxleSide
4242
4343class FlourMillActivity : GameActivity (GameType .FLOUR_MILL ) {
4444
@@ -64,8 +64,7 @@ class FlourMillActivity : GameActivity(GameType.FLOUR_MILL) {
6464 @Composable
6565 fun FlourMill () {
6666 val (centerX, centerY) = remember { polarToCartesian(0f , 0.0 ) }
67- val axle = remember { viewModel.axle }
68- var showAxle by remember { mutableStateOf(false ) }
67+ val axle = viewModel.axle.collectAsState().value
6968 val myTouchPoint = viewModel.myTouchPoint.collectAsState().value
7069 val opponentTouchPoint = viewModel.opponentTouchPoint.collectAsState().value
7170
@@ -78,21 +77,15 @@ class FlourMillActivity : GameActivity(GameType.FLOUR_MILL) {
7877 val pointerEvent = awaitPointerEvent()
7978 val inputChange = pointerEvent.changes.first()
8079 inputChange.consume()
81- val position = inputChange.position
82- val (radius, angle) = cartesianToPolar(position.x, position.y)
83- val relativeRadius = radius / SCREEN_RADIUS
8480 when (pointerEvent.type) {
85- PointerEventType .Press -> {
86- axle.angle = angle + - 90f
87- viewModel.setTouchPoint(relativeRadius, angle)
88- showAxle = true
89- }
90- PointerEventType .Move -> {
81+ PointerEventType .Press , PointerEventType .Move -> {
82+ val position = inputChange.position
83+ val (radius, angle) = cartesianToPolar(position.x, position.y)
84+ val relativeRadius = radius / SCREEN_RADIUS
9185 viewModel.setTouchPoint(relativeRadius, angle)
9286 }
9387 PointerEventType .Release -> {
9488 viewModel.setTouchPoint(- 1f , Angle .undefined())
95- showAxle = false
9689 }
9790 }
9891 }
@@ -138,46 +131,68 @@ class FlourMillActivity : GameActivity(GameType.FLOUR_MILL) {
138131 .background(color = DARK_BACKGROUND_COLOR )
139132 )
140133 Canvas (modifier = Modifier .Companion .fillMaxSize()){
141- for (touchPoint in listOf (myTouchPoint, opponentTouchPoint)){
142- if (touchPoint.first < 0f ) continue
143-
144- val (leftX, leftY) = polarToCartesian(SCREEN_RADIUS * 0.7f , axle.angle + 90f )
145- val (leftX2, leftY2) = polarToCartesian(SCREEN_RADIUS * 0.9f , axle.angle + 90f )
146- val (rightX, rightY) = polarToCartesian(SCREEN_RADIUS * 0.9f , axle.angle + - 90f )
134+ if (axle != null ){
147135
148- val touchPointDistance = touchPoint.first * SCREEN_RADIUS
149- val touchPointAngle = touchPoint.second
150- val touchPointInBounds = (touchPointAngle - (axle.angle + 90f ) <= (360f / TOUCH_CIRCLE_RADIUS ))
151- && touchPointDistance > (SCREEN_RADIUS * 0.7f - TOUCH_CIRCLE_RADIUS )
152- && touchPointDistance < (SCREEN_RADIUS * 0.9f + TOUCH_CIRCLE_RADIUS )
153-
154- val (x,y) = polarToCartesian(touchPointDistance, touchPointAngle)
136+ val axleLeftAngle = axle.angle + - 90f
137+ val axleRightAngle = axle.angle + 90f
138+ val (leftTipStartX, leftTipStartY) = polarToCartesian(SCREEN_RADIUS * 0.7f , axleLeftAngle)
139+ val (leftTipEndX, leftTipEndY) = polarToCartesian(SCREEN_RADIUS * 0.9f , axleLeftAngle)
140+ val (rightTipStartX, rightTipStartY) = polarToCartesian(SCREEN_RADIUS * 0.7f , axleRightAngle)
141+ val (rightTipEndX, rightTipEndY) = polarToCartesian(SCREEN_RADIUS * 0.9f , axleRightAngle)
155142
156143 val axlePath = Path ().apply {
157- moveTo(rightX,rightY )
158- lineTo(leftX, leftY )
144+ moveTo(rightTipStartX,rightTipStartY )
145+ lineTo(leftTipStartX, leftTipStartY )
159146 }
160-
161- val axlePath2 = Path ().apply {
162- moveTo(leftX,leftY)
163- lineTo(leftX2, leftY2)
147+ val leftTipPath = Path ().apply {
148+ moveTo(leftTipStartX,leftTipStartY)
149+ lineTo(leftTipEndX, leftTipEndY)
164150 }
151+ val rightTipPath = Path ().apply {
152+ moveTo(rightTipStartX,rightTipStartY)
153+ lineTo(rightTipEndX, rightTipEndY)
154+ }
155+ drawPath(
156+ path = axlePath,
157+ color = GLOWING_YELLOW_COLOR ,
158+ style = Stroke (width = AXLE_WIDTH )
159+ )
165160
166- if (showAxle){
167- drawPath(
168- path = axlePath,
169- color = GLOWING_YELLOW_COLOR ,
170- style = Stroke (width = AXLE_WIDTH )
171- )
161+ val sides = listOf (myTouchPoint to axle.mySide, opponentTouchPoint to axle.mySide.otherSide())
162+ for ((touchPoint, side) in sides){
163+ val color = if (side == axle.mySide) BRIGHT_CYAN_COLOR else SILVER_COLOR
164+ val path: Path
165+ val sideAngle: Angle
166+ when (side) {
167+ AxleSide .LEFT -> {
168+ path = leftTipPath
169+ sideAngle = axleLeftAngle
170+ }
171+ AxleSide .RIGHT -> {
172+ path = rightTipPath
173+ sideAngle = axleRightAngle
174+ }
175+ }
172176 drawPath(
173- path = axlePath2 ,
174- color = BRIGHT_CYAN_COLOR ,
177+ path = path ,
178+ color = color ,
175179 style = Stroke (width = AXLE_WIDTH )
176180 )
181+
182+ if (touchPoint.first < 0f ) continue
183+
184+ val touchPointDistance = touchPoint.first * SCREEN_RADIUS
185+ val touchPointAngle = touchPoint.second
186+ val touchPointInBounds = (touchPointAngle - sideAngle <= (360f / TOUCH_CIRCLE_RADIUS ))
187+ && touchPointDistance > (SCREEN_RADIUS * 0.7f - TOUCH_CIRCLE_RADIUS )
188+ && touchPointDistance < (SCREEN_RADIUS * 0.9f + TOUCH_CIRCLE_RADIUS )
189+
190+ val (x,y) = polarToCartesian(touchPointDistance, touchPointAngle)
191+
177192 drawCircle(
178193 radius = TOUCH_CIRCLE_RADIUS ,
179194 center = Offset (x, y),
180- color = if (touchPointInBounds) BRIGHT_CYAN_COLOR else BRIGHT_CYAN_COLOR .copy(alpha = 0.5f ),
195+ color = if (touchPointInBounds) color else color .copy(alpha = 0.5f ),
181196 )
182197 }
183198 }
0 commit comments