Skip to content

Commit

Permalink
feat: 加入用户名录入
Browse files Browse the repository at this point in the history
  • Loading branch information
Jim-shop committed May 12, 2023
1 parent ba4377f commit a0d67cd
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 4 deletions.
48 changes: 47 additions & 1 deletion app/src/main/java/net/imshit/aircraftwar/gui/GameActivity.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
package net.imshit.aircraftwar.gui

import android.content.Context
import android.content.DialogInterface
import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.os.Message
import android.text.InputType
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.content.res.AppCompatResources
import androidx.constraintlayout.widget.ConstraintLayout.LayoutParams
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.WindowInsetsControllerCompat
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.android.material.textfield.TextInputEditText
import com.google.android.material.textfield.TextInputLayout
import com.google.android.material.textfield.TextInputLayout.END_ICON_CLEAR_TEXT
import net.imshit.aircraftwar.R
import net.imshit.aircraftwar.databinding.ActivityGameBinding
import net.imshit.aircraftwar.logic.Difficulty
import net.imshit.aircraftwar.logic.game.Games
Expand All @@ -30,7 +41,14 @@ class GameActivity : AppCompatActivity() {
gameMode = getSerializableExtra("gameMode", Difficulty::class.java) ?: Difficulty.EASY
soundMode = getBooleanExtra("soundMode", true)
}
val game: Games = Games.getGames(this, gameMode, soundMode)
val game: Games = Games.getGames(this, gameMode, soundMode).apply {
mainHandle = object : Handler(Looper.getMainLooper()) {
override fun handleMessage(msg: Message) {
super.handleMessage(msg)
onGameOver(gameMode, msg.what)
}
}
}
with(ActivityGameBinding.inflate(layoutInflater)) {
root.addView(game, LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT))
setContentView(root)
Expand All @@ -41,4 +59,32 @@ class GameActivity : AppCompatActivity() {
hide(WindowInsetsCompat.Type.systemBars())
}
}

private fun onGameOver(gameMode: Difficulty, score: Int) {
val edit = TextInputLayout(this).apply {
setPadding(64, 64, 64, 0)
hint = getString(R.string.game_dialog_content)
startIconDrawable =
AppCompatResources.getDrawable(this@GameActivity, R.drawable.ic_account_circle_24)
endIconMode = END_ICON_CLEAR_TEXT
isCounterEnabled = true
counterMaxLength = 16
addView(TextInputEditText(context).apply {
inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PERSON_NAME
})
}
val listener = DialogInterface.OnClickListener { dialog, which ->
when (which) {
DialogInterface.BUTTON_POSITIVE -> {
// TODO: 添加记录
}
}
ScoreboardActivity.actionStart(this, gameMode)
this@GameActivity.finish()
}
MaterialAlertDialogBuilder(this).setTitle(R.string.game_dialog_title)
.setIcon(R.drawable.ic_assignment_turned_in_24)
.setPositiveButton(android.R.string.ok, listener)
.setNegativeButton(android.R.string.cancel, listener).setView(edit).show()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ import net.imshit.aircraftwar.logic.Difficulty

class ScoreboardActivity : AppCompatActivity() {
companion object {
fun actionStart(context: Context, gameMode: Difficulty, score: Int) {
fun actionStart(context: Context, gameMode: Difficulty) {
context.startActivity(Intent(context, ScoreboardActivity::class.java).apply {
putExtra("gameMode", gameMode)
putExtra("score", score)
})
}
}
Expand Down
9 changes: 8 additions & 1 deletion app/src/main/java/net/imshit/aircraftwar/logic/game/Games.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.os.Handler
import android.os.Message
import android.util.AttributeSet
import android.view.SurfaceHolder
import android.view.SurfaceView
Expand Down Expand Up @@ -46,6 +48,8 @@ sealed class Games(context: Context, attrs: AttributeSet?, soundMode: Boolean) :
context = context, attrs = attrs, soundMode = false
)

var mainHandle: Handler? = null

// utils
lateinit var images: ImageManager
private val musicStrategy: MusicStrategies =
Expand Down Expand Up @@ -91,7 +95,7 @@ sealed class Games(context: Context, attrs: AttributeSet?, soundMode: Boolean) :
private val enemyListenerLists = listOf<List<EnemyListener>>(enemyAircrafts, enemyBullets)
private val heroList = mutableListOf<HeroAircraft>()
private val drawingElementLists =
listOf(heroList, animations, props, enemyBullets, heroBullets, enemyAircrafts)
listOf(enemyBullets, heroBullets, enemyAircrafts, props, animations, heroList)
private val lifeBarElementLists = listOf(heroList, enemyAircrafts)

init {
Expand Down Expand Up @@ -248,6 +252,9 @@ sealed class Games(context: Context, attrs: AttributeSet?, soundMode: Boolean) :
this.musicStrategy.setBgm(BgmType.NONE)
this.musicStrategy.playGameOver()
this.logicThread.interrupt()
this.mainHandle?.sendMessage(Message.obtain().apply {
what = score
})
}

private fun gameStop() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="@android:color/white"
android:pathData="M19,3h-4.18C14.4,1.84 13.3,1 12,1c-1.3,0 -2.4,0.84 -2.82,2L5,3c-1.1,0 -2,0.9 -2,2v14c0,1.1 0.9,2 2,2h14c1.1,0 2,-0.9 2,-2L21,5c0,-1.1 -0.9,-2 -2,-2zM12,3c0.55,0 1,0.45 1,1s-0.45,1 -1,1 -1,-0.45 -1,-1 0.45,-1 1,-1zM10,17l-4,-4 1.41,-1.41L10,14.17l6.59,-6.59L18,9l-8,8z" />
</vector>
10 changes: 10 additions & 0 deletions app/src/main/res/drawable/ic_account_circle_24.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:tint="#000000"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="@android:color/white"
android:pathData="M12,2C6.48,2 2,6.48 2,12s4.48,10 10,10s10,-4.48 10,-10S17.52,2 12,2zM12,6c1.93,0 3.5,1.57 3.5,3.5S13.93,13 12,13s-3.5,-1.57 -3.5,-3.5S10.07,6 12,6zM12,20c-2.03,0 -4.43,-0.82 -6.14,-2.88C7.55,15.8 9.68,15 12,15s4.45,0.8 6.14,2.12C16.43,19.18 14.03,20 12,20z" />
</vector>
10 changes: 10 additions & 0 deletions app/src/main/res/drawable/ic_assignment_turned_in_24.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:tint="#000000"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="@android:color/white"
android:pathData="M19,3h-4.18C14.4,1.84 13.3,1 12,1c-1.3,0 -2.4,0.84 -2.82,2L5,3c-1.1,0 -2,0.9 -2,2v14c0,1.1 0.9,2 2,2h14c1.1,0 2,-0.9 2,-2L21,5c0,-1.1 -0.9,-2 -2,-2zM12,3c0.55,0 1,0.45 1,1s-0.45,1 -1,1 -1,-0.45 -1,-1 0.45,-1 1,-1zM10,17l-4,-4 1.41,-1.41L10,14.17l6.59,-6.59L18,9l-8,8z" />
</vector>
2 changes: 2 additions & 0 deletions app/src/main/res/values-en/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
<string name="button_medium">Medium Mode</string>
<string name="button_hard">Hard Mode</string>
<string name="checkbox_sound">Sound</string>
<string name="game_dialog_title">New record!</string>
<string name="game_dialog_content">Your name</string>
<string name="scoreboard_label">Scoreboard</string>
<string name="manage_space_label">Manage Space</string>
<string name="item_about">About</string>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
<string name="button_hard">困难模式</string>
<string name="checkbox_sound">音效</string>
<string name="game_canvas_text_score">得分:%d</string>
<string name="game_dialog_title">新的游戏记录!</string>
<string name="game_dialog_content">你的名字</string>
<string name="scoreboard_label">排行榜</string>
<string name="item_about">关于</string>
<string name="manage_space_label">管理空间</string>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-zh/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
<string name="button_hard">困难模式</string>
<string name="checkbox_sound">音效</string>
<string name="game_canvas_text_score">得分:%d</string>
<string name="game_dialog_title">新的游戏记录!</string>
<string name="game_dialog_content">你的名字</string>
<string name="scoreboard_label">排行榜</string>
<string name="item_about">关于</string>
<string name="manage_space_label">管理空间</string>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
<string name="main_label" translatable="false">@string/app_name</string>
<string name="game_label">游戏界面</string>
<string name="game_canvas_text_score">得分:%d</string>
<string name="game_dialog_title">新的游戏记录!</string>
<string name="game_dialog_content">你的名字</string>
<string name="scoreboard_label">排行榜</string>
<string name="manage_space_label">管理空间</string>
<string name="button_easy">简单模式</string>
Expand Down

0 comments on commit a0d67cd

Please sign in to comment.