summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorColin Okay <okay@toyful.space>2021-02-16 07:39:05 -0600
committerColin Okay <okay@toyful.space>2021-02-16 07:39:05 -0600
commit8ef33a2973303779ef2d36dea13aeb170877e611 (patch)
treeba45c4cd40c2b97760af962cb5f5b4122516e018
parent1b1a7f6600fe7ffbdb4eeeda7a2b856270b62844 (diff)
wiggler intersection
-rw-r--r--Source/Main.hx52
-rw-r--r--project.xml2
2 files changed, 51 insertions, 3 deletions
diff --git a/Source/Main.hx b/Source/Main.hx
index 8103d6a..864e1a9 100644
--- a/Source/Main.hx
+++ b/Source/Main.hx
@@ -115,6 +115,8 @@ class Wiggler extends Sprite
static inline var radiusGradient:Float = 5.0;
static inline var radiiSizes:Int = 15;
+ public static var allWigglers:Array<Wiggler> = [];
+
var path:Array<Point> = [];
var circles:Array<ColoredCircle> = [];
@@ -133,9 +135,37 @@ class Wiggler extends Sprite
drift = {x: Math.random() * Util.randomSign(), y: Math.random() * Util.randomSign()};
addEventListener(Event.ENTER_FRAME, perFrame);
- //render();
+
+ addEventListener(Event.ADDED_TO_STAGE, (e) -> Wiggler.allWigglers.push(this));
}
+ var thisP0 = new Point();
+ var thisP1 = new Point();
+ var otherP0 = new Point();
+ var otherP1 = new Point();
+
+ public function intersects( other:Wiggler )
+ {
+ if (this == other) return false;
+
+ for (i in 1...path.length)
+ for (j in 1... other.path.length)
+ {
+ thisP0.x = path[i-1].x + this.x;
+ thisP0.y = path[i-1].y + this.y;
+ thisP1.x = path[i].x + this.x;
+ thisP1.y = path[i].y + this.y;
+
+ otherP0.x = other.path[j-1].x + other.x;
+ otherP0.y = other.path[j-1].y + other.y;
+ otherP1.x = other.path[j].x + other.x;
+ otherP1.y = other.path[j].y + other.y;
+
+ if (Util.segmentsIntersect(thisP0,thisP1, otherP0,otherP1))
+ return true;
+ }
+ return false;
+ }
// A circle is valid if it is contained within the boundary of the
// path and if it does not intersect any other circles.
function isValidCircle( circ:ColoredCircle ): Bool
@@ -412,6 +442,22 @@ function perFrame (e)
drift.x *= -1;
if (this.y <= 0 || this.y + this.height >= stage.stageHeight)
drift.y *= -1;
+
+ for (wiggler in Wiggler.allWigglers)
+ if (wiggler != this && this.intersects( wiggler) )
+ bounceOff( wiggler );
+ }
+
+ function bounceOff(other: Wiggler)
+ {
+ var tmp = other.drift;
+ other.drift = this.drift;
+ this.drift = tmp;
+
+ this.x += drift.x;
+ this.y += drift.y;
+ other.x += other.drift.x;
+ other.y += other.drift.y;
}
static inline var ONE_DEGREE: Float = 0.01745329;
@@ -434,7 +480,7 @@ class DrawingScreen extends Sprite
suitable for passing in as the "skin" of a wiggler. */
var path: Array<Point> = [];
- var wigglers:Array<Wiggler> = [];
+
var candidateWiggler:Wiggler;
@@ -485,7 +531,6 @@ class DrawingScreen extends Sprite
if (candidateWiggler != null)
{
//removeChild( candidateWiggler );
- wigglers.push(candidateWiggler);
candidateWiggler = null;
}
graphics.clear();
@@ -505,6 +550,7 @@ class DrawingScreen extends Sprite
function onMouseUp (e)
{
drawing = false;
+ graphics.clear();
if (!holdPath) fadeAndRefresh();
}
diff --git a/project.xml b/project.xml
index b14d5f6..3578a22 100644
--- a/project.xml
+++ b/project.xml
@@ -1,5 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<project>
+ <window fps="30"/>
+
<meta title="WiggleWorldApp"
package="space.toyful.wiggleworld"
version="1.0.0"