|
|
@@ -25,6 +25,7 @@ import com.yingyangfly.baselib.utils.LiveEventBusUtil
|
|
|
import com.yingyangfly.baselib.utils.RxBusCodes
|
|
|
import com.yingyangfly.game.R
|
|
|
import com.yingyangfly.game.databinding.ActivityPlayGameBinding
|
|
|
+import com.yingyangfly.game.dialog.CountdownDialog
|
|
|
import com.yingyangfly.game.dialog.GameResultDialog
|
|
|
import com.yingyangfly.game.entity.GameBean
|
|
|
import com.yingyangfly.game.entity.GameDataBean
|
|
|
@@ -40,36 +41,25 @@ import gorden.rxbus2.ThreadMode
|
|
|
class PlayGameActivity : BaseMVVMActivity<ActivityPlayGameBinding, PlayGameViewModel>(),
|
|
|
View.OnTouchListener {
|
|
|
|
|
|
+ private var loadingOver = false
|
|
|
+ private var newStartGame = true
|
|
|
+ private var gameCode: String = ""
|
|
|
+ private var playClass: String = ""
|
|
|
+ private var url: String = ""
|
|
|
private var gameBean: GameBean? = null
|
|
|
private lateinit var webSettings: WebSettings
|
|
|
private var gameResultDialog: GameResultDialog? = null
|
|
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
- gameBean = intent.getSerializableExtra("bean") as GameBean
|
|
|
+ gameCode = intent.getStringExtra("gameCode") ?: ""
|
|
|
+ playClass = intent.getStringExtra("playClass") ?: ""
|
|
|
+ url = intent.getStringExtra("url") ?: ""
|
|
|
super.onCreate(savedInstanceState)
|
|
|
}
|
|
|
|
|
|
- override fun onNewIntent(intent: Intent?) {
|
|
|
- super.onNewIntent(intent)
|
|
|
- runOnUiThread {
|
|
|
- binding.webView.evaluateJavascript(
|
|
|
- "javascript:CallContinueGame()"
|
|
|
- ) {
|
|
|
-
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
override fun initViews() {
|
|
|
- if (gameBean != null) {
|
|
|
- val url = gameBean!!.gameUrl + "?startLv=" + gameBean!!.gameLevel + "&maxLv=" +
|
|
|
- gameBean!!.totalNum + "&gameSetTime=" + gameBean!!.gameDuration + "&difficultyRate=" +
|
|
|
- gameBean!!.gameDifficultyRate + "&totalTime=" + gameBean!!.gameTotalTime +
|
|
|
- "&totalScore=" + gameBean!!.gameTotalScore
|
|
|
- Log.e("wpp", url)
|
|
|
+ if (TextUtils.isEmpty(url).not()) {
|
|
|
initWebView(url)
|
|
|
- } else {
|
|
|
- "数据错误".toast()
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -86,40 +76,17 @@ class PlayGameActivity : BaseMVVMActivity<ActivityPlayGameBinding, PlayGameViewM
|
|
|
.asGif()
|
|
|
.load(R.drawable.fish)
|
|
|
.into(binding.loadingImage)
|
|
|
- //游戏时间
|
|
|
- LiveEventBusUtil.observer<String>(this, RxBusCodes.SETTIME) {
|
|
|
- binding.tvTime.post {
|
|
|
- if (TextUtils.isEmpty(it).not()) {
|
|
|
- binding.tvTime.text = it
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- //分数
|
|
|
- LiveEventBusUtil.observer<String>(this, RxBusCodes.SETSCORE) {
|
|
|
- binding.tvScore.post {
|
|
|
- if (TextUtils.isEmpty(it).not()) {
|
|
|
- binding.tvScore.text = it
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- //游戏关卡
|
|
|
- LiveEventBusUtil.observer<String>(this, RxBusCodes.SETLEVEL) {
|
|
|
- binding.tvGameLevel.post {
|
|
|
- if (TextUtils.isEmpty(it).not()) {
|
|
|
- binding.tvGameLevel.text = "第" + it + "关"
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- //游戏进度
|
|
|
- LiveEventBusUtil.observer<String>(this, RxBusCodes.SAVEDATA) {
|
|
|
- binding.tvScore.post {
|
|
|
- if (TextUtils.isEmpty(it).not()) {
|
|
|
- Log.e("wpp", it)
|
|
|
- val gameDataBean = GsonUtil.GsonToBean(it, GameDataBean::class.java)
|
|
|
- if (gameDataBean != null) {
|
|
|
- saveData(gameDataBean)
|
|
|
- }
|
|
|
- }
|
|
|
+ initLiveData()
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun onNewIntent(intent: Intent?) {
|
|
|
+ super.onNewIntent(intent)
|
|
|
+ newStartGame = false
|
|
|
+ runOnUiThread {
|
|
|
+ binding.webView.evaluateJavascript(
|
|
|
+ "javascript:CallContinueGame()"
|
|
|
+ ) {
|
|
|
+
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -252,6 +219,34 @@ class PlayGameActivity : BaseMVVMActivity<ActivityPlayGameBinding, PlayGameViewM
|
|
|
return true
|
|
|
}
|
|
|
|
|
|
+ override fun onResume() {
|
|
|
+ super.onResume()
|
|
|
+ if (newStartGame) {
|
|
|
+ newStartGame = false
|
|
|
+ val countdownDialog = CountdownDialog()
|
|
|
+ countdownDialog.onDialogClickListener = {
|
|
|
+ if (loadingOver) {
|
|
|
+ callStartGame()
|
|
|
+ }
|
|
|
+ }
|
|
|
+ countdownDialog.show(supportFragmentManager, "countdownDialog")
|
|
|
+ getGameDetailByCode()
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取游戏数据
|
|
|
+ */
|
|
|
+ private fun getGameDetailByCode() {
|
|
|
+ viewModel.getGameDetailByCode(gameCode, playClass, fail = {
|
|
|
+
|
|
|
+ }, success = {
|
|
|
+ if (it != null) {
|
|
|
+ gameBean = it
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
override fun onDestroy() {
|
|
|
destoryWebView()
|
|
|
super.onDestroy()
|
|
|
@@ -299,4 +294,63 @@ class PlayGameActivity : BaseMVVMActivity<ActivityPlayGameBinding, PlayGameViewM
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 游戏开始
|
|
|
+ */
|
|
|
+ fun callStartGame() {
|
|
|
+ runOnUiThread {
|
|
|
+ binding.webView.evaluateJavascript(
|
|
|
+ "javascript:CallStartGame()"
|
|
|
+ ) {
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 游戏进度监听
|
|
|
+ */
|
|
|
+ private fun initLiveData() {
|
|
|
+ //游戏时间
|
|
|
+ LiveEventBusUtil.observer<String>(this, RxBusCodes.SETTIME) {
|
|
|
+ binding.tvTime.post {
|
|
|
+ if (TextUtils.isEmpty(it).not()) {
|
|
|
+ binding.tvTime.text = it
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //分数
|
|
|
+ LiveEventBusUtil.observer<String>(this, RxBusCodes.SETSCORE) {
|
|
|
+ binding.tvScore.post {
|
|
|
+ if (TextUtils.isEmpty(it).not()) {
|
|
|
+ binding.tvScore.text = it
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //游戏关卡
|
|
|
+ LiveEventBusUtil.observer<String>(this, RxBusCodes.SETLEVEL) {
|
|
|
+ binding.tvGameLevel.post {
|
|
|
+ if (TextUtils.isEmpty(it).not()) {
|
|
|
+ binding.tvGameLevel.text = "第" + it + "关"
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //游戏进度
|
|
|
+ LiveEventBusUtil.observer<String>(this, RxBusCodes.SAVEDATA) {
|
|
|
+ binding.tvScore.post {
|
|
|
+ if (TextUtils.isEmpty(it).not()) {
|
|
|
+ Log.e("wpp", it)
|
|
|
+ val gameDataBean = GsonUtil.GsonToBean(it, GameDataBean::class.java)
|
|
|
+ if (gameDataBean != null) {
|
|
|
+ saveData(gameDataBean)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //游戏加载完成
|
|
|
+ LiveEventBusUtil.observer<String>(this, RxBusCodes.LOADINGOVER) {
|
|
|
+ loadingOver = true
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|