Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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

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

0 notes on commit 2338573

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