Skip to content

Commit

Permalink
Merge pull request #12 from SuperSlide-Game/dev
Browse files Browse the repository at this point in the history
First deliver
  • Loading branch information
peremunoz authored May 7, 2023
2 parents 05efa0c + b38953d commit 4a4d1cd
Show file tree
Hide file tree
Showing 41 changed files with 901 additions and 398 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ import com.example.superslidegame.databinding.FragmentPopUpBinding
import com.example.superslidegame.game.screen.SelectLevel
import com.example.superslidegame.log.screen.LogScreen

/**
* Fragment for the pop up menu, which appears when the player wins the game
*/
class PopUpFragment : DialogFragment() {

private lateinit var binding : FragmentPopUpBinding

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
//setStyle(STYLE_NO_TITLE, android.R.style.Theme_DeviceDefault_Light_Dialog_NoActionBar_MinWidth)
isCancelable = false
}
override fun onCreateView(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ import androidx.fragment.app.DialogFragment
import com.example.superslidegame.databinding.FragmentTimeUpBinding
import com.example.superslidegame.log.screen.LogScreen


/**
* Fragment for the time up pop up, which appears when the player runs out of time or moves
*/
class TimeUpFragment : DialogFragment() {

private lateinit var binding : FragmentTimeUpBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
//setStyle(STYLE_NO_TITLE, android.R.style.Theme_DeviceDefault_Light_Dialog_NoActionBar_MinWidth)
isCancelable = false
}
override fun onCreateView(
Expand Down
454 changes: 237 additions & 217 deletions app/src/main/java/com/example/superslidegame/game/GameLogic.kt

Large diffs are not rendered by default.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,34 @@ package com.example.superslidegame.game.elements
import android.content.Context
import android.view.View
import android.view.View.OnClickListener
import android.widget.Toast
import com.example.superslidegame.game.GameLogic

class ClickListener(private val context: Context, private val positionClicked: Int, private val adapter: ImageAdapter) : OnClickListener {
/**
* Listener for the 1-cell movement
* @param context Context of the application
* @param positionClicked Position of the piece clicked
* @param adapter Adapter of the board
*/
class ClickListener(context: Context, private val positionClicked: Int, private val adapter: ImageAdapter) : OnClickListener {

private val gameLogic : GameLogic = GameLogic(context, adapter)
override fun onClick(view: View?) {

val animationHelper = adapter.animationHelper
val pieceClicked = adapter.getPiecesState()[positionClicked]
val actualState = adapter.getPiecesState()

when (pieceClicked.type) {
PieceType.EMPTY -> {
Toast.makeText(context, "You cannot perform any action in an empty position", Toast.LENGTH_SHORT).show()
// Empty position, do nothing
}
else -> {
val positionToMove = gameLogic.whereToMove(positionClicked, actualState)
if (positionToMove != null && GameLogic.GAME_STATE == GameState.Type.IN_PROGRESS) {
Toast.makeText(context,
"You can move this piece to position: $positionToMove", Toast.LENGTH_SHORT).show()
animationHelper.playMoveSound()
gameLogic.move(positionClicked, positionToMove, actualState)
adapter.updateBoard()
gameLogic.checkWin(actualState)
} else {
Toast.makeText(context, "You cannot move this piece", Toast.LENGTH_SHORT).show()
// Do nothing
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.example.superslidegame.game.elements

/**
* Enum class for the directions
*/
enum class Direction {
UP,
DOWN,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@ package com.example.superslidegame.game.elements
import android.os.Parcelable
import kotlinx.parcelize.Parcelize

/**
* GamePiece is a class that represents a piece in the game.
* @param type The type of piece.
* @param groupId The group id of the piece.
* @param rotation The rotation of the piece.
* @param imgSrc The image source of the piece.
* @constructor Creates a GamePiece.
* @see PieceType
*/
@Parcelize
class GamePiece(val type: PieceType, val groupId: Int, val rotation: Float = 0f, val imgSrc: Int = type.imgSrc) : Parcelable {
constructor(type: PieceType) : this(type, 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,51 @@ package com.example.superslidegame.game.elements

import android.os.Bundle

data class GameState(val nickname : String, val difficulty : String, val level : Int, var board : MutableList<GamePiece>? = null) {
/**
* GameState is a data class that holds all the information about the current state of the game.
* It is used to save and restore the game state.
* @param nickname The nickname of the player
* @param difficulty The difficulty of the game
* @param level The current level of the game
* @param board The current board of the game
* @param timeLeft The time left in the game
* @param pieceGroups The piece groups of the game
* @constructor Creates a GameState object
* @see GamePiece
* @see PieceGroup
*/
data class GameState(val nickname : String, val difficulty : String, val level : Int, var board : MutableList<GamePiece>? = null, var timeLeft : Long? = null, var pieceGroups : MutableList<PieceGroup>? = null) {

enum class Type {
WIN, LOSE, IN_PROGRESS
}
companion object {
/**
* Creates a GameState object from a Bundle
*/
fun fromBundle(bundle: Bundle) : GameState {
return GameState(
bundle.getString("nickname")!!,
bundle.getString("difficulty")!!,
bundle.getInt("level"),
bundle.getParcelableArray("board")?.map { it as GamePiece }?.toMutableList()
bundle.getParcelableArray("board")?.map { it as GamePiece }?.toMutableList(),
bundle.getLong("timeLeft"),
bundle.getParcelableArray("pieceGroups")?.map { it as PieceGroup }?.toMutableList()
)
}
}

/**
* Returns a Bundle representation of the GameState object
*/
fun toBundle() : Bundle {
val bundle = Bundle()
bundle.putString("nickname", nickname)
bundle.putString("difficulty", difficulty)
bundle.putInt("level", level)
bundle.putParcelableArray("board", board?.toTypedArray())
timeLeft?.let { bundle.putLong("timeLeft", it) }
bundle.putParcelableArray("pieceGroups", pieceGroups?.toTypedArray())
return bundle
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,20 @@ import android.widget.ImageButton
import android.widget.ImageView
import androidx.core.content.ContextCompat
import com.example.superslidegame.R
import com.example.superslidegame.game.animations.AnimationHelper
import com.example.superslidegame.game.levels.Level
import com.example.superslidegame.game.screen.GameScreen

class ImageAdapter(private val screenActivity: GameScreen, val level: Level, val animationHelper: AnimationHelper) : BaseAdapter() {
/**
* Adapter for the GridView that contains the pieces of the game
* @param screenActivity The activity that contains the GridView
* @param level The level that is being played
* @property pieces The list of pieces that are being played
* @property groups The list of groups that are being played
* @property context The context of the activity
* @constructor Creates an adapter for the GridView
* @see BaseAdapter
*/
class ImageAdapter(private val screenActivity: GameScreen, val level: Level) : BaseAdapter() {

private val pieces: MutableList<GamePiece> = level.getPieces()

Expand All @@ -39,7 +48,11 @@ class ImageAdapter(private val screenActivity: GameScreen, val level: Level, val
imageButton.scaleType = ImageView.ScaleType.FIT_CENTER
imageButton.adjustViewBounds = true
imageButton.setPadding(0, 0, 0, 0)
//
// If the device is in landscape mode, the width of the buttons is reduced in size
if (screenActivity.resources.configuration.orientation == 2) {
imageButton.layoutParams = ViewGroup.LayoutParams(150, 150)
imageButton.adjustViewBounds = false
}

} else {
imageButton = convertView as ImageButton
Expand All @@ -61,6 +74,8 @@ class ImageAdapter(private val screenActivity: GameScreen, val level: Level, val
return imageButton
}

// Auxiliary functions

fun getPiecesState() : MutableList<GamePiece> {
return pieces
}
Expand All @@ -84,7 +99,6 @@ class ImageAdapter(private val screenActivity: GameScreen, val level: Level, val
}

fun swapPositions(fromPosition: Int, toPosition: Int) {
// Swap the positions of the pieces list
val temp = pieces[fromPosition]
pieces[fromPosition] = pieces[toPosition]
pieces[toPosition] = temp
Expand All @@ -102,4 +116,17 @@ class ImageAdapter(private val screenActivity: GameScreen, val level: Level, val
fun getLevelNumber() : Int {
return screenActivity.getPlayingLevel()
}

fun updateMoves(moves: Int) {
screenActivity.updateMoves(moves)
}

fun onGameFinished() {
val seconds = screenActivity.getGameTimer().cancelAndGetTimeLeft()
screenActivity.onGameFinished(seconds)
}

fun isExtremeModeGame(): Boolean {
return screenActivity.isExtremeModeGame()
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
package com.example.superslidegame.game.elements

import android.app.Activity
import android.content.res.ColorStateList
import android.graphics.Color
import android.util.SparseBooleanArray
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.view.ViewGroup.LayoutParams
import android.widget.BaseAdapter
import android.widget.Button
import com.example.superslidegame.game.levels.GameLevel


class LevelListAdapter(private val activity: Activity) : BaseAdapter() {
private val buttonStateArray = SparseBooleanArray()
import com.example.superslidegame.game.screen.SelectLevel

/**
* Adapter for the level list displayed in the level selection screen.
* @param activity The activity that contains the level list.
* @constructor Creates a new adapter for the level list.
* @property maxLevel The maximum level that can be selected.
*/
class LevelListAdapter(private val activity: SelectLevel) : BaseAdapter() {
private val maxLevel = GameLevel.MAX_LEVEL

override fun getCount(): Int {
Expand All @@ -36,30 +40,23 @@ class LevelListAdapter(private val activity: Activity) : BaseAdapter() {
button.gravity = Gravity.CENTER
button.layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)
button.textSize = 15f

} else {
button = convertView as Button
}
button.text = String.format("Level %d", position + 1)
if (buttonStateArray[position, false]) {
button.setBackgroundColor(Color.GREEN) // Change to the color you want when clicked
} else {
button.setBackgroundColor(Color.WHITE) // Change to the default color
}

// Set a click listener for the button

// Set a click listener for the button
button.setOnClickListener {
val currentState = buttonStateArray[position, false]
buttonStateArray.put(position, !currentState)

// Set the background color based on the new state
if (!currentState) {
button.setBackgroundColor(Color.GREEN) // Change to the color you want when clicked
} else {
button.setBackgroundColor(Color.WHITE) // Change to the default color
}
button.setOnClickListener(LevelSelectorClickListener(
position,
activity,
this
))
val selectedLevel = activity.selectedLevel
val buttonText = button.text.toString()
val levelNumber = buttonText.substring(buttonText.length - 1).toInt()
if (levelNumber == selectedLevel) {
button.backgroundTintList = ColorStateList.valueOf(Color.GREEN)
} else {
button.backgroundTintList = ColorStateList.valueOf(Color.WHITE)
}
return button
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
package com.example.superslidegame.game.elements

import android.content.res.ColorStateList
import android.graphics.Color
import android.util.SparseBooleanArray
import android.view.View
import android.view.View.OnClickListener
import android.widget.Button
import com.example.superslidegame.game.screen.SelectLevel

class LevelSelectorClickListener(private val levelClicked: Int, private val activity: SelectLevel, private val button : Button, private val buttonStateArray : SparseBooleanArray) : OnClickListener {
/**
* LevelSelectorClickListener
* This class is used to handle the click events of the level buttons in the level selector screen.
* @param levelClicked The level that was clicked
* @param activity The activity that the level selector is in
* @param adapter The adapter that is used to display the level buttons
*/
class LevelSelectorClickListener(
private val levelClicked: Int,
private val activity: SelectLevel,
private val adapter: LevelListAdapter
) : OnClickListener {

override fun onClick(position: View?) {
if (buttonStateArray.get(levelClicked, false)) {
button.setBackgroundColor(Color.GREEN); // Change to the color you want when clicked
} else {
button.setBackgroundColor(Color.TRANSPARENT); // Change to the default color
}
// Set the level to the level clicked
activity.setLevel(levelClicked + 1)
// Set the background tint list of the button clicked to green
position?.backgroundTintList = ColorStateList.valueOf(Color.GREEN)
// Set the background tint list of the other buttons to the default color by refreshing the adapter
adapter.notifyDataSetChanged()
}
}
Loading

0 comments on commit 4a4d1cd

Please sign in to comment.