Skip to content

Commit 88e3cba

Browse files
committed
2 players works
1 parent 72e147f commit 88e3cba

2 files changed

Lines changed: 30 additions & 7 deletions

File tree

game_server/src/main/kotlin/com/imsproject/gameserver/business/games/FlourMillGame.kt

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,23 @@ class FlourMillGame(
2222
private var packetNumber = 0L
2323

2424
// player 1 data
25+
@Volatile
2526
private var player1TouchPoint = -1f to Angle.undefined
27+
@Volatile
2628
private var player1InBounds = false
29+
@Volatile
2730
private var player1LastUpdate = 0L
31+
@Volatile
2832
private var player1LastSequenceNumber = 0L
2933

3034
// player 2 data
35+
@Volatile
3136
private var player2TouchPoint = -1f to Angle.undefined
37+
@Volatile
3238
private var player2InBounds = false
39+
@Volatile
3340
private var player2LastUpdate = 0L
41+
@Volatile
3442
private var player2LastSequenceNumber = 0L
3543

3644
// axle data
@@ -49,6 +57,12 @@ class FlourMillGame(
4957
} else {
5058
axleAngle = player2TouchPoint.second + Angle(-90f)
5159
}
60+
} else if (player1TouchPoint.first > 0f) {
61+
axleAngle = player1TouchPoint.second + Angle(90f)
62+
} else if (player2TouchPoint.first > 0f) {
63+
axleAngle = player2TouchPoint.second + Angle(-90f)
64+
} else {
65+
axleAngle = Angle.undefined
5266
}
5367
} else {
5468
if(player1TouchPoint.first > 0f && player2TouchPoint.first > 0f) {
@@ -66,6 +80,8 @@ class FlourMillGame(
6680
axleAngle += amountToRotate * player1Direction
6781
}
6882
}
83+
} else if (player1TouchPoint.first < 0f && player2TouchPoint.first < 0f) {
84+
axleAngle = Angle.undefined
6985
}
7086
}
7187
sendGameAction(GameAction.builder(GameAction.Type.USER_INPUT)
@@ -148,8 +164,8 @@ class FlourMillGame(
148164
}
149165

150166
override fun endGame(errorMessage: String?) {
151-
super.endGame(errorMessage)
152167
scope.cancel()
168+
super.endGame(errorMessage)
153169
}
154170

155171
companion object {

watch/app/src/main/java/com/imsproject/watch/viewmodel/FlourMillViewModel.kt

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,17 +122,24 @@ class FlourMillViewModel : GameViewModel(GameType.FLOUR_MILL) {
122122
}
123123
FLOUR_MILL_SYNC_TIME_THRESHOLD = syncTolerance
124124
Log.d(TAG, "syncTolerance: $syncTolerance")
125+
126+
viewModelScope.launch(Dispatchers.IO) {
127+
while(true){
128+
delay(16)
129+
val timestamp = super.getCurrentGameTime()
130+
val (relativeRadius,angle) = _myTouchPoint.value
131+
val touchPointInbounds = _myInBounds.value
132+
val data = "$relativeRadius,$angle,$touchPointInbounds"
133+
// TODO: add event logging of action
134+
model.sendUserInput(timestamp, packetTracker.newPacket(),data)
135+
}
136+
}
125137
}
126138

127139
fun setTouchPoint(relativeRadius: Float, angle: Angle) {
128140
_myTouchPoint.value = relativeRadius to angle
129141
val touchPointInbounds = isTouchPointInbounds()
130142
_myInBounds.value = touchPointInbounds
131-
val data = "$relativeRadius,$angle,$touchPointInbounds"
132-
viewModelScope.launch(Dispatchers.IO) {
133-
// TODO: add event logging of action
134-
model.sendUserInput(super.getCurrentGameTime(), packetTracker.newPacket(),data)
135-
}
136143
}
137144

138145
// ================================================================================ |
@@ -179,7 +186,7 @@ class FlourMillViewModel : GameViewModel(GameType.FLOUR_MILL) {
179186
}
180187
} else {
181188
if(newAxleAngle != Angle.undefined){
182-
axle.angle = newAxleAngle
189+
axle.targetAngle = newAxleAngle
183190
} else {
184191
_axle.value = null
185192
}

0 commit comments

Comments
 (0)