Skip to content

Instantly share code, notes, and snippets.

View johansjoberg's full-sized avatar

Johan Sjöberg johansjoberg

View GitHub Profile
@johansjoberg
johansjoberg / MoreTest.scala
Created August 31, 2012 19:11
Possible solution for GIST 3517707 on url https://gist.github.com/3517707 Uses abstract types and overrides the type in the subclass
class BaseElement {
def foo() { println("I like foo") }
}
class SubElement extends BaseElement {
def bar() { println("I like bar") }
}
trait Base {
//Abstract type T must be BaseElement or a subclass of BaseElement
@johansjoberg
johansjoberg / MoreTest.scala
Created August 31, 2012 19:02
Possible solution to GIST 3517707 in https://gist.github.com/3517707
class BaseElement {
def foo() { println("I like foo") }
}
class SubElement extends BaseElement {
def bar() { println("I like bar") }
}
trait Base[T <: BaseElement] {
var map = Map[Int, T]()
@johansjoberg
johansjoberg / subpub
Created March 2, 2012 19:03
Why "type mismatch; found : Cow required: world.Sub" at world.subscribe(cow)?
package org.wololo.wastelands.core.fgfd
import scala.collection.mutable.Publisher
import scala.collection.mutable.Subscriber
object Main extends App {
override def main(args: Array[String]) {
val world = new World
@johansjoberg
johansjoberg / UnitAction
Created February 5, 2012 16:25
En typ av lösning
trait UnitAction {
def performAction(myself: Unit) {
//do nothing
}
}
object Idle extends UnitAction
class Moving(val from: Coordinate, val to: Coordinate) extends UnitAction {
override def performAction(myself: Unit){
if(myself.at(to)) {
@johansjoberg
johansjoberg / Game
Created January 6, 2012 23:52
option refactoring
def click(x: Int, y: Int) {
val mx = screen.calculateTileIndex(screen.sx+x)
val my = screen.calculateTileIndex(screen.sy+y)
val tile = map.tiles(mx,my)
performPossibleUnitAction(tile.unit, mx, my)
}
private def performPossibleUnitAction(tileUnit:Option[Unit], mx: Int, my: Int) {
(tileUnit, selectedUnit) match {
@johansjoberg
johansjoberg / Game
Created January 6, 2012 22:43
Testing... tailrec optimization with annotation
@tailrec
def tileSizeCalc(f: Int): Int = {
val tileSize = math.pow(2, f)
if (graphicsContext.screenWidth / tileSize < 22)
math.pow(2, f).toInt
else
tileSizeCalc(f+1)
}