Better Scala code

This commit is contained in:
Yohann D'ANELLO 2020-04-04 21:56:14 +02:00
parent 58509e0d5b
commit 031815e055
22 changed files with 249 additions and 281 deletions

View File

@ -1,11 +1,10 @@
package fr.ynerant.leveleditor.api.editor package fr.ynerant.leveleditor.api.editor
import fr.ynerant.leveleditor.api.editor.Collision.Collision
import fr.ynerant.leveleditor.api.editor.sprites.Sprite import fr.ynerant.leveleditor.api.editor.sprites.Sprite
object Case { 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 val c = new Case
c.x = posX c.x = posX
c.y = posY c.y = posY
@ -23,7 +22,7 @@ class Case {
private var couche1 = null: Sprite private var couche1 = null: Sprite
private var couche2 = null: Sprite private var couche2 = null: Sprite
private var couche3 = null: Sprite private var couche3 = null: Sprite
private var collision = null: Collision private var collision = null: String
def getPosX: Int = x def getPosX: Int = x
@ -35,7 +34,7 @@ class Case {
def getCoucheThree: Sprite = couche3 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" override def toString: String = "{Case x=" + x + " y=" + y + " couche1=" + couche1 + " couche2=" + couche2 + " couche3=" + couche3 + " collision=" + collision.toString.toUpperCase + "}\n"
} }

View File

@ -1,6 +1,9 @@
package fr.ynerant.leveleditor.api.editor package fr.ynerant.leveleditor.api.editor
object Collision extends Enumeration { object Collision {
type Collision = Value val FULL: String = "FULL"
val FULL, PARTIAL, ANY = Value val PARTIAL: String = "PARTIAL"
val ANY: String = "ANY"
val values: IndexedSeq[String] = IndexedSeq(FULL, PARTIAL, ANY)
} }

View File

@ -3,22 +3,19 @@ package fr.ynerant.leveleditor.api.editor
import java.awt.Color import java.awt.Color
import java.awt.image.BufferedImage import java.awt.image.BufferedImage
import java.io._ import java.io._
import java.lang.reflect.Type
import java.nio.charset.StandardCharsets import java.nio.charset.StandardCharsets
import java.util
import java.util.zip.{GZIPInputStream, GZIPOutputStream} import java.util.zip.{GZIPInputStream, GZIPOutputStream}
import com.google.gson.{Gson, GsonBuilder, JsonDeserializationContext, JsonDeserializer, JsonElement, JsonPrimitive, JsonSerializationContext, JsonSerializer} import fr.ynerant.leveleditor.editor.GMap
import fr.ynerant.leveleditor.api.editor.Collision.Collision
import fr.ynerant.leveleditor.editor.Map
import javax.swing.JFileChooser import javax.swing.JFileChooser
import javax.swing.filechooser.FileNameExtensionFilter import javax.swing.filechooser.FileNameExtensionFilter
import net.liftweb.json._
object EditorAPI { object EditorAPI {
private var LAST_FILE = null: File private var LAST_FILE = null: File
def toRawMap(width: Int, height: Int): RawMap = { def toRawMap(width: Int, height: Int): RawMap = {
val cases = new util.ArrayList[RawCase] var cases = Nil: List[RawCase]
var y = 1 var y = 1
while ( { while ( {
y < height y < height
@ -28,7 +25,7 @@ object EditorAPI {
x < width x < width
}) { }) {
val c = RawCase.create(x / 16, y / 16, RawSprite.BLANK, RawSprite.BLANK, RawSprite.BLANK, Collision.ANY) val c = RawCase.create(x / 16, y / 16, RawSprite.BLANK, RawSprite.BLANK, RawSprite.BLANK, Collision.ANY)
cases.add(c) cases ::= c
x += 16 x += 16
} }
@ -38,20 +35,6 @@ object EditorAPI {
RawMap.create(cases, width, height) 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 = { def createJFC: JFileChooser = {
val jfc = new JFileChooser val jfc = new JFileChooser
jfc.setFileFilter(new FileNameExtensionFilter("Fichiers monde (*.gmap, *.dat)", "gmap", "dat")) jfc.setFileFilter(new FileNameExtensionFilter("Fichiers monde (*.gmap, *.dat)", "gmap", "dat"))
@ -80,7 +63,8 @@ object EditorAPI {
} }
def save(file: File, map: RawMap): Unit = { def save(file: File, map: RawMap): Unit = {
val json = createGson.toJson(map) implicit val formats: DefaultFormats.type = DefaultFormats
val json = Serialization.writePretty(map)
try { try {
assert(file.createNewFile) assert(file.createNewFile)
val bos = new BufferedOutputStream(new GZIPOutputStream(new FileOutputStream(file))) val bos = new BufferedOutputStream(new GZIPOutputStream(new FileOutputStream(file)))
@ -92,7 +76,7 @@ object EditorAPI {
} }
} }
def open: Map = { def open: GMap = {
val jfc = createJFC val jfc = createJFC
var file = null: File var file = null: File
jfc.showOpenDialog(null) jfc.showOpenDialog(null)
@ -119,12 +103,13 @@ object EditorAPI {
case e: IOException => case e: IOException =>
e.printStackTrace() 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) { if (map.getFont == null) {
val baseWidth = map.getWidth val baseWidth = map.getWidth
val baseHeight = map.getHeight val baseHeight = map.getHeight
@ -155,6 +140,6 @@ object EditorAPI {
} }
map.setFont(image) map.setFont(image)
} }
new Map(map) new GMap(map)
} }
} }

View File

@ -1,9 +1,7 @@
package fr.ynerant.leveleditor.api.editor package fr.ynerant.leveleditor.api.editor
import fr.ynerant.leveleditor.api.editor.Collision.Collision
object RawCase { 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) 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 getPosX: Int = x
def getPosY: Int = y 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 getCoucheThree: RawSprite = couche3
def getCollision: Collision = collision def getCollision: String = collision
def setCollision(collision: Collision): Unit = { def setCollision(collision: String): Unit = {
this.collision = collision this.collision = collision
} }
} }

View File

@ -1,50 +1,45 @@
package fr.ynerant.leveleditor.api.editor package fr.ynerant.leveleditor.api.editor
import fr.ynerant.leveleditor.editor.Map
import java.awt.image.BufferedImage 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 { 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) new RawMap(cases, width, height)
} }
def create(map: Map): RawMap = { def create(map: GMap): RawMap = {
val raw = new RawMap(new util.ArrayList[RawCase], 0, 0) val raw = new RawMap(Nil, map.getWidth, map.getHeight)
raw.width = map.getWidth map.getAllCases.foreach(c => raw.cases ::= RawCase.create(c))
raw.height = map.getHeight
raw.cases = new util.ArrayList[RawCase]
map.getAllCases.forEach(c => raw.cases.add(RawCase.create(c)))
raw raw
} }
} }
case class RawMap(var cases: util.List[RawCase], var width: Int, var height: Int) { case class RawMap(var cases: List[RawCase], var width: Int, var height: Int) {
private var cases_map = null: util.HashMap[Integer, RawCase] private var cases_map = null: Map[Integer, RawCase]
private var font = null: BufferedImage 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 = { def getCase(x: Int, y: Int): RawCase = {
if (cases_map == null) { if (cases_map == null) {
cases_map = new util.HashMap[Integer, RawCase] cases_map = Map()
getCases.forEach(c => cases_map.put(c.getPosY * width + c.getPosX, c)) 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] = { def getCases: List[RawCase] = cases
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 getWidth: Int = width 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 this.font = font
} }
override def toString: String = new Gson().toJson(this) override def toString: String = {
implicit val formats: DefaultFormats.type = DefaultFormats
Serialization.writePretty(this)
}
} }

View File

@ -1,9 +1,9 @@
package fr.ynerant.leveleditor.api.editor.sprites package fr.ynerant.leveleditor.api.editor.sprites
import java.util import scala.collection.mutable.ListBuffer
object Category { object Category {
def create(name: String, sprites: util.List[Sprite]): Category = { def create(name: String, sprites: ListBuffer[Sprite]): Category = {
val c = new Category val c = new Category
c.name = name c.name = name
c.sprites = sprites c.sprites = sprites
@ -12,12 +12,16 @@ object Category {
} }
class Category private() { class Category private() {
private var sprites = null: util.List[Sprite] private var sprites = null: ListBuffer[Sprite]
private var name = null: String private var name = null: String
def getName: String = name 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 override def toString: String = name
} }

View File

@ -2,15 +2,13 @@ package fr.ynerant.leveleditor.api.editor.sprites
import java.awt._ import java.awt._
import java.awt.image.BufferedImage import java.awt.image.BufferedImage
import java.util
import java.util.ArrayList
import scala.collection.mutable.ListBuffer
object Sprite { 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 val g: Graphics2D = BLANK.getImage.createGraphics
= BLANK.getImage.createGraphics
g.setComposite(AlphaComposite.Clear) g.setComposite(AlphaComposite.Clear)
g.setColor(new Color(0, true)) g.setColor(new Color(0, true))
g.fillRect(0, 0, 16, 16) g.fillRect(0, 0, 16, 16)
@ -18,7 +16,7 @@ object Sprite {
} }
class Sprite(val img: BufferedImage, val cat: Category, val index: Int) { 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 def getImage: BufferedImage = this.img

View File

@ -1,19 +1,20 @@
package fr.ynerant.leveleditor.api.editor.sprites 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.net.{URISyntaxException, URLDecoder}
import java.nio.file.{Files, Paths} import java.nio.file.{Files, Paths}
import java.util
import java.util.Objects import java.util.Objects
import java.util.jar.JarFile import java.util.jar.JarFile
import com.google.gson.Gson
import javax.imageio.ImageIO
import fr.ynerant.leveleditor.client.main.Main import fr.ynerant.leveleditor.client.main.Main
import javax.imageio.ImageIO
import net.liftweb.json._
import scala.collection.mutable.ListBuffer
object SpriteRegister { object SpriteRegister {
private var nameToCoords = null: util.Map[String, util.List[util.List[Double]]] private var nameToCoords = Map(): Map[String, List[List[Int]]]
private val sprites = new util.HashMap[String, Category] private var sprites = Map(): Map[String, Category]
@throws[IOException] @throws[IOException]
def unpack(): Unit = { def unpack(): Unit = {
@ -63,32 +64,32 @@ object SpriteRegister {
} }
@SuppressWarnings(Array("unchecked")) def refreshAllSprites(): Unit = { @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 assetsDir = new File("assets")
val assets = new util.ArrayList[String] var assets = Nil: List[String]
for (dir <- Objects.requireNonNull(assetsDir.listFiles)) { for (dir <- Objects.requireNonNull(assetsDir.listFiles)) {
assets.add(dir.getName) assets ::= dir.getName
} }
assets.forEach(asset => { assets.foreach(asset => {
try { try {
val f = new File(assetsDir.getAbsolutePath + "/" + asset + "/textures/sprites") val f = new File(assetsDir.getAbsolutePath + "/" + asset + "/textures/sprites")
assert(f.isDirectory || f.mkdirs) assert(f.isDirectory || f.mkdirs)
val br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(f, "sprites.json")))) val json = Files.readString(new File(f, "sprites.json").toPath)
nameToCoords = new Gson().fromJson(br, classOf[util.Map[_, _]]) implicit val formats: DefaultFormats.type = DefaultFormats
br.close() nameToCoords = parse(json).extract[Map[String, List[List[Int]]]]
nameToCoords.keySet.forEach(key => { nameToCoords.keySet.foreach(key => {
try { try {
val is = new BufferedInputStream(new FileInputStream(new File(f, key + ".png"))) val is = new BufferedInputStream(new FileInputStream(new File(f, key + ".png")))
val img = ImageIO.read(is) val img = ImageIO.read(is)
val cat = Category.create(key, new util.ArrayList[Sprite]) val cat = Category.create(key, ListBuffer())
nameToCoords.get(key).forEach(list => { nameToCoords(key).foreach(list => {
val x = list.get(0).intValue val x = list.head.intValue
val y = list.get(1).intValue val y = list(1).intValue
val child = img.getSubimage(x, y, 16, 16) 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 { } catch {
case t: Throwable => case t: Throwable =>
System.err.println("Erreur lors de la lecture du sprite '" + key + "'") 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
} }

View File

@ -1,19 +1,16 @@
package fr.ynerant.leveleditor.client.main 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.EditorAPI
import fr.ynerant.leveleditor.api.editor.sprites.SpriteRegister import fr.ynerant.leveleditor.api.editor.sprites.SpriteRegister
import fr.ynerant.leveleditor.frame.MainFrame import fr.ynerant.leveleditor.frame.MainFrame
import fr.ynerant.leveleditor.game.GameFrame import fr.ynerant.leveleditor.game.GameFrame
import javax.swing._ 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
/** /**

View File

@ -1,9 +1,10 @@
package fr.ynerant.leveleditor.editor package fr.ynerant.leveleditor.editor
import java.awt._
import java.awt.image.BufferedImage
import fr.ynerant.leveleditor.api.editor.Collision import fr.ynerant.leveleditor.api.editor.Collision
import javax.swing._ import javax.swing._
import java.awt._
import java.awt.image.BufferedImage
@SerialVersionUID(-138754019431984881L) @SerialVersionUID(-138754019431984881L)
@ -21,8 +22,6 @@ object CollidPanel {
@SerialVersionUID(-138754019431984881L) @SerialVersionUID(-138754019431984881L)
class CollidPanel(val frame: EditorFrame) extends JPanel { class CollidPanel(val frame: EditorFrame) extends JPanel {
def getMap: Map = frame.getMap
override def paintComponent(g: Graphics): Unit = { override def paintComponent(g: Graphics): Unit = {
g.fillRect(0, 0, getWidth, getHeight) g.fillRect(0, 0, getWidth, getHeight)
val img = getMap.getFont val img = getMap.getFont
@ -31,7 +30,7 @@ class CollidPanel(val frame: EditorFrame) extends JPanel {
val width = img.getWidth * 2 val width = img.getWidth * 2
val height = img.getHeight * 2 val height = img.getHeight * 2
g.drawImage(getMap.getFont, x, y, width, height, null) g.drawImage(getMap.getFont, x, y, width, height, null)
getMap.getAllCases.forEach(c => { getMap.getAllCases.foreach(c => {
if (!CollidPanel.isEmpty(c.getCoucheOne.getImage)) { if (!CollidPanel.isEmpty(c.getCoucheOne.getImage)) {
g.drawImage(c.getCoucheOne.getImage, x + c.getPosX * 34 + 2, y + c.getPosY * 34 + 2, 32, 32, null) g.drawImage(c.getCoucheOne.getImage, x + c.getPosX * 34 + 2, y + c.getPosY * 34 + 2, 32, 32, null)
if (!CollidPanel.isEmpty(c.getCoucheTwo.getImage)) { if (!CollidPanel.isEmpty(c.getCoucheTwo.getImage)) {
@ -42,8 +41,8 @@ class CollidPanel(val frame: EditorFrame) extends JPanel {
} }
}) })
getMap.getAllCases.forEach(c => { getMap.getAllCases.foreach(c => {
if (c.getCollision ne Collision.ANY) { if (c.getCollision.equals(Collision.ANY)) {
val alpha = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB) val alpha = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB)
if (c.getCollision eq Collision.FULL) { if (c.getCollision eq Collision.FULL) {
val grap = alpha.createGraphics val grap = alpha.createGraphics
@ -61,4 +60,6 @@ class CollidPanel(val frame: EditorFrame) extends JPanel {
} }
}) })
} }
def getMap: GMap = frame.getMap
} }

View File

@ -1,19 +1,18 @@
package fr.ynerant.leveleditor.editor 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._
import java.awt.event._ import java.awt.event._
import java.io.File import java.io.File
import fr.ynerant.leveleditor.api.editor.sprites.SpriteRegister
import fr.ynerant.leveleditor.api.editor.{EditorAPI, RawMap} 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) @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 save = new JMenuItem("Sauvegarder")
final private val saveAs = new JMenuItem("Sauvegarder sous ...") final private val saveAs = new JMenuItem("Sauvegarder sous ...")
final private val exit = new JMenuItem("Quitter") final private val exit = new JMenuItem("Quitter")
@ -126,8 +125,8 @@ class EditorFrame(val map: Map) extends JFrame("Level Editor") with ChangeListen
couche2.repaint() couche2.repaint()
couche3.repaint() couche3.repaint()
} }
SpriteRegister.getAllCategories.forEach(cat => { SpriteRegister.getAllCategories.foreach(cat => {
cat.getSprites.forEach(spr => { cat.getSprites.foreach(spr => {
val sprc1 = new SpriteComp(spr, 0) val sprc1 = new SpriteComp(spr, 0)
val sprc2 = new SpriteComp(spr, 1) val sprc2 = new SpriteComp(spr, 1)
val sprc3 = new SpriteComp(spr, 2) 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) resources.getSelectedComponent.asInstanceOf[JScrollPane].getVerticalScrollBar.setValue(cursorPos)
} }
def getMap: Map = map def getMap: GMap = map
def getSelectedSprite: SpriteComp = selectedSprite def getSelectedSprite: SpriteComp = selectedSprite

View File

@ -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)
}
}

View File

@ -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
}
}

View File

@ -1,14 +1,13 @@
package fr.ynerant.leveleditor.editor package fr.ynerant.leveleditor.editor
import javax.swing._
import java.awt._ import java.awt._
import java.awt.image.BufferedImage import java.awt.image.BufferedImage
import javax.swing._
@SerialVersionUID(2629019576253690557L) @SerialVersionUID(2629019576253690557L)
class MapPanel(val frame: EditorFrame) extends JPanel { class MapPanel(val frame: EditorFrame) extends JPanel {
def getMap: Map = frame.getMap
override def paintComponent(g: Graphics): Unit = { override def paintComponent(g: Graphics): Unit = {
g.fillRect(0, 0, getWidth, getHeight) g.fillRect(0, 0, getWidth, getHeight)
val img = getMap.getFont val img = getMap.getFont
@ -17,7 +16,7 @@ class MapPanel(val frame: EditorFrame) extends JPanel {
val width = img.getWidth * 2 val width = img.getWidth * 2
val height = img.getHeight * 2 val height = img.getHeight * 2
g.drawImage(getMap.getFont, x, y, width, height, null) 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 (!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) /* 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) = { @SuppressWarnings(Array("BooleanMethodIsAlwaysInverted")) private def isEmpty(image: BufferedImage) = {
var allrgba = 0 var allrgba = 0
for (x <- 0 until image.getWidth) { for (x <- 0 until image.getWidth) {

View File

@ -1,9 +1,8 @@
package fr.ynerant.leveleditor.frame.listeners package fr.ynerant.leveleditor.frame.listeners
import fr.ynerant.leveleditor.api.editor.{Case, Collision} import java.awt.event.{MouseAdapter, MouseEvent}
import java.awt.event.MouseAdapter
import java.awt.event.MouseEvent
import fr.ynerant.leveleditor.api.editor.Case
import fr.ynerant.leveleditor.editor.{CollidPanel, EditorFrame} 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 y = panel.getHeight / 2 - map.getFont.getHeight
val c = map.getCase((event.getX - x + 2) / 34, (event.getY - y + 2) / 34) 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) { if (c != null && event.getX - x >= 2 && event.getY - y >= 2) {
val colIndex = c.getCollision.id val n = Case.create(c.getPosX, c.getPosY, c.getCoucheOne, c.getCoucheTwo, c.getCoucheThree, c.getCollision)
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)
map.setCase((event.getX - x + 2) / 34, (event.getY - y + 2) / 34, n) map.setCase((event.getX - x + 2) / 34, (event.getY - y + 2) / 34, n)
panel.repaint() panel.repaint()
} }

View File

@ -1,7 +1,6 @@
package fr.ynerant.leveleditor.frame.listeners package fr.ynerant.leveleditor.frame.listeners
import java.awt.event.MouseAdapter import java.awt.event.{MouseAdapter, MouseEvent}
import java.awt.event.MouseEvent
import fr.ynerant.leveleditor.api.editor.Case import fr.ynerant.leveleditor.api.editor.Case
import fr.ynerant.leveleditor.editor.{EditorFrame, MapPanel} 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) { else if (frame.getSelectedPaintingMode == 1) {
getFrame.getMap.getAllCases.forEach(c => { getFrame.getMap.getAllCases.foreach(c => {
val map = getFrame.getMap val map = getFrame.getMap
if (getFrame.getSelectedSprite != null) { if (getFrame.getSelectedSprite != null) {
if (getFrame.getSelectedSprite.getCouche - 1 > getFrame.getSelectedLayerIndex) return if (getFrame.getSelectedSprite.getCouche - 1 > getFrame.getSelectedLayerIndex) return

View File

@ -1,20 +1,17 @@
package fr.ynerant.leveleditor.game package fr.ynerant.leveleditor.game
import fr.ynerant.leveleditor.api.editor.{Collision, RawMap} import java.awt.event.{MouseEvent, MouseListener}
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._ import java.awt._
import java.awt.event.MouseEvent
import java.awt.event.MouseListener
import java.util
import java.util.Random 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.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") { 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 round = 0
private var hp = 5 private var hp = 5
private var reward = 20 private var reward = 20
final private val mobs = new util.ArrayList[Mob] private var mobs = ListBuffer[Mob]()
final private val towers = new util.ArrayList[Tower] private var towers = ListBuffer[Tower]()
final private var basicTower = null: JRadioButton final private var basicTower = null: JRadioButton
final private var nullTower = null: JRadioButton final private var nullTower = null: JRadioButton
final private var autoTower = null: JRadioButton final private var autoTower = null: JRadioButton
@ -74,7 +71,7 @@ class GameFrame(val map: RawMap) extends JFrame("Jeu") {
setVisible(true) setVisible(true)
new Thread(() => { new Thread(() => {
while ( { while ( {
hp > 0 && (round < 4 || !mobs.isEmpty) hp > 0 && (round < 4 || mobs.nonEmpty)
}) { }) {
tick() tick()
try Thread.sleep(50L) try Thread.sleep(50L)
@ -92,25 +89,25 @@ class GameFrame(val map: RawMap) extends JFrame("Jeu") {
if (mobs.isEmpty && round < 4) { if (mobs.isEmpty && round < 4) {
round += 1 round += 1
val nb_mobs = round * (RANDOM.nextInt(16) + 1) val nb_mobs = round * (RANDOM.nextInt(16) + 1)
for (i <- 1 to nb_mobs) { for (_ <- 1 to nb_mobs) {
val mob = Mob.getRandomMob val mob = Mob.getRandomMob
do mob.move(RANDOM.nextInt(getMap.getWidth / 16), RANDOM.nextInt(getMap.getHeight / 16)) while ( { 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) getMap.getCase(mob.getX, mob.getY).setCollision(Collision.PARTIAL)
mobs.add(mob) mobs += mob
} }
} }
towers.forEach(tower => { towers.foreach(tower => {
tower.filterDetectedMobs(mobs).forEach(mob => { tower.filterDetectedMobs(mobs).foreach(mob => {
mob.hit(tower.getDamagePerShot) mob.hit(tower.getDamagePerShot)
}) })
}) })
new util.ArrayList[Mob](mobs).forEach(mob => { mobs.foreach(mob => {
getMap.getCase(mob.getX, mob.getY).setCollision(Collision.ANY) getMap.getCase(mob.getX, mob.getY).setCollision(Collision.ANY)
mob.tick(this) mob.tick(this)
if (mob.getX < 0 || mob.isDead) { if (mob.getX < 0 || mob.isDead) {
mobs.remove(mob) mobs -= mob
if (mob.getX < 0) { if (mob.getX < 0) {
hp -= 1 hp -= 1
if (hp == 0) { if (hp == 0) {
@ -138,36 +135,6 @@ class GameFrame(val map: RawMap) extends JFrame("Jeu") {
class Grid() extends JComponent with MouseListener { class Grid() extends JComponent with MouseListener {
addMouseListener(this) 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 = { override def mouseReleased(event: MouseEvent): Unit = {
val x = event.getX / 32 val x = event.getX / 32
val y = event.getY / 32 val y = event.getY / 32
@ -177,10 +144,41 @@ class GameFrame(val map: RawMap) extends JFrame("Jeu") {
else null else null
if (tower == null || tower.getPrice > reward) return if (tower == null || tower.getPrice > reward) return
val c = getMap.getCase(x, y) 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) c.setCollision(Collision.FULL)
reward -= tower.getPrice 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 = { override def mouseEntered(event: MouseEvent): Unit = {

View File

@ -1,12 +1,11 @@
package fr.ynerant.leveleditor.game.mobs 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
import java.util.Random import java.util.Random
import fr.ynerant.leveleditor.api.editor.RawCase import fr.ynerant.leveleditor.api.editor.RawCase
import fr.ynerant.leveleditor.api.editor.sprites.{Sprite, SpriteRegister}
import fr.ynerant.leveleditor.game.GameFrame
object Mob { object Mob {
@ -38,7 +37,7 @@ abstract class Mob() {
def getName: String def getName: String
def getSprite: Sprite = { def getSprite: Sprite = {
if (sprite == null) sprite = SpriteRegister.getCategory(getName).getSprites.get(0) if (sprite == null) sprite = SpriteRegister.getCategory(getName).getSprites.head
sprite sprite
} }
@ -77,17 +76,17 @@ abstract class Mob() {
return return
} }
val visited = new util.ArrayList[RawCase] var visited = Nil: List[RawCase]
val queue = new util.ArrayDeque[RawCase] val queue = new util.ArrayDeque[RawCase]
val pred = new util.HashMap[RawCase, RawCase] var pred = Map(): Map[RawCase, RawCase]
var last = null: RawCase var last = null: RawCase
queue.add(current) queue.add(current)
while (!queue.isEmpty) { while (!queue.isEmpty) {
val visiting = queue.poll val visiting = queue.poll
visited.add(visiting) visited ::= visiting
game.getMap.getNeighbours(visiting).forEach(neighbour => { game.getMap.getNeighbours(visiting).foreach(neighbour => {
if (neighbour != null && !visited.contains(neighbour)) { if (neighbour != null && !visited.contains(neighbour)) {
pred.put(neighbour, visiting) pred += (neighbour -> visiting)
queue.add(neighbour) queue.add(neighbour)
if (neighbour.getPosX == 0) { if (neighbour.getPosX == 0) {
last = neighbour last = neighbour
@ -97,7 +96,7 @@ abstract class Mob() {
} }
}) })
if (last != null) { if (last != null) {
while (pred.get(last) != current) last = pred.get(last) while (pred(last) != current) last = pred(last)
move(last.getPosX, last.getPosY) move(last.getPosX, last.getPosY)
} }
} }

View File

@ -1,7 +1,6 @@
package fr.ynerant.leveleditor.game.towers package fr.ynerant.leveleditor.game.towers
import fr.ynerant.leveleditor.game.mobs.Mob import fr.ynerant.leveleditor.game.mobs.Mob
import java.util
class AutoTower(override val x: Int, override val y: Int) extends Tower(x, y) { 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 def getPrice = 142
override private[towers] def _filterDetectedMobs(mobs: util.Collection[Mob]) = mobs override private[towers] def _filterDetectedMobs(mobs: Iterable[Mob]) = mobs
} }

View File

@ -1,7 +1,6 @@
package fr.ynerant.leveleditor.game.towers package fr.ynerant.leveleditor.game.towers
import fr.ynerant.leveleditor.game.mobs.Mob import fr.ynerant.leveleditor.game.mobs.Mob
import java.util
class BasicTower(override val x: Int, override val y: Int) extends Tower(x, y) { 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 def getPrice = 10
override private[towers] def _filterDetectedMobs(mobs: util.Collection[Mob]) = { override private[towers] def _filterDetectedMobs(mobs: Iterable[Mob]) = {
val filtered = new util.ArrayList[Mob] mobs.filter(mob => (mob.getX == getX || mob.getY == getY) && Math.abs(mob.getX - getX) <= 3 && Math.abs(mob.getY - getY) <= 3)
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
} }
} }

View File

@ -1,7 +1,6 @@
package fr.ynerant.leveleditor.game.towers package fr.ynerant.leveleditor.game.towers
import fr.ynerant.leveleditor.game.mobs.Mob import fr.ynerant.leveleditor.game.mobs.Mob
import java.util
class NullTower(override val x: Int, override val y: Int) extends Tower(x, y) { 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 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
} }

View File

@ -1,18 +1,17 @@
package fr.ynerant.leveleditor.game.towers 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 java.util.Random
import fr.ynerant.leveleditor.api.editor.sprites.{Sprite, SpriteRegister}
import fr.ynerant.leveleditor.game.mobs.Mob
object Tower { object Tower {
private val RANDOM = new Random private val RANDOM = new Random
} }
abstract class Tower(val x: Int, val y: Int) { 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 private var remainingTicks = 0L
def getSprite: Sprite = sprite def getSprite: Sprite = sprite
@ -25,17 +24,17 @@ abstract class Tower(val x: Int, val y: Int) {
def getPrice: Int def getPrice: Int
private[towers] def _filterDetectedMobs(mobs: util.Collection[Mob]): util.Collection[Mob] def filterDetectedMobs(mobs: Iterable[Mob]): Iterable[Mob] = if (remainingTicks > 0) {
def filterDetectedMobs(mobs: util.Collection[Mob]): util.Collection[Mob] = if (remainingTicks > 0) {
remainingTicks -= 1 remainingTicks -= 1
new util.ArrayList[Mob] Nil
} }
else { else {
remainingTicks = getPeriod remainingTicks = getPeriod
_filterDetectedMobs(mobs) _filterDetectedMobs(mobs)
} }
private[towers] def _filterDetectedMobs(mobs: Iterable[Mob]): Iterable[Mob]
def getX: Int = x def getX: Int = x
def getY: Int = y def getY: Int = y