Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

5 Shields #54

Merged
merged 7 commits into from Apr 21, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 35 additions & 8 deletions android/src/group16/project/game/AndroidFirebaseConnection.kt
Expand Up @@ -207,13 +207,14 @@ class AndroidFirebaseConnection : FirebaseInterface, Activity() {
println("UPDATE GAME STATE: $state")
}

override fun setPlayersChoice(position: Int, targetPostion: Int, gameScreen: GameScreen) {
override fun setPlayersChoice(position: Int, targetPostion: Int, shieldPosition: Int, gameScreen: GameScreen) {
var myRef: DatabaseReference = database.getReference("lobbies").child(GameInfo.currentGame)

myRef.get().addOnSuccessListener {
if(it.value != null) {
myRef.child(GameInfo.player).child("position").setValue(position)
myRef.child(GameInfo.player).child("target_position").setValue(targetPostion)
myRef.child(GameInfo.player).child("shield_position").setValue(shieldPosition)
myRef.child(GameInfo.player).child("ready_to_fire").setValue(true)
println("YOU FIRE")
fire(gameScreen)
Expand Down Expand Up @@ -260,17 +261,35 @@ class AndroidFirebaseConnection : FirebaseInterface, Activity() {
database.getReference("lobbies").updateChildren(updates)
}

override fun removeShield() {
val updates: MutableMap<String, Any> = HashMap()
updates["${GameInfo.currentGame}/${GameInfo.player}/shield_position"] = -2
updates["${GameInfo.currentGame}/${GameInfo.player}/shield_destroyed"] = true
database.getReference("lobbies").updateChildren(updates)

}

override fun updatePlayerHealth(){
var myRef: DatabaseReference = database.getReference("lobbies").child(GameInfo.currentGame)
myRef.get().addOnSuccessListener {
val opponentTargetPosition = (it.child(GameInfo.opponent).child("target_position").value as Long).toInt()
val shieldPosition = (it.child(GameInfo.player).child("shield_position").value as Long).toInt()
var shieldDestroyed = it.child(GameInfo.player).child("shield_destroyed").value
if (shieldDestroyed != null) shieldDestroyed = shieldDestroyed as Boolean
else shieldDestroyed = false
val yourPosition = (it.child(GameInfo.player).child("position").value as Long).toInt()
if (opponentTargetPosition == yourPosition) {
println("UPDATE PLAYER HEALTH")
println(opponentTargetPosition)
println(yourPosition)
reduceHeartsAmount()
if (!shieldDestroyed && opponentTargetPosition == shieldPosition) {
println("REMOVE SHIELD")
removeShield()
} else {
if (opponentTargetPosition == yourPosition) {
println("UPDATE PLAYER HEALTH")
println(opponentTargetPosition)
println(yourPosition)
reduceHeartsAmount()
}
}

}
}

Expand Down Expand Up @@ -304,21 +323,29 @@ class AndroidFirebaseConnection : FirebaseInterface, Activity() {
if(hostReady and player2Ready) {
val player1MovingTo = host.child("position").value.toString().toInt()
val player1Shooting = host.child("target_position").value.toString().toInt()
var player1Shields = host.child("shield_position").value
if (player1Shields != null) player1Shields = player1Shields.toString().toInt()
else player1Shields = -1

val player2MovingTo = player2.child("position").value.toString().toInt()
val player2Shooting = player2.child("target_position").value.toString().toInt()
var player2Shields = player2.child("shield_position").value
if (player2Shields != null) player2Shields = player2Shields.toString().toInt()
else player2Shields = -1

if (player1MovingTo == player2Shooting && player2MovingTo == player1Shooting) bothHit = true

if (GameInfo.player == "host") screen.bothReady(
player2MovingTo,
player2Shooting,
bothHit
bothHit,
player2Shields
)
else screen.bothReady(
player1MovingTo,
player1Shooting,
bothHit
bothHit,
player1Shields
)
}
}
Expand Down
Binary file added assets/Shield.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/Shieldbtn.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions core/src/group16/project/game/controllers/InputHandler.kt
Expand Up @@ -5,10 +5,12 @@ import com.badlogic.gdx.math.MathUtils

object InputHandler {
var playerPosition = 0
var playerShieldPosition = -1
var enemyPosition = MathUtils.random(0, 3)

var playerTrajectoryPosition = 0
var enemyTrajectoryPosition = 0
var enemyShieldPosition = -1

var fireShots = false
}
7 changes: 2 additions & 5 deletions core/src/group16/project/game/ecs/Engine.kt
Expand Up @@ -4,11 +4,7 @@ import com.badlogic.ashley.core.PooledEngine
import com.badlogic.gdx.graphics.OrthographicCamera
import com.badlogic.gdx.graphics.g2d.Batch
import com.badlogic.gdx.math.Rectangle
import group16.project.game.ecs.system.HeartSystem
import group16.project.game.ecs.system.MovementSystem
import group16.project.game.ecs.system.PositioningSystem
import group16.project.game.ecs.system.RenderingSystem
import group16.project.game.ecs.system.ShootingSystem
import group16.project.game.ecs.system.*

class Engine(
batch: Batch,
Expand All @@ -25,6 +21,7 @@ class Engine(
addSystem(ShootingSystem())
addSystem(renderingSystem)
addSystem(heartSystem)
addSystem(ShieldSystem())
}

fun dispose() {
Expand Down
Expand Up @@ -6,6 +6,13 @@ import com.badlogic.ashley.core.Component
class ShieldComponent: Component {

var shield = 0
var player = false
var position = -1
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since InputHandler.playerShieldPosition is a thing, you can just use it to draw the shield just like what we've have with the ufos.

If this is however intentional that the shield is planned to still exist after being placed and not shot on that round, as well as that we can trigger/place multiple shields then ignore this :)
(if it is planned like so, then the firebase data needs to be reworked, as the data as of now only allows for 1 shield to be available at a time)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

having a position value in ShieldComponent makes it easier to check position in ShieldSystem without having to check whether the ShieldComponent is left or right, so that's why we've implemented it that way

Copy link
Collaborator

@Shirajuki Shirajuki Apr 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, if it is only used for that, then shieldComponent.player would suffice.
It is okay, a small fix / code cleanup in the future can be done. :)

var destroyed = false

fun setPos(position: Int) {
println("Set SHIELD POSITION to "+position+" (was "+this.position+")")
this.position = position
}

}
23 changes: 22 additions & 1 deletion core/src/group16/project/game/ecs/system/PositioningSystem.kt
Expand Up @@ -13,7 +13,7 @@ class PositioningSystem () : IteratingSystem(
Family.all(
BodyComponent::class.java,
PositionComponent::class.java
).one(UfoComponent::class.java, TargetComponent::class.java).get()
).one(UfoComponent::class.java, TargetComponent::class.java, ShieldComponent::class.java).get()
) {


Expand All @@ -23,6 +23,10 @@ class PositioningSystem () : IteratingSystem(
this.position(ComponentMapper.position[entity],
ComponentMapper.body[entity],
ComponentMapper.ufo[entity])
}else if (ComponentMapper.shield[entity] != null) {
this.position(ComponentMapper.position[entity],
ComponentMapper.body[entity],
ComponentMapper.shield[entity])
} else { // Target
this.position(ComponentMapper.position[entity],
ComponentMapper.body[entity],
Expand All @@ -45,6 +49,23 @@ class PositioningSystem () : IteratingSystem(
bodyComponent.rectangle.y = lerp(positionComponent.y, target, 0.1f)
positionComponent.y = lerp(positionComponent.y, target, 0.1f)
}
private fun position(
positionComponent: PositionComponent,
bodyComponent: BodyComponent,
shieldComponent: ShieldComponent
) {
val padding = (Configuration.gameHeight - 4*100) / 2
val buttonHeight = 100

var shield = InputHandler.enemyShieldPosition
if (shieldComponent.player)
shield = InputHandler.playerShieldPosition
val shieldPosition = shield * buttonHeight + padding

shieldComponent.position = shield
bodyComponent.rectangle.y = lerp(positionComponent.y, shieldPosition, 0.1f)
positionComponent.y = lerp(positionComponent.y, shieldPosition, 0.1f)
}
private fun position(
positionComponent: PositionComponent,
bodyComponent: BodyComponent,
Expand Down
50 changes: 41 additions & 9 deletions core/src/group16/project/game/ecs/system/ShieldSystem.kt
@@ -1,16 +1,48 @@
package group16.project.game.ecs.system

class ShieldSystem {
import com.badlogic.ashley.core.Entity
import com.badlogic.ashley.core.Family
import com.badlogic.ashley.systems.IteratingSystem
import group16.project.game.ecs.component.*
import group16.project.game.ecs.notNull
import com.badlogic.gdx.graphics.g2d.Batch
import group16.project.game.Configuration
import group16.project.game.ecs.utils.ComponentMapper
import javax.xml.crypto.dsig.Transform

fun increase() {
//if the ufo and shield are at the same spot, increase the shield strength
// if
// shield++;
class ShieldSystem: IteratingSystem(
Family.all(
PositionComponent::class.java,
BodyComponent::class.java,
ShieldComponent::class.java,
TransformComponent::class.java,
).get()) {


override fun processEntity(entity: Entity?, deltaTime: Float) {
notNull(
ComponentMapper.position[entity],
ComponentMapper.shield[entity],
ComponentMapper.body[entity],
ComponentMapper.transform[entity],
::render
)
}

fun decrease() {
//if the ufo is hit by a bullet, decrease the shield strength
// if..
// shield--;
private fun render(positionComponent: PositionComponent, shieldComponent: ShieldComponent, bodyComponent: BodyComponent, transformComponent: TransformComponent) {


//println("yeah "+positionComponent.x)
val padding = (Configuration.gameHeight - 4*100) / 2
val buttonHeight = 100

//println(positionComponent.y)
if (shieldComponent.destroyed || shieldComponent.position < 0) {
transformComponent.opacity = 0f
} else {
transformComponent.opacity = 1f
}

//println(positionComponent.y.toString() + " "+shieldComponent.position)
}
}
1 change: 1 addition & 0 deletions core/src/group16/project/game/ecs/utils/ComponentMapper.kt
Expand Up @@ -13,4 +13,5 @@ object ComponentMapper {
val trajectory = ComponentMapper.getFor(TrajectoryComponent::class.java)
val transform = ComponentMapper.getFor(TransformComponent::class.java)
val health = ComponentMapper.getFor((HealthComponent::class.java))
val shield = ComponentMapper.getFor((ShieldComponent::class.java))
}
24 changes: 24 additions & 0 deletions core/src/group16/project/game/ecs/utils/EntityFactory.kt
Expand Up @@ -96,6 +96,30 @@ class EntityFactory {
opacity = if (isPlayer) 1f else 0f
})
}
fun createShield(engine: Engine, posx: Float, posy: Float, isPlayer: Boolean) =
engine.createEntity().also { entity ->
entity.add(engine.createComponent(PositionComponent::class.java).apply {
z = 1f
x = posx
y = posy
})
entity.add(engine.createComponent(BodyComponent::class.java).apply {
rectangle.setWidth(64f)
rectangle.setHeight(120f)
rectangle.setPosition(posx, posy)
})
entity.add(engine.createComponent(TextureComponent::class.java).apply {
texture = Texture("Shield.png")
})
entity.add(engine.createComponent(ShieldComponent::class.java).apply {
player = isPlayer
})
entity.add(engine.createComponent(TransformComponent::class.java).apply {
rotation = 0f
flipped = !isPlayer
opacity = 1f
})
}

fun createTrajectory(
engine: Engine,
Expand Down
4 changes: 3 additions & 1 deletion core/src/group16/project/game/models/FirebaseInterface.kt
Expand Up @@ -15,12 +15,13 @@ interface FirebaseInterface {
fun joinLobby(lobbyCode: String, screen: JoinLobbyScreen)

fun heartListener(player: String, screen: GameScreen)
//fun shieldListener(player: String, screen: GameScreen)

fun getHighScoreListener(screen: HighScoreScreen)

fun updateCurrentGameState(state: GameState)

fun setPlayersChoice(position: Int, targetPostion: Int, gameScreen: GameScreen)
fun setPlayersChoice(position: Int, targetPostion: Int, shieldPosition: Int, gameScreen: GameScreen)

fun checkIfOpponentReady(screen: GameScreen)
fun fire(gameScreen: GameScreen)
Expand All @@ -35,4 +36,5 @@ interface FirebaseInterface {


//TODO: more functions should be added based on what we need for our project.
fun removeShield()
}
13 changes: 13 additions & 0 deletions core/src/group16/project/game/models/Game.kt
Expand Up @@ -28,13 +28,22 @@ class Game(private val screenRect: Rectangle, private val camera: OrthographicCa
lateinit var ship1: Entity
lateinit var ship2: Entity

lateinit var shield1: Entity
lateinit var shield2: Entity

fun init() {
state = GameState.START
// Ship
ship1 = EntityFactory.createUfo(engine, -10f, 0f, true)
ship2 = EntityFactory.createUfo(engine, Configuration.gameWidth - 170f, 0f, false)
engine.addEntity(ship1)
engine.addEntity(ship2)
// Shields
shield1 = EntityFactory.createShield(engine, 190f, 0f, true)
shield2 = EntityFactory.createShield(engine, Configuration.gameWidth - 370f, 0f, false)
Shirajuki marked this conversation as resolved.
Show resolved Hide resolved

engine.addEntity(shield1)
engine.addEntity(shield2)
// Target
engine.addEntity(EntityFactory.createTarget(engine, Configuration.gameWidth - 160f, 0f, true))
engine.addEntity(EntityFactory.createTarget(engine, 10f, 0f, false))
Expand All @@ -51,6 +60,7 @@ class Game(private val screenRect: Rectangle, private val camera: OrthographicCa
gameScreen.fbic.setPlayersChoice(
InputHandler.playerPosition,
InputHandler.playerTrajectoryPosition,
InputHandler.playerShieldPosition,
gameScreen
)
}
Expand All @@ -59,6 +69,9 @@ class Game(private val screenRect: Rectangle, private val camera: OrthographicCa
fun deleteLobby() {
gameScreen.fbic.updateCurrentGameState(GameState.LOBBY_DELETED)
gameScreen.fbic.deleteLobby()
gameScreen.usedShield = false
InputHandler.playerShieldPosition = -1
InputHandler.enemyShieldPosition = -1
gameScreen.gameController.updateCurrentGame("null", "null", "null")
}

Expand Down