Skip to content

Commit

Permalink
Making low-level data types 3D-compatible.
Browse files Browse the repository at this point in the history
  • Loading branch information
alecmce committed Aug 12, 2013
1 parent c9f1826 commit 429f0e2
Show file tree
Hide file tree
Showing 13 changed files with 107 additions and 37 deletions.
Binary file added assets/numbers.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 7 additions & 1 deletion src/dust/camera/data/Camera.hx
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,20 @@ class Camera
public var worldY:Float;
public var scalar:Float;

public function new(screenCenterX:Int, screenCenterY:Int, scalar:Float = 1)
public var isPerspective:Bool;
public var fieldOfView:Float;

public function new(screenCenterX:Int, screenCenterY:Int, scalar:Float = 1.0)
{
this.screenCenterX = screenCenterX;
this.screenCenterY = screenCenterY;
this.scalar = scalar;

worldX = 0;
worldY = 0;

isPerspective = false;
fieldOfView = 110.0;
}

public function set(worldX:Float, worldY:Float)
Expand Down
3 changes: 1 addition & 2 deletions src/dust/collections/control/CollectionListenersMap.hx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ class CollectionListenersMap
var injector:Injector;
var types:Array<Class<CollectionListeners>>;

@inject
public function new(injector:Injector)
@inject public function new(injector:Injector)
{
this.injector = injector;
}
Expand Down
40 changes: 31 additions & 9 deletions src/dust/geom/data/Delta.hx
Original file line number Diff line number Diff line change
@@ -1,53 +1,66 @@
package dust.geom.data;

import dust.math.AnglesUtil;

class Delta
{
public static function fromPositions(a:Position, b:Position):Delta
{
return new Delta(b.x - a.x, b.y - a.y, b.rotation - a.rotation);
}

public static function addDeltas(a:Delta, b:Delta):Delta
{
return new Delta(a.dx + b.dx, a.dy + b.dy, a.dr + b.dr);
}

public static function subtractDeltas(a:Delta, b:Delta):Delta
{
return new Delta(b.dx - a.dx, b.dy - a.dy, b.dr - a.dr);
}

public static function dotProduct(a:Delta, b:Delta):Float
return a.dx * b.dx + a.dy * b.dy;
{
return a.dx * b.dx + a.dy * b.dy + a.dz * b.dz;
}

public var dx:Float;
public var dy:Float;
public var dz:Float;
public var dr:Float;

public function new(dx:Float = 0.0, dy:Float = 0.0, dr:Float = 0.0)
set(dx, dy, dr);
public function new(dx:Float = 0.0, dy:Float = 0.0, dz:Float = 0.0, dr:Float = 0.0)
{
set(dx, dy, dz, dr);
}

inline public function set(dx:Float = 0.0, dy:Float = 0.0, dr:Float = 0.0)
inline public function set(dx:Float = 0.0, dy:Float = 0.0, dz:Float = 0.0, dr:Float = 0.0)
{
this.dx = dx;
this.dy = dy;
this.dz = dz;
this.dr = dr;
}

inline public function add(delta:Delta)
{
this.dx += delta.dx;
this.dy += delta.dy;
this.dz += delta.dz;
this.dr += delta.dr;
}

inline public function subtract(delta:Delta)
{
this.dx -= delta.dx;
this.dy -= delta.dy;
this.dz -= delta.dz;
this.dr -= delta.dr;
}

inline public function multiply(value:Float):Delta
{
dx *= value;
dy *= value;
dz *= value;
dr *= value;
return this;
}
Expand All @@ -60,16 +73,21 @@ class Delta
}

inline public function clone():Delta
return new Delta(dx, dy, dr);
{
return new Delta(dx, dy, dz, dr);
}

inline public function reset()
{
dx = 0.0;
dy = 0.0;
dz = 0.0;
}

inline public function getMagnitude():Float
return Math.sqrt(dx * dx + dy * dy);
{
return Math.sqrt(dx * dx + dy * dy + dz * dz);
}

inline public function setMagnitude(value:Float)
{
Expand All @@ -80,8 +98,12 @@ class Delta
}

inline public function normalize()
{
multiply(1 / getMagnitude());
}

public function toString():String
return "[Delta dx=$dx, dy=$dy]";
{
return "[Delta dx: $dx, dy: $dy, dz: $dz]";
}
}
40 changes: 29 additions & 11 deletions src/dust/geom/data/Position.hx
Original file line number Diff line number Diff line change
Expand Up @@ -3,77 +3,93 @@ package dust.geom.data;
class Position
{
public static function areEqual(a:Position, b:Position):Bool
{
return a.x == b.x && a.y == b.y;
}

public static function areClose(a:Position, b:Position, threshold:Float):Bool
{
var dx = b.x - a.x;
var dy = b.y - a.y;
return ((dx > 0 && dx < threshold) || (dx < 0 && dx > -threshold)) &&
((dy > 0 && dy < threshold) || (dy < 0 && dy > -threshold));
var dz = b.z - a.z;

return ((dx >= 0 && dx < threshold) || (dx < 0 && dx > -threshold)) &&
((dy >= 0 && dy < threshold) || (dy < 0 && dy > -threshold)) &&
((dz >= 0 && dz < threshold) || (dz < 0 && dz > -threshold));
}

public static function squareDistance(a:Position, b:Position):Float
{
var dx = b.x - a.x;
var dy = b.y - a.y;
return dx * dx + dy * dy;
var dz = b.z - a.z;

return dx * dx + dy * dy + dz * dz;
}

public var x:Float;
public var y:Float;
public var z:Float;
public var rotation:Float;

public function new(x:Float = 0.0, y:Float = 0.0, rotation:Float = 0.0)
public function new(x:Float = 0.0, y:Float = 0.0, z:Float = 0.0, rotation:Float = 0.0)
{
this.x = x;
this.y = y;
this.z = z;
this.rotation = rotation;
}

public function set(x:Float, y:Float)
public function set(x:Float, y:Float, z:Float)
{
#if debug
if (x != x || y != y)
if (x != x || y != y || z != z)
throw "Position set to null!";
#end

this.x = x;
this.y = y;
this.z = z;
}

public function offset(dx:Float, dy:Float):Position
public function offset(dx:Float, dy:Float, dz:Float):Position
{
x += dx;
y += dy;
z += dz;
return this;
}

public function offsetDelta(delta:Delta):Position
{
x += delta.dx;
y += delta.dy;
z += delta.dz;
return this;
}

public function clone():Position
return new Position(x, y);
{
return new Position(x, y, z);
}

public function setTo(position:Position):Position
{
this.x = position.x;
this.y = position.y;
this.z = position.z;
this.rotation = position.rotation;
return this;
}

public function setToPositionXY(position:Position)
public function setToPosition(position:Position)
{
this.x = position.x;
this.y = position.y;
this.z = position.z;
}

public function rotateAbout(angle:Float, center:Position)
public function rotate2DAbout(angle:Float, center:Position)
{
x -= center.x;
y -= center.y;
Expand All @@ -89,5 +105,7 @@ class Position
}

public function toString():String
return "[Position x=" + x + ", y=" + y + ", angle=" + rotation + "]";
{
return '[Position x: $x, y: $y, z: $z, rotation: $rotation]';
}
}
16 changes: 16 additions & 0 deletions src/dust/gui/data/Color.hx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dust.gui.data;

import flash.geom.ColorTransform;

// FIXME for OpenGL rendering, makes more sense it's stored internally as floats
class Color
{
public var rgb:Int;
Expand Down Expand Up @@ -63,6 +64,21 @@ class Color
return this;
}

inline public function getR():Float
{
return getRed() / 0xFF;
}

inline public function getG():Float
{
return getGreen() / 0xFF;
}

inline public function getB():Float
{
return getBlue() / 0xFF;
}

public function getWhiteTransform(alpha:Float = 1.0):ColorTransform
{
var r = getRed() / 0xFF;
Expand Down
2 changes: 1 addition & 1 deletion src/dust/interactive/systems/OffsetSystem.hx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ class OffsetSystem implements System
current.setTo(position);

for (offset in offsets.offsets)
offset.offset(dx, dy);
offset.offset(dx, dy, 0);
}
}
2 changes: 1 addition & 1 deletion src/dust/interactive/systems/ReflectionSystem.hx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class ReflectionSystem implements System

var x = center.x + (center.x - position.x) * scale;
var y = center.y + (center.y - position.y) * scale;
target.set(x, y);
target.set(x, y, 0);
}
}
}
3 changes: 2 additions & 1 deletion src/dust/lists/SortedList.hx
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ class SortedList<T>
list.head = item.prev;
}

public function remove(data:T)
public function remove(data:T):T
{
list.remove(data);
return data;
}

public function iterator():Iterator<T>
Expand Down
6 changes: 3 additions & 3 deletions src/dust/multitouch/data/Touch.hx
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ class Touch
public function init(id:Int, x:Float, y:Float, time:Float):Touch
{
this.id = id;
this.start.set(x, y);
this.start.set(x, y, 0);
this.startTime = time;

this.current.set(x, y);
this.current.set(x, y, 0);
this.currentTime = time;

this.isCurrent = true;
Expand All @@ -51,7 +51,7 @@ class Touch

public function update(x:Float, y:Float, time:Float):Touch
{
this.current.set(x, y);
this.current.set(x, y, 0);
this.currentTime = time;
return this;
}
Expand Down
3 changes: 1 addition & 2 deletions src/dust/systems/impl/SystemMap.hx
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ class SystemMap
public function unmap(type:Class<System>)
{
var mapping = getMapping(type);
if (mapping != null)
mappings.remove(mapping);
return mapping != null ? mappings.remove(mapping) : null;
}

function getMapping(type:Class<System>):SystemMapping
Expand Down
13 changes: 11 additions & 2 deletions src/dust/systems/impl/Systems.hx
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,22 @@ class Systems
systemMap.setMetrics(metrics);

public function map(type:Class<System>, priority:Int):SystemMapping
return systemMap.map(type, priority);
{
var mapping = systemMap.map(type, priority);
if (areRunning)
mapping.apply(loop);
return mapping;
}

public function hasMapping(type:Class<System>):Bool
return systemMap.hasMapping(type);

public function unmap(type:Class<System>)
systemMap.unmap(type);
{
var mapping = systemMap.unmap(type);
if (mapping != null)
mapping.clear(loop);
}

public function setRate(millisecondsBetweenUpdates:Int)
loop.setRate(millisecondsBetweenUpdates);
Expand Down
Loading

0 comments on commit 429f0e2

Please sign in to comment.