Skip to content
Browse files

Refactored color.

  • Loading branch information...
1 parent c1ec97d commit dd2bbfb465a5c6fadbb26da9c23e0b1e7ca2c040 @fversnel committed May 12, 2012
View
24 src/main/scala/org/frankversnel/poortjes/common/color.scala
@@ -1,21 +1,23 @@
package org.frankversnel.poortjes
-trait Color extends Component {
- val color: ColorValue
-}
-
-case class ColorValue private(r: Int, g: Int, b: Int, a: Int) {
+case class Color private(r: Int, g: Int, b: Int, a: Int) {
require(inRange(r), "red value was out of range 0-255, value was: " + r)
require(inRange(g), "green value was out of range 0-255, value was: " + g)
require(inRange(b), "blue value was out of range 0-255, value was: " + b)
require(inRange(a), "alpha value was out of range 0-255, value was: " + a)
private def inRange(value: Int): Boolean = value >= 0 && value <= 255
- def r(value: Int): ColorValue = ColorValue(value, g, b, a)
- def g(value: Int): ColorValue = ColorValue(r, value, b, a)
- def b(value: Int): ColorValue = ColorValue(r, g, value, a)
- def a(value: Int): ColorValue = ColorValue(r, g, b, value)
+ def r(value: Int): Color = Color(value, g, b, a)
+ def g(value: Int): Color = Color(r, value, b, a)
+ def b(value: Int): Color = Color(r, g, value, a)
+ def a(value: Int): Color = Color(r, g, b, value)
}
-object ColorValue {
- def apply(): ColorValue = ColorValue(0, 0, 0, 255)
+object Color {
+ def apply(): Color = Color(0, 0, 0, 255)
+
+ val red = Color().r(255)
+ val green = Color().g(255)
+ val blue = Color().b(255)
+ val white = Color().r(255).g(255).b(255)
+ val black = Color().r(0).g(0).b(0)
}
View
7 src/main/scala/org/frankversnel/poortjes/game/gameobjects/explosion.scala
@@ -7,9 +7,8 @@ import org.frankversnel.poortjes.collision.CollidableCircle
import org.frankversnel.poortjes.resource_loading.ResourceLoader
import org.frankversnel.poortjes.util.DeltaTime
-class Explosion extends Drawable with Color with CollidableCircle
- with TimeBasedLife {
- val color = ColorValue().r(255).a(125)
+class Explosion extends Drawable with CollidableCircle with TimeBasedLife {
+ val color = Color.red.a(125)
var dimension = DimensionValue().width(0).height(0)
protected val maxTimeAliveMillis = 500L
@@ -34,6 +33,6 @@ class Explosion extends Drawable with Color with CollidableCircle
}
override def draw(renderer: Renderer) {
- renderer.drawCircle(this)
+ renderer.drawCircle(this, color)
}
}
View
8 src/main/scala/org/frankversnel/poortjes/game/gameobjects/score.scala
@@ -4,7 +4,7 @@ import org.slf4j.scala.Logging
import org.frankversnel.poortjes.Component
import org.frankversnel.poortjes.DimensionValue
-import org.frankversnel.poortjes.ColorValue
+import org.frankversnel.poortjes.Color
import org.frankversnel.poortjes.rendering.Drawable
import org.frankversnel.poortjes.rendering.Renderer
@@ -15,7 +15,7 @@ class Score extends Component with Drawable with Logging {
def multiplier = _multiplier
def value = _value
- val color = ColorValue().r(255).g(255).b(255)
+ val textColor = Color.white
var dimension = DimensionValue()
def incrementMultiplier = {
@@ -28,7 +28,7 @@ class Score extends Component with Drawable with Logging {
}
override def draw(renderer: Renderer) {
- renderer.drawText(_value.toString, color, 10, 20)
- renderer.drawText("x" + _multiplier, color, 10, 30)
+ renderer.drawText(_value.toString, textColor, 10, 20)
+ renderer.drawText("x" + _multiplier, textColor, 10, 30)
}
}
View
2 src/main/scala/org/frankversnel/poortjes/game/gameobjects/shepherd_spawner.scala
@@ -8,7 +8,7 @@ import org.frankversnel.poortjes.resource_loading.ResourceLoader
class ShepherSpawner(private val spawnArea: SpawnArea,
private val resourceLoader: ResourceLoader) extends Component {
private val SpawnIntervalInSeconds = 10f
- private val MaxNumberOfShepherds = 30
+ private val MaxNumberOfShepherds = 100
private val MaxNumberOfShepherdsAtOnce = 5
private var spawnTime = SpawnIntervalInSeconds
View
16 src/main/scala/org/frankversnel/poortjes/game/poortjes_slick.scala
@@ -16,24 +16,14 @@ import org.frankversnel.poortjes.game.gameobjects._
class PoortjesSlick extends BasicGame("Poortjes") {
- private var renderer: Renderer = null
- private var renderable: Transform with Color = null
-
- private var resourceLoader: SlickImageLoader = null
- private var newPlayer: Player with SlickKeyboardAdapter = null
-
override def init(container: GameContainer) {
container.getGraphics().setBackground(org.newdawn.slick.Color.black);
- resourceLoader = new SlickImageLoader
- renderer = new Slick2DRenderer(container.getGraphics(), resourceLoader)
+ val resourceLoader = new SlickImageLoader
+ val renderer = new Slick2DRenderer(container.getGraphics(), resourceLoader)
EntityManager.initialize(renderer)
- newPlayer = new Player(resourceLoader) with SlickKeyboardAdapter {
- //speed
- override val distanceInMs = 0.09f
- override val rotationInMs = 0.010f
-
+ val newPlayer = new Player(resourceLoader) with SlickKeyboardAdapter {
val shape = resourceLoader.addResource("ship-green.svg")
val keybindings = KeyboardBindings('w', 's', 'a', 'd')
View
2 src/main/scala/org/frankversnel/poortjes/rendering/drawable_shape.scala
@@ -9,6 +9,6 @@ trait DrawableShape extends Drawable {
protected val shape: ResourceId
override def draw(renderer: Renderer) {
- renderer.drawShape(shape, this)
+ renderer.drawShape(this, shape)
}
}
View
28 src/main/scala/org/frankversnel/poortjes/rendering/processing_2d_renderer.scala
@@ -11,35 +11,35 @@ class Processing2DRenderer(
private val shapeLoader: ProcessingShapeLoader,
private val backgroundColor: Int) extends Renderer {
- def drawText(text: String, color: ColorValue, x: Int, y: Int) {
+ def drawText(text: String, color: Color, x: Int, y: Int) {
fill(color)
graphics.text(text, x, y)
}
- def drawRectangle(component: Drawable with Color) {
- draw(component) {
- graphics.rect(0, 0, component.dimension.width, component.dimension.height)
+ def drawRectangle(transform: Transform, color: Color) {
+ draw(transform, color) {
+ graphics.rect(0, 0, transform.dimension.width, transform.dimension.height)
}
}
- def drawCircle(component: Drawable with Color) {
- draw(component) {
- graphics.ellipse(0, 0, component.dimension.width, component.dimension.height)
+ def drawCircle(transform: Transform, color: Color) {
+ draw(transform, color) {
+ graphics.ellipse(0, 0, transform.dimension.width, transform.dimension.height)
}
}
- def drawShape(resourceId: ResourceId, component: Drawable) {
- drawTransform(component) {
+ def drawShape(transform: Transform, resourceId: ResourceId) {
+ drawTransform(transform) {
graphics.shape(shapeLoader.getResource(resourceId),
- 0, 0, component.dimension.width, component.dimension.height)
+ 0, 0, transform.dimension.width, transform.dimension.height)
}
}
def clearScreen = graphics.background(backgroundColor)
- private def draw(component: Drawable with Color) (drawFunction: => Unit) {
- fill(component.color)
- drawTransform(component)(drawFunction)
+ private def draw(transform: Transform, color: Color) (drawFunction: => Unit) {
+ fill(color)
+ drawTransform(transform)(drawFunction)
}
private def drawTransform(transform: Transform) (drawFunction: => Unit) {
@@ -52,7 +52,7 @@ class Processing2DRenderer(
graphics.resetMatrix
}
- private def fill(color: ColorValue) {
+ private def fill(color: Color) {
graphics.fill(color.r, color.g, color.b, color.a)
}
}
View
8 src/main/scala/org/frankversnel/poortjes/rendering/renderer.scala
@@ -4,10 +4,10 @@ import org.frankversnel.poortjes._
import org.frankversnel.poortjes.resource_loading.ResourceId
trait Renderer {
- def drawText(text: String, color: ColorValue, x: Int, y: Int)
- def drawRectangle(component: Drawable with Color)
- def drawCircle(component: Drawable with Color)
- def drawShape(resourceId: ResourceId, component: Drawable)
+ def drawText(text: String, color: Color, x: Int, y: Int)
+ def drawRectangle(component: Transform, color: Color)
+ def drawCircle(component: Transform, color: Color)
+ def drawShape(transform: Transform, resourceId: ResourceId)
def clearScreen
}
View
23 src/main/scala/org/frankversnel/poortjes/rendering/slick_2d_renderer.scala
@@ -12,19 +12,21 @@ class Slick2DRenderer(
private val graphics: Graphics,
private val shapeLoader: SlickImageLoader) extends Renderer {
- def drawText(text: String, color: ColorValue, x: Int, y: Int) {
+ def drawText(text: String, color: Color, x: Int, y: Int) {
throw new RuntimeException("TODO Implement this method")
}
- def drawRectangle(component: Drawable with Color) {
- drawShapeWithColor(new Rectangle(0, 0, component.dimension.width, component.dimension.height), component)
+ def drawRectangle(component: Transform, color: Color) {
+ drawShapeWithColor(new Rectangle(0, 0, component.dimension.width,
+ component.dimension.height), component, color)
}
- def drawCircle(component: Drawable with Color) {
- drawShapeWithColor(new Ellipse(0, 0, component.dimension.width / 2, component.dimension.height / 2), component)
+ def drawCircle(component: Transform, color: Color) {
+ drawShapeWithColor(new Ellipse(0, 0, component.dimension.width / 2,
+ component.dimension.height / 2), component, color)
}
- def drawShape(resourceId: ResourceId, component: Drawable) {
+ def drawShape(component: Transform, resourceId: ResourceId) {
val shape = new Rectangle(0, 0, component.dimension.width, component.dimension.height)
drawShape(shape, component) { transformedShape =>
val image = shapeLoader.getResource(resourceId)
@@ -35,9 +37,8 @@ class Slick2DRenderer(
def clearScreen = { /* No need to implement this method */ }
- private def drawShapeWithColor(shape: Shape, component: Drawable with Color) {
- graphics.setColor(new org.newdawn.slick.Color(component.color.r, component.color.g,
- component.color.b, component.color.a))
+ private def drawShapeWithColor(shape: Shape, component: Transform, color: Color) {
+ fill(color)
drawShape(shape, component) { transformedShape =>
graphics.fill(transformedShape)
}
@@ -48,4 +49,8 @@ class Slick2DRenderer(
drawFunction(transformedShape)
}
+ private def fill(color: Color) {
+ graphics.setColor(new org.newdawn.slick.Color(color.r, color.g, color.b, color.a))
+ }
+
}

0 comments on commit dd2bbfb

Please sign in to comment.
Something went wrong with that request. Please try again.