This repository was archived by the owner on May 20, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLinkPoint.as
More file actions
137 lines (112 loc) · 3.24 KB
/
LinkPoint.as
File metadata and controls
137 lines (112 loc) · 3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package ui
{
import flash.display.BlendMode;
import flash.display.CapsStyle;
import flash.display.JointStyle;
import flash.display.LineScaleMode;
import flash.display.Shape;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.ColorTransform;
import flash.geom.Rectangle;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;
import flash.geom.Vector3D;
/**
* ...
* @author Mark Wonnacott
*/
public class LinkPoint extends Sprite
{
private var _app:SfxrApp;
private var _individual:Individual;
private var _update:Function;
private var _linked:Shape;
private var _unlinked:Shape;
private var _links:Shape;
public function get linked():Boolean
{
return (_individual != null);
}
public function get individual():Individual { return _individual; }
public function set individual(value:Individual):void
{
_individual = value;
redrawLinks();
_update(this, _individual != null);
}
public function LinkPoint(app:SfxrApp, update:Function)
{
_app = app;
_update = update;
_linked = drawRect(0, 0x807060, false);
_unlinked = drawRect(0, 0x807060, true);
addChild(_linked);
_links = new Shape();
addChild(_links);
_app.addLinkListener(this.onLink);
}
public function play():void
{
if (linked) {
_individual.synth.play();
}
}
private function onLink(individual:Individual, x:int, y:int):void
{
if (new Rectangle(this.x, this.y, 16, 16).contains(x, y))
{
if (this.individual != null) {
delete this.individual._connects[this];
}
if (individual != this.individual) {
this.individual = individual;
this.individual._connects[this] = true;
} else {
this.individual = null;
}
}
}
public function unlink(individual:Individual):void
{
this.individual = null;
}
private function drawRect(borderColour:uint, fillColour:uint, linked:Boolean):Shape
{
var rect:Shape = new Shape();
rect.graphics.lineStyle(1, borderColour, 1, true);
rect.graphics.beginFill(fillColour, 1);
rect.graphics.drawRect(0, 0, 16, 16);
rect.graphics.drawCircle(8.5, 8.5, 4);
rect.graphics.endFill();
return rect;
}
public function onDrawFrame(e:Event):void
{
redrawLinks();
}
private function redrawLinks():void
{
removeChild(_links);
_links = new Shape();
addChild(_links);
if (_individual == null) { return; }
var start:Vector3D = new Vector3D(8.5, 8.5, 0);
var end:Vector3D = new Vector3D(_individual.x - x, _individual.y - y, 0);
var vector:Vector3D = end.subtract(start);
vector.normalize();
vector.scaleBy(_individual.radius);
//start.incrementBy(vector);
end.decrementBy(vector);
_links.graphics.moveTo(start.x, start.y);
_links.graphics.lineStyle(1, 0x000000, 1, true, LineScaleMode.NORMAL, CapsStyle.SQUARE, JointStyle.MITER);
_links.graphics.lineTo(end.x, end.y);
_links.graphics.beginFill(0xF0C090, 1);
_links.graphics.drawCircle(Math.floor(start.x) + 0.5, Math.floor(start.y) + 0.5, 4);
_links.graphics.drawCircle(Math.floor(end.x) + 0.5, Math.floor(end.y) + 0.5, 4);
_links.graphics.endFill();
}
}
}