diff --git a/src/main/scala/fr/ynerant/leveleditor/api/editor/Case.scala b/src/main/scala/fr/ynerant/leveleditor/api/editor/Case.scala index 79bbd9f..f084bd7 100644 --- a/src/main/scala/fr/ynerant/leveleditor/api/editor/Case.scala +++ b/src/main/scala/fr/ynerant/leveleditor/api/editor/Case.scala @@ -1,11 +1,10 @@ package fr.ynerant.leveleditor.api.editor -import fr.ynerant.leveleditor.api.editor.Collision.Collision import fr.ynerant.leveleditor.api.editor.sprites.Sprite object Case { - def create(posX: Int, posY: Int, couche1: Sprite, couche2: Sprite, couche3: Sprite, collision: Collision): Case = { + def create(posX: Int, posY: Int, couche1: Sprite, couche2: Sprite, couche3: Sprite, collision: String): Case = { val c = new Case c.x = posX c.y = posY @@ -23,7 +22,7 @@ class Case { private var couche1 = null: Sprite private var couche2 = null: Sprite private var couche3 = null: Sprite - private var collision = null: Collision + private var collision = null: String def getPosX: Int = x @@ -35,7 +34,7 @@ class Case { def getCoucheThree: Sprite = couche3 - def getCollision: Collision = collision + def getCollision: String = collision override def toString: String = "{Case x=" + x + " y=" + y + " couche1=" + couche1 + " couche2=" + couche2 + " couche3=" + couche3 + " collision=" + collision.toString.toUpperCase + "}\n" } diff --git a/src/main/scala/fr/ynerant/leveleditor/api/editor/Collision.scala b/src/main/scala/fr/ynerant/leveleditor/api/editor/Collision.scala index cacf513..5de47e7 100644 --- a/src/main/scala/fr/ynerant/leveleditor/api/editor/Collision.scala +++ b/src/main/scala/fr/ynerant/leveleditor/api/editor/Collision.scala @@ -1,6 +1,9 @@ package fr.ynerant.leveleditor.api.editor -object Collision extends Enumeration { - type Collision = Value - val FULL, PARTIAL, ANY = Value +object Collision { + val FULL: String = "FULL" + val PARTIAL: String = "PARTIAL" + val ANY: String = "ANY" + + val values: IndexedSeq[String] = IndexedSeq(FULL, PARTIAL, ANY) } diff --git a/src/main/scala/fr/ynerant/leveleditor/api/editor/EditorAPI.scala b/src/main/scala/fr/ynerant/leveleditor/api/editor/EditorAPI.scala index baa02e4..9f12661 100644 --- a/src/main/scala/fr/ynerant/leveleditor/api/editor/EditorAPI.scala +++ b/src/main/scala/fr/ynerant/leveleditor/api/editor/EditorAPI.scala @@ -3,22 +3,19 @@ package fr.ynerant.leveleditor.api.editor import java.awt.Color import java.awt.image.BufferedImage import java.io._ -import java.lang.reflect.Type import java.nio.charset.StandardCharsets -import java.util import java.util.zip.{GZIPInputStream, GZIPOutputStream} -import com.google.gson.{Gson, GsonBuilder, JsonDeserializationContext, JsonDeserializer, JsonElement, JsonPrimitive, JsonSerializationContext, JsonSerializer} -import fr.ynerant.leveleditor.api.editor.Collision.Collision -import fr.ynerant.leveleditor.editor.Map +import fr.ynerant.leveleditor.editor.GMap import javax.swing.JFileChooser import javax.swing.filechooser.FileNameExtensionFilter +import net.liftweb.json._ object EditorAPI { private var LAST_FILE = null: File def toRawMap(width: Int, height: Int): RawMap = { - val cases = new util.ArrayList[RawCase] + var cases = Nil: List[RawCase] var y = 1 while ( { y < height @@ -28,7 +25,7 @@ object EditorAPI { x < width }) { val c = RawCase.create(x / 16, y / 16, RawSprite.BLANK, RawSprite.BLANK, RawSprite.BLANK, Collision.ANY) - cases.add(c) + cases ::= c x += 16 } @@ -38,20 +35,6 @@ object EditorAPI { RawMap.create(cases, width, height) } - def createGson: Gson = { - val builder = new GsonBuilder - builder.registerTypeAdapter(classOf[Collision.Collision], new JsonDeserializer[Collision.Collision] { - override def deserialize(jsonElement: JsonElement, `type`: Type, jsonDeserializationContext: JsonDeserializationContext): Collision = Collision.withName(jsonElement.getAsString) - }) - builder.registerTypeAdapter(classOf[Collision.Collision], new JsonSerializer[Collision.Collision] { - override def serialize(t: Collision, `type`: Type, jsonSerializationContext: JsonSerializationContext): JsonElement = new JsonPrimitive(t.toString) - }) - builder.enableComplexMapKeySerialization - builder.serializeNulls - builder.setPrettyPrinting() - builder.create - } - def createJFC: JFileChooser = { val jfc = new JFileChooser jfc.setFileFilter(new FileNameExtensionFilter("Fichiers monde (*.gmap, *.dat)", "gmap", "dat")) @@ -80,7 +63,8 @@ object EditorAPI { } def save(file: File, map: RawMap): Unit = { - val json = createGson.toJson(map) + implicit val formats: DefaultFormats.type = DefaultFormats + val json = Serialization.writePretty(map) try { assert(file.createNewFile) val bos = new BufferedOutputStream(new GZIPOutputStream(new FileOutputStream(file))) @@ -92,7 +76,7 @@ object EditorAPI { } } - def open: Map = { + def open: GMap = { val jfc = createJFC var file = null: File jfc.showOpenDialog(null) @@ -119,12 +103,13 @@ object EditorAPI { case e: IOException => e.printStackTrace() } - createGson.fromJson(json, classOf[RawMap]) + implicit val formats: DefaultFormats.type = DefaultFormats + parse(json).extract[RawMap] } - def open(f: File): Map = open(getRawMap(f)) + def open(f: File): GMap = open(getRawMap(f)) - def open(map: RawMap): Map = { + def open(map: RawMap): GMap = { if (map.getFont == null) { val baseWidth = map.getWidth val baseHeight = map.getHeight @@ -155,6 +140,6 @@ object EditorAPI { } map.setFont(image) } - new Map(map) + new GMap(map) } } diff --git a/src/main/scala/fr/ynerant/leveleditor/api/editor/RawCase.scala b/src/main/scala/fr/ynerant/leveleditor/api/editor/RawCase.scala index 79706b5..548fce6 100644 --- a/src/main/scala/fr/ynerant/leveleditor/api/editor/RawCase.scala +++ b/src/main/scala/fr/ynerant/leveleditor/api/editor/RawCase.scala @@ -1,9 +1,7 @@ package fr.ynerant.leveleditor.api.editor -import fr.ynerant.leveleditor.api.editor.Collision.Collision - object RawCase { - def create(posX: Int, posY: Int, couche1: RawSprite, couche2: RawSprite, couche3: RawSprite, collision: Collision): RawCase = { + def create(posX: Int, posY: Int, couche1: RawSprite, couche2: RawSprite, couche3: RawSprite, collision: String): RawCase = { new RawCase(posX, posY, couche1, couche2, couche3, collision) } @@ -12,7 +10,7 @@ object RawCase { } } -case class RawCase(var x: Int, var y: Int, var couche1: RawSprite, var couche2: RawSprite, var couche3: RawSprite, var collision: Collision) { +case class RawCase(var x: Int, var y: Int, var couche1: RawSprite, var couche2: RawSprite, var couche3: RawSprite, var collision: String) { def getPosX: Int = x def getPosY: Int = y @@ -23,9 +21,9 @@ case class RawCase(var x: Int, var y: Int, var couche1: RawSprite, var couche2: def getCoucheThree: RawSprite = couche3 - def getCollision: Collision = collision + def getCollision: String = collision - def setCollision(collision: Collision): Unit = { + def setCollision(collision: String): Unit = { this.collision = collision } } diff --git a/src/main/scala/fr/ynerant/leveleditor/api/editor/RawMap.scala b/src/main/scala/fr/ynerant/leveleditor/api/editor/RawMap.scala index 2ebead2..6264c28 100644 --- a/src/main/scala/fr/ynerant/leveleditor/api/editor/RawMap.scala +++ b/src/main/scala/fr/ynerant/leveleditor/api/editor/RawMap.scala @@ -1,50 +1,45 @@ package fr.ynerant.leveleditor.api.editor -import fr.ynerant.leveleditor.editor.Map import java.awt.image.BufferedImage -import java.util -import java.util.stream.Collectors -import com.google.gson.Gson +import fr.ynerant.leveleditor.editor.GMap +import net.liftweb.json._ object RawMap { - def create(cases: util.List[RawCase], width: Int, height: Int): RawMap = { + def create(cases: List[RawCase], width: Int, height: Int): RawMap = { new RawMap(cases, width, height) } - def create(map: Map): RawMap = { - val raw = new RawMap(new util.ArrayList[RawCase], 0, 0) - raw.width = map.getWidth - raw.height = map.getHeight - raw.cases = new util.ArrayList[RawCase] - map.getAllCases.forEach(c => raw.cases.add(RawCase.create(c))) + def create(map: GMap): RawMap = { + val raw = new RawMap(Nil, map.getWidth, map.getHeight) + map.getAllCases.foreach(c => raw.cases ::= RawCase.create(c)) raw } } -case class RawMap(var cases: util.List[RawCase], var width: Int, var height: Int) { - private var cases_map = null: util.HashMap[Integer, RawCase] +case class RawMap(var cases: List[RawCase], var width: Int, var height: Int) { + private var cases_map = null: Map[Integer, RawCase] private var font = null: BufferedImage - def getCases: util.List[RawCase] = cases + def getNeighbours(c: RawCase): Iterable[RawCase] = { + var list = Nil: List[RawCase] + list ::= getCase(c.getPosX, c.getPosY + 1) + list ::= getCase(c.getPosX + 1, c.getPosY) + list ::= getCase(c.getPosX, c.getPosY - 1) + list ::= getCase(c.getPosX - 1, c.getPosY) + list.filter((_c: RawCase) => _c != null && _c.getCollision.equals(Collision.ANY)) + } def getCase(x: Int, y: Int): RawCase = { if (cases_map == null) { - cases_map = new util.HashMap[Integer, RawCase] - getCases.forEach(c => cases_map.put(c.getPosY * width + c.getPosX, c)) + cases_map = Map() + getCases.foreach(c => cases_map = cases_map.updated(c.getPosY * width + c.getPosX, c)) } - cases_map.get(y * getWidth + x) + cases_map.getOrElse(y * getWidth + x, null) } - def getNeighbours(c: RawCase): util.Collection[RawCase] = { - val list = new util.ArrayList[RawCase] - list.add(getCase(c.getPosX - 1, c.getPosY)) - list.add(getCase(c.getPosX, c.getPosY - 1)) - list.add(getCase(c.getPosX + 1, c.getPosY)) - list.add(getCase(c.getPosX, c.getPosY + 1)) - list.stream.filter((_c: RawCase) => _c != null && (_c.getCollision == Collision.ANY)).collect(Collectors.toList()) - } + def getCases: List[RawCase] = cases def getWidth: Int = width @@ -56,5 +51,8 @@ case class RawMap(var cases: util.List[RawCase], var width: Int, var height: Int this.font = font } - override def toString: String = new Gson().toJson(this) + override def toString: String = { + implicit val formats: DefaultFormats.type = DefaultFormats + Serialization.writePretty(this) + } } diff --git a/src/main/scala/fr/ynerant/leveleditor/api/editor/sprites/Category.scala b/src/main/scala/fr/ynerant/leveleditor/api/editor/sprites/Category.scala index c691993..8b32892 100644 --- a/src/main/scala/fr/ynerant/leveleditor/api/editor/sprites/Category.scala +++ b/src/main/scala/fr/ynerant/leveleditor/api/editor/sprites/Category.scala @@ -1,9 +1,9 @@ package fr.ynerant.leveleditor.api.editor.sprites -import java.util +import scala.collection.mutable.ListBuffer object Category { - def create(name: String, sprites: util.List[Sprite]): Category = { + def create(name: String, sprites: ListBuffer[Sprite]): Category = { val c = new Category c.name = name c.sprites = sprites @@ -12,12 +12,16 @@ object Category { } class Category private() { - private var sprites = null: util.List[Sprite] + private var sprites = null: ListBuffer[Sprite] private var name = null: String def getName: String = name - def getSprites: util.List[Sprite] = sprites + def getSprites: ListBuffer[Sprite] = sprites + + def addSprite(s: Sprite): Unit = { + this.sprites += s + } override def toString: String = name } diff --git a/src/main/scala/fr/ynerant/leveleditor/api/editor/sprites/Sprite.scala b/src/main/scala/fr/ynerant/leveleditor/api/editor/sprites/Sprite.scala index 8909732..07ad085 100644 --- a/src/main/scala/fr/ynerant/leveleditor/api/editor/sprites/Sprite.scala +++ b/src/main/scala/fr/ynerant/leveleditor/api/editor/sprites/Sprite.scala @@ -2,15 +2,13 @@ package fr.ynerant.leveleditor.api.editor.sprites import java.awt._ import java.awt.image.BufferedImage -import java.util -import java.util.ArrayList +import scala.collection.mutable.ListBuffer object Sprite { - val BLANK = new Sprite(new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB), Category.create("blank", new util.ArrayList[Sprite]), 0) + val BLANK = new Sprite(new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB), Category.create("blank", ListBuffer()), 0) - val g: Graphics2D - = BLANK.getImage.createGraphics + val g: Graphics2D = BLANK.getImage.createGraphics g.setComposite(AlphaComposite.Clear) g.setColor(new Color(0, true)) g.fillRect(0, 0, 16, 16) @@ -18,7 +16,7 @@ object Sprite { } class Sprite(val img: BufferedImage, val cat: Category, val index: Int) { - if (!this.cat.getSprites.contains(this)) this.cat.getSprites.add(this) + if (!this.cat.getSprites.contains(this)) this.cat.addSprite(this) def getImage: BufferedImage = this.img diff --git a/src/main/scala/fr/ynerant/leveleditor/api/editor/sprites/SpriteRegister.scala b/src/main/scala/fr/ynerant/leveleditor/api/editor/sprites/SpriteRegister.scala index 2c1559d..34f83d2 100644 --- a/src/main/scala/fr/ynerant/leveleditor/api/editor/sprites/SpriteRegister.scala +++ b/src/main/scala/fr/ynerant/leveleditor/api/editor/sprites/SpriteRegister.scala @@ -1,19 +1,20 @@ package fr.ynerant.leveleditor.api.editor.sprites -import java.io.{BufferedInputStream, BufferedReader, File, FileInputStream, IOException, InputStreamReader} +import java.io.{BufferedInputStream, File, FileInputStream, IOException} import java.net.{URISyntaxException, URLDecoder} import java.nio.file.{Files, Paths} -import java.util import java.util.Objects import java.util.jar.JarFile -import com.google.gson.Gson -import javax.imageio.ImageIO import fr.ynerant.leveleditor.client.main.Main +import javax.imageio.ImageIO +import net.liftweb.json._ + +import scala.collection.mutable.ListBuffer object SpriteRegister { - private var nameToCoords = null: util.Map[String, util.List[util.List[Double]]] - private val sprites = new util.HashMap[String, Category] + private var nameToCoords = Map(): Map[String, List[List[Int]]] + private var sprites = Map(): Map[String, Category] @throws[IOException] def unpack(): Unit = { @@ -63,32 +64,32 @@ object SpriteRegister { } @SuppressWarnings(Array("unchecked")) def refreshAllSprites(): Unit = { - if (nameToCoords != null && !nameToCoords.isEmpty && !sprites.isEmpty) return + if (nameToCoords != null && nameToCoords.nonEmpty && sprites.nonEmpty) return val assetsDir = new File("assets") - val assets = new util.ArrayList[String] + var assets = Nil: List[String] for (dir <- Objects.requireNonNull(assetsDir.listFiles)) { - assets.add(dir.getName) + assets ::= dir.getName } - assets.forEach(asset => { + assets.foreach(asset => { try { val f = new File(assetsDir.getAbsolutePath + "/" + asset + "/textures/sprites") assert(f.isDirectory || f.mkdirs) - val br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(f, "sprites.json")))) - nameToCoords = new Gson().fromJson(br, classOf[util.Map[_, _]]) - br.close() - nameToCoords.keySet.forEach(key => { + val json = Files.readString(new File(f, "sprites.json").toPath) + implicit val formats: DefaultFormats.type = DefaultFormats + nameToCoords = parse(json).extract[Map[String, List[List[Int]]]] + nameToCoords.keySet.foreach(key => { try { val is = new BufferedInputStream(new FileInputStream(new File(f, key + ".png"))) val img = ImageIO.read(is) - val cat = Category.create(key, new util.ArrayList[Sprite]) - nameToCoords.get(key).forEach(list => { - val x = list.get(0).intValue - val y = list.get(1).intValue + val cat = Category.create(key, ListBuffer()) + nameToCoords(key).foreach(list => { + val x = list.head.intValue + val y = list(1).intValue val child = img.getSubimage(x, y, 16, 16) - new Sprite(child, cat, nameToCoords.get(key).indexOf(list)) + new Sprite(child, cat, nameToCoords(key).toIndexedSeq.indexOf(list)) }) - sprites.put(key, cat) + sprites += (key -> cat) } catch { case t: Throwable => System.err.println("Erreur lors de la lecture du sprite '" + key + "'") @@ -102,7 +103,7 @@ object SpriteRegister { }) } - def getCategory(name: String): Category = sprites.get(name) + def getCategory(name: String): Category = sprites(name) - def getAllCategories = new util.ArrayList[Category](sprites.values) + def getAllCategories: List[Category] = sprites.values.toList } diff --git a/src/main/scala/fr/ynerant/leveleditor/client/main/Main.scala b/src/main/scala/fr/ynerant/leveleditor/client/main/Main.scala index 8fcc208..e1a0df0 100644 --- a/src/main/scala/fr/ynerant/leveleditor/client/main/Main.scala +++ b/src/main/scala/fr/ynerant/leveleditor/client/main/Main.scala @@ -1,19 +1,16 @@ package fr.ynerant.leveleditor.client.main +import java.awt._ +import java.awt.image.BufferedImage +import java.io.{File, IOException} +import java.net.{URISyntaxException, URL} +import java.util.Locale + import fr.ynerant.leveleditor.api.editor.EditorAPI import fr.ynerant.leveleditor.api.editor.sprites.SpriteRegister import fr.ynerant.leveleditor.frame.MainFrame import fr.ynerant.leveleditor.game.GameFrame import javax.swing._ -import java.awt._ -import java.awt.image.BufferedImage -import java.io.File -import java.io.IOException -import java.net.URISyntaxException -import java.net.URL -import java.util.Locale - -import fr.ynerant.leveleditor.client.main.Main.DEV /** diff --git a/src/main/scala/fr/ynerant/leveleditor/editor/CollidPanel.scala b/src/main/scala/fr/ynerant/leveleditor/editor/CollidPanel.scala index bead2dd..d0e58f3 100644 --- a/src/main/scala/fr/ynerant/leveleditor/editor/CollidPanel.scala +++ b/src/main/scala/fr/ynerant/leveleditor/editor/CollidPanel.scala @@ -1,9 +1,10 @@ package fr.ynerant.leveleditor.editor +import java.awt._ +import java.awt.image.BufferedImage + import fr.ynerant.leveleditor.api.editor.Collision import javax.swing._ -import java.awt._ -import java.awt.image.BufferedImage @SerialVersionUID(-138754019431984881L) @@ -21,8 +22,6 @@ object CollidPanel { @SerialVersionUID(-138754019431984881L) class CollidPanel(val frame: EditorFrame) extends JPanel { - def getMap: Map = frame.getMap - override def paintComponent(g: Graphics): Unit = { g.fillRect(0, 0, getWidth, getHeight) val img = getMap.getFont @@ -31,7 +30,7 @@ class CollidPanel(val frame: EditorFrame) extends JPanel { val width = img.getWidth * 2 val height = img.getHeight * 2 g.drawImage(getMap.getFont, x, y, width, height, null) - getMap.getAllCases.forEach(c => { + getMap.getAllCases.foreach(c => { if (!CollidPanel.isEmpty(c.getCoucheOne.getImage)) { g.drawImage(c.getCoucheOne.getImage, x + c.getPosX * 34 + 2, y + c.getPosY * 34 + 2, 32, 32, null) if (!CollidPanel.isEmpty(c.getCoucheTwo.getImage)) { @@ -42,8 +41,8 @@ class CollidPanel(val frame: EditorFrame) extends JPanel { } }) - getMap.getAllCases.forEach(c => { - if (c.getCollision ne Collision.ANY) { + getMap.getAllCases.foreach(c => { + if (c.getCollision.equals(Collision.ANY)) { val alpha = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB) if (c.getCollision eq Collision.FULL) { val grap = alpha.createGraphics @@ -61,4 +60,6 @@ class CollidPanel(val frame: EditorFrame) extends JPanel { } }) } + + def getMap: GMap = frame.getMap } diff --git a/src/main/scala/fr/ynerant/leveleditor/editor/EditorFrame.scala b/src/main/scala/fr/ynerant/leveleditor/editor/EditorFrame.scala index bbbe6f8..a7fa70d 100644 --- a/src/main/scala/fr/ynerant/leveleditor/editor/EditorFrame.scala +++ b/src/main/scala/fr/ynerant/leveleditor/editor/EditorFrame.scala @@ -1,19 +1,18 @@ package fr.ynerant.leveleditor.editor -import fr.ynerant.leveleditor.api.editor.sprites.SpriteRegister -import fr.ynerant.leveleditor.frame.listeners._ -import javax.swing._ -import javax.swing.event.ChangeEvent -import javax.swing.event.ChangeListener import java.awt._ import java.awt.event._ import java.io.File +import fr.ynerant.leveleditor.api.editor.sprites.SpriteRegister import fr.ynerant.leveleditor.api.editor.{EditorAPI, RawMap} +import fr.ynerant.leveleditor.frame.listeners._ +import javax.swing._ +import javax.swing.event.{ChangeEvent, ChangeListener} @SerialVersionUID(-2705122356101556462L) -class EditorFrame(val map: Map) extends JFrame("Level Editor") with ChangeListener with ActionListener with WindowListener { +class EditorFrame(val map: GMap) extends JFrame("Level Editor") with ChangeListener with ActionListener with WindowListener { final private val save = new JMenuItem("Sauvegarder") final private val saveAs = new JMenuItem("Sauvegarder sous ...") final private val exit = new JMenuItem("Quitter") @@ -126,8 +125,8 @@ class EditorFrame(val map: Map) extends JFrame("Level Editor") with ChangeListen couche2.repaint() couche3.repaint() } - SpriteRegister.getAllCategories.forEach(cat => { - cat.getSprites.forEach(spr => { + SpriteRegister.getAllCategories.foreach(cat => { + cat.getSprites.foreach(spr => { val sprc1 = new SpriteComp(spr, 0) val sprc2 = new SpriteComp(spr, 1) val sprc3 = new SpriteComp(spr, 2) @@ -170,7 +169,7 @@ class EditorFrame(val map: Map) extends JFrame("Level Editor") with ChangeListen resources.getSelectedComponent.asInstanceOf[JScrollPane].getVerticalScrollBar.setValue(cursorPos) } - def getMap: Map = map + def getMap: GMap = map def getSelectedSprite: SpriteComp = selectedSprite diff --git a/src/main/scala/fr/ynerant/leveleditor/editor/GMap.scala b/src/main/scala/fr/ynerant/leveleditor/editor/GMap.scala new file mode 100644 index 0000000..2018ff1 --- /dev/null +++ b/src/main/scala/fr/ynerant/leveleditor/editor/GMap.scala @@ -0,0 +1,61 @@ +package fr.ynerant.leveleditor.editor + +import java.awt.image.BufferedImage + +import fr.ynerant.leveleditor.api.editor.sprites.SpriteRegister +import fr.ynerant.leveleditor.api.editor.{Case, RawMap} + + +object GMap { + private var cases = Nil: List[Case] +} + +class GMap(val raw: RawMap) { + final private var frame = null: EditorFrame + final private var width = 0 + final private var height = 0 + private var casesMap = Map(): Map[Int, Map[Int, Case]] + final private var font = null: BufferedImage + + GMap.cases = Nil + this.width = raw.getWidth + this.height = raw.getHeight + this.font = raw.getFont + + raw.getCases.foreach(rc => + GMap.cases = Case.create(rc.getPosX, rc.getPosY, SpriteRegister.getCategory(rc.getCoucheOne.getCategory).getSprites(rc.getCoucheOne.getIndex), SpriteRegister.getCategory(rc.getCoucheTwo.getCategory).getSprites(rc.getCoucheTwo.getIndex), SpriteRegister.getCategory(rc.getCoucheThree.getCategory).getSprites(rc.getCoucheThree.getIndex), rc.getCollision) :: GMap.cases + ) + + reorganizeMap() + frame = new EditorFrame(this) + getFrame.setVisible(true) + + def getFrame: EditorFrame = frame + + def getWidth: Int = width + + def getHeight: Int = height + + def getCase(x: Int, y: Int): Case = casesMap.getOrElse(x, Map())(y) + + def getAllCases: List[Case] = { + val list = Nil: List[Case] + casesMap.values.foreach(l => list.appendedAll(l.values)) + list + } + + def getFont: BufferedImage = font + + private def reorganizeMap(): Unit = { + for (i <- 0 until width) { + casesMap += (i -> Map()) + } + GMap.cases.foreach(c => setCase(c.getPosX, c.getPosY, c)) + } + + def setCase(x: Int, y: Int, c: Case): Unit = { + var map = casesMap(x) + map = map + (y -> c) + casesMap = casesMap.updated(x, map) + } +} diff --git a/src/main/scala/fr/ynerant/leveleditor/editor/Map.scala b/src/main/scala/fr/ynerant/leveleditor/editor/Map.scala deleted file mode 100644 index 75bd2c7..0000000 --- a/src/main/scala/fr/ynerant/leveleditor/editor/Map.scala +++ /dev/null @@ -1,60 +0,0 @@ -package fr.ynerant.leveleditor.editor - -import fr.ynerant.leveleditor.api.editor.sprites.SpriteRegister -import java.awt.image.BufferedImage -import java.util - -import fr.ynerant.leveleditor.api.editor.{Case, RawMap} - - -object Map { - private var cases = null: util.ArrayList[Case] -} - -class Map(val raw: RawMap) { - final private var frame = null: EditorFrame - final private var width = 0 - final private var height = 0 - final private val casesMap = new util.HashMap[Integer, util.Map[Integer, Case]] - final private var font = null: BufferedImage - - Map.cases = new util.ArrayList[Case] - this.width = raw.getWidth - this.height = raw.getHeight - this.font = raw.getFont - - raw.getCases.forEach(rc => - Map.cases.add(Case.create(rc.getPosX, rc.getPosY, SpriteRegister.getCategory(rc.getCoucheOne.getCategory).getSprites.get(rc.getCoucheOne.getIndex), SpriteRegister.getCategory(rc.getCoucheTwo.getCategory).getSprites.get(rc.getCoucheTwo.getIndex), SpriteRegister.getCategory(rc.getCoucheThree.getCategory).getSprites.get(rc.getCoucheThree.getIndex), rc.getCollision)) - ) - - reorganizeMap() - frame = new EditorFrame(this) - getFrame.setVisible(true) - - def getFrame: EditorFrame = frame - - def getWidth: Int = width - - def getHeight: Int = height - - def getCase(x: Int, y: Int): Case = casesMap.getOrDefault(x, new util.HashMap[Integer, Case]).get(y) - - def setCase(x: Int, y: Int, c: Case): Unit = { - casesMap.get(x).put(y, c) - } - - def getFont: BufferedImage = font - - private def reorganizeMap(): Unit = { - for (i <- 0 until width) { - casesMap.put(i, new util.HashMap[Integer, Case]) - } - Map.cases.forEach(c => setCase(c.getPosX, c.getPosY, c)) - } - - def getAllCases: util.List[Case] = { - val list = new util.ArrayList[Case] - casesMap.values.forEach(l => list.addAll(l.values())) - list - } -} diff --git a/src/main/scala/fr/ynerant/leveleditor/editor/MapPanel.scala b/src/main/scala/fr/ynerant/leveleditor/editor/MapPanel.scala index 421b3b8..5146e49 100644 --- a/src/main/scala/fr/ynerant/leveleditor/editor/MapPanel.scala +++ b/src/main/scala/fr/ynerant/leveleditor/editor/MapPanel.scala @@ -1,14 +1,13 @@ package fr.ynerant.leveleditor.editor -import javax.swing._ import java.awt._ import java.awt.image.BufferedImage +import javax.swing._ + @SerialVersionUID(2629019576253690557L) class MapPanel(val frame: EditorFrame) extends JPanel { - def getMap: Map = frame.getMap - override def paintComponent(g: Graphics): Unit = { g.fillRect(0, 0, getWidth, getHeight) val img = getMap.getFont @@ -17,7 +16,7 @@ class MapPanel(val frame: EditorFrame) extends JPanel { val width = img.getWidth * 2 val height = img.getHeight * 2 g.drawImage(getMap.getFont, x, y, width, height, null) - getMap.getAllCases.forEach(c => { // BufferedImage image; + getMap.getAllCases.foreach(c => { // BufferedImage image; if (!isEmpty(c.getCoucheOne.getImage)) g.drawImage(c.getCoucheOne.getImage, x + c.getPosX * 34 + 2, y + c.getPosY * 34 + 2, 32, 32, null) /* if (frame.getSelectedLayerIndex() != 0) { @@ -37,6 +36,8 @@ class MapPanel(val frame: EditorFrame) extends JPanel { }) } + def getMap: GMap = frame.getMap + @SuppressWarnings(Array("BooleanMethodIsAlwaysInverted")) private def isEmpty(image: BufferedImage) = { var allrgba = 0 for (x <- 0 until image.getWidth) { diff --git a/src/main/scala/fr/ynerant/leveleditor/frame/listeners/CollidMapMouseListener.scala b/src/main/scala/fr/ynerant/leveleditor/frame/listeners/CollidMapMouseListener.scala index a6cb33c..84b2b5e 100644 --- a/src/main/scala/fr/ynerant/leveleditor/frame/listeners/CollidMapMouseListener.scala +++ b/src/main/scala/fr/ynerant/leveleditor/frame/listeners/CollidMapMouseListener.scala @@ -1,9 +1,8 @@ package fr.ynerant.leveleditor.frame.listeners -import fr.ynerant.leveleditor.api.editor.{Case, Collision} -import java.awt.event.MouseAdapter -import java.awt.event.MouseEvent +import java.awt.event.{MouseAdapter, MouseEvent} +import fr.ynerant.leveleditor.api.editor.Case import fr.ynerant.leveleditor.editor.{CollidPanel, EditorFrame} @@ -16,11 +15,7 @@ class CollidMapMouseListener(val panel: CollidPanel, val frame: EditorFrame) ext val y = panel.getHeight / 2 - map.getFont.getHeight val c = map.getCase((event.getX - x + 2) / 34, (event.getY - y + 2) / 34) if (c != null && event.getX - x >= 2 && event.getY - y >= 2) { - val colIndex = c.getCollision.id - var newColIndex = colIndex + 1 - if (newColIndex >= Collision.values.size) newColIndex = 0 - val col = Collision.apply(newColIndex) - val n = Case.create(c.getPosX, c.getPosY, c.getCoucheOne, c.getCoucheTwo, c.getCoucheThree, col) + val n = Case.create(c.getPosX, c.getPosY, c.getCoucheOne, c.getCoucheTwo, c.getCoucheThree, c.getCollision) map.setCase((event.getX - x + 2) / 34, (event.getY - y + 2) / 34, n) panel.repaint() } diff --git a/src/main/scala/fr/ynerant/leveleditor/frame/listeners/MapMouseListener.scala b/src/main/scala/fr/ynerant/leveleditor/frame/listeners/MapMouseListener.scala index 099078e..7692b5c 100644 --- a/src/main/scala/fr/ynerant/leveleditor/frame/listeners/MapMouseListener.scala +++ b/src/main/scala/fr/ynerant/leveleditor/frame/listeners/MapMouseListener.scala @@ -1,7 +1,6 @@ package fr.ynerant.leveleditor.frame.listeners -import java.awt.event.MouseAdapter -import java.awt.event.MouseEvent +import java.awt.event.{MouseAdapter, MouseEvent} import fr.ynerant.leveleditor.api.editor.Case import fr.ynerant.leveleditor.editor.{EditorFrame, MapPanel} @@ -37,7 +36,7 @@ class MapMouseListener(val panel: MapPanel, val frame: EditorFrame) extends Mous } } else if (frame.getSelectedPaintingMode == 1) { - getFrame.getMap.getAllCases.forEach(c => { + getFrame.getMap.getAllCases.foreach(c => { val map = getFrame.getMap if (getFrame.getSelectedSprite != null) { if (getFrame.getSelectedSprite.getCouche - 1 > getFrame.getSelectedLayerIndex) return diff --git a/src/main/scala/fr/ynerant/leveleditor/game/GameFrame.scala b/src/main/scala/fr/ynerant/leveleditor/game/GameFrame.scala index f244593..635b2aa 100644 --- a/src/main/scala/fr/ynerant/leveleditor/game/GameFrame.scala +++ b/src/main/scala/fr/ynerant/leveleditor/game/GameFrame.scala @@ -1,20 +1,17 @@ package fr.ynerant.leveleditor.game -import fr.ynerant.leveleditor.api.editor.{Collision, RawMap} -import fr.ynerant.leveleditor.api.editor.sprites.SpriteRegister -import fr.ynerant.leveleditor.game.mobs.Mob -import fr.ynerant.leveleditor.game.towers.AutoTower -import fr.ynerant.leveleditor.game.towers.BasicTower -import fr.ynerant.leveleditor.game.towers.NullTower -import fr.ynerant.leveleditor.game.towers.Tower -import javax.swing._ +import java.awt.event.{MouseEvent, MouseListener} import java.awt._ -import java.awt.event.MouseEvent -import java.awt.event.MouseListener -import java.util import java.util.Random +import fr.ynerant.leveleditor.api.editor.sprites.SpriteRegister +import fr.ynerant.leveleditor.api.editor.{Collision, RawMap} import fr.ynerant.leveleditor.editor.CollidPanel +import fr.ynerant.leveleditor.game.mobs.Mob +import fr.ynerant.leveleditor.game.towers.{AutoTower, BasicTower, NullTower, Tower} +import javax.swing._ + +import scala.collection.mutable.ListBuffer class GameFrame(val map: RawMap) extends JFrame("Jeu") { @@ -22,8 +19,8 @@ class GameFrame(val map: RawMap) extends JFrame("Jeu") { private var round = 0 private var hp = 5 private var reward = 20 - final private val mobs = new util.ArrayList[Mob] - final private val towers = new util.ArrayList[Tower] + private var mobs = ListBuffer[Mob]() + private var towers = ListBuffer[Tower]() final private var basicTower = null: JRadioButton final private var nullTower = null: JRadioButton final private var autoTower = null: JRadioButton @@ -74,7 +71,7 @@ class GameFrame(val map: RawMap) extends JFrame("Jeu") { setVisible(true) new Thread(() => { while ( { - hp > 0 && (round < 4 || !mobs.isEmpty) + hp > 0 && (round < 4 || mobs.nonEmpty) }) { tick() try Thread.sleep(50L) @@ -92,25 +89,25 @@ class GameFrame(val map: RawMap) extends JFrame("Jeu") { if (mobs.isEmpty && round < 4) { round += 1 val nb_mobs = round * (RANDOM.nextInt(16) + 1) - for (i <- 1 to nb_mobs) { + for (_ <- 1 to nb_mobs) { val mob = Mob.getRandomMob do mob.move(RANDOM.nextInt(getMap.getWidth / 16), RANDOM.nextInt(getMap.getHeight / 16)) while ( { - getMap.getCase(mob.getX, mob.getY).getCollision != Collision.ANY + !getMap.getCase(mob.getX, mob.getY).getCollision.equals(Collision.ANY) }) getMap.getCase(mob.getX, mob.getY).setCollision(Collision.PARTIAL) - mobs.add(mob) + mobs += mob } } - towers.forEach(tower => { - tower.filterDetectedMobs(mobs).forEach(mob => { + towers.foreach(tower => { + tower.filterDetectedMobs(mobs).foreach(mob => { mob.hit(tower.getDamagePerShot) }) }) - new util.ArrayList[Mob](mobs).forEach(mob => { + mobs.foreach(mob => { getMap.getCase(mob.getX, mob.getY).setCollision(Collision.ANY) mob.tick(this) if (mob.getX < 0 || mob.isDead) { - mobs.remove(mob) + mobs -= mob if (mob.getX < 0) { hp -= 1 if (hp == 0) { @@ -138,36 +135,6 @@ class GameFrame(val map: RawMap) extends JFrame("Jeu") { class Grid() extends JComponent with MouseListener { addMouseListener(this) - override protected def paintComponent(_g: Graphics): Unit = { - val g = _g.asInstanceOf[Graphics2D] - if (getMap.getFont != null) g.drawImage(getMap.getFont, null, null) - SpriteRegister.refreshAllSprites() - val SPRITE_SIZE = 32 - getMap.getCases.forEach(c => { - val s1 = SpriteRegister.getCategory(c.getCoucheOne.getCategory).getSprites.get(c.getCoucheOne.getIndex) - val s2 = SpriteRegister.getCategory(c.getCoucheTwo.getCategory).getSprites.get(c.getCoucheTwo.getIndex) - val s3 = SpriteRegister.getCategory(c.getCoucheThree.getCategory).getSprites.get(c.getCoucheThree.getIndex) - g.drawImage(s1.getImage, SPRITE_SIZE * c.getPosX, SPRITE_SIZE * c.getPosY, SPRITE_SIZE, SPRITE_SIZE, Color.white, null) - if (!CollidPanel.isEmpty(s2.getImage)) g.drawImage(s2.getImage, SPRITE_SIZE * c.getPosX, SPRITE_SIZE * c.getPosY, SPRITE_SIZE, SPRITE_SIZE, null, null) - if (!CollidPanel.isEmpty(s3.getImage)) g.drawImage(s3.getImage, SPRITE_SIZE * c.getPosX, SPRITE_SIZE * c.getPosY, SPRITE_SIZE, SPRITE_SIZE, null, null) - }) - new util.ArrayList[Mob](mobs).forEach(mob => { - val s = mob.getSprite - g.drawImage(s.getImage, SPRITE_SIZE * mob.getX, SPRITE_SIZE * mob.getY, SPRITE_SIZE, SPRITE_SIZE, null, null) - }) - towers.forEach(tower => { - val s = tower.getSprite - g.drawImage(s.getImage, SPRITE_SIZE * tower.getX, SPRITE_SIZE * tower.getY, SPRITE_SIZE, SPRITE_SIZE, null, null) - }) - repaint() - } - - override def mouseClicked(event: MouseEvent): Unit = { - } - - override def mousePressed(event: MouseEvent): Unit = { - } - override def mouseReleased(event: MouseEvent): Unit = { val x = event.getX / 32 val y = event.getY / 32 @@ -177,10 +144,41 @@ class GameFrame(val map: RawMap) extends JFrame("Jeu") { else null if (tower == null || tower.getPrice > reward) return val c = getMap.getCase(x, y) - if (c == null || (c.getCollision ne Collision.ANY)) return + println(x + ", " + y + ", " + tower + ", " + c) + if (c == null || !c.getCollision.equals(Collision.ANY)) return c.setCollision(Collision.FULL) reward -= tower.getPrice - towers.add(tower) + towers += tower + } + + override def mouseClicked(event: MouseEvent): Unit = { + } + + override def mousePressed(event: MouseEvent): Unit = { + } + + override protected def paintComponent(_g: Graphics): Unit = { + val g = _g.asInstanceOf[Graphics2D] + if (getMap.getFont != null) g.drawImage(getMap.getFont, null, null) + SpriteRegister.refreshAllSprites() + val SPRITE_SIZE = 32 + getMap.getCases.foreach(c => { + val s1 = SpriteRegister.getCategory(c.getCoucheOne.getCategory).getSprites(c.getCoucheOne.getIndex) + val s2 = SpriteRegister.getCategory(c.getCoucheTwo.getCategory).getSprites(c.getCoucheTwo.getIndex) + val s3 = SpriteRegister.getCategory(c.getCoucheThree.getCategory).getSprites(c.getCoucheThree.getIndex) + g.drawImage(s1.getImage, SPRITE_SIZE * c.getPosX, SPRITE_SIZE * c.getPosY, SPRITE_SIZE, SPRITE_SIZE, Color.white, null) + if (!CollidPanel.isEmpty(s2.getImage)) g.drawImage(s2.getImage, SPRITE_SIZE * c.getPosX, SPRITE_SIZE * c.getPosY, SPRITE_SIZE, SPRITE_SIZE, null, null) + if (!CollidPanel.isEmpty(s3.getImage)) g.drawImage(s3.getImage, SPRITE_SIZE * c.getPosX, SPRITE_SIZE * c.getPosY, SPRITE_SIZE, SPRITE_SIZE, null, null) + }) + mobs.foreach(mob => { + val s = mob.getSprite + g.drawImage(s.getImage, SPRITE_SIZE * mob.getX, SPRITE_SIZE * mob.getY, SPRITE_SIZE, SPRITE_SIZE, null, null) + }) + towers.foreach(tower => { + val s = tower.getSprite + g.drawImage(s.getImage, SPRITE_SIZE * tower.getX, SPRITE_SIZE * tower.getY, SPRITE_SIZE, SPRITE_SIZE, null, null) + }) + repaint() } override def mouseEntered(event: MouseEvent): Unit = { diff --git a/src/main/scala/fr/ynerant/leveleditor/game/mobs/Mob.scala b/src/main/scala/fr/ynerant/leveleditor/game/mobs/Mob.scala index 1579338..cda4f5e 100644 --- a/src/main/scala/fr/ynerant/leveleditor/game/mobs/Mob.scala +++ b/src/main/scala/fr/ynerant/leveleditor/game/mobs/Mob.scala @@ -1,12 +1,11 @@ package fr.ynerant.leveleditor.game.mobs -import fr.ynerant.leveleditor.api.editor.sprites.Sprite -import fr.ynerant.leveleditor.api.editor.sprites.SpriteRegister -import fr.ynerant.leveleditor.game.GameFrame import java.util import java.util.Random import fr.ynerant.leveleditor.api.editor.RawCase +import fr.ynerant.leveleditor.api.editor.sprites.{Sprite, SpriteRegister} +import fr.ynerant.leveleditor.game.GameFrame object Mob { @@ -38,7 +37,7 @@ abstract class Mob() { def getName: String def getSprite: Sprite = { - if (sprite == null) sprite = SpriteRegister.getCategory(getName).getSprites.get(0) + if (sprite == null) sprite = SpriteRegister.getCategory(getName).getSprites.head sprite } @@ -77,17 +76,17 @@ abstract class Mob() { return } - val visited = new util.ArrayList[RawCase] + var visited = Nil: List[RawCase] val queue = new util.ArrayDeque[RawCase] - val pred = new util.HashMap[RawCase, RawCase] + var pred = Map(): Map[RawCase, RawCase] var last = null: RawCase queue.add(current) while (!queue.isEmpty) { val visiting = queue.poll - visited.add(visiting) - game.getMap.getNeighbours(visiting).forEach(neighbour => { + visited ::= visiting + game.getMap.getNeighbours(visiting).foreach(neighbour => { if (neighbour != null && !visited.contains(neighbour)) { - pred.put(neighbour, visiting) + pred += (neighbour -> visiting) queue.add(neighbour) if (neighbour.getPosX == 0) { last = neighbour @@ -97,7 +96,7 @@ abstract class Mob() { } }) if (last != null) { - while (pred.get(last) != current) last = pred.get(last) + while (pred(last) != current) last = pred(last) move(last.getPosX, last.getPosY) } } diff --git a/src/main/scala/fr/ynerant/leveleditor/game/towers/AutoTower.scala b/src/main/scala/fr/ynerant/leveleditor/game/towers/AutoTower.scala index 9481bb5..dbfd876 100644 --- a/src/main/scala/fr/ynerant/leveleditor/game/towers/AutoTower.scala +++ b/src/main/scala/fr/ynerant/leveleditor/game/towers/AutoTower.scala @@ -1,7 +1,6 @@ package fr.ynerant.leveleditor.game.towers import fr.ynerant.leveleditor.game.mobs.Mob -import java.util class AutoTower(override val x: Int, override val y: Int) extends Tower(x, y) { @@ -13,5 +12,5 @@ class AutoTower(override val x: Int, override val y: Int) extends Tower(x, y) { override def getPrice = 142 - override private[towers] def _filterDetectedMobs(mobs: util.Collection[Mob]) = mobs + override private[towers] def _filterDetectedMobs(mobs: Iterable[Mob]) = mobs } diff --git a/src/main/scala/fr/ynerant/leveleditor/game/towers/BasicTower.scala b/src/main/scala/fr/ynerant/leveleditor/game/towers/BasicTower.scala index d095341..a5782f3 100644 --- a/src/main/scala/fr/ynerant/leveleditor/game/towers/BasicTower.scala +++ b/src/main/scala/fr/ynerant/leveleditor/game/towers/BasicTower.scala @@ -1,7 +1,6 @@ package fr.ynerant.leveleditor.game.towers import fr.ynerant.leveleditor.game.mobs.Mob -import java.util class BasicTower(override val x: Int, override val y: Int) extends Tower(x, y) { @@ -13,11 +12,7 @@ class BasicTower(override val x: Int, override val y: Int) extends Tower(x, y) { override def getPrice = 10 - override private[towers] def _filterDetectedMobs(mobs: util.Collection[Mob]) = { - val filtered = new util.ArrayList[Mob] - mobs.forEach(mob => { - if ((mob.getX == getX || mob.getY == getY) && Math.abs(mob.getX - getX) <= 3 && Math.abs(mob.getY - getY) <= 3) filtered.add(mob) - }) - filtered + override private[towers] def _filterDetectedMobs(mobs: Iterable[Mob]) = { + mobs.filter(mob => (mob.getX == getX || mob.getY == getY) && Math.abs(mob.getX - getX) <= 3 && Math.abs(mob.getY - getY) <= 3) } } diff --git a/src/main/scala/fr/ynerant/leveleditor/game/towers/NullTower.scala b/src/main/scala/fr/ynerant/leveleditor/game/towers/NullTower.scala index e24814d..9714172 100644 --- a/src/main/scala/fr/ynerant/leveleditor/game/towers/NullTower.scala +++ b/src/main/scala/fr/ynerant/leveleditor/game/towers/NullTower.scala @@ -1,7 +1,6 @@ package fr.ynerant.leveleditor.game.towers import fr.ynerant.leveleditor.game.mobs.Mob -import java.util class NullTower(override val x: Int, override val y: Int) extends Tower(x, y) { @@ -13,5 +12,5 @@ class NullTower(override val x: Int, override val y: Int) extends Tower(x, y) { override def getPrice = 5 - override private[towers] def _filterDetectedMobs(mobs: util.Collection[Mob]) = new util.ArrayList[Mob] + override private[towers] def _filterDetectedMobs(mobs: Iterable[Mob]) = Nil } diff --git a/src/main/scala/fr/ynerant/leveleditor/game/towers/Tower.scala b/src/main/scala/fr/ynerant/leveleditor/game/towers/Tower.scala index 431e690..2884076 100644 --- a/src/main/scala/fr/ynerant/leveleditor/game/towers/Tower.scala +++ b/src/main/scala/fr/ynerant/leveleditor/game/towers/Tower.scala @@ -1,18 +1,17 @@ package fr.ynerant.leveleditor.game.towers -import fr.ynerant.leveleditor.api.editor.sprites.Sprite -import fr.ynerant.leveleditor.api.editor.sprites.SpriteRegister -import fr.ynerant.leveleditor.game.mobs.Mob -import java.util import java.util.Random +import fr.ynerant.leveleditor.api.editor.sprites.{Sprite, SpriteRegister} +import fr.ynerant.leveleditor.game.mobs.Mob + object Tower { private val RANDOM = new Random } abstract class Tower(val x: Int, val y: Int) { - final private val sprite = SpriteRegister.getCategory(getName).getSprites.get(0) + final private val sprite = SpriteRegister.getCategory(getName).getSprites.head private var remainingTicks = 0L def getSprite: Sprite = sprite @@ -25,17 +24,17 @@ abstract class Tower(val x: Int, val y: Int) { def getPrice: Int - private[towers] def _filterDetectedMobs(mobs: util.Collection[Mob]): util.Collection[Mob] - - def filterDetectedMobs(mobs: util.Collection[Mob]): util.Collection[Mob] = if (remainingTicks > 0) { + def filterDetectedMobs(mobs: Iterable[Mob]): Iterable[Mob] = if (remainingTicks > 0) { remainingTicks -= 1 - new util.ArrayList[Mob] + Nil } else { remainingTicks = getPeriod _filterDetectedMobs(mobs) } + private[towers] def _filterDetectedMobs(mobs: Iterable[Mob]): Iterable[Mob] + def getX: Int = x def getY: Int = y