Permalink
Browse files

Initial revision

  • Loading branch information...
jaz303 committed Nov 18, 2010
0 parents commit 848986d7cdd0b014000e1d952df9628f1621f450
@@ -0,0 +1,2 @@
+*.swf
+.DS_Store
@@ -0,0 +1,112 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
+<plist version="1.0">
+<dict>
+ <key>currentDocument</key>
+ <string>src/FOV.as</string>
+ <key>documents</key>
+ <array>
+ <dict>
+ <key>expanded</key>
+ <true/>
+ <key>name</key>
+ <string>FOV</string>
+ <key>regexFolderFilter</key>
+ <string>!.*/(\.[^/]*|CVS|_darcs|_MTN|\{arch\}|blib|.*~\.nib|.*\.(framework|app|pbproj|pbxproj|xcode(proj)?|bundle))$</string>
+ <key>sourceDirectory</key>
+ <string></string>
+ </dict>
+ </array>
+ <key>fileHierarchyDrawerWidth</key>
+ <integer>331</integer>
+ <key>metaData</key>
+ <dict>
+ <key>src/FOV.as</key>
+ <dict>
+ <key>caret</key>
+ <dict>
+ <key>column</key>
+ <integer>14</integer>
+ <key>line</key>
+ <integer>56</integer>
+ </dict>
+ <key>firstVisibleColumn</key>
+ <integer>0</integer>
+ <key>firstVisibleLine</key>
+ <integer>0</integer>
+ </dict>
+ <key>src/Thing.as</key>
+ <dict>
+ <key>caret</key>
+ <dict>
+ <key>column</key>
+ <integer>61</integer>
+ <key>line</key>
+ <integer>32</integer>
+ </dict>
+ <key>firstVisibleColumn</key>
+ <integer>0</integer>
+ <key>firstVisibleLine</key>
+ <integer>0</integer>
+ </dict>
+ <key>src/uk/co/jasonframe/demo_box/Demo.as</key>
+ <dict>
+ <key>caret</key>
+ <dict>
+ <key>column</key>
+ <integer>25</integer>
+ <key>line</key>
+ <integer>58</integer>
+ </dict>
+ <key>firstVisibleColumn</key>
+ <integer>0</integer>
+ <key>firstVisibleLine</key>
+ <integer>24</integer>
+ </dict>
+ <key>src/uk/co/jasonframe/math/Vec2.as</key>
+ <dict>
+ <key>caret</key>
+ <dict>
+ <key>column</key>
+ <integer>78</integer>
+ <key>line</key>
+ <integer>143</integer>
+ </dict>
+ <key>firstVisibleColumn</key>
+ <integer>0</integer>
+ <key>firstVisibleLine</key>
+ <integer>0</integer>
+ </dict>
+ </dict>
+ <key>openDocuments</key>
+ <array>
+ <string>src/FOV.as</string>
+ <string>src/Thing.as</string>
+ <string>src/uk/co/jasonframe/demo_box/Demo.as</string>
+ <string>src/uk/co/jasonframe/math/Vec2.as</string>
+ </array>
+ <key>shellVariables</key>
+ <array>
+ <dict>
+ <key>enabled</key>
+ <true/>
+ <key>value</key>
+ <string>src/FOV.as</string>
+ <key>variable</key>
+ <string>TM_FLEX_FILE_SPECS</string>
+ </dict>
+ <dict>
+ <key>enabled</key>
+ <true/>
+ <key>value</key>
+ <string>bin/FOV.swf</string>
+ <key>variable</key>
+ <string>TM_FLEX_OUTPUT</string>
+ </dict>
+ </array>
+ <key>showFileHierarchyDrawer</key>
+ <true/>
+ <key>windowFrame</key>
+ <string>{{-4, 34}, {735, 842}}</string>
+</dict>
+</plist>
No changes.
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="utf-8"?>
+<flex-config>
+ <compiler>
+ <library-path append="true">
+ <path-element>../lib</path-element>
+ </library-path>
+ </compiler>
+</flex-config>
@@ -0,0 +1,68 @@
+package
+{
+ import flash.events.Event;
+ import flash.net.URLRequest;
+ import flash.display.Sprite;
+
+ import uk.co.jasonframe.demo_box.Demo;
+ import uk.co.jasonframe.math.Vec2;
+
+ import com.bit101.components.Label;
+ import com.bit101.components.HSlider;
+
+ public class FOV extends Demo
+ {
+ private var _guard:Thing = null;
+ private var _rotate:int = 0;
+ private var _prey:Array = [];
+
+ protected override function getName():String {
+ return "Field of View Demo";
+ }
+
+ protected override function setup():void {
+
+ var label:Label = new Label(controls, PADDING, 26, "FOV");
+
+ var slider:HSlider = new HSlider(controls, PADDING, 43, handleFOV);
+ slider.backClick = true;
+ slider.minimum = 0;
+ slider.maximum = 360;
+ slider.tick = 20;
+ slider.value = 90;
+ slider.setSize(CONTROLS_INNER_WIDTH, 10);
+
+ _guard = new Thing();
+ _guard.position = new Vec2(world.width / 2, world.height / 2);
+ _guard.color = 0xff0000;
+ world.addChild(_guard);
+
+ for (var i:int = 0; i < 20; i++) {
+ var p:Thing = new Thing();
+ p.position = new Vec2(Math.random() * world.width, Math.random() * world.height);
+ p.color = 0x00ff00;
+ p.fov = 0;
+ world.addChild(p);
+ _prey.push(p);
+ }
+
+ }
+
+ protected function handleFOV(e:Event):void {
+ _guard.fov = e.target.value;
+ }
+
+ protected override function tick(e:Event):void {
+ _guard.heading = Vec2.north()._rotateDeg(_rotate += 3);
+ var h:Vec2;
+ for (var i:int = 0; i < _prey.length; i++) {
+ var p:Thing = _prey[i];
+ if (_guard.hasInFOV(p)) {
+ p.color = 0x0000ff;
+ } else {
+ p.color = 0x00ff00;
+ }
+ }
+ }
+ }
+}
@@ -0,0 +1,135 @@
+package
+{
+ import flash.display.Sprite;
+ import flash.display.GradientType;
+ import uk.co.jasonframe.math.Vec2;
+
+ public class Thing extends Sprite
+ {
+ private const RADIUS:int = 10;
+ private const FOV_SIZE:int = 100;
+
+ private var _mass : Number = 1;
+ private var _position : Vec2 = new Vec2(0, 0);
+ private var _velocity : Vec2 = new Vec2(0, 0);
+ private var _maxForce : Number = 0;
+ private var _maxSpeed : Number = 0;
+ private var _heading : Vec2 = new Vec2(1, 0);
+ private var _color : int = 0xa0ffa0;
+ private var _fov : Number = 90.0;
+
+ public function get mass():Number { return _mass; }
+ public function set mass(m:Number):void { _mass = m; }
+
+ public function get position():Vec2 { return _position; }
+ public function set position(p:Vec2):void { _position = p; x = p.x; y = p.y; }
+
+ public function get velocity():Vec2 { return _velocity; }
+ public function set velocity(v:Vec2):void { _velocity = v; }
+
+ public function get maxForce():Number { return _maxForce; }
+ public function set maxForce(mf:Number):void { _maxForce = mf; }
+
+ public function get maxSpeed():Number { return _maxSpeed; }
+ public function set maxSpeed(ms:Number):void { _maxSpeed = ms; }
+
+ public function get heading():Vec2 { return _heading; }
+ public function set heading(h:Vec2):void { _heading = h; rotation = h.heading(); }
+
+ public function get color():int { return _color; }
+ public function set color(c:int):void { _color = c; redraw(); }
+
+ public function get fov():Number { return _fov; }
+ public function set fov(c:Number):void { _fov = c; redraw(); }
+
+ //
+ // Predicates
+
+ // returns true if other in this thing's FOV
+ public function hasInFOV(other:Thing):Boolean {
+
+ var meToOther:Vec2 = other.position.dup()._sub(position)._normalize();
+ var h:Vec2 = heading.dup()._normalize();
+
+ var dot:Number = h.dot(meToOther);
+
+ return dot > Math.cos((_fov / 2) * (Math.PI / 180));
+ }
+
+
+ // http://forum.unity3d.com/threads/53188-how-do-i-detect-if-an-object-is-in-front-of-another-object
+ public function isInFrontOf(other:Thing):Boolean {
+
+ var heading:Vec2 = position.dup();
+ heading.sub(other.position);
+ heading.normalize();
+
+ var direction:Vec2 = velocity.dup();
+ //direction.normalize();
+
+ var dot:Number = heading.dot(direction);
+
+ return dot > 1;
+ }
+
+ //
+ // Constructor
+
+ public function Thing() {
+ redraw();
+ }
+
+ public function updatePosition():void {
+ this.position.x += this.velocity.x;
+ this.position.y += this.velocity.y;
+ this.x = this.position.x;
+ this.y = this.position.y;
+ }
+
+ public function redraw():void {
+ with (this.graphics) {
+ clear();
+
+ if (fov > 0) {
+ beginGradientFill(GradientType.RADIAL, [_color, _color], [0.15, 0], [0, 255]);
+ drawSlice(0, 0, FOV_SIZE, -_fov / 2 - 90, _fov);
+ endFill();
+ }
+
+ beginFill(_color, 1);
+ drawCircle(0, 0, RADIUS);
+ endFill();
+ }
+ }
+
+ /**
+ * Draw a circle slice
+ * x0 slice center x position
+ * y0 slice center y position
+ * r radius
+ * startingAngle in degrees, 0 = 3 o'clock
+ * arcAngle in degrees, draws clockwise.
+ */
+ private function drawSlice(x0:Number, y0:Number, r:Number, startAngle:Number, arcAngle:Number):void
+ {
+ var deg2rad:Number = Math.PI / 180.0;
+ var angle:Number;
+ var arcStep:Number = 5;
+ var steps:Number = arcAngle / arcStep;
+ var x1:Number = x0 + Math.cos(startAngle * deg2rad) * r;
+ var y1:Number = y0 + Math.sin(startAngle * deg2rad) * r;
+
+ graphics.moveTo(x0, y0);
+ graphics.lineTo(x1, y1);
+
+ for (var i:int = 1; i <= steps; i++)
+ {
+ angle = (startAngle + i * arcStep) * deg2rad;
+ x1 = x0 + Math.cos(angle) * r;
+ y1 = y0 + Math.sin(angle) * r;
+ graphics.lineTo(x1, y1);
+ }
+ graphics.lineTo(x0, y0);
+ }
+ }
+}
@@ -0,0 +1,25 @@
+Behaviours left to implement:
+
+ - obstacle avoidance
+
+Other stuff:
+
+ - understand the dot product
+ - build a basis of a entity/component framework
+ - construct gameobjects from factories
+ - register gameobjects with relevant subsystems
+ - ability to add gameobjects to word
+
+Entity component:
+ - an object that can be positioned in the game world
+ - can be moveable or immoveable
+
+ - every entity has:
+ - position
+ - mass
+ - facing (angle)
+ - velocity (vector)
+ - max force
+ - max speed
+
+ facing needs to be separate from velocity!!!
Oops, something went wrong.

0 comments on commit 848986d

Please sign in to comment.