From 810be74b0ab2fa7e19e2b0aede7d3bf6c120fa2a Mon Sep 17 00:00:00 2001 From: nizienko Date: Fri, 9 Aug 2024 00:15:55 +0200 Subject: [PATCH] fixing concurrency issues #5, #6 --- .../nizienko/spaceinvaders/GameDisplay.kt | 25 ++++++----------- ...gisterInvaderShowingInStatusBarActivity.kt | 28 +++++++++---------- .../nizienko/spaceinvaders/objects/Invader.kt | 4 +-- 3 files changed, 24 insertions(+), 33 deletions(-) diff --git a/src/main/kotlin/com/github/nizienko/spaceinvaders/GameDisplay.kt b/src/main/kotlin/com/github/nizienko/spaceinvaders/GameDisplay.kt index e9df2ab..50f4651 100644 --- a/src/main/kotlin/com/github/nizienko/spaceinvaders/GameDisplay.kt +++ b/src/main/kotlin/com/github/nizienko/spaceinvaders/GameDisplay.kt @@ -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 @@ -22,15 +21,15 @@ class GameDisplay(val gameWidth: Int, val gameHeight: Int) : JPanel() { } // this is fast but error can occur - private val gameObjects = mutableSetOf() - private val viewObjects = mutableSetOf() + private val gameObjects = ConcurrentHashMap.newKeySet()//mutableSetOf() + private val viewObjects = ConcurrentHashMap.newKeySet() 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 { @@ -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 @@ -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 @@ -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() } diff --git a/src/main/kotlin/com/github/nizienko/spaceinvaders/RegisterInvaderShowingInStatusBarActivity.kt b/src/main/kotlin/com/github/nizienko/spaceinvaders/RegisterInvaderShowingInStatusBarActivity.kt index 39d6dc3..4522dfe 100644 --- a/src/main/kotlin/com/github/nizienko/spaceinvaders/RegisterInvaderShowingInStatusBarActivity.kt +++ b/src/main/kotlin/com/github/nizienko/spaceinvaders/RegisterInvaderShowingInStatusBarActivity.kt @@ -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 @@ -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 = { @@ -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 ) + ) } } } diff --git a/src/main/kotlin/com/github/nizienko/spaceinvaders/objects/Invader.kt b/src/main/kotlin/com/github/nizienko/spaceinvaders/objects/Invader.kt index 6fb04ce..72fa1fb 100644 --- a/src/main/kotlin/com/github/nizienko/spaceinvaders/objects/Invader.kt +++ b/src/main/kotlin/com/github/nizienko/spaceinvaders/objects/Invader.kt @@ -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