11package com.imsproject.watch.view
22
33import android.os.Bundle
4- import android.view.WindowManager
54import androidx.activity.compose.setContent
65import androidx.activity.viewModels
76import androidx.compose.foundation.Canvas
@@ -32,13 +31,14 @@ import com.imsproject.watch.DARK_BACKGROUND_COLOR
3231import com.imsproject.watch.GLOWING_YELLOW_COLOR
3332import com.imsproject.watch.LIGHT_BROWN_COLOR
3433import com.imsproject.watch.SCREEN_RADIUS
35- import com.imsproject.watch.initProperties
3634import com.imsproject.watch.utils.cartesianToPolar
3735import com.imsproject.watch.utils.polarToCartesian
3836import com.imsproject.watch.viewmodel.FlourMillViewModel
3937import com.imsproject.watch.viewmodel.GameViewModel
4038import androidx.compose.ui.input.pointer.PointerEventType
4139import com.imsproject.watch.BRIGHT_CYAN_COLOR
40+ import com.imsproject.watch.TOUCH_CIRCLE_RADIUS
41+ import com.imsproject.watch.utils.Angle
4242
4343class FlourMillActivity : GameActivity (GameType .FLOUR_MILL ) {
4444
@@ -65,8 +65,9 @@ class FlourMillActivity : GameActivity(GameType.FLOUR_MILL) {
6565 fun FlourMill () {
6666 val (centerX, centerY) = remember { polarToCartesian(0f , 0.0 ) }
6767 val axle = remember { viewModel.axle }
68- var touchPoint by remember { mutableStateOf(- 1f to - 1f ) }
6968 var showAxle by remember { mutableStateOf(false ) }
69+ val myTouchPoint = viewModel.myTouchPoint.collectAsState().value
70+ val opponentTouchPoint = viewModel.opponentTouchPoint.collectAsState().value
7071
7172 Box (
7273 modifier = Modifier
@@ -78,20 +79,19 @@ class FlourMillActivity : GameActivity(GameType.FLOUR_MILL) {
7879 val inputChange = pointerEvent.changes.first()
7980 inputChange.consume()
8081 val position = inputChange.position
82+ val (radius, angle) = cartesianToPolar(position.x, position.y)
83+ val relativeRadius = radius / SCREEN_RADIUS
8184 when (pointerEvent.type) {
8285 PointerEventType .Press -> {
83- val (_, angle) = cartesianToPolar(position.x, position.y)
8486 axle.angle = angle + - 90f
85- touchPoint = position.x to position.y
87+ viewModel.setTouchPoint(relativeRadius, angle)
8688 showAxle = true
8789 }
8890 PointerEventType .Move -> {
89- viewModel.setTouchPoint(position.x, position.y)
90- touchPoint = position.x to position.y
91+ viewModel.setTouchPoint(relativeRadius, angle)
9192 }
9293 PointerEventType .Release -> {
93- viewModel.setTouchPoint(- 1f , - 1f )
94- touchPoint = - 1f to - 1f
94+ viewModel.setTouchPoint(- 1f , Angle .undefined())
9595 showAxle = false
9696 }
9797 }
@@ -138,42 +138,48 @@ class FlourMillActivity : GameActivity(GameType.FLOUR_MILL) {
138138 .background(color = DARK_BACKGROUND_COLOR )
139139 )
140140 Canvas (modifier = Modifier .Companion .fillMaxSize()){
141- val touchCircleRadius = 30f
142- val (leftX, leftY) = polarToCartesian(SCREEN_RADIUS * 0.7f , axle.angle + 90f )
143- val (leftX2, leftY2) = polarToCartesian(SCREEN_RADIUS * 0.9f , axle.angle + 90f )
144- val (rightX, rightY) = polarToCartesian(SCREEN_RADIUS * 0.9f , axle.angle + - 90f )
145-
146- val (touchPointDistance,touchPointAngle) = cartesianToPolar(touchPoint.first, touchPoint.second)
147- val touchPointInBounds = (touchPointAngle - (axle.angle + 90f ) <= (360f / touchCircleRadius))
148- && touchPointDistance > (SCREEN_RADIUS * 0.7f - touchCircleRadius)
149- && touchPointDistance < (SCREEN_RADIUS * 0.9f + touchCircleRadius)
150-
151- val axlePath = Path ().apply {
152- moveTo(rightX,rightY)
153- lineTo(leftX, leftY)
154- }
141+ for (touchPoint in listOf (myTouchPoint, opponentTouchPoint)){
142+ if (touchPoint.first < 0f ) continue
155143
156- val axlePath2 = Path ().apply {
157- moveTo(leftX,leftY)
158- lineTo(leftX2, leftY2)
159- }
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 )
160147
161- if (showAxle){
162- drawPath(
163- path = axlePath,
164- color = GLOWING_YELLOW_COLOR ,
165- style = Stroke (width = AXLE_WIDTH )
166- )
167- drawPath(
168- path = axlePath2,
169- color = BRIGHT_CYAN_COLOR ,
170- style = Stroke (width = AXLE_WIDTH )
171- )
172- drawCircle(
173- radius = touchCircleRadius,
174- center = Offset (touchPoint.first, touchPoint.second),
175- color = if (touchPointInBounds) BRIGHT_CYAN_COLOR else BRIGHT_CYAN_COLOR .copy(alpha = 0.5f ),
176- )
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)
155+
156+ val axlePath = Path ().apply {
157+ moveTo(rightX,rightY)
158+ lineTo(leftX, leftY)
159+ }
160+
161+ val axlePath2 = Path ().apply {
162+ moveTo(leftX,leftY)
163+ lineTo(leftX2, leftY2)
164+ }
165+
166+ if (showAxle){
167+ drawPath(
168+ path = axlePath,
169+ color = GLOWING_YELLOW_COLOR ,
170+ style = Stroke (width = AXLE_WIDTH )
171+ )
172+ drawPath(
173+ path = axlePath2,
174+ color = BRIGHT_CYAN_COLOR ,
175+ style = Stroke (width = AXLE_WIDTH )
176+ )
177+ drawCircle(
178+ radius = TOUCH_CIRCLE_RADIUS ,
179+ center = Offset (x, y),
180+ color = if (touchPointInBounds) BRIGHT_CYAN_COLOR else BRIGHT_CYAN_COLOR .copy(alpha = 0.5f ),
181+ )
182+ }
177183 }
178184 }
179185 }
0 commit comments