@@ -6,29 +6,23 @@ import android.util.Log
66import androidx.compose.runtime.getValue
77import androidx.compose.runtime.mutableStateOf
88import androidx.compose.runtime.setValue
9- import androidx.compose.ui.graphics.Color
109import androidx.lifecycle.viewModelScope
1110import com.imsproject.common.gameserver.GameAction
1211import com.imsproject.common.gameserver.GameType
1312import com.imsproject.common.gameserver.SessionEvent
1413import com.imsproject.watch.ACTIVITY_DEBUG_MODE
15- import com.imsproject.watch.AXLE_STARTING_ANGLE
16- import com.imsproject.watch.BRIGHT_CYAN_COLOR
1714import com.imsproject.watch.FLOUR_MILL_SYNC_TIME_THRESHOLD
18- import com.imsproject.watch.LIGHT_GRAY_COLOR
1915import com.imsproject.watch.PACKAGE_PREFIX
20- import com.imsproject.watch.RESET_COOLDOWN_WAIT_TIME
21- import com.imsproject.watch.STRETCH_PEAK
16+ import com.imsproject.watch.SCREEN_RADIUS
17+ import com.imsproject.watch.TOUCH_CIRCLE_RADIUS
2218import com.imsproject.watch.utils.Angle
23- import com.imsproject.watch.utils.toAngle
2419import com.imsproject.watch.view.contracts.Result
2520import kotlinx.coroutines.Dispatchers
2621import kotlinx.coroutines.delay
2722import kotlinx.coroutines.flow.MutableStateFlow
2823import kotlinx.coroutines.flow.StateFlow
2924import kotlinx.coroutines.launch
30- import kotlinx.coroutines.withContext
31- import kotlin.math.absoluteValue
25+ import kotlin.math.abs
3226
3327class FlourMillViewModel : GameViewModel (GameType .FLOUR_MILL ) {
3428
@@ -50,7 +44,7 @@ class FlourMillViewModel : GameViewModel(GameType.FLOUR_MILL) {
5044 }
5145 }
5246
53- class Axle (startingAngle : Angle , val mySide : AxleSide ) {
47+ class Axle (startingAngle : Angle ) {
5448 var angle by mutableStateOf(startingAngle)
5549 var targetAngle = startingAngle
5650 }
@@ -84,13 +78,25 @@ class FlourMillViewModel : GameViewModel(GameType.FLOUR_MILL) {
8478 override fun onCreate (intent : Intent , context : Context ) {
8579 super .onCreate(intent,context)
8680
81+ viewModelScope.launch {
82+ while (true ){
83+ delay(16 )
84+ updateAxleAngle()
85+ }
86+ }
87+
8788 if (ACTIVITY_DEBUG_MODE ) {
8889 myAxleSide = AxleSide .RIGHT
89- // axle = Axle(AXLE_STARTING_ANGLE.toAngle(), myAxleSide)
9090 viewModelScope.launch(Dispatchers .Default ) {
9191 while (true ) {
92- delay(1000 )
93- // TODO: add rotation code
92+ delay(100 )
93+ val axle = _axle .value
94+ if (axle == null ){
95+ _opponentTouchPoint .value = - 1f to Angle .undefined()
96+ } else {
97+ val angle = axle.angle + - 80f
98+ _opponentTouchPoint .value = 0.8f to angle
99+ }
94100 }
95101 }
96102 return
@@ -113,9 +119,27 @@ class FlourMillViewModel : GameViewModel(GameType.FLOUR_MILL) {
113119 if (relativeRadius < 0f ){
114120 _axle .value = null
115121 } else if (relativeRadius >= 0f && _axle .value == null ){
116- val axleAngle = if (myAxleSide == AxleSide .RIGHT ) angle + - 90f else angle + 90f
117- _axle .value = Axle (axleAngle, myAxleSide)
122+
123+ // TODO: do this in the correct place as it won't be local in the future
124+ val axleAngle = angle + if (myAxleSide == AxleSide .RIGHT ) - AxleSide .RIGHT .angle else AxleSide .LEFT .angle
125+ _axle .value = Axle (axleAngle)
118126 }
127+
128+ // TODO: network calls
129+ }
130+
131+ fun isTouchPointInbounds (side : AxleSide ): Boolean {
132+ val axle = _axle .value ? : return false
133+
134+ val touchPoint = if (side == myAxleSide) _myTouchPoint .value else _opponentTouchPoint .value
135+ if (touchPoint.first < 0f ) return false
136+
137+ val touchPointDistance = touchPoint.first * SCREEN_RADIUS
138+ val touchPointAngle = touchPoint.second
139+ val sideAngle = axle.angle + if (side == AxleSide .LEFT ) AxleSide .LEFT .angle else AxleSide .RIGHT .angle
140+ return (touchPointAngle - sideAngle <= (360f / TOUCH_CIRCLE_RADIUS ))
141+ && touchPointDistance > (SCREEN_RADIUS * 0.7f - TOUCH_CIRCLE_RADIUS )
142+ && touchPointDistance < (SCREEN_RADIUS * 0.9f + TOUCH_CIRCLE_RADIUS )
119143 }
120144
121145 // ================================================================================ |
@@ -160,6 +184,24 @@ class FlourMillViewModel : GameViewModel(GameType.FLOUR_MILL) {
160184 }
161185 }
162186
187+ private fun updateAxleAngle (){
188+ val axle = _axle .value ? : return
189+ if (isTouchPointInbounds(AxleSide .LEFT ) && isTouchPointInbounds(AxleSide .RIGHT )){
190+ val myAngle = _myTouchPoint .value.second
191+ val opponentAngle = _opponentTouchPoint .value.second
192+ val mySideAngle = axle.angle + myAxleSide.angle
193+ val opponentSideAngle = axle.angle + myAxleSide.otherSide().angle
194+ val myAngleDiff = myAngle - mySideAngle
195+ val opponentAngleDiff = opponentAngle - opponentSideAngle
196+ val myDirection = if (Angle .isClockwise(mySideAngle,myAngle)) 1 else - 1
197+ val opponentDirection = if (Angle .isClockwise(opponentSideAngle, opponentAngle)) 1 else - 1
198+ if (myAngleDiff > 0 && opponentAngleDiff > 0 && myDirection == opponentDirection){
199+ val amountToRotate = abs(myAngleDiff - opponentAngleDiff)
200+ axle.targetAngle = axle.targetAngle + (amountToRotate * myDirection)
201+ }
202+ }
203+ }
204+
163205 companion object {
164206 private const val TAG = " FlourMillViewModel"
165207 }
0 commit comments