Permalink
Browse files

updated IntersectablePolygon to to make it movable, added more functi…

…onality to Sparkles, updated TestLevel, added StaticPolygonTest to test StaticPolygon motion
  • Loading branch information...
1 parent 3c67c55 commit 233857388a0e44dee147447c460354578f1cbb0c Andrey Borunov committed Apr 10, 2012
View
51 blases/src/main/scala/net/scage/blases/IntersectablePolygon.scala
@@ -4,14 +4,14 @@ import net.scage.support.Vec
import net.scage.blases.Blases._
trait IntersectablePolygon {
- def vertices: List[Vec]
+ def intersectableVertices: List[Vec]
- private lazy val vertices_zipped = if (vertices.length >= 2) {
+ private def verticesZipped(vertices: List[Vec]) = if(vertices.length >= 2) {
val vertices_shift = vertices.last :: vertices.init
vertices_shift.zip(vertices)
} else List[(Vec, Vec)]()
- def containsCoord(coord: Vec): Boolean = {
+ private def containsCoord(coord: Vec, vertices: List[Vec]): Boolean = {
def _areLinesIntersect(a: Vec, b: Vec, c: Vec, d: Vec): Boolean = {
val common = (b.x - a.x) * (d.y - c.y) - (b.y - a.y) * (d.x - c.x)
if (common == 0) false
@@ -30,36 +30,43 @@ trait IntersectablePolygon {
else {
val a = coord
val b = Vec(Integer.MAX_VALUE, coord.y)
- val intersections = vertices_zipped.foldLeft(0) {
+ val intersections = verticesZipped(vertices).foldLeft(0) {
case (result, (c, d)) => if (_areLinesIntersect(a, b, c, d)) result + 1 else result
}
intersections % 2 != 0
}
}
- private lazy val (min_x, max_x, min_y, max_y) = vertices.map(vertice => tracer.point(vertice)).foldLeft((tracer.N_x, 0, tracer.N_y, 0)) {
- case ((current_min_x, current_max_x, current_min_y, current_max_y), vertice) =>
- val new_min_x = math.min(current_min_x, vertice.ix)
- val new_max_x = math.max(current_max_x, vertice.ix)
- val new_min_y = math.min(current_min_y, vertice.iy)
- val new_max_y = math.max(current_max_y, vertice.iy)
- (new_min_x, new_max_x, new_min_y, new_max_y)
- }
+ private def verticesPoints(vertices: List[Vec]) = {
+ val (min_x, max_x, min_y, max_y) = vertices.map(vertice => tracer.point(vertice)).foldLeft((tracer.N_x, 0, tracer.N_y, 0)) {
+ case ((current_min_x, current_max_x, current_min_y, current_max_y), vertice) =>
+ val new_min_x = math.min(current_min_x, vertice.ix)
+ val new_max_x = math.max(current_max_x, vertice.ix)
+ val new_min_y = math.min(current_min_y, vertice.iy)
+ val new_max_y = math.max(current_max_y, vertice.iy)
+ (new_min_x, new_max_x, new_min_y, new_max_y)
+ }
- lazy val polygon_points = (for {
- i <- min_x to max_x
- j <- min_y to max_y
- coord = tracer.pointCenter(Vec(i, j))
- if containsCoord(coord)
- } yield List(Vec(i, j), Vec(i-1, j), Vec(i, j-1), Vec(i+1, j), Vec(i, j+1))).flatten.toSet
+ (for {
+ i <- min_x to max_x
+ j <- min_y to max_y
+ coord = tracer.pointCenter(Vec(i, j))
+ if containsCoord(coord, vertices)
+ } yield List(Vec(i, j), Vec(i-1, j), Vec(i, j-1), Vec(i+1, j), Vec(i, j+1))).flatten.toSet
+ }
- def blasesInside = polygon_points.map(point => tracer.tracesInPoint(point)).flatten.filter(trace => containsCoord(trace.location))
+ def containsCoord(coord: Vec):Boolean = containsCoord(coord, intersectableVertices)
+ def blasesInside = {
+ def _blasesInside(vertices:List[Vec]) = verticesPoints(vertices).map(point => tracer.tracesInPoint(point)).flatten.filter(trace => containsCoord(trace.location, vertices))
+ _blasesInside(intersectableVertices)
+ }
def forEachBlaseInside(func:Blase => Any) {
- for {
- point <- polygon_points
+ def _forEachBlaseInside(vertices:List[Vec]) = for {
+ point <- verticesPoints(vertices)
blases = tracer.tracesInPoint(point)
blase <- blases
- if containsCoord(blase.location)
+ if containsCoord(blase.location, vertices)
} func(blase)
+ _forEachBlaseInside(intersectableVertices)
}
}
View
11 blases/src/main/scala/net/scage/blases/levelparts/MovingObstacle.scala
@@ -1,13 +1,8 @@
package net.scage.blases.levelparts
-/**
- * Created by IntelliJ IDEA.
- * User: andrey
- * Date: 06.04.12
- * Time: 16:43
- * To change this template use File | Settings | File Templates.
- */
+import net.scage.support.Vec
+import net.scage.support.physics.objects.StaticPolygon
-class MovingObstacle {
+class MovingObstacle(vertices:List[Vec], start_coord: Vec, end_coord:Vec, val speed:Int) extends StaticPolygon(vertices) {
}
View
5 blases/src/main/scala/net/scage/blases/levelparts/SimpleObstacle.scala
@@ -7,7 +7,8 @@ import net.scage.ScageLib._
import net.scage.blases.Relatives._
import net.scage.blases.{IntersectablePolygon, Blase}
-class SimpleObstacle(vertices: Vec*) extends StaticPolygon(vertices:_*) with IntersectablePolygon {
+class SimpleObstacle(obstacle_vertices: Vec*) extends StaticPolygon(obstacle_vertices:_*) with IntersectablePolygon {
+ val intersectableVertices = obstacle_vertices.toList
physics.addPhysicals(this)
private val action_id = action {
@@ -21,7 +22,7 @@ class SimpleObstacle(vertices: Vec*) extends StaticPolygon(vertices:_*) with Int
}
}
- val control_id = leftMouse(onBtnDown = {m => forEachBlaseInside {blase => blase.burst(); blases_shot -= 1}})
+ private val control_id = leftMouse(onBtnDown = {m => forEachBlaseInside {blase => blase.burst(); blases_shot -= 1}})
private val render_id = render {
drawPolygon(points, rColor(WHITE))
View
72 blases/src/main/scala/net/scage/blases/levelparts/Sparkles.scala
@@ -4,39 +4,69 @@ import net.scage.support.Vec
import net.scage.blases.IntersectablePolygon
import net.scage.blases.Blases._
import net.scage.ScageLib._
+import collection.mutable.ArrayBuffer
class Sparkles(from:Vec, to:Vec, height:Int, active_period:Int, inactive_period:Int) extends IntersectablePolygon {
- val vertices = List(from+Vec(0, height/2), to+Vec(0, height/2), to+Vec(0, -height/2), from+Vec(0, -height/2))
+ val intersectableVertices = List(from+Vec(0, height/2), to+Vec(0, height/2), to+Vec(0, -height/2), from+Vec(0, -height/2))
+
+ private val sparkle_box = displayList {
+ val ang = 360 - ((to - from).deg(Vec(0,1)) - 90).toFloat
+ openglLocalTransform {
+ openglMove(from)
+ openglRotate(ang)
+ drawRectCentered(Vec.zero, 10, height, WHITE)
+ }
+ openglLocalTransform {
+ openglMove(to)
+ openglRotate(ang)
+ drawRectCentered(Vec.zero, 10, height, WHITE)
+ }
+ }
+
+ def sparkleLine(from:Vec, to:Vec) = {
+ val line = to - from
+ val step_n = line.n.rotate(math.Pi/2)
+ val result = ArrayBuffer(from, from+line*0.05f)
+ var side = 1
+ var point = 0.05f
+ while(point < 0.95f) {
+ point += math.min((math.random*0.05).toFloat, 0.95f)
+ result ++= Array(result.last, (from + line*point + step_n*(math.random*20).toFloat*side))
+ side *= -1
+ }
+ result ++= Array(result.last, to)
+ result.toList
+ }
+ private var sparkle_line = sparkleLine(from, to)
private val render_id = render {
- drawRectCentered(from, 10, height, WHITE)
- drawRectCentered(to, 10, height, WHITE)
- drawLines(List(from+Vec(0, height/2), to+Vec(0, height/2), from+Vec(0, -height/2), to+Vec(0, -height/2)), RED)
+ drawDisplayList(sparkle_box, Vec.zero)
+ if(is_active) drawLines(sparkle_line, RED)
}
private var is_active = false
private var start = System.currentTimeMillis
- def inactive() {
- is_active = false
- start = System.currentTimeMillis
- action {
- if(System.currentTimeMillis - start > inactive_period) {
- active()
- deleteSelf()
+ private val action_id = action {
+ if(is_active) {
+ forEachBlaseInside {blase =>
+ blase.burst()
+ score_for_level -= 100
+ new FlyingWord(-100, YELLOW, blase.location, blase.velocity)
}
- }
- }
-
- def active() {
- is_active = true
- start = System.currentTimeMillis
- action {
if(System.currentTimeMillis - start > active_period) {
- inactive()
- deleteSelf()
+ is_active = false
+ start = System.currentTimeMillis
+ }
+ } else {
+ if(System.currentTimeMillis - start > inactive_period) {
+ is_active = true
+ start = System.currentTimeMillis
+ sparkle_line = sparkleLine(from, to)
}
}
}
-
+ clear {
+ delOperations(action_id, render_id, currentOperation)
+ }
}
View
6 blases/src/main/scala/net/scage/blases/levelparts/SpeedPolygon.scala
@@ -7,7 +7,7 @@ import net.scage.blases.Blases._
import net.scage.blases.Relatives._
import net.scage.blases.{IntersectablePolygon, Blase}
-class SpeedPolygon(val vertices: List[Vec], direction: Vec) extends IntersectablePolygon {
+class SpeedPolygon(val intersectableVertices: List[Vec], direction: Vec) extends IntersectablePolygon {
private val dir = direction.n * rInt(200)
/*{
@@ -59,14 +59,14 @@ class SpeedPolygon(val vertices: List[Vec], direction: Vec) extends Intersectabl
}
}
- private val avg = Vec(vertices.map(_.x).sum/vertices.length, vertices.map(_.y).sum/vertices.length)
+ private val avg = Vec(intersectableVertices.map(_.x).sum/intersectableVertices.length, intersectableVertices.map(_.y).sum/intersectableVertices.length)
private val arrow = List(avg + dir.n*rInt(60),
avg - dir.n*rInt(60),
avg + dir.n*rInt(60) - dir.n.rotateDeg(30)*rInt(30),
avg + dir.n*rInt(60) - dir.n.rotateDeg(-30)*rInt(30))
private val render_id = render {
- drawPolygon(vertices, rColor(BLUE))
+ drawPolygon(intersectableVertices, rColor(BLUE))
drawLines(arrow(0), arrow(1), arrow(0), arrow(2), arrow(0), arrow(3))
}
View
6 blases/src/main/scala/net/scage/blases/levelparts/Star.scala
@@ -7,7 +7,7 @@ import net.scage.blases.Relatives._
import net.scage.blases.IntersectablePolygon
class Star(coord: Vec, num_beams: Int = 5, radius1: Int = 60, radius2: Int = 30) extends IntersectablePolygon {
- val vertices = {
+ val intersectableVertices = {
val radius = Array(radius1, radius2)
(for {
i <- 0 until num_beams * 2
@@ -16,7 +16,7 @@ class Star(coord: Vec, num_beams: Int = 5, radius1: Int = 60, radius2: Int = 30)
}
private val action_id = action {
- val blases_touch = tracer.tracesNearCoord(coord, -1 to 1).filter(blase => containsCoord(blase.location))
+ val blases_touch = blasesInside
if(!blases_touch.isEmpty) {
score_for_level += 300
new FlyingWord(300, YELLOW, coord, blases_touch.head.velocity)
@@ -26,7 +26,7 @@ class Star(coord: Vec, num_beams: Int = 5, radius1: Int = 60, radius2: Int = 30)
}
private val render_id = render {
- drawPolygon(vertices, rColor(YELLOW))
+ drawPolygon(intersectableVertices, rColor(YELLOW))
}
private val clear_id:Int = clear {
View
5 blases/src/main/scala/net/scage/blases/levels/TestLevel.scala
@@ -1,14 +1,15 @@
package net.scage.blases.levels
import net.scage.blases.Level
-import net.scage.blases.levelparts.MovingSpikes
import net.scage.support.Vec
import net.scage.ScageLib._
import net.scage.blases.Relatives._
+import net.scage.blases.levelparts.{Sparkles, MovingSpikes}
object TestLevel extends Level {
def constructLevel() {
- new MovingSpikes(Vec(100, windowHeight/4), Vec(100, 3*windowHeight/4), 1)
+ new MovingSpikes(rVec(100, 576), rVec(250, 192), 1)
+ new Sparkles(rVec(300, 576), rVec(500, 192), 40, 1000, 3000)
}
def startCoord = rVec(134, 52)
View
46 blases/src/main/scala/net/scage/blases/tests/StaticPolygonTest.scala
@@ -0,0 +1,46 @@
+package net.scage.blases.tests
+
+import net.scage.ScageScreenApp
+import net.scage.support.Vec
+import net.scage.ScageLib._
+import net.scage.support.physics.ScagePhysics
+import net.scage.support.physics.objects.{DynaBall, StaticPolygon}
+
+object StaticPolygonTest extends ScageScreenApp("Static Polygon Test", 640, 480) {
+ val physics = new ScagePhysics()
+ action {
+ physics.step()
+ }
+
+ val pew = physics.addPhysical(new StaticPolygon(Vec(100, 300), Vec(150, 250), Vec(300, 300), Vec(300, 450), Vec(200, 400)) {
+ render {
+ drawPolygon(points, RED)
+ drawFilledCircle(coord, 3, YELLOW)
+ }
+ })
+
+ key(KEY_LEFT, onKeyDown = pew.coord += Vec(10,0))
+ key(KEY_RIGHT, onKeyDown = pew.coord += Vec(-10,0))
+ key(KEY_UP, onKeyDown = pew.coord += Vec(0,10))
+ key(KEY_DOWN, onKeyDown = pew.coord += Vec(0,-10))
+
+ private var start_coord = Vec.zero
+ private var is_mouse_drag = false
+ leftMouse(onBtnDown = {m => start_coord = m; is_mouse_drag = true}, onBtnUp = {m =>
+ val init_velocity = (m - start_coord).n*10
+ physics.addPhysical(new DynaBall(start_coord, 10) {
+ velocity = init_velocity
+ render {
+ drawCircle(coord, 10, YELLOW)
+ }
+ })
+ is_mouse_drag = false
+ })
+
+ private var drag_coord = Vec.zero
+ leftMouseDrag(onDrag = {m => drag_coord = m})
+
+ render {
+ if(is_mouse_drag) drawLine(start_coord, drag_coord, YELLOW)
+ }
+}

0 comments on commit 2338573

Please sign in to comment.