Skip to content

Commit

Permalink
fixing concurrency issues #5, #6
Browse files Browse the repository at this point in the history
  • Loading branch information
nizienko committed Aug 8, 2024
1 parent 76f5422 commit 810be74
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 33 deletions.
25 changes: 8 additions & 17 deletions src/main/kotlin/com/github/nizienko/spaceinvaders/GameDisplay.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package com.github.nizienko.spaceinvaders

import com.github.nizienko.spaceinvaders.objects.GameObject
import java.awt.Color
import com.jetbrains.rd.util.ConcurrentHashMap
import java.awt.Graphics
import java.awt.Graphics2D
import java.awt.Point
import java.awt.event.MouseAdapter
import java.awt.event.MouseEvent
import java.awt.event.MouseListener
import javax.swing.JPanel
import kotlin.math.roundToInt

Expand All @@ -22,15 +21,15 @@ class GameDisplay(val gameWidth: Int, val gameHeight: Int) : JPanel() {
}

// this is fast but error can occur
private val gameObjects = mutableSetOf<GameObject>()
private val viewObjects = mutableSetOf<GameObject>()
private val gameObjects = ConcurrentHashMap.newKeySet<GameObject>()//mutableSetOf<GameObject>()
private val viewObjects = ConcurrentHashMap.newKeySet<GameObject>()

private val xMultiplier: Double
get() = (width.toDouble() / gameWidth.toDouble())
private val yMultiplier: Double
get() = (height.toDouble() / gameHeight.toDouble())

fun addObject(gameObject: GameObject, zoomable: Boolean = true) {
fun addObject(gameObject: GameObject, zoomable: Boolean = true) {
if (zoomable) {
gameObjects.add(gameObject)
} else {
Expand All @@ -44,11 +43,12 @@ class GameDisplay(val gameWidth: Int, val gameHeight: Int) : JPanel() {
}

var defaultColors = Colors(1)
override fun paint(g: Graphics?) {

override fun paintComponent(g: Graphics?) {
if (g != null && g is Graphics2D) {
g.background = defaultColors.background
g.clearRect(0, 0, width, height)
gameObjects.toList().forEach {
gameObjects.forEach {
val realX = (it.position.x - camera.position.x).toDouble() * xMultiplier * camera.zoom
val realY = (it.position.y - camera.position.y).toDouble() * yMultiplier * camera.zoom
val realWidth = it.width.toDouble() * xMultiplier * camera.zoom
Expand All @@ -63,7 +63,7 @@ class GameDisplay(val gameWidth: Int, val gameHeight: Int) : JPanel() {
realHeight.roundToInt()
)
}
viewObjects.toList().forEach {
viewObjects.forEach {
val realX = (it.position.x).toDouble() * xMultiplier
val realY = (it.position.y).toDouble() * yMultiplier
val realWidth = it.width.toDouble() * xMultiplier
Expand All @@ -81,15 +81,6 @@ class GameDisplay(val gameWidth: Int, val gameHeight: Int) : JPanel() {
}
}

private fun Color.isColorBright(): Boolean {
// Calculate the luminance value
val luminance = (0.299 * red + 0.587 * green + 0.114 * blue) / 255

// Check if the luminance value is above a threshold
val brightnessThreshold = 0.8 // Adjust the threshold as desired
return luminance > brightnessThreshold
}

fun clean() {
gameObjects.clear()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import com.intellij.openapi.project.Project
import com.intellij.openapi.startup.StartupActivity
import com.intellij.openapi.wm.IconLikeCustomStatusBarWidget
import com.intellij.openapi.wm.StatusBar
import com.intellij.openapi.wm.StatusBarWidgetFactory
import com.intellij.openapi.wm.WindowManager
import com.intellij.task.ProjectTaskContext
import com.intellij.task.ProjectTaskListener
Expand All @@ -29,9 +28,10 @@ internal class RegisterInvaderShowingInStatusBarActivity : StartupActivity, Dumb

val show: () -> Unit = {
statusBar.addWidget(
widget,
StatusBar.Anchors.before(StatusBar.StandardWidgets.POSITION_PANEL),
project)
widget,
StatusBar.Anchors.before(StatusBar.StandardWidgets.POSITION_PANEL),
project
)
}

val hide: () -> Unit = {
Expand Down Expand Up @@ -131,18 +131,18 @@ internal class SpaceInvadersWidget : IconLikeCustomStatusBarWidget {

override fun getComponent(): JComponent {
return IconLabelButton(MyIcons.Monster) {
DataManager.getInstance().getDataContextFromFocusAsync().onSuccess { dataContext ->
DataManager.getInstance().dataContextFromFocusAsync.onSuccess { dataContext ->
ActionManager.getInstance().getAction("com.github.nizienko.spaceinvaders.OpenGameAction")
.actionPerformed(
AnActionEvent(
null,
dataContext,
ActionPlaces.UNKNOWN,
Presentation(),
ActionManager.getInstance(),
0
)
.actionPerformed(
AnActionEvent(
null,
dataContext,
ActionPlaces.UNKNOWN,
Presentation(),
ActionManager.getInstance(),
0
)
)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.github.nizienko.spaceinvaders.objects

import com.github.nizienko.spaceinvaders.CanExpired

import java.awt.Point
import java.util.concurrent.ThreadLocalRandom
import kotlin.random.Random


class Invader(private val shiftX: Int, private val shiftY: Int, private val n: Int) : GameObject() {
private var x: Int = 10 + shiftX
Expand Down

0 comments on commit 810be74

Please sign in to comment.