Refactoring ready for 1.0 release.

This commit is contained in:
Richard Davey 2013-05-25 04:21:24 +01:00
parent 4ef0750c07
commit 204ec46b2b
90 changed files with 15043 additions and 15223 deletions

View file

@ -56,16 +56,30 @@
<TypeScriptOutFile>../build/phaser.js</TypeScriptOutFile>
<TypeScriptGeneratesDeclarations>true</TypeScriptGeneratesDeclarations>
</PropertyGroup>
<ItemGroup />
<ItemGroup>
<Folder Include="plugins\" />
</ItemGroup>
<ItemGroup>
<Content Include="AnimationManager.js">
<Content Include="animation\AnimationManager.js">
<DependentUpon>AnimationManager.ts</DependentUpon>
</Content>
<TypeScriptCompile Include="cameras\OrthographicCamera.ts" />
<Content Include="cameras\OrthographicCamera.js">
<DependentUpon>OrthographicCamera.ts</DependentUpon>
</Content>
<Content Include="Collision.js">
<DependentUpon>Collision.ts</DependentUpon>
</Content>
<TypeScriptCompile Include="core\Rectangle.ts" />
<TypeScriptCompile Include="core\Point.ts" />
<Content Include="core\Point.js">
<DependentUpon>Point.ts</DependentUpon>
</Content>
<Content Include="core\Rectangle.js">
<DependentUpon>Rectangle.ts</DependentUpon>
</Content>
<TypeScriptCompile Include="core\Vec2.ts" />
<Content Include="core\Vec2.js">
<DependentUpon>Vec2.ts</DependentUpon>
</Content>
<Content Include="DynamicTexture.js">
<DependentUpon>DynamicTexture.ts</DependentUpon>
</Content>
@ -109,84 +123,62 @@
<Content Include="gameobjects\Tilemap.js">
<DependentUpon>Tilemap.ts</DependentUpon>
</Content>
<TypeScriptCompile Include="verlet\AngleConstraint.ts" />
<TypeScriptCompile Include="VerletManager.ts" />
<TypeScriptCompile Include="geom\Polygon.ts" />
<TypeScriptCompile Include="geom\PointUtils.ts" />
<TypeScriptCompile Include="geom\CircleUtils.ts" />
<Content Include="geom\CircleUtils.js">
<DependentUpon>CircleUtils.ts</DependentUpon>
</Content>
<Content Include="geom\PointUtils.js">
<DependentUpon>PointUtils.ts</DependentUpon>
</Content>
<Content Include="geom\Polygon.js">
<DependentUpon>Polygon.ts</DependentUpon>
</Content>
<TypeScriptCompile Include="geom\Response.ts" />
<TypeScriptCompile Include="geom\RectangleUtils.ts" />
<Content Include="geom\RectangleUtils.js">
<DependentUpon>RectangleUtils.ts</DependentUpon>
</Content>
<Content Include="geom\Response.js">
<DependentUpon>Response.ts</DependentUpon>
</Content>
<Content Include="VerletManager.js">
<DependentUpon>VerletManager.ts</DependentUpon>
</Content>
<Content Include="verlet\AngleConstraint.js">
<DependentUpon>AngleConstraint.ts</DependentUpon>
</Content>
<Content Include="geom\Circle.js">
<Content Include="core\Circle.js">
<DependentUpon>Circle.ts</DependentUpon>
</Content>
<TypeScriptCompile Include="verlet\DistanceConstraint.ts" />
<TypeScriptCompile Include="verlet\Composite.ts" />
<Content Include="verlet\Composite.js">
<DependentUpon>Composite.ts</DependentUpon>
</Content>
<Content Include="verlet\DistanceConstraint.js">
<DependentUpon>DistanceConstraint.ts</DependentUpon>
</Content>
<Content Include="geom\IntersectResult.js">
<DependentUpon>IntersectResult.ts</DependentUpon>
</Content>
<Content Include="geom\Line.js">
<DependentUpon>Line.ts</DependentUpon>
</Content>
<TypeScriptCompile Include="geom\MicroPoint.ts" />
<Content Include="geom\MicroPoint.js">
<DependentUpon>MicroPoint.ts</DependentUpon>
<TypeScriptCompile Include="geom\Vec2Utils.ts" />
<Content Include="geom\Vec2Utils.js">
<DependentUpon>Vec2Utils.ts</DependentUpon>
</Content>
<TypeScriptCompile Include="verlet\PinConstraint.ts" />
<TypeScriptCompile Include="verlet\Particle.ts" />
<Content Include="verlet\Particle.js">
<DependentUpon>Particle.ts</DependentUpon>
</Content>
<Content Include="verlet\PinConstraint.js">
<DependentUpon>PinConstraint.ts</DependentUpon>
</Content>
<Content Include="geom\Point.js">
<DependentUpon>Point.ts</DependentUpon>
</Content>
<TypeScriptCompile Include="geom\Quad.ts" />
<Content Include="geom\Quad.js">
<DependentUpon>Quad.ts</DependentUpon>
</Content>
<Content Include="geom\Rectangle.js">
<DependentUpon>Rectangle.ts</DependentUpon>
</Content>
<TypeScriptCompile Include="geom\Vector2.ts" />
<Content Include="geom\Vector2.js">
<TypeScriptCompile Include="math\Vector2.ts" />
<Content Include="math\Vector2.js">
<DependentUpon>Vector2.ts</DependentUpon>
</Content>
<Content Include="SoundManager.js">
<Content Include="sound\SoundManager.js">
<DependentUpon>SoundManager.ts</DependentUpon>
</Content>
<TypeScriptCompile Include="system\screens\PauseScreen.ts" />
<TypeScriptCompile Include="system\screens\BootScreen.ts" />
<TypeScriptCompile Include="system\input\MSPointer.ts" />
<TypeScriptCompile Include="system\input\Gestures.ts" />
<TypeScriptCompile Include="input\MSPointer.ts" />
<TypeScriptCompile Include="input\Gestures.ts" />
<TypeScriptCompile Include="system\CollisionMask.ts" />
<Content Include="system\CollisionMask.js">
<DependentUpon>CollisionMask.ts</DependentUpon>
</Content>
<Content Include="system\input\Gestures.js">
<Content Include="input\Gestures.js">
<DependentUpon>Gestures.ts</DependentUpon>
</Content>
<Content Include="system\input\MSPointer.js">
<Content Include="input\MSPointer.js">
<DependentUpon>MSPointer.ts</DependentUpon>
</Content>
<TypeScriptCompile Include="system\input\Pointer.ts" />
<Content Include="system\input\Pointer.js">
<TypeScriptCompile Include="input\Pointer.ts" />
<Content Include="input\Pointer.js">
<DependentUpon>Pointer.ts</DependentUpon>
</Content>
<Content Include="system\screens\BootScreen.js">
@ -195,24 +187,24 @@
<Content Include="system\screens\PauseScreen.js">
<DependentUpon>PauseScreen.ts</DependentUpon>
</Content>
<Content Include="system\Sound.js">
<Content Include="sound\Sound.js">
<DependentUpon>Sound.ts</DependentUpon>
</Content>
<TypeScriptCompile Include="system\Sound.ts" />
<TypeScriptCompile Include="SoundManager.ts" />
<Content Include="system\animation\Animation.js">
<TypeScriptCompile Include="sound\Sound.ts" />
<TypeScriptCompile Include="sound\SoundManager.ts" />
<Content Include="animation\Animation.js">
<DependentUpon>Animation.ts</DependentUpon>
</Content>
<Content Include="system\animation\AnimationLoader.js">
<Content Include="animation\AnimationLoader.js">
<DependentUpon>AnimationLoader.ts</DependentUpon>
</Content>
<Content Include="system\animation\Frame.js">
<Content Include="animation\Frame.js">
<DependentUpon>Frame.ts</DependentUpon>
</Content>
<Content Include="system\animation\FrameData.js">
<Content Include="animation\FrameData.js">
<DependentUpon>FrameData.ts</DependentUpon>
</Content>
<Content Include="system\Camera.js">
<Content Include="cameras\Camera.js">
<DependentUpon>Camera.ts</DependentUpon>
</Content>
<Content Include="system\Device.js">
@ -240,10 +232,10 @@
<Content Include="system\TilemapLayer.js">
<DependentUpon>TilemapLayer.ts</DependentUpon>
</Content>
<Content Include="system\Tween.js">
<Content Include="tweens\Tween.js">
<DependentUpon>Tween.ts</DependentUpon>
</Content>
<TypeScriptCompile Include="system\Tween.ts" />
<TypeScriptCompile Include="tweens\Tween.ts" />
<TypeScriptCompile Include="system\Tile.ts" />
<TypeScriptCompile Include="system\StageScaleMode.ts" />
<TypeScriptCompile Include="system\RequestAnimationFrame.ts" />
@ -251,76 +243,74 @@
<TypeScriptCompile Include="system\QuadTree.ts" />
<TypeScriptCompile Include="system\LinkedList.ts" />
<TypeScriptCompile Include="system\Device.ts" />
<TypeScriptCompile Include="system\Camera.ts" />
<Content Include="system\easing\Back.js">
<TypeScriptCompile Include="cameras\Camera.ts" />
<Content Include="tweens\easing\Back.js">
<DependentUpon>Back.ts</DependentUpon>
</Content>
<Content Include="system\easing\Bounce.js">
<Content Include="tweens\easing\Bounce.js">
<DependentUpon>Bounce.ts</DependentUpon>
</Content>
<Content Include="system\easing\Circular.js">
<Content Include="tweens\easing\Circular.js">
<DependentUpon>Circular.ts</DependentUpon>
</Content>
<Content Include="system\easing\Cubic.js">
<Content Include="tweens\easing\Cubic.js">
<DependentUpon>Cubic.ts</DependentUpon>
</Content>
<Content Include="system\easing\Elastic.js">
<Content Include="tweens\easing\Elastic.js">
<DependentUpon>Elastic.ts</DependentUpon>
</Content>
<Content Include="system\easing\Exponential.js">
<Content Include="tweens\easing\Exponential.js">
<DependentUpon>Exponential.ts</DependentUpon>
</Content>
<Content Include="system\easing\Linear.js">
<Content Include="tweens\easing\Linear.js">
<DependentUpon>Linear.ts</DependentUpon>
</Content>
<Content Include="system\easing\Quadratic.js">
<Content Include="tweens\easing\Quadratic.js">
<DependentUpon>Quadratic.ts</DependentUpon>
</Content>
<Content Include="system\easing\Quartic.js">
<Content Include="tweens\easing\Quartic.js">
<DependentUpon>Quartic.ts</DependentUpon>
</Content>
<Content Include="system\easing\Quintic.js">
<Content Include="tweens\easing\Quintic.js">
<DependentUpon>Quintic.ts</DependentUpon>
</Content>
<Content Include="system\easing\Sinusoidal.js">
<Content Include="tweens\easing\Sinusoidal.js">
<DependentUpon>Sinusoidal.ts</DependentUpon>
</Content>
<Content Include="system\input\Input.js">
<Content Include="input\Input.js">
<DependentUpon>Input.ts</DependentUpon>
</Content>
<Content Include="system\input\Keyboard.js">
<Content Include="input\Keyboard.js">
<DependentUpon>Keyboard.ts</DependentUpon>
</Content>
<Content Include="system\input\Mouse.js">
<Content Include="input\Mouse.js">
<DependentUpon>Mouse.ts</DependentUpon>
</Content>
<Content Include="system\input\Touch.js">
<Content Include="input\Touch.js">
<DependentUpon>Touch.ts</DependentUpon>
</Content>
<TypeScriptCompile Include="system\input\Touch.ts" />
<TypeScriptCompile Include="system\input\Mouse.ts" />
<TypeScriptCompile Include="system\input\Keyboard.ts" />
<TypeScriptCompile Include="system\input\Input.ts" />
<TypeScriptCompile Include="system\easing\Sinusoidal.ts" />
<TypeScriptCompile Include="system\easing\Quintic.ts" />
<TypeScriptCompile Include="system\easing\Quartic.ts" />
<TypeScriptCompile Include="system\easing\Quadratic.ts" />
<TypeScriptCompile Include="system\easing\Linear.ts" />
<TypeScriptCompile Include="system\easing\Exponential.ts" />
<TypeScriptCompile Include="system\easing\Elastic.ts" />
<TypeScriptCompile Include="system\easing\Cubic.ts" />
<TypeScriptCompile Include="system\easing\Circular.ts" />
<TypeScriptCompile Include="system\easing\Bounce.ts" />
<TypeScriptCompile Include="system\easing\Back.ts" />
<TypeScriptCompile Include="system\animation\FrameData.ts" />
<TypeScriptCompile Include="system\animation\Frame.ts" />
<TypeScriptCompile Include="system\animation\AnimationLoader.ts" />
<TypeScriptCompile Include="system\animation\Animation.ts" />
<TypeScriptCompile Include="geom\Rectangle.ts" />
<TypeScriptCompile Include="geom\Point.ts" />
<TypeScriptCompile Include="input\Touch.ts" />
<TypeScriptCompile Include="input\Mouse.ts" />
<TypeScriptCompile Include="input\Keyboard.ts" />
<TypeScriptCompile Include="input\Input.ts" />
<TypeScriptCompile Include="tweens\easing\Sinusoidal.ts" />
<TypeScriptCompile Include="tweens\easing\Quintic.ts" />
<TypeScriptCompile Include="tweens\easing\Quartic.ts" />
<TypeScriptCompile Include="tweens\easing\Quadratic.ts" />
<TypeScriptCompile Include="tweens\easing\Linear.ts" />
<TypeScriptCompile Include="tweens\easing\Exponential.ts" />
<TypeScriptCompile Include="tweens\easing\Elastic.ts" />
<TypeScriptCompile Include="tweens\easing\Cubic.ts" />
<TypeScriptCompile Include="tweens\easing\Circular.ts" />
<TypeScriptCompile Include="tweens\easing\Bounce.ts" />
<TypeScriptCompile Include="tweens\easing\Back.ts" />
<TypeScriptCompile Include="animation\FrameData.ts" />
<TypeScriptCompile Include="animation\Frame.ts" />
<TypeScriptCompile Include="animation\AnimationLoader.ts" />
<TypeScriptCompile Include="animation\Animation.ts" />
<TypeScriptCompile Include="geom\Line.ts" />
<TypeScriptCompile Include="geom\IntersectResult.ts" />
<TypeScriptCompile Include="geom\Circle.ts" />
<TypeScriptCompile Include="core\Circle.ts" />
<TypeScriptCompile Include="gameobjects\Tilemap.ts" />
<TypeScriptCompile Include="gameobjects\Sprite.ts" />
<TypeScriptCompile Include="gameobjects\Particle.ts" />
@ -330,7 +320,7 @@
<Content Include="Group.js">
<DependentUpon>Group.ts</DependentUpon>
</Content>
<Content Include="Loader.js">
<Content Include="loader\Loader.js">
<DependentUpon>Loader.ts</DependentUpon>
</Content>
<Content Include="Motion.js">
@ -354,14 +344,14 @@
<Content Include="Time.js">
<DependentUpon>Time.ts</DependentUpon>
</Content>
<Content Include="TweenManager.js">
<Content Include="tweens\TweenManager.js">
<DependentUpon>TweenManager.ts</DependentUpon>
</Content>
<Content Include="World.js">
<DependentUpon>World.ts</DependentUpon>
</Content>
<TypeScriptCompile Include="World.ts" />
<TypeScriptCompile Include="TweenManager.ts" />
<TypeScriptCompile Include="tweens\TweenManager.ts" />
<TypeScriptCompile Include="Time.ts" />
<TypeScriptCompile Include="State.ts" />
<TypeScriptCompile Include="Stage.ts" />
@ -369,24 +359,24 @@
<TypeScriptCompile Include="Signal.ts" />
<TypeScriptCompile Include="Phaser.ts" />
<TypeScriptCompile Include="Motion.ts" />
<TypeScriptCompile Include="Loader.ts" />
<TypeScriptCompile Include="loader\Loader.ts" />
<TypeScriptCompile Include="Group.ts" />
<TypeScriptCompile Include="GameMath.ts" />
<TypeScriptCompile Include="Game.ts" />
<TypeScriptCompile Include="DynamicTexture.ts" />
<TypeScriptCompile Include="Collision.ts" />
<TypeScriptCompile Include="AnimationManager.ts" />
<TypeScriptCompile Include="animation\AnimationManager.ts" />
<Content Include="Basic.js">
<DependentUpon>Basic.ts</DependentUpon>
</Content>
<Content Include="Cache.js">
<Content Include="loader\Cache.js">
<DependentUpon>Cache.ts</DependentUpon>
</Content>
<Content Include="CameraManager.js">
<Content Include="cameras\CameraManager.js">
<DependentUpon>CameraManager.ts</DependentUpon>
</Content>
<TypeScriptCompile Include="CameraManager.ts" />
<TypeScriptCompile Include="Cache.ts" />
<TypeScriptCompile Include="cameras\CameraManager.ts" />
<TypeScriptCompile Include="loader\Cache.ts" />
<TypeScriptCompile Include="Basic.ts" />
</ItemGroup>
<Import Project="$(VSToolsPath)\TypeScript\Microsoft.TypeScript.targets" />

View file

@ -0,0 +1,66 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<ProjectGuid>{FC407544-CFE2-414B-8936-7355D00E881A}</ProjectGuid>
<ProjectTypeGuids>{349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}</ProjectTypeGuids>
<OutputType>Library</OutputType>
<OutputPath>bin</OutputPath>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<DebugType>full</DebugType>
<DebugSymbols>true</DebugSymbols>
<UseIISExpress>true</UseIISExpress>
<IISExpressSSLPort />
<IISExpressAnonymousAuthentication />
<IISExpressWindowsAuthentication />
<IISExpressUseClassicPipelineMode />
</PropertyGroup>
<ItemGroup>
<Content Include="app.css" />
<Content Include="default.htm" />
<Content Include="app.js">
<DependentUpon>app.ts</DependentUpon>
</Content>
</ItemGroup>
<ItemGroup>
<TypeScriptCompile Include="app.ts" />
</ItemGroup>
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
</PropertyGroup>
<PropertyGroup>
<RootNamespace>Scratch</RootNamespace>
</PropertyGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Import Project="$(VSToolsPath)\WebApplications\Microsoft.WebApplication.targets" Condition="'$(VSToolsPath)' != ''" />
<ProjectExtensions>
<VisualStudio>
<FlavorProperties GUID="{349c5851-65df-11da-9384-00065b846f21}">
<WebProjectProperties>
<UseIIS>True</UseIIS>
<AutoAssignPort>True</AutoAssignPort>
<DevelopmentServerPort>0</DevelopmentServerPort>
<DevelopmentServerVPath>/</DevelopmentServerVPath>
<IISUrl>http://localhost:65337/</IISUrl>
<NTLMAuthentication>False</NTLMAuthentication>
<UseCustomServer>False</UseCustomServer>
<CustomServerUrl>
</CustomServerUrl>
<SaveServerSettingsInUserFile>False</SaveServerSettingsInUserFile>
</WebProjectProperties>
</FlavorProperties>
</VisualStudio>
</ProjectExtensions>
<PropertyGroup Condition="'$(Configuration)' == 'Debug'">
<TypeScriptTarget>ES5</TypeScriptTarget>
<TypeScriptIncludeComments>true</TypeScriptIncludeComments>
<TypeScriptSourceMap>false</TypeScriptSourceMap>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)' == 'Release'">
<TypeScriptTarget>ES5</TypeScriptTarget>
<TypeScriptIncludeComments>false</TypeScriptIncludeComments>
<TypeScriptSourceMap>false</TypeScriptSourceMap>
</PropertyGroup>
<Import Project="$(VSToolsPath)\TypeScript\Microsoft.TypeScript.targets" />
</Project>

8
Phaser/Scratch/app.css Normal file
View file

@ -0,0 +1,8 @@
body
{
font-family: 'Segoe UI', sans-serif
}
span {
font-style: italic
}

95
Phaser/Scratch/app.ts Normal file
View file

@ -0,0 +1,95 @@
module Phaser {
export class Point1 {
constructor(x: number = 0, y: number = 0) {
//this.values = (typeof Float32Array !== 'undefined') ? new Float32Array(2) : new Array;
this.x = x;
this.y = y;
}
x: number;
y: number;
public add(dx: number, dy: number): Point1 {
this.x += dx;
this.y += dy;
return this;
}
}
export class Point2 {
constructor(x: number = 0, y: number = 0) {
this.values = new Float32Array(2);
this.values[0] = x;
this.values[1] = y;
}
get x(): number {
return this.values[0];
}
get y(): number {
return this.values[1];
}
set x(v: number) {
this.values[0] = v;
}
set y(v: number) {
this.values[1] = v;
}
values;
public add(dx: number, dy: number): Point2 {
this.values[0] += dx;
this.values[1] += dy;
return this;
}
}
}
window.onload = () => {
var b = document.getElementById('start').onclick = test;
var e = document.getElementById('content');
function test() {
var started = Date.now();
var a = new Phaser.Point1(Math.random(), Math.random());
for (var i = 0; i < 10000000; i++)
{
var b = new Phaser.Point1(Math.random(), Math.random());
a.add(b.x, b.y);
}
var ended = Date.now();
var duration = (ended - started) / 1000;
var s = 'Test started: ' + started + '\n';
s = s + 'Test ended: ' + ended + '\n';
s = s + 'Duration: ' + duration + '\n';
e.textContent = s;
}
};

View file

@ -0,0 +1,20 @@
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title>Point Test</title>
<link rel="stylesheet" href="app.css" type="text/css" />
<script src="app.js"></script>
</head>
<body>
<h1>Point Test</h1>
<textarea id="content" style="width: 800px; height: 800px"></textarea>
<br />
<button id="start">Start</button>
</body>
</html>

View file

@ -14,6 +14,7 @@ module Phaser {
export class Camera {
/**
*Sprite constructor
* Instantiates a new camera at the specified location, with the specified size and zoom level.
*
@ -407,9 +408,6 @@ module Phaser {
this._game.stage.context.save();
}
// It may be safer/quicker to just save the context every frame regardless (needs testing on mobile - sucked on Android 2.x)
//this._game.stage.context.save();
this.fx.preRender(this, this._stageX, this._stageY, this.worldView.width, this.worldView.height);
if (this.alpha !== 1)
@ -486,6 +484,7 @@ module Phaser {
this._game.stage.context.clip();
}
// Render all the Sprites
this._game.world.group.render(this, this._sx, this._sy);
if (this.showBorder == true)

View file

View file

@ -21,7 +21,6 @@ module Phaser {
**/
constructor(x: number = 0, y: number = 0, diameter: number = 0) {
this._pos = new Vector2;
this.setTo(x, y, diameter);
}
@ -44,15 +43,6 @@ module Phaser {
**/
public y: number = 0;
/**
* The position of this Circle object represented by a Vector2
* @property pos
* @type Vector2
**/
public get pos(): Vector2 {
return this._pos.setTo(this.x, this.y);
}
/**
* The diameter of the circle. The largest distance between any two points on the circle. The same as the radius * 2.
* @method diameter
@ -83,9 +73,7 @@ module Phaser {
* @return {Number}
**/
get radius(): number {
return this._radius;
}
/**
@ -108,10 +96,8 @@ module Phaser {
* @method circumference
* @return {Number}
**/
public circumference(): number {
circumference(): number {
return 2 * (Math.PI * this._radius);
}
/**
@ -120,9 +106,7 @@ module Phaser {
* @return {Number}
**/
get bottom(): number {
return this.y + this._radius;
}
/**
@ -132,18 +116,14 @@ module Phaser {
**/
set bottom(value: number) {
if (!isNaN(value))
if (value < this.y)
{
if (value < this.y)
{
this._radius = 0;
this._diameter = 0;
}
else
{
this.radius = value - this.y;
}
this._radius = 0;
this._diameter = 0;
}
else
{
this.radius = value - this.y;
}
}
@ -154,9 +134,7 @@ module Phaser {
* @return {Number} The x coordinate of the leftmost point of the circle.
**/
get left(): number {
return this.x - this._radius;
}
/**
@ -166,17 +144,14 @@ module Phaser {
**/
set left(value: number) {
if (!isNaN(value))
if (value > this.x)
{
if (value < this.x)
{
this.radius = this.x - value;
}
else
{
this._radius = 0;
this._diameter = 0;
}
this._radius = 0;
this._diameter = 0;
}
else
{
this.radius = this.x - value;
}
}
@ -187,9 +162,7 @@ module Phaser {
* @return {Number}
**/
get right(): number {
return this.x + this._radius;
}
/**
@ -199,17 +172,14 @@ module Phaser {
**/
set right(value: number) {
if (!isNaN(value))
if (value < this.x)
{
if (value > this.x)
{
this.radius = value - this.x;
}
else
{
this._radius = 0;
this._diameter = 0;
}
this._radius = 0;
this._diameter = 0;
}
else
{
this.radius = value - this.x;
}
}
@ -220,9 +190,7 @@ module Phaser {
* @return {Number}
**/
get top(): number {
return this.y - this._radius;
}
/**
@ -232,17 +200,14 @@ module Phaser {
**/
set top(value: number) {
if (!isNaN(value))
if (value > this.y)
{
if (value > this.y)
{
this._radius = 0;
this._diameter = 0;
}
else
{
this.radius = this.y - value;
}
this._radius = 0;
this._diameter = 0;
}
else
{
this.radius = this.y - value;
}
}
@ -265,219 +230,6 @@ module Phaser {
}
/**
* Determines whether or not this Circle object is empty.
* @method isEmpty
* @return {Boolean} A value of true if the Circle objects diameter is less than or equal to 0; otherwise false.
**/
get isEmpty(): bool {
if (this._diameter <= 0)
{
return true;
}
return false;
}
/**
* Whether the circle intersects with a line. Checks against infinite line defined by the two points on the line, not the line segment.
* If you need details about the intersection then use Collision.lineToCircle instead.
* @method intersectCircleLine
* @param {Object} the line object to check.
* @return {Boolean}
**/
public intersectCircleLine(line: Line): bool {
return Collision.lineToCircle(line, this).result;
}
/**
* Returns a new Circle object with the same values for the x, y, width, and height properties as the original Circle object.
* @method clone
* @param {Circle} [optional] output Optional Circle object. If given the values will be set into the object, otherwise a brand new Circle object will be created and returned.
* @return {Phaser.Circle}
**/
public clone(output?: Circle = new Circle): Circle {
return output.setTo(this.x, this.y, this._diameter);
}
/**
* Return true if the given x/y coordinates are within this Circle object.
* If you need details about the intersection then use Phaser.Intersect.circleContainsPoint instead.
* @method contains
* @param {Number} The X value of the coordinate to test.
* @param {Number} The Y value of the coordinate to test.
* @return {Boolean} True if the coordinates are within this circle, otherwise false.
**/
public contains(x: number, y: number): bool {
return Collision.circleContainsPoint(this, <Point> { x: x, y: y }).result;
}
/**
* Return true if the coordinates of the given Point object are within this Circle object.
* If you need details about the intersection then use Phaser.Intersect.circleContainsPoint instead.
* @method containsPoint
* @param {Phaser.Point} The Point object to test.
* @return {Boolean} True if the coordinates are within this circle, otherwise false.
**/
public containsPoint(point:Point): bool {
return Collision.circleContainsPoint(this, point).result;
}
/**
* Return true if the given Circle is contained entirely within this Circle object.
* If you need details about the intersection then use Phaser.Intersect.circleToCircle instead.
* @method containsCircle
* @param {Phaser.Circle} The Circle object to test.
* @return {Boolean} True if the coordinates are within this circle, otherwise false.
**/
public containsCircle(circle:Circle): bool {
return Collision.circleToCircle(this, circle).result;
}
/**
* Copies all of circle data from the source Circle object into the calling Circle object.
* @method copyFrom
* @param {Circle} rect The source circle object to copy from
* @return {Circle} This circle object
**/
public copyFrom(source: Circle): Circle {
return this.setTo(source.x, source.y, source.diameter);
}
/**
* Copies all of circle data from this Circle object into the destination Circle object.
* @method copyTo
* @param {Circle} circle The destination circle object to copy in to
* @return {Circle} The destination circle object
**/
public copyTo(target: Circle) {
return target.copyFrom(this);
}
/**
* Returns the distance from the center of this Circle object to the given object (can be Circle, Point or anything with x/y values)
* @method distanceFrom
* @param {Circle/Point} target - The destination Point object.
* @param {Boolean} [optional] round - Round the distance to the nearest integer (default false)
* @return {Number} The distance between this Point object and the destination Point object.
**/
public distanceTo(target: any, round?: bool = false): number {
var dx = this.x - target.x;
var dy = this.y - target.y;
if (round === true)
{
return Math.round(Math.sqrt(dx * dx + dy * dy));
}
else
{
return Math.sqrt(dx * dx + dy * dy);
}
}
/**
* Determines whether the object specified in the toCompare parameter is equal to this Circle object. This method compares the x, y and diameter properties of an object against the same properties of this Circle object.
* @method equals
* @param {Circle} toCompare The circle to compare to this Circle object.
* @return {Boolean} A value of true if the object has exactly the same values for the x, y and diameter properties as this Circle object; otherwise false.
**/
public equals(toCompare: Circle): bool {
if (this.x === toCompare.x && this.y === toCompare.y && this.diameter === toCompare.diameter)
{
return true;
}
return false;
}
/**
* Determines whether the Circle object specified in the toIntersect parameter intersects with this Circle object. This method checks the radius distances between the two Circle objects to see if they intersect.
* @method intersects
* @param {Circle} toIntersect The Circle object to compare against to see if it intersects with this Circle object.
* @return {Boolean} A value of true if the specified object intersects with this Circle object; otherwise false.
**/
public intersects(toIntersect: Circle): bool {
if (this.distanceTo(toIntersect, false) < (this._radius + toIntersect._radius)) {
return true;
}
return false;
}
/**
* Returns a Point object containing the coordinates of a point on the circumference of this Circle based on the given angle.
* @method circumferencePoint
* @param {Number} angle The angle in radians (unless asDegrees is true) to return the point from.
* @param {Boolean} asDegrees Is the given angle in radians (false) or degrees (true)?
* @param {Phaser.Point} [optional] output An optional Point object to put the result in to. If none specified a new Point object will be created.
* @return {Phaser.Point} The Point object holding the result.
**/
public circumferencePoint(angle: number, asDegrees: bool = false, output?: Point = new Point): Point {
if (asDegrees === true)
{
angle = angle * GameMath.DEG_TO_RAD;
}
output.x = this.x + this._radius * Math.cos(angle);
output.y = this.y + this._radius * Math.sin(angle);
return output;
}
/**
* Adjusts the location of the Circle object, as determined by its center coordinate, by the specified amounts.
* @method offset
* @param {Number} dx Moves the x value of the Circle object by this amount.
* @param {Number} dy Moves the y value of the Circle object by this amount.
* @return {Circle} This Circle object.
**/
public offset(dx: number, dy: number): Circle {
if (!isNaN(dx) && !isNaN(dy))
{
this.x += dx;
this.y += dy;
}
return this;
}
/**
* Adjusts the location of the Circle object using a Point object as a parameter. This method is similar to the Circle.offset() method, except that it takes a Point object as a parameter.
* @method offsetPoint
* @param {Point} point A Point object to use to offset this Circle object.
* @return {Circle} This Circle object.
**/
public offsetPoint(point: Point): Circle {
return this.offset(point.x, point.y);
}
/**
* Sets the members of Circle to the specified values.
* @method setTo
@ -497,15 +249,57 @@ module Phaser {
}
/**
* Determines whether or not this Circle object is empty.
* @method empty
* @return {Boolean} A value of true if the Circle objects diameter is less than or equal to 0; otherwise false.
**/
get empty(): bool {
return (this._diameter == 0);
}
/**
* Sets all of the Circle objects properties to 0. A Circle object is empty if its diameter is less than or equal to 0.
* @method setEmpty
* @return {Circle} This Circle object
**/
set empty(value: bool) {
return this.setTo(0, 0, 0);
}
/**
* Adjusts the location of the Circle object, as determined by its center coordinate, by the specified amounts.
* @method offset
* @param {Number} dx Moves the x value of the Circle object by this amount.
* @param {Number} dy Moves the y value of the Circle object by this amount.
* @return {Circle} This Circle object.
**/
public offset(dx: number, dy: number): Circle {
this.x += dx;
this.y += dy;
return this;
}
/**
* Adjusts the location of the Circle object using a Point object as a parameter. This method is similar to the Circle.offset() method, except that it takes a Point object as a parameter.
* @method offsetPoint
* @param {Point} point A Point object to use to offset this Circle object.
* @return {Circle} This Circle object.
**/
public offsetPoint(point: Point): Circle {
return this.offset(point.x, point.y);
}
/**
* Returns a string representation of this object.
* @method toString
* @return {string} a string representation of the instance.
**/
public toString(): string {
return "[{Circle (x=" + this.x + " y=" + this.y + " diameter=" + this.diameter + " radius=" + this.radius + ")}]";
}
}

91
Phaser/core/Point.ts Normal file
View file

@ -0,0 +1,91 @@
/// <reference path="../Game.ts" />
/**
* Phaser - Point
*
* The Point object represents a location in a two-dimensional coordinate system, where x represents the horizontal axis and y represents the vertical axis.
*/
module Phaser {
export class Point {
/**
* Creates a new Point. If you pass no parameters a Point is created set to (0,0).
* @class Point
* @constructor
* @param {Number} x The horizontal position of this Point (default 0)
* @param {Number} y The vertical position of this Point (default 0)
**/
constructor(x: number = 0, y: number = 0) {
this.x = x;
this.y = y;
}
x: number;
y: number;
/**
* Copies the x and y properties from any given object to this Point.
* @method copyFrom
* @param {any} source - The object to copy from.
* @return {Point} This Point object.
**/
public copyFrom(source: any): Point {
return this.setTo(source.x, source.y);
}
/**
* Copies the x and y values from this Point to any given object.
* @method copyTo
* @param {any} target - The object to copy to.
* @return {any} The target object.
**/
public copyTo(target: any): Point {
target.x = this.x;
target.y = this.y;
return target;
}
/**
* Inverts the x and y values of this Point
* @method invert
* @return {Point} This Point object.
**/
public invert(): Point {
return this.setTo(this.y, this.x);
}
/**
* Sets the x and y values of this MicroPoint object to the given coordinates.
* @method setTo
* @param {Number} x - The horizontal position of this point.
* @param {Number} y - The vertical position of this point.
* @return {MicroPoint} This MicroPoint object. Useful for chaining method calls.
**/
public setTo(x: number, y: number): Point {
this.x = x;
this.y = y;
return this;
}
/**
* Returns a string representation of this object.
* @method toString
* @return {string} a string representation of the instance.
**/
public toString(): string {
return '[{Point (x=' + this.x + ' y=' + this.y + ')}]';
}
}
}

314
Phaser/core/Rectangle.ts Normal file
View file

@ -0,0 +1,314 @@
/// <reference path="Point.ts" />
/**
* Rectangle
*
* @desc A Rectangle object is an area defined by its position, as indicated by its top-left corner (x,y) and width and height.
*
* @version 1.6 - 24th May 2013
* @author Richard Davey
*/
module Phaser {
export class Rectangle {
/**
* Creates a new Rectangle object with the top-left corner specified by the x and y parameters and with the specified width and height parameters. If you call this function without parameters, a rectangle with x, y, width, and height properties set to 0 is created.
* @class Rectangle
* @constructor
* @param {Number} x The x coordinate of the top-left corner of the rectangle.
* @param {Number} y The y coordinate of the top-left corner of the rectangle.
* @param {Number} width The width of the rectangle in pixels.
* @param {Number} height The height of the rectangle in pixels.
* @return {Rectangle} This rectangle object
**/
constructor(x: number = 0, y: number = 0, width: number = 0, height: number = 0) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
/**
* The x coordinate of the top-left corner of the rectangle
* @property x
* @type Number
**/
x: number;
/**
* The y coordinate of the top-left corner of the rectangle
* @property y
* @type Number
**/
y: number;
/**
* The width of the rectangle in pixels
* @property width
* @type Number
**/
width: number;
/**
* The height of the rectangle in pixels
* @property height
* @type Number
**/
height: number;
/**
* Half of the width of the rectangle
* @property halfWidth
* @type Number
**/
get halfWidth(): number {
return Math.round(this.width / 2);
}
/**
* Half of the height of the rectangle
* @property halfHeight
* @type Number
**/
get halfHeight(): number {
return Math.round(this.height / 2);
}
/**
* The sum of the y and height properties. Changing the bottom property of a Rectangle object has no effect on the x, y and width properties, but does change the height property.
* @method bottom
* @return {Number}
**/
get bottom(): number {
return this.y + this.height;
}
/**
* The sum of the y and height properties. Changing the bottom property of a Rectangle object has no effect on the x, y and width properties, but does change the height property.
* @method bottom
* @param {Number} value
**/
set bottom(value: number) {
if (value <= this.y)
{
this.height = 0;
}
else
{
this.height = (this.y - value);
}
}
/**
* Sets the bottom-right corner of the Rectangle, determined by the values of the given Point object.
* @method bottomRight
* @param {Point} value
**/
set bottomRight(value: Point) {
this.right = value.x;
this.bottom = value.y;
}
/**
* The x coordinate of the left of the Rectangle. Changing the left property of a Rectangle object has no effect on the y and height properties. However it does affect the width property, whereas changing the x value does not affect the width property.
* @method left
* @ return {number}
**/
get left(): number {
return this.x;
}
/**
* The x coordinate of the left of the Rectangle. Changing the left property of a Rectangle object has no effect on the y and height properties.
* However it does affect the width, whereas changing the x value does not affect the width property.
* @method left
* @param {Number} value
**/
set left(value: number) {
if (value >= this.right)
{
this.width = 0;
}
else
{
this.width = this.right - value;
}
this.x = value;
}
/**
* The sum of the x and width properties. Changing the right property of a Rectangle object has no effect on the x, y and height properties.
* However it does affect the width property.
* @method right
* @return {Number}
**/
get right(): number {
return this.x + this.width;
}
/**
* The sum of the x and width properties. Changing the right property of a Rectangle object has no effect on the x, y and height properties.
* However it does affect the width property.
* @method right
* @param {Number} value
**/
set right(value: number) {
if (value <= this.x)
{
this.width = 0;
}
else
{
this.width = this.x + value;
}
}
/**
* The volume of the Rectangle derived from width * height
* @method volume
* @return {Number}
**/
get volume(): number {
return this.width * this.height;
}
/**
* The perimeter size of the Rectangle. This is the sum of all 4 sides.
* @method perimeter
* @return {Number}
**/
get perimeter(): number {
return (this.width * 2) + (this.height * 2);
}
/**
* The y coordinate of the top of the Rectangle. Changing the top property of a Rectangle object has no effect on the x and width properties.
* However it does affect the height property, whereas changing the y value does not affect the height property.
* @method top
* @return {Number}
**/
get top(): number {
return this.y;
}
/**
* The y coordinate of the top of the Rectangle. Changing the top property of a Rectangle object has no effect on the x and width properties.
* However it does affect the height property, whereas changing the y value does not affect the height property.
* @method top
* @param {Number} value
**/
set top(value: number) {
if (value >= this.bottom)
{
this.height = 0;
this.y = value;
}
else
{
this.height = (this.bottom - value);
}
}
/**
* The location of the Rectangles top-left corner, determined by the x and y coordinates of the Point.
* @method topLeft
* @param {Point} value
**/
set topLeft(value: Point) {
this.x = value.x;
this.y = value.y;
}
/**
* Determines whether or not this Rectangle object is empty.
* @method isEmpty
* @return {Boolean} A value of true if the Rectangle object's width or height is less than or equal to 0; otherwise false.
**/
get empty(): bool {
return (!this.width || !this.height);
}
/**
* Sets all of the Rectangle object's properties to 0. A Rectangle object is empty if its width or height is less than or equal to 0.
* @method setEmpty
* @return {Rectangle} This rectangle object
**/
set empty(value: bool) {
return this.setTo(0, 0, 0, 0);
}
/**
* Adjusts the location of the Rectangle object, as determined by its top-left corner, by the specified amounts.
* @method offset
* @param {Number} dx Moves the x value of the Rectangle object by this amount.
* @param {Number} dy Moves the y value of the Rectangle object by this amount.
* @return {Rectangle} This Rectangle object.
**/
offset(dx: number, dy: number): Rectangle {
this.x += dx;
this.y += dy;
return this;
}
/**
* Adjusts the location of the Rectangle object using a Point object as a parameter. This method is similar to the Rectangle.offset() method, except that it takes a Point object as a parameter.
* @method offsetPoint
* @param {Point} point A Point object to use to offset this Rectangle object.
* @return {Rectangle} This Rectangle object.
**/
offsetPoint(point: Point): Rectangle {
return this.offset(point.x, point.y);
}
/**
* Sets the members of Rectangle to the specified values.
* @method setTo
* @param {Number} x The x coordinate of the top-left corner of the rectangle.
* @param {Number} y The y coordinate of the top-left corner of the rectangle.
* @param {Number} width The width of the rectangle in pixels.
* @param {Number} height The height of the rectangle in pixels.
* @return {Rectangle} This rectangle object
**/
setTo(x: number, y: number, width: number, height: number): Rectangle {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
return this;
}
/**
* Returns a string representation of this object.
* @method toString
* @return {string} a string representation of the instance.
**/
toString(): string {
return "[{Rectangle (x=" + this.x + " y=" + this.y + " width=" + this.width + " height=" + this.height + " empty=" + this.empty + ")}]";
}
}
}

234
Phaser/core/Vec2.ts Normal file
View file

@ -0,0 +1,234 @@
/// <reference path="../Game.ts" />
/**
* Phaser - Vec2
*
* A Circle object is an area defined by its position, as indicated by its center point (x,y) and diameter.
*/
module Phaser {
export class Vec2 {
/**
* Creates a new Vec2 object.
* @class Vec2
* @constructor
* @param {Number} x The x position of the vector
* @param {Number} y The y position of the vector
* @return {Vec2} This object
**/
constructor(x: number = 0, y: number = 0) {
this.x = x;
this.y = y;
}
/**
* The x coordinate of the vector
* @property x
* @type Number
**/
public x: number;
/**
* The y coordinate of the vector
* @property y
* @type Number
**/
public y: number;
/**
* Sets the x and y properties of the Vector.
* @param {Number} x The x position of the vector
* @param {Number} y The y position of the vector
* @return {Vec2} This object
**/
public setTo(x: number, y: number): Vec2 {
this.x = x;
this.y = y;
return this;
}
/**
* Add another vector to this one.
*
* @param {Vec2} other The other Vector.
* @return {Vec2} This for chaining.
*/
public add(a: Vec2): Vec2 {
this.x += a.x;
this.y += a.y;
return this;
}
/**
* Subtract another vector from this one.
*
* @param {Vec2} other The other Vector.
* @return {Vec2} This for chaining.
*/
public subtract(v: Vec2): Vec2 {
this.x -= v.x;
this.y -= v.y;
return this;
}
/**
* Multiply another vector with this one.
*
* @param {Vec2} other The other Vector.
* @return {Vec2} This for chaining.
*/
public multiply(v: Vec2): Vec2 {
this.x *= v.x;
this.y *= v.y;
return this;
}
/**
* Divide this vector by another one.
*
* @param {Vec2} other The other Vector.
* @return {Vec2} This for chaining.
*/
public divide(v: Vec2): Vec2 {
this.x /= v.x;
this.y /= v.y;
return this;
}
/**
* Get the length of this vector.
*
* @return {number} The length of this vector.
*/
public length(): number {
return Math.sqrt((this.x * this.x) + (this.y * this.y));
}
/**
* Get the length squared of this vector.
*
* @return {number} The length^2 of this vector.
*/
public lengthSq(): number {
return (this.x * this.x) + (this.y * this.y);
}
/**
* The dot product of two 2D vectors.
*
* @param {Vec2} a Reference to a source Vec2 object.
* @return {Number}
*/
public dot(a: Vec2): number {
return ((this.x * a.x) + (this.y * a.y));
}
/**
* The cross product of two 2D vectors.
*
* @param {Vec2} a Reference to a source Vec2 object.
* @return {Number}
*/
public cross(a: Vec2): number {
return ((this.x * a.y) - (this.y * a.x));
}
/**
* The projection magnitude of two 2D vectors.
*
* @param {Vec2} a Reference to a source Vec2 object.
* @return {Number}
*/
public projectionLength(a: Vec2): number {
var den: number = a.dot(a);
if (den == 0)
{
return 0;
}
else
{
return Math.abs(this.dot(a) / den);
}
}
/**
* The angle between two 2D vectors.
*
* @param {Vec2} a Reference to a source Vec2 object.
* @return {Number}
*/
public angle(a: Vec2): number {
return Math.atan2(a.x * this.y - a.y * this.x, a.x * this.x + a.y * this.y);
}
/**
* Scale this vector.
*
* @param {number} x The scaling factor in the x direction.
* @param {?number=} y The scaling factor in the y direction. If this is not specified, the x scaling factor will be used.
* @return {Vec2} This for chaining.
*/
public scale(x: number, y?:number): Vec2 {
this.x *= x;
this.y *= y || x;
return this;
}
/**
* Divide this vector by the given scalar.
*
* @param {number} scalar
* @return {Vec2} This for chaining.
*/
public divideByScalar(scalar: number): Vec2 {
this.x /= scalar;
this.y /= scalar;
return this;
}
/**
* Reverse this vector.
*
* @return {Vec2} This for chaining.
*/
public reverse(): Vec2 {
this.x = -this.x;
this.y = -this.y;
return this;
}
/**
* Returns a string representation of this object.
* @method toString
* @return {string} a string representation of the object.
**/
public toString(): string {
return "[{Vec2 (x=" + this.x + " y=" + this.y + ")}]";
}
}
}

134
Phaser/geom/CircleUtils.ts Normal file
View file

@ -0,0 +1,134 @@
/// <reference path="../Game.ts" />
/// <reference path="../core/Point.ts" />
/// <reference path="../core/Rectangle.ts" />
/// <reference path="../core/Circle.ts" />
/**
* Phaser - CircleUtils
*
* A collection of methods useful for manipulating and comparing Circle objects.
*
* TODO:
*/
module Phaser {
export class CircleUtils {
/**
* Returns a new Circle object with the same values for the x, y, width, and height properties as the original Circle object.
* @method clone
* @param {Circle} a - The Circle object.
* @param {Circle} [optional] out Optional Circle object. If given the values will be set into the object, otherwise a brand new Circle object will be created and returned.
* @return {Phaser.Circle}
**/
static clone(a: Circle, out?: Circle = new Circle): Circle {
return out.setTo(a.x, a.y, a.diameter);
}
/**
* Return true if the given x/y coordinates are within the Circle object.
* If you need details about the intersection then use Phaser.Intersect.circleContainsPoint instead.
* @method contains
* @param {Circle} a - The Circle object.
* @param {Number} The X value of the coordinate to test.
* @param {Number} The Y value of the coordinate to test.
* @return {Boolean} True if the coordinates are within this circle, otherwise false.
**/
static contains(a: Circle, x: number, y: number): bool {
return (a.radius * a.radius >= Collision.distanceSquared(a.x, a.y, x, y));
}
/**
* Return true if the coordinates of the given Point object are within this Circle object.
* If you need details about the intersection then use Phaser.Intersect.circleContainsPoint instead.
* @method containsPoint
* @param {Circle} a - The Circle object.
* @param {Point} The Point object to test.
* @return {Boolean} True if the coordinates are within this circle, otherwise false.
**/
static containsPoint(a: Circle, point:Point): bool {
return CircleUtils.contains(a, point.x, point.y);
}
/**
* Return true if the given Circle is contained entirely within this Circle object.
* If you need details about the intersection then use Phaser.Intersect.circleToCircle instead.
* @method containsCircle
* @param {Circle} The Circle object to test.
* @return {Boolean} True if the coordinates are within this circle, otherwise false.
**/
static containsCircle(a:Circle, b:Circle): bool {
return ((a.radius + b.radius) * (a.radius + b.radius)) >= Collision.distanceSquared(a.x, a.y, b.x, b.y);
}
/**
* Returns the distance from the center of the Circle object to the given object (can be Circle, Point or anything with x/y properties)
* @method distanceBetween
* @param {Circle} a - The Circle object.
* @param {Circle} b - The target object. Must have visible x and y properties that represent the center of the object.
* @param {Boolean} [optional] round - Round the distance to the nearest integer (default false)
* @return {Number} The distance between this Point object and the destination Point object.
**/
static distanceBetween(a:Circle, target: any, round?: bool = false): number {
var dx = a.x - target.x;
var dy = a.y - target.y;
if (round === true)
{
return Math.round(Math.sqrt(dx * dx + dy * dy));
}
else
{
return Math.sqrt(dx * dx + dy * dy);
}
}
/**
* Determines whether the two Circle objects match. This method compares the x, y and diameter properties.
* @method equals
* @param {Circle} a - The first Circle object.
* @param {Circle} b - The second Circle object.
* @return {Boolean} A value of true if the object has exactly the same values for the x, y and diameter properties as this Circle object; otherwise false.
**/
static equals(a:Circle, b: Circle): bool {
return (a.x == b.x && a.y == b.y && a.diameter == b.diameter);
}
/**
* Determines whether the two Circle objects intersect.
* This method checks the radius distances between the two Circle objects to see if they intersect.
* @method intersects
* @param {Circle} a - The first Circle object.
* @param {Circle} b - The second Circle object.
* @return {Boolean} A value of true if the specified object intersects with this Circle object; otherwise false.
**/
static intersects(a:Circle, b: Circle): bool {
return (CircleUtils.distanceBetween(a, b) <= (a.radius + b.radius));
}
/**
* Returns a Point object containing the coordinates of a point on the circumference of the Circle based on the given angle.
* @method circumferencePoint
* @param {Circle} a - The first Circle object.
* @param {Number} angle The angle in radians (unless asDegrees is true) to return the point from.
* @param {Boolean} asDegrees Is the given angle in radians (false) or degrees (true)?
* @param {Phaser.Point} [optional] output An optional Point object to put the result in to. If none specified a new Point object will be created.
* @return {Phaser.Point} The Point object holding the result.
**/
static circumferencePoint(a:Circle, angle: number, asDegrees: bool = false, out?: Point = new Point): Point {
if (asDegrees === true)
{
angle = angle * GameMath.DEG_TO_RAD;
}
return out.setTo(a.x + a.radius * Math.cos(angle), a.y + a.radius * Math.sin(angle));
}
}
}

View file

@ -1,170 +0,0 @@
/// <reference path="../Game.ts" />
/**
* Phaser - MicroPoint
*
* The MicroPoint object represents a location in a two-dimensional coordinate system,
* where x represents the horizontal axis and y represents the vertical axis.
* It is different to the Point class in that it doesn't contain any of the help methods like add/substract/distanceTo, etc.
* Use a MicroPoint when all you literally need is a solid container for x and y (such as in the Rectangle class).
*/
module Phaser {
export class MicroPoint {
/**
* Creates a new point. If you pass no parameters to this method, a point is created at (0,0).
* @class MicroPoint
* @constructor
* @param {Number} x The horizontal position of this point (default 0)
* @param {Number} y The vertical position of this point (default 0)
**/
constructor(x: number = 0, y: number = 0, parent?:any = null) {
this._x = x;
this._y = y;
this.parent = parent;
}
private _x: number;
private _y: number;
public parent: any;
/**
* The x coordinate of the top-left corner of the rectangle
* @property x
* @type {Number}
**/
public get x(): number {
return this._x;
}
/**
* The y coordinate of the top-left corner of the rectangle
* @property y
* @type {Number}
**/
public get y(): number {
return this._y;
}
/**
* The x coordinate of the top-left corner of the rectangle
* @property x
* @type {Number}
**/
public set x(value: number) {
this._x = value;
if (this.parent)
{
this.parent.updateBounds();
}
}
/**
* The y coordinate of the top-left corner of the rectangle
* @property y
* @type {Number}
**/
public set y(value:number) {
this._y = value;
if (this.parent)
{
this.parent.updateBounds();
}
}
/**
* Copies the x and y values from any given object to this MicroPoint.
* @method copyFrom
* @param {any} source - The object to copy from.
* @return {MicroPoint} This MicroPoint object. Useful for chaining method calls.
**/
public copyFrom(source: any): MicroPoint {
return this.setTo(source.x, source.y);
}
/**
* Copies the x and y values from this MicroPoint to any given object.
* @method copyTo
* @param {any} target - The object to copy to.
* @return {any} The target object.
**/
public copyTo(target: any): MicroPoint {
target.x = this._x;
target.y = this._y;
return target;
}
/**
* Sets the x and y values of this MicroPoint object to the given coordinates.
* @method setTo
* @param {Number} x - The horizontal position of this point.
* @param {Number} y - The vertical position of this point.
* @return {MicroPoint} This MicroPoint object. Useful for chaining method calls.
**/
public setTo(x: number, y: number, callParent?:bool = true): MicroPoint {
this._x = x;
this._y = y;
if (this.parent != null && callParent == true)
{
this.parent.updateBounds();
}
return this;
}
/**
* Determines whether this MicroPoint object and the given object are equal. They are equal if they have the same x and y values.
* @method equals
* @param {any} point - The object to compare against. Must have x and y properties.
* @return {Boolean} A value of true if the object is equal to this MicroPoin object; false if it is not equal.
**/
public equals(toCompare): bool {
if (this._x === toCompare.x && this._y === toCompare.y)
{
return true;
}
else
{
return false;
}
}
/**
* Returns a string representation of this object.
* @method toString
* @return {string} a string representation of the instance.
**/
public toString(): string {
return '[{MicroPoint (x=' + this._x + ' y=' + this._y + ')}]';
}
}
}

View file

@ -1,367 +0,0 @@
/// <reference path="../Game.ts" />
/**
* Phaser - Point
*
* The Point object represents a location in a two-dimensional coordinate system, where x represents the horizontal axis and y represents the vertical axis.
*/
module Phaser {
export class Point {
/**
* Creates a new point. If you pass no parameters to this method, a point is created at (0,0).
* @class Point
* @constructor
* @param {Number} x The horizontal position of this point (default 0)
* @param {Number} y The vertical position of this point (default 0)
**/
constructor(x: number = 0, y: number = 0) {
this.setTo(x, y);
}
/**
* The horizontal position of this point (default 0)
* @property x
* @type {Number}
**/
public x: number;
/**
* The vertical position of this point (default 0)
* @property y
* @type {Number}
**/
public y: number;
/**
* Adds the coordinates of another point to the coordinates of this point to create a new point.
* @method add
* @param {Point} point - The point to be added.
* @return {Point} The new Point object.
**/
public add(toAdd: Point, output?: Point = new Point): Point {
return output.setTo(this.x + toAdd.x, this.y + toAdd.y);
}
/**
* Adds the given values to the coordinates of this point and returns it
* @method addTo
* @param {Number} x - The amount to add to the x value of the point
* @param {Number} y - The amount to add to the x value of the point
* @return {Point} This Point object.
**/
public addTo(x?: number = 0, y?: number = 0): Point {
return this.setTo(this.x + x, this.y + y);
}
/**
* Adds the given values to the coordinates of this point and returns it
* @method addTo
* @param {Number} x - The amount to add to the x value of the point
* @param {Number} y - The amount to add to the x value of the point
* @return {Point} This Point object.
**/
public subtractFrom(x?: number = 0, y?: number = 0): Point {
return this.setTo(this.x - x, this.y - y);
}
/**
* Inverts the x and y values of this point
* @method invert
* @return {Point} This Point object.
**/
public invert(): Point {
return this.setTo(this.y, this.x);
}
/**
* Clamps this Point object to be between the given min and max
* @method clamp
* @param {number} The minimum value to clamp this Point to
* @param {number} The maximum value to clamp this Point to
* @return {Point} This Point object.
**/
public clamp(min: number, max: number): Point {
this.clampX(min, max);
this.clampY(min, max);
return this;
}
/**
* Clamps the x value of this Point object to be between the given min and max
* @method clampX
* @param {number} The minimum value to clamp this Point to
* @param {number} The maximum value to clamp this Point to
* @return {Point} This Point object.
**/
public clampX(min: number, max: number): Point {
this.x = Math.max(Math.min(this.x, max), min);
return this;
}
/**
* Clamps the y value of this Point object to be between the given min and max
* @method clampY
* @param {number} The minimum value to clamp this Point to
* @param {number} The maximum value to clamp this Point to
* @return {Point} This Point object.
**/
public clampY(min: number, max: number): Point {
this.x = Math.max(Math.min(this.x, max), min);
this.y = Math.max(Math.min(this.y, max), min);
return this;
}
/**
* Creates a copy of this Point.
* @method clone
* @param {Point} output Optional Point object. If given the values will be set into this object, otherwise a brand new Point object will be created and returned.
* @return {Point} The new Point object.
**/
public clone(output?: Point = new Point): Point {
return output.setTo(this.x, this.y);
}
/**
* Copies the point data from the source Point object into this Point object.
* @method copyFrom
* @param {Point} source - The point to copy from.
* @return {Point} This Point object. Useful for chaining method calls.
**/
public copyFrom(source: Point): Point {
return this.setTo(source.x, source.y);
}
/**
* Copies the point data from this Point object to the given target Point object.
* @method copyTo
* @param {Point} target - The point to copy to.
* @return {Point} The target Point object.
**/
public copyTo(target: Point): Point {
return target.setTo(this.x, this.y);
}
/**
* Returns the distance from this Point object to the given Point object.
* @method distanceFrom
* @param {Point} target - The destination Point object.
* @param {Boolean} round - Round the distance to the nearest integer (default false)
* @return {Number} The distance between this Point object and the destination Point object.
**/
public distanceTo(target: Point, round?: bool = false): number {
var dx = this.x - target.x;
var dy = this.y - target.y;
if (round === true)
{
return Math.round(Math.sqrt(dx * dx + dy * dy));
}
else
{
return Math.sqrt(dx * dx + dy * dy);
}
}
/**
* Returns the distance between the two Point objects.
* @method distanceBetween
* @param {Point} pointA - The first Point object.
* @param {Point} pointB - The second Point object.
* @param {Boolean} round - Round the distance to the nearest integer (default false)
* @return {Number} The distance between the two Point objects.
**/
public static distanceBetween(pointA: Point, pointB: Point, round?: bool = false): number {
var dx: number = pointA.x - pointB.x;
var dy: number = pointA.y - pointB.y;
if (round === true)
{
return Math.round(Math.sqrt(dx * dx + dy * dy));
}
else
{
return Math.sqrt(dx * dx + dy * dy);
}
}
/**
* Returns true if the distance between this point and a target point is greater than or equal a specified distance.
* This avoids using a costly square root operation
* @method distanceCompare
* @param {Point} target - The Point object to use for comparison.
* @param {Number} distance - The distance to use for comparison.
* @return {Boolena} True if distance is >= specified distance.
**/
public distanceCompare(target: Point, distance: number): bool {
if (this.distanceTo(target) >= distance)
{
return true;
}
else
{
return false;
}
}
/**
* Determines whether this Point object and the given point object are equal. They are equal if they have the same x and y values.
* @method equals
* @param {Point} point - The point to compare against.
* @return {Boolean} A value of true if the object is equal to this Point object; false if it is not equal.
**/
public equals(toCompare: Point): bool {
if (this.x === toCompare.x && this.y === toCompare.y)
{
return true;
}
else
{
return false;
}
}
/**
* Determines a point between two specified points. The parameter f determines where the new interpolated point is located relative to the two end points specified by parameters pt1 and pt2.
* The closer the value of the parameter f is to 1.0, the closer the interpolated point is to the first point (parameter pt1). The closer the value of the parameter f is to 0, the closer the interpolated point is to the second point (parameter pt2).
* @method interpolate
* @param {Point} pointA - The first Point object.
* @param {Point} pointB - The second Point object.
* @param {Number} f - The level of interpolation between the two points. Indicates where the new point will be, along the line between pt1 and pt2. If f=1, pt1 is returned; if f=0, pt2 is returned.
* @return {Point} The new interpolated Point object.
**/
public interpolate(pointA, pointB, f) {
}
/**
* Offsets the Point object by the specified amount. The value of dx is added to the original value of x to create the new x value.
* The value of dy is added to the original value of y to create the new y value.
* @method offset
* @param {Number} dx - The amount by which to offset the horizontal coordinate, x.
* @param {Number} dy - The amount by which to offset the vertical coordinate, y.
* @return {Point} This Point object. Useful for chaining method calls.
**/
public offset(dx: number, dy: number): Point {
this.x += dx;
this.y += dy;
return this;
}
/**
* Converts a pair of polar coordinates to a Cartesian point coordinate.
* @method polar
* @param {Number} length - The length coordinate of the polar pair.
* @param {Number} angle - The angle, in radians, of the polar pair.
* @return {Point} The new Cartesian Point object.
**/
public polar(length, angle) {
}
/**
* Rotates the point around the x/y coordinates given to the desired angle
* @param x {number} The x coordinate of the anchor point
* @param y {number} The y coordinate of the anchor point
* @param {Number} angle The angle in radians (unless asDegrees is true) to return the point from.
* @param {Boolean} asDegrees Is the given angle in radians (false) or degrees (true)?
* @param {Number} distance An optional distance constraint between the point and the anchor
* @return The modified point object
*/
public rotate(cx: number, cy: number, angle: number, asDegrees: bool = false, distance?:number = null) {
if (asDegrees)
{
angle = angle * GameMath.DEG_TO_RAD;
}
// Get distance from origin (cx/cy) to this point
if (distance === null)
{
distance = Math.sqrt(((cx - this.x) * (cx - this.x)) + ((cy - this.y) * (cy - this.y)));
}
return this.setTo(cx + distance * Math.cos(angle), cy + distance * Math.sin(angle));
}
/**
* Sets the x and y values of this Point object to the given coordinates.
* @method setTo
* @param {Number} x - The horizontal position of this point.
* @param {Number} y - The vertical position of this point.
* @return {Point} This Point object. Useful for chaining method calls.
**/
public setTo(x: number, y: number): Point {
this.x = x;
this.y = y;
return this;
}
/**
* Subtracts the coordinates of another point from the coordinates of this point to create a new point.
* @method subtract
* @param {Point} point - The point to be subtracted.
* @param {Point} output Optional Point object. If given the values will be set into this object, otherwise a brand new Point object will be created and returned.
* @return {Point} The new Point object.
**/
public subtract(point: Point, output?: Point = new Point): Point {
return output.setTo(this.x - point.x, this.y - point.y);
}
/**
* Returns a string representation of this object.
* @method toString
* @return {string} a string representation of the instance.
**/
public toString(): string {
return '[{Point (x=' + this.x + ' y=' + this.y + ')}]';
}
}
}

221
Phaser/geom/PointUtils.ts Normal file
View file

@ -0,0 +1,221 @@
/// <reference path="../Game.ts" />
/// <reference path="../core/Point.ts" />
/**
* Phaser - PointUtils
*
* A collection of methods useful for manipulating and comparing Point objects.
*
* TODO: interpolate & polar
*/
module Phaser {
class PointUtils {
/**
* Adds the coordinates of two points together to create a new point.
* @method add
* @param {Point} a - The first Point object.
* @param {Point} b - The second Point object.
* @param {Point} out - Optional Point to store the value in, if not supplied a new Point object will be created.
* @return {Point} The new Point object.
**/
static add(a: Point, b: Point, out?: Point = new Point): Point {
return out.setTo(a.x + b.x, a.y + b.y);
}
/**
* Subtracts the coordinates of two points to create a new point.
* @method subtract
* @param {Point} a - The first Point object.
* @param {Point} b - The second Point object.
* @param {Point} out - Optional Point to store the value in, if not supplied a new Point object will be created.
* @return {Point} The new Point object.
**/
static subtract(a: Point, b: Point, out?: Point = new Point): Point {
return out.setTo(a.x - b.x, a.y - b.y);
}
/**
* Multiplies the coordinates of two points to create a new point.
* @method subtract
* @param {Point} a - The first Point object.
* @param {Point} b - The second Point object.
* @param {Point} out - Optional Point to store the value in, if not supplied a new Point object will be created.
* @return {Point} The new Point object.
**/
static multiply(a: Point, b: Point, out?: Point = new Point): Point {
return out.setTo(a.x * b.x, a.y * b.y);
}
/**
* Divides the coordinates of two points to create a new point.
* @method subtract
* @param {Point} a - The first Point object.
* @param {Point} b - The second Point object.
* @param {Point} out - Optional Point to store the value in, if not supplied a new Point object will be created.
* @return {Point} The new Point object.
**/
static divide(a: Point, b: Point, out?: Point = new Point): Point {
return out.setTo(a.x / b.x, a.y / b.y);
}
/**
* Clamps the Point object values to be between the given min and max
* @method clamp
* @param {Point} a - The point.
* @param {number} The minimum value to clamp this Point to
* @param {number} The maximum value to clamp this Point to
* @return {Point} This Point object.
**/
static clamp(a: Point, min: number, max: number): Point {
PointUtils.clampX(a, min, max);
PointUtils.clampY(a, min, max);
return a;
}
/**
* Clamps the x value of the given Point object to be between the min and max values.
* @method clampX
* @param {Point} a - The point.
* @param {number} The minimum value to clamp this Point to
* @param {number} The maximum value to clamp this Point to
* @return {Point} This Point object.
**/
static clampX(a: Point, min: number, max: number): Point {
a.x = Math.max(Math.min(a.x, max), min);
return a;
}
/**
* Clamps the y value of the given Point object to be between the min and max values.
* @method clampY
* @param {Point} a - The point.
* @param {number} The minimum value to clamp this Point to
* @param {number} The maximum value to clamp this Point to
* @return {Point} This Point object.
**/
static clampY(a: Point, min: number, max: number): Point {
a.y = Math.max(Math.min(a.y, max), min);
return a;
}
/**
* Creates a copy of the given Point.
* @method clone
* @param {Point} output Optional Point object. If given the values will be set into this object, otherwise a brand new Point object will be created and returned.
* @return {Point} The new Point object.
**/
static clone(a: Point, output?: Point = new Point): Point {
return output.setTo(a.x, a.y);
}
/**
* Returns the distance between the two given Point objects.
* @method distanceBetween
* @param {Point} a - The first Point object.
* @param {Point} b - The second Point object.
* @param {Boolean} round - Round the distance to the nearest integer (default false)
* @return {Number} The distance between the two Point objects.
**/
static distanceBetween(a: Point, b: Point, round?: bool = false): number {
var dx = a.x - b.x;
var dy = a.y - b.y;
if (round === true)
{
return Math.round(Math.sqrt(dx * dx + dy * dy));
}
else
{
return Math.sqrt(dx * dx + dy * dy);
}
}
/**
* Determines whether the two given Point objects are equal. They are considered equal if they have the same x and y values.
* @method equals
* @param {Point} a - The first Point object.
* @param {Point} b - The second Point object.
* @return {Boolean} A value of true if the Points are equal, otherwise false.
**/
static equals(a: Point, b: Point): bool {
return (a.x == b.x && a.y == b.y);
}
/**
* Determines a point between two specified points. The parameter f determines where the new interpolated point is located relative to the two end points specified by parameters pt1 and pt2.
* The closer the value of the parameter f is to 1.0, the closer the interpolated point is to the first point (parameter pt1). The closer the value of the parameter f is to 0, the closer the interpolated point is to the second point (parameter pt2).
* @method interpolate
* @param {Point} pointA - The first Point object.
* @param {Point} pointB - The second Point object.
* @param {Number} f - The level of interpolation between the two points. Indicates where the new point will be, along the line between pt1 and pt2. If f=1, pt1 is returned; if f=0, pt2 is returned.
* @return {Point} The new interpolated Point object.
**/
//static interpolate(pointA, pointB, f) {
//}
/**
* Converts a pair of polar coordinates to a Cartesian point coordinate.
* @method polar
* @param {Number} length - The length coordinate of the polar pair.
* @param {Number} angle - The angle, in radians, of the polar pair.
* @return {Point} The new Cartesian Point object.
**/
//static polar(length, angle) {
//}
/**
* Rotates a Point around the x/y coordinates given to the desired angle.
* @param a {Point} The Point object to rotate.
* @param x {number} The x coordinate of the anchor point
* @param y {number} The y coordinate of the anchor point
* @param {Number} angle The angle in radians (unless asDegrees is true) to rotate the Point to.
* @param {Boolean} asDegrees Is the given angle in radians (false) or degrees (true)?
* @param {Number} distance An optional distance constraint between the Point and the anchor.
* @return The modified point object
*/
static rotate(a: Point, x: number, y: number, angle: number, asDegrees: bool = false, distance?: number = null): Point {
if (asDegrees)
{
angle = angle * GameMath.DEG_TO_RAD;
}
// Get distance from origin (cx/cy) to this point
if (distance === null)
{
distance = Math.sqrt(((x - a.x) * (x - a.x)) + ((y - a.y) * (y - a.y)));
}
return a.setTo(x + distance * Math.cos(angle), y + distance * Math.sin(angle));
}
/**
* Rotates a Point around the given Point to the desired angle.
* @param a {Point} The Point object to rotate.
* @param b {Point} The Point object to serve as point of rotation.
* @param x {number} The x coordinate of the anchor point
* @param y {number} The y coordinate of the anchor point
* @param {Number} angle The angle in radians (unless asDegrees is true) to rotate the Point to.
* @param {Boolean} asDegrees Is the given angle in radians (false) or degrees (true)?
* @param {Number} distance An optional distance constraint between the Point and the anchor.
* @return The modified point object
*/
static rotateAroundPoint(a: Point, b: Point, angle: number, asDegrees: bool = false, distance?: number = null): Point {
return PointUtils.rotate(a, b.x, b.y, angle, asDegrees, distance);
}
}
}

View file

@ -1,180 +0,0 @@
/// <reference path="../Game.ts" />
/// <reference path="Polygon.ts" />
/**
* Phaser - Quad
*
* A Quad object is an area defined by its position, as indicated by its top-left corner (x,y) and width and height.
* Very much like a Rectangle only without all of the additional methods and properties of that class.
*/
module Phaser {
export class Quad {
/**
* Creates a new Quad object with the top-left corner specified by the x and y parameters and with the specified width and height parameters. If you call this function without parameters, a rectangle with x, y, width, and height properties set to 0 is created.
* @class Quad
* @constructor
* @param {Number} x The x coordinate of the top-left corner of the quad.
* @param {Number} y The y coordinate of the top-left corner of the quad.
* @param {Number} width The width of the quad.
* @param {Number} height The height of the quad.
* @return {Quad} This object
**/
constructor(x: number = 0, y: number = 0, width: number = 0, height: number = 0) {
this.setTo(x, y, width, height);
}
public x: number;
public y: number;
public width: number;
public height: number;
/**
* Sets the Quad to the specified size.
* @method setTo
* @param {Number} x The x coordinate of the top-left corner of the quad.
* @param {Number} y The y coordinate of the top-left corner of the quad.
* @param {Number} width The width of the quad.
* @param {Number} height The height of the quad.
* @return {Quad} This object
**/
public setTo(x: number, y: number, width: number, height: number): Quad {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
return this;
}
public get left(): number {
return this.x;
}
public get right(): number {
return this.x + this.width;
}
public get top(): number {
return this.y;
}
public get bottom(): number {
return this.y + this.height;
}
public get halfWidth(): number {
return this.width / 2;
}
public get halfHeight(): number {
return this.height / 2;
}
/**
* Determines whether the object specified intersects (overlaps) with this Quad object.
* This method checks the x, y, width, and height properties of the specified Quad object to see if it intersects with this Quad object.
* @method intersects
* @param {Object} quad The object to check for intersection with this Quad. Must have left/right/top/bottom properties (Rectangle, Quad).
* @param {Number} tolerance A tolerance value to allow for an intersection test with padding, default to 0
* @return {Boolean} A value of true if the specified object intersects with this Quad; otherwise false.
**/
public intersects(quad, tolerance?: number = 0): bool {
return !(quad.left > this.right + tolerance || quad.right < this.left - tolerance || quad.top > this.bottom + tolerance || quad.bottom < this.top - tolerance);
}
/**
* Determines whether the object specified intersects (overlaps) with the given values.
* @method intersectsProps
* @param {Number} left
* @param {Number} right
* @param {Number} top
* @param {Number} bottomt
* @param {Number} tolerance A tolerance value to allow for an intersection test with padding, default to 0
* @return {Boolean} A value of true if the specified object intersects with this Quad; otherwise false.
**/
public intersectsRaw(left: number, right: number, top: number, bottom: number, tolerance?: number = 0): bool {
return !(left > this.right + tolerance || right < this.left - tolerance || top > this.bottom + tolerance || bottom < this.top - tolerance);
}
/**
* Determines whether the specified coordinates are contained within the region defined by this Quad object.
* @method contains
* @param {Number} x The x coordinate of the point to test.
* @param {Number} y The y coordinate of the point to test.
* @return {Boolean} A value of true if the Rectangle object contains the specified point; otherwise false.
**/
public contains(x: number, y: number): bool {
if (x >= this.x && x <= this.right && y >= this.y && y <= this.bottom)
{
return true;
}
return false;
}
/**
* Copies the x/y/width/height values from the source object into this Quad
* @method copyFrom
* @param {Any} source The source object to copy from. Can be a Quad, Rectangle or any object with exposed x/y/width/height properties
* @return {Quad} This object
**/
public copyFrom(source): Quad {
return this.setTo(source.x, source.y, source.width, source.height);
}
/**
* Copies the x/y/width/height values from this Quad into the given target object
* @method copyTo
* @param {Any} target The object to copy this quads values in to. Can be a Quad, Rectangle or any object with exposed x/y/width/height properties
* @return {Any} The target object
**/
public copyTo(target): any {
return target.copyFrom(this);
}
/**
* Creates and returns a Polygon that is the same as this Quad.
* @method toPolygon
* @return {Polygon} A new Polygon that represents this quad.
**/
public toPolygon(): Polygon {
return new Polygon(new Vector2(this.x, this.y), [
new Vector2(), new Vector2(this.width, 0),
new Vector2(this.width, this.height), new Vector2(0, this.height)
]);
}
/**
* Returns a string representation of this object.
* @method toString
* @return {string} a string representation of the object.
**/
public toString(): string {
return "[{Quad (x=" + this.x + " y=" + this.y + " width=" + this.width + " height=" + this.height + ")}]";
}
}
}

View file

@ -1,810 +0,0 @@
/// <reference path="../Game.ts" />
/// <reference path="MicroPoint.ts" />
/**
* Phaser - Rectangle
*
* A Rectangle object is an area defined by its position, as indicated by its top-left corner (x,y) and width and height.
*/
module Phaser {
export class Rectangle {
/**
* Creates a new Rectangle object with the top-left corner specified by the x and y parameters and with the specified width and height parameters.
* If you call this function without parameters, a rectangle with x, y, width, and height properties set to 0 is created.
* @class Rectangle
* @constructor
* @param {Number} x The x coordinate of the top-left corner of the rectangle.
* @param {Number} y The y coordinate of the top-left corner of the rectangle.
* @param {Number} width The width of the rectangle.
* @param {Number} height The height of the rectangle.
* @return {Rectangle} This rectangle object
**/
constructor(x: number = 0, y: number = 0, width: number = 0, height: number = 0) {
this._width = width;
if (width > 0)
{
this._halfWidth = Math.round(width / 2);
}
this._height = height;
if (height > 0)
{
this._halfHeight = Math.round(height / 2);
}
this.length = Math.max(this._width, this._height);
this.topLeft = new MicroPoint(x, y, this);
this.topCenter = new MicroPoint(x + this._halfWidth, y, this);
this.topRight = new MicroPoint(x + this._width - 1, y, this);
this.leftCenter = new MicroPoint(x, y + this._halfHeight, this);
this.center = new MicroPoint(x + this._halfWidth, y + this._halfHeight, this);
this.rightCenter = new MicroPoint(x + this._width - 1, y + this._halfHeight, this);
this.bottomLeft = new MicroPoint(x, y + this._height - 1, this);
this.bottomCenter = new MicroPoint(x + this._halfWidth, y + this._height - 1, this);
this.bottomRight = new MicroPoint(x + this._width - 1, y + this._height - 1, this);
}
private _tempX: number = null;
private _tempY: number = null;
private _tempWidth: number = null;
private _tempHeight: number = null;
/**
* The x coordinate of the top-left corner of the rectangle
* @property x
* @type {Number}
**/
public get x(): number {
return this.topLeft.x;
}
/**
* The y coordinate of the top-left corner of the rectangle
* @property y
* @type {Number}
**/
public get y(): number {
return this.topLeft.y;
}
/**
* The x coordinate of the top-left corner of the rectangle
* @property x
* @type {Number}
**/
public set x(value: number) {
this.topLeft.x = value;
}
/**
* The y coordinate of the top-left corner of the rectangle
* @property y
* @type {Number}
**/
public set y(value:number) {
this.topLeft.y = value;
}
/**
* The x and y coordinate of the top-left corner of the rectangle (same as x/y)
* @property topLeft
* @type {MicroPoint}
**/
public topLeft: MicroPoint;
/**
* The x and y coordinate of the top-center of the rectangle
* @property topCenter
* @type {MicroPoint}
**/
public topCenter: MicroPoint;
/**
* The x and y coordinate of the top-right corner of the rectangle
* @property topRight
* @type {MicroPoint}
**/
public topRight: MicroPoint;
/**
* The x and y coordinate of the left-center of the rectangle
* @property leftCenter
* @type {MicroPoint}
**/
public leftCenter: MicroPoint;
/**
* The x and y coordinate of the center of the rectangle
* @property center
* @type {MicroPoint}
**/
public center: MicroPoint;
/**
* The x and y coordinate of the right-center of the rectangle
* @property rightCenter
* @type {MicroPoint}
**/
public rightCenter: MicroPoint;
/**
* The x and y coordinate of the bottom-left corner of the rectangle
* @property bottomLeft
* @type {MicroPoint}
**/
public bottomLeft: MicroPoint;
/**
* The x and y coordinate of the bottom-center of the rectangle
* @property bottomCenter
* @type {MicroPoint}
**/
public bottomCenter: MicroPoint;
/**
* The x and y coordinate of the bottom-right corner of the rectangle
* @property bottomRight
* @type {MicroPoint}
**/
public bottomRight: MicroPoint;
/**
* The width of the rectangle
* @property width
* @type {Number}
**/
private _width: number = 0;
/**
* The height of the rectangle
* @property height
* @type {Number}
**/
private _height: number = 0;
/**
* Half of the width of the rectangle
* @property halfWidth
* @type {Number}
**/
private _halfWidth: number = 0;
/**
* Half of the height of the rectangle
* @property halfHeight
* @type {Number}
**/
private _halfHeight: number = 0;
/**
* The size of the longest side (width or height)
* @property length
* @type {Number}
**/
public length: number = 0;
/**
* Updates all of the MicroPoints based on the values of width and height.
* You should not normally call this directly.
**/
public updateBounds() {
if (this._tempWidth !== null)
{
this._width = this._tempWidth;
this._halfWidth = 0;
if (this._width > 0)
{
this._halfWidth = Math.round(this._width / 2);
}
}
if (this._tempHeight !== null)
{
this._height = this._tempHeight;
this._halfHeight = 0;
if (this._height > 0)
{
this._halfHeight = Math.round(this._height / 2);
}
}
this.length = Math.max(this._width, this._height);
if (this._tempX !== null && this._tempY !== null)
{
this.topLeft.setTo(this._tempX, this._tempY, false);
}
else if (this._tempX !== null && this._tempY == null)
{
this.topLeft.setTo(this._tempX, this.topLeft.y, false);
}
else if (this._tempX == null && this._tempY !== null)
{
this.topLeft.setTo(this.topLeft.x, this._tempY, false);
}
else
{
this.topLeft.setTo(this.x, this.y, false);
}
this.topCenter.setTo(this.x + this._halfWidth, this.y, false);
this.topRight.setTo(this.x + this._width - 1, this.y, false);
this.leftCenter.setTo(this.x, this.y + this._halfHeight, false);
this.center.setTo(this.x + this._halfWidth, this.y + this._halfHeight, false);
this.rightCenter.setTo(this.x + this._width - 1, this.y + this._halfHeight, false);
this.bottomLeft.setTo(this.x, this.y + this._height - 1, false);
this.bottomCenter.setTo(this.x + this._halfWidth, this.y + this._height - 1, false);
this.bottomRight.setTo(this.x + this._width - 1, this.y + this._height - 1, false);
this._tempX = null;
this._tempY = null;
this._tempWidth = null;
this._tempHeight = null;
}
/**
* The width of the rectangle
* @property width
* @type {Number}
**/
public set width(value: number) {
this._width = value;
this._halfWidth = Math.round(value / 2);
this.updateBounds();
}
/**
* The height of the rectangle
* @property height
* @type {Number}
**/
public set height(value: number) {
this._height = value;
this._halfHeight = Math.round(value / 2);
this.updateBounds();
}
/**
* The width of the rectangle
* @property width
* @type {Number}
**/
public get width(): number {
return this._width;
}
/**
* The height of the rectangle
* @property height
* @type {Number}
**/
public get height(): number {
return this._height;
}
/**
* Half of the width of the rectangle
* @property halfWidth
* @type {Number}
**/
public get halfWidth(): number {
return this._halfWidth;
}
/**
* Half of the height of the rectangle
* @property halfHeight
* @type {Number}
**/
public get halfHeight(): number {
return this._halfHeight;
}
/**
* The sum of the y and height properties.
* Changing the bottom property of a Rectangle object has no effect on the x, y and width properties, but does change the height property.
* @method bottom
* @return {Number}
**/
public get bottom(): number {
return this.bottomCenter.y;
}
/**
* The sum of the y and height properties.
* Changing the bottom property of a Rectangle object has no effect on the x, y and width properties, but does change the height property.
* @method bottom
* @param {Number} value
**/
public set bottom(value: number) {
if (value < this.y)
{
this._tempHeight = 0;
}
else
{
this._tempHeight = this.y + value;
}
this.updateBounds();
}
/**
* The x coordinate of the top-left corner of the rectangle.
* Changing the left property of a Rectangle object has no effect on the y and height properties.
* However it does affect the width property, whereas changing the x value does not affect the width property.
* @method left
* @ return {Number}
**/
public get left(): number {
return this.x;
}
/**
* The x coordinate of the top-left corner of the rectangle.
* Changing the left property of a Rectangle object has no effect on the y and height properties.
* However it does affect the width property, whereas changing the x value does not affect the width property.
* @method left
* @param {Number} value
**/
public set left(value: number) {
var diff = this.x - value;
if (this._width + diff < 0)
{
this._tempWidth = 0;
this._tempX = value;
}
else
{
this._tempWidth = this._width + diff;
this._tempX = value;
}
this.updateBounds();
}
/**
* The sum of the x and width properties.
* Changing the right property of a Rectangle object has no effect on the x, y and height properties.
* However it does affect the width property.
* @method right
* @return {Number}
**/
public get right(): number {
return this.rightCenter.x;
}
/**
* The sum of the x and width properties.
* Changing the right property of a Rectangle object has no effect on the x, y and height properties.
* However it does affect the width property.
* @method right
* @param {Number} value
**/
public set right(value: number) {
if (value < this.topLeft.x)
{
this._tempWidth = 0;
}
else
{
this._tempWidth = (value - this.topLeft.x);
}
this.updateBounds();
}
/**
* The size of the Rectangle object, expressed as a Point object with the values of the width and height properties.
* @method size
* @param {Point} output Optional Point object. If given the values will be set into the object, otherwise a brand new Point object will be created and returned.
* @return {Point} The size of the Rectangle object
**/
public size(output?: Point = new Point): Point {
return output.setTo(this._width, this._height);
}
/**
* The volume of the Rectangle object in pixels, derived from width * height
* @method volume
* @return {Number}
**/
public get volume(): number {
return this._width * this._height;
}
/**
* The perimeter size of the Rectangle object in pixels. This is the sum of all 4 sides.
* @method perimeter
* @return {Number}
**/
public get perimeter(): number {
return (this._width * 2) + (this._height * 2);
}
/**
* The y coordinate of the top-left corner of the rectangle.
* Changing the top property of a Rectangle object has no effect on the x and width properties.
* However it does affect the height property, whereas changing the y value does not affect the height property.
* @method top
* @return {Number}
**/
public get top(): number {
return this.topCenter.y;
}
/**
* The y coordinate of the top-left corner of the rectangle.
* Changing the top property of a Rectangle object has no effect on the x and width properties.
* However it does affect the height property, whereas changing the y value does not affect the height property.
* @method top
* @param {Number} value
**/
public set top(value: number) {
var diff = this.topCenter.y - value;
if (this._height + diff < 0)
{
this._tempHeight = 0;
this._tempY = value;
}
else
{
this._tempHeight = this._height + diff;
this._tempY = value;
}
this.updateBounds();
}
/**
* Returns a new Rectangle object with the same values for the x, y, width, and height properties as the original Rectangle object.
* @method clone
* @param {Rectangle} output Optional Rectangle object. If given the values will be set into the object, otherwise a brand new Rectangle object will be created and returned.
* @return {Rectangle}
**/
public clone(output?: Rectangle = new Rectangle): Rectangle {
return output.setTo(this.x, this.y, this.width, this.height);
}
/**
* Determines whether the specified coordinates are contained within the region defined by this Rectangle object.
* @method contains
* @param {Number} x The x coordinate of the point to test.
* @param {Number} y The y coordinate of the point to test.
* @return {Boolean} A value of true if the Rectangle object contains the specified point; otherwise false.
**/
public contains(x: number, y: number): bool {
if (x >= this.topLeft.x && x <= this.topRight.x && y >= this.topLeft.y && y <= this.bottomRight.y)
{
return true;
}
return false;
}
/**
* Determines whether the specified point is contained within the rectangular region defined by this Rectangle object.
* This method is similar to the Rectangle.contains() method, except that it takes a Point object as a parameter.
* @method containsPoint
* @param {Point} point The point object being checked. Can be Point or any object with .x and .y values.
* @return {Boolean} A value of true if the Rectangle object contains the specified point; otherwise false.
**/
public containsPoint(point: any): bool {
return this.contains(point.x, point.y);
}
/**
* Determines whether the Rectangle object specified by the rect parameter is contained within this Rectangle object.
* A Rectangle object is said to contain another if the second Rectangle object falls entirely within the boundaries of the first.
* @method containsRect
* @param {Rectangle} rect The rectangle object being checked.
* @return {Boolean} A value of true if the Rectangle object contains the specified point; otherwise false.
**/
public containsRect(rect: Rectangle): bool {
// If the given rect has a larger volume than this one then it can never contain it
if (rect.volume > this.volume)
{
return false;
}
if (rect.x >= this.topLeft.x && rect.y >= this.topLeft.y && rect.rightCenter.x <= this.rightCenter.x && rect.bottomCenter.y <= this.bottomCenter.y)
{
return true;
}
return false;
}
/**
* Copies all of rectangle data from the source Rectangle object into the calling Rectangle object.
* @method copyFrom
* @param {Rectangle} rect The source rectangle object to copy from
* @return {Rectangle} This rectangle object
**/
public copyFrom(source: Rectangle): Rectangle {
return this.setTo(source.x, source.y, source.width, source.height);
}
/**
* Copies all the rectangle data from this Rectangle object into the destination Rectangle object.
* @method copyTo
* @param {Rectangle} rect The destination rectangle object to copy in to
* @return {Rectangle} The destination rectangle object
**/
public copyTo(target: Rectangle): Rectangle {
return target.copyFrom(this);
}
/**
* Determines whether the object specified in the toCompare parameter is equal to this Rectangle object.
* This method compares the x, y, width, and height properties of an object against the same properties of this Rectangle object.
* @method equals
* @param {Rectangle} toCompare The rectangle to compare to this Rectangle object.
* @return {Boolean} A value of true if the object has exactly the same values for the x, y, width, and height properties as this Rectangle object; otherwise false.
**/
public equals(toCompare: Rectangle): bool {
if (this.topLeft.equals(toCompare.topLeft) && this.bottomRight.equals(toCompare.bottomRight))
{
return true;
}
return false;
}
/**
* Increases the size of the Rectangle object by the specified amounts.
* The center point of the Rectangle object stays the same, and its size increases to the left and right by the dx value,
* and to the top and the bottom by the dy value.
* @method inflate
* @param {Number} dx The amount to be added to the left side of this Rectangle.
* @param {Number} dy The amount to be added to the bottom side of this Rectangle.
* @return {Rectangle} This Rectangle object.
**/
public inflate(dx: number, dy: number): Rectangle {
this._tempX = this.topLeft.x - dx;
this._tempWidth = this._width + (2 * dx);
this._tempY = this.topLeft.y - dy;
this._tempHeight = this._height + (2 * dy);
this.updateBounds();
return this;
}
/**
* Increases the size of the Rectangle object.
* This method is similar to the Rectangle.inflate() method except it takes a Point object as a parameter.
* @method inflatePoint
* @param {Point} point The x property of this Point object is used to increase the horizontal dimension of the Rectangle object. The y property is used to increase the vertical dimension of the Rectangle object.
* @return {Rectangle} This Rectangle object.
**/
public inflatePoint(point: Point): Rectangle {
return this.inflate(point.x, point.y);
}
/**
* If the Rectangle object specified in the toIntersect parameter intersects with this Rectangle object,
* returns the area of intersection as a Rectangle object. If the rectangles do not intersect, this method
* returns an empty Rectangle object with its properties set to 0.
* @method intersection
* @param {Rectangle} toIntersect The Rectangle object to compare against to see if it intersects with this Rectangle object.
* @param {Rectangle} output Optional Rectangle object. If given the intersection values will be set into this object, otherwise a brand new Rectangle object will be created and returned.
* @return {Rectangle} A Rectangle object that equals the area of intersection. If the rectangles do not intersect, this method returns an empty Rectangle object; that is, a rectangle with its x, y, width, and height properties set to 0.
**/
public intersection(toIntersect: Rectangle, output?: Rectangle = new Rectangle): Rectangle {
if (this.intersects(toIntersect) === true)
{
output.x = Math.max(toIntersect.topLeft.x, this.topLeft.x);
output.y = Math.max(toIntersect.topLeft.y, this.topLeft.y);
output.width = Math.min(toIntersect.rightCenter.x, this.rightCenter.x) - output.x;
output.height = Math.min(toIntersect.bottomCenter.y, this.bottomCenter.y) - output.y;
}
return output;
}
/**
* Determines whether the object specified intersects (overlaps) with this Rectangle object.
* This method checks the x, y, width, and height properties of the specified Rectangle object to see if it intersects with this Rectangle object.
* @method intersects
* @param {Rectangle} r2 The Rectangle object to compare against to see if it intersects with this Rectangle object.
* @param {Number} t A tolerance value to allow for an intersection test with padding, default to 0
* @return {Boolean} A value of true if the specified object intersects with this Rectangle object; otherwise false.
**/
public intersects(r2: Rectangle, t?: number = 0): bool {
return !(r2.left > this.right + t || r2.right < this.left - t || r2.top > this.bottom + t || r2.bottom < this.top - t);
}
/**
* Determines whether or not this Rectangle object is empty.
* @method isEmpty
* @return {Boolean} A value of true if the Rectangle object's width or height is less than or equal to 0; otherwise false.
**/
public get isEmpty(): bool {
if (this.width < 1 || this.height < 1)
{
return true;
}
return false;
}
/**
* Adjusts the location of the Rectangle object, as determined by its top-left corner, by the specified amounts.
* @method offset
* @param {Number} dx Moves the x value of the Rectangle object by this amount.
* @param {Number} dy Moves the y value of the Rectangle object by this amount.
* @return {Rectangle} This Rectangle object.
**/
public offset(dx: number, dy: number): Rectangle {
if (!isNaN(dx) && !isNaN(dy))
{
this.x += dx;
this.y += dy;
}
return this;
}
/**
* Adjusts the location of the Rectangle object using a Point object as a parameter. This method is similar to the Rectangle.offset() method, except that it takes a Point object as a parameter.
* @method offsetPoint
* @param {Point} point A Point object to use to offset this Rectangle object.
* @return {Rectangle} This Rectangle object.
**/
public offsetPoint(point: Point): Rectangle {
return this.offset(point.x, point.y);
}
/**
* Sets all of the Rectangle object's properties to 0. A Rectangle object is empty if its width or height is less than or equal to 0.
* @method setEmpty
* @return {Rectangle} This rectangle object
**/
public setEmpty() {
return this.setTo(0, 0, 0, 0);
}
/**
* Sets the members of Rectangle to the specified values.
* @method setTo
* @param {Number} x The x coordinate of the top-left corner of the rectangle.
* @param {Number} y The y coordinate of the top-left corner of the rectangle.
* @param {Number} width The width of the rectangle in pixels.
* @param {Number} height The height of the rectangle in pixels.
* @return {Rectangle} This rectangle object
**/
public setTo(x: number, y: number, width: number, height: number): Rectangle {
this._tempX = x;
this._tempY = y;
this._tempWidth = width;
this._tempHeight = height;
this.updateBounds();
return this;
}
/**
* Adds two rectangles together to create a new Rectangle object, by filling in the horizontal and vertical space between the two rectangles.
* @method union
* @param {Rectangle} toUnion A Rectangle object to add to this Rectangle object.
* @param {Rectangle} output Optional Rectangle object. If given the new values will be set into this object, otherwise a brand new Rectangle object will be created and returned.
* @return {Rectangle} A Rectangle object that is the union of the two rectangles.
**/
public union(toUnion: Rectangle, output?: Rectangle = new Rectangle): Rectangle {
return output.setTo(
Math.min(toUnion.x, this.x),
Math.min(toUnion.y, this.y),
Math.max(toUnion.right, this.right),
Math.max(toUnion.bottom, this.bottom)
);
}
/**
* Returns a string representation of this object.
* @method toString
* @return {String} a string representation of the instance.
**/
public toString(): string {
return "[{Rectangle (x=" + this.x + " y=" + this.y + " width=" + this.width + " height=" + this.height + " empty=" + this.isEmpty + ")}]";
}
}
}

View file

@ -0,0 +1,221 @@
/// <reference path="../Game.ts" />
/// <reference path="../core/Point.ts" />
/// <reference path="../core/Rectangle.ts" />
/**
* Phaser - RectangleUtils
*
* A collection of methods useful for manipulating and comparing Rectangle objects.
*
* TODO: Check docs + overlap + intersect + toPolygon?
*/
module Phaser {
export class RectangleUtils {
/**
* Get the location of the Rectangles top-left corner as a Point object.
* @method getTopLeftAsPoint
* @param {Rectangle} a - The Rectangle object.
* @param {Point} out - Optional Point to store the value in, if not supplied a new Point object will be created.
* @return {Point} The new Point object.
**/
static getTopLeftAsPoint(a: Rectangle, out?: Point = new Point): Point {
return out.setTo(a.x, a.y);
}
/**
* Get the location of the Rectangles bottom-right corner as a Point object.
* @method getTopLeftAsPoint
* @param {Rectangle} a - The Rectangle object.
* @param {Point} out - Optional Point to store the value in, if not supplied a new Point object will be created.
* @return {Point} The new Point object.
**/
static getBottomRightAsPoint(a: Rectangle, out?: Point = new Point): Point {
return out.setTo(a.right, a.bottom);
}
/**
* Increases the size of the Rectangle object by the specified amounts. The center point of the Rectangle object stays the same, and its size increases to the left and right by the dx value, and to the top and the bottom by the dy value.
* @method inflate
* @param {Rectangle} a - The Rectangle object.
* @param {Number} dx The amount to be added to the left side of the Rectangle.
* @param {Number} dy The amount to be added to the bottom side of the Rectangle.
* @return {Rectangle} This Rectangle object.
**/
static inflate(a: Rectangle, dx: number, dy: number): Rectangle {
a.x -= dx;
a.width += 2 * dx;
a.y -= dy;
a.height += 2 * dy;
return a;
}
/**
* Increases the size of the Rectangle object. This method is similar to the Rectangle.inflate() method except it takes a Point object as a parameter.
* @method inflatePoint
* @param {Rectangle} a - The Rectangle object.
* @param {Point} point The x property of this Point object is used to increase the horizontal dimension of the Rectangle object. The y property is used to increase the vertical dimension of the Rectangle object.
* @return {Rectangle} The Rectangle object.
**/
static inflatePoint(a: Rectangle, point: Point): Rectangle {
return RectangleUtils.inflate(a, point.x, point.y);
}
/**
* The size of the Rectangle object, expressed as a Point object with the values of the width and height properties.
* @method size
* @param {Rectangle} a - The Rectangle object.
* @param {Point} output Optional Point object. If given the values will be set into the object, otherwise a brand new Point object will be created and returned.
* @return {Point} The size of the Rectangle object
**/
static size(a: Rectangle, output?: Point = new Point): Point {
return output.setTo(a.width, a.height);
}
/**
* Returns a new Rectangle object with the same values for the x, y, width, and height properties as the original Rectangle object.
* @method clone
* @param {Rectangle} a - The Rectangle object.
* @param {Rectangle} output Optional Rectangle object. If given the values will be set into the object, otherwise a brand new Rectangle object will be created and returned.
* @return {Rectangle}
**/
static clone(a: Rectangle, output?: Rectangle = new Rectangle): Rectangle {
return output.setTo(a.x, a.y, a.width, a.height);
}
/**
* Determines whether the specified coordinates are contained within the region defined by this Rectangle object.
* @method contains
* @param {Rectangle} a - The Rectangle object.
* @param {Number} x The x coordinate of the point to test.
* @param {Number} y The y coordinate of the point to test.
* @return {Boolean} A value of true if the Rectangle object contains the specified point; otherwise false.
**/
static contains(a: Rectangle, x: number, y: number): bool {
return (x >= a.x && x <= a.right && y >= a.y && y <= a.bottom);
}
/**
* Determines whether the specified point is contained within the rectangular region defined by this Rectangle object. This method is similar to the Rectangle.contains() method, except that it takes a Point object as a parameter.
* @method containsPoint
* @param {Rectangle} a - The Rectangle object.
* @param {Point} point The point object being checked. Can be Point or any object with .x and .y values.
* @return {Boolean} A value of true if the Rectangle object contains the specified point; otherwise false.
**/
static containsPoint(a: Rectangle, point: Point): bool {
return RectangleUtils.contains(a, point.x, point.y);
}
/**
* Determines whether the first Rectangle object is fully contained within the second Rectangle object.
* A Rectangle object is said to contain another if the second Rectangle object falls entirely within the boundaries of the first.
* @method containsRect
* @param {Rectangle} a - The first Rectangle object.
* @param {Rectangle} b - The second Rectangle object.
* @return {Boolean} A value of true if the Rectangle object contains the specified point; otherwise false.
**/
static containsRect(a: Rectangle, b: Rectangle): bool {
// If the given rect has a larger volume than this one then it can never contain it
if (a.volume > b.volume)
{
return false;
}
return (a.x >= b.x && a.y >= b.y && a.right <= b.right && a.bottom <= b.bottom);
}
/**
* Determines whether the two Rectangles are equal.
* This method compares the x, y, width and height properties of each Rectangle.
* @method equals
* @param {Rectangle} a - The first Rectangle object.
* @param {Rectangle} b - The second Rectangle object.
* @return {Boolean} A value of true if the two Rectangles have exactly the same values for the x, y, width and height properties; otherwise false.
**/
static equals(a: Rectangle, b: Rectangle): bool {
return (a.x == b.x && a.y == b.y && a.width == b.width && a.height == b.height);
}
/**
* If the Rectangle object specified in the toIntersect parameter intersects with this Rectangle object, returns the area of intersection as a Rectangle object. If the rectangles do not intersect, this method returns an empty Rectangle object with its properties set to 0.
* @method intersection
* @param {Rectangle} a - The first Rectangle object.
* @param {Rectangle} b - The second Rectangle object.
* @param {Rectangle} output Optional Rectangle object. If given the intersection values will be set into this object, otherwise a brand new Rectangle object will be created and returned.
* @return {Rectangle} A Rectangle object that equals the area of intersection. If the rectangles do not intersect, this method returns an empty Rectangle object; that is, a rectangle with its x, y, width, and height properties set to 0.
**/
static intersection(a: Rectangle, b: Rectangle, out?: Rectangle = new Rectangle): Rectangle {
if (RectangleUtils.intersects(a, b))
{
out.x = Math.max(a.x, b.x);
out.y = Math.max(a.y, b.y);
out.width = Math.min(a.right, b.right) - out.x;
out.height = Math.min(a.bottom, b.bottom) - out.y;
}
return out;
}
/**
* Determines whether the two Rectangles intersect with each other.
* This method checks the x, y, width, and height properties of the Rectangles.
* @method intersects
* @param {Rectangle} a - The first Rectangle object.
* @param {Rectangle} b - The second Rectangle object.
* @param {Number} tolerance A tolerance value to allow for an intersection test with padding, default to 0
* @return {Boolean} A value of true if the specified object intersects with this Rectangle object; otherwise false.
**/
static intersects(a: Rectangle, b: Rectangle, tolerance?: number = 0): bool {
// ?
//return (a.right > b.x) && (a.x < b.right) && (a.bottom > b.y) && (a.y < b.bottom);
return !(a.left > b.right + tolerance || a.right < b.left - tolerance || a.top > b.bottom + tolerance || a.bottom < b.top - tolerance);
//if (a.x >= b.right || a.right <= b.x || a.bottom <= b.y || a.y >= b.bottom)
//{
// return false;
//}
//return true;
}
/**
* Determines whether the object specified intersects (overlaps) with the given values.
* @method intersectsRaw
* @param {Number} left
* @param {Number} right
* @param {Number} top
* @param {Number} bottomt
* @param {Number} tolerance A tolerance value to allow for an intersection test with padding, default to 0
* @return {Boolean} A value of true if the specified object intersects with the Rectangle; otherwise false.
**/
static intersectsRaw(a: Rectangle, left: number, right: number, top: number, bottom: number, tolerance?: number = 0): bool {
return !(left > a.right + tolerance || right < a.left - tolerance || top > a.bottom + tolerance || bottom < a.top - tolerance);
}
/**
* Adds two rectangles together to create a new Rectangle object, by filling in the horizontal and vertical space between the two rectangles.
* @method union
* @param {Rectangle} a - The first Rectangle object.
* @param {Rectangle} b - The second Rectangle object.
* @param {Rectangle} output Optional Rectangle object. If given the new values will be set into this object, otherwise a brand new Rectangle object will be created and returned.
* @return {Rectangle} A Rectangle object that is the union of the two rectangles.
**/
static union(a: Rectangle, b: Rectangle, out?: Rectangle = new Rectangle): Rectangle {
return out.setTo(Math.min(a.x, b.x), Math.min(a.y, b.y), Math.max(a.right, b.right), Math.max(a.bottom, b.bottom));
}
}
}

319
Phaser/geom/Vec2Utils.ts Normal file
View file

@ -0,0 +1,319 @@
/// <reference path="../Game.ts" />
/// <reference path="../core/Vec2.ts" />
/**
* Phaser - Vec2Utils
*
* A collection of methods useful for manipulating and performing operations on 2D vectors.
*
*/
module Phaser {
export class Vec2Utils {
/**
* Adds two 2D vectors.
*
* @param {Vec2} a Reference to a source Vec2 object.
* @param {Vec2} b Reference to a source Vec2 object.
* @param {Vec2} out The output Vec2 that is the result of the operation.
* @return {Vec2} A Vec2 that is the sum of the two vectors.
*/
static add(a: Vec2, b: Vec2, out?: Vec2 = new Vec2): Vec2 {
return out.setTo(a.x + b.x, a.y + b.y);
}
/**
* Subtracts two 2D vectors.
*
* @param {Vec2} a Reference to a source Vec2 object.
* @param {Vec2} b Reference to a source Vec2 object.
* @param {Vec2} out The output Vec2 that is the result of the operation.
* @return {Vec2} A Vec2 that is the difference of the two vectors.
*/
static subtract(a: Vec2, b: Vec2, out?: Vec2 = new Vec2): Vec2 {
return out.setTo(a.x - b.x, a.y - b.y);
}
/**
* Multiplies two 2D vectors.
*
* @param {Vec2} a Reference to a source Vec2 object.
* @param {Vec2} b Reference to a source Vec2 object.
* @param {Vec2} out The output Vec2 that is the result of the operation.
* @return {Vec2} A Vec2 that is the sum of the two vectors multiplied.
*/
static multiply(a: Vec2, b: Vec2, out?: Vec2 = new Vec2): Vec2 {
return out.setTo(a.x * b.x, a.y * b.y);
}
/**
* Divides two 2D vectors.
*
* @param {Vec2} a Reference to a source Vec2 object.
* @param {Vec2} b Reference to a source Vec2 object.
* @param {Vec2} out The output Vec2 that is the result of the operation.
* @return {Vec2} A Vec2 that is the sum of the two vectors divided.
*/
static divide(a: Vec2, b: Vec2, out?: Vec2 = new Vec2): Vec2 {
return out.setTo(a.x / b.x, a.y / b.y);
}
/**
* Scales a 2D vector.
*
* @param {Vec2} a Reference to a source Vec2 object.
* @param {number} s Scaling value.
* @param {Vec2} out The output Vec2 that is the result of the operation.
* @return {Vec2} A Vec2 that is the scaled vector.
*/
static scale(a: Vec2, s: number, out?: Vec2 = new Vec2): Vec2 {
return out.setTo(a.x * s, a.y * s);
}
/**
* Rotate a 2D vector by 90 degrees.
*
* @param {Vec2} a Reference to a source Vec2 object.
* @param {Vec2} out The output Vec2 that is the result of the operation.
* @return {Vec2} A Vec2 that is the scaled vector.
*/
static perp(a: Vec2, out?: Vec2 = new Vec2): Vec2 {
return out.setTo(a.y, -a.x);
}
/**
* Checks if two 2D vectors are equal.
*
* @param {Vec2} a Reference to a source Vec2 object.
* @param {Vec2} b Reference to a source Vec2 object.
* @return {Boolean}
*/
static equals(a: Vec2, b: Vec2): bool {
return a.x == b.x && a.y == b.y;
}
/**
*
*
* @param {Vec2} a Reference to a source Vec2 object.
* @param {Vec2} b Reference to a source Vec2 object.
* @param {Vec2} epsilon
* @return {Boolean}
*/
static epsilonEquals(a: Vec2, b: Vec2, epsilon: number): bool {
return Math.abs(a.x - b.x) <= epsilon && Math.abs(a.y - b.y) <= epsilon;
}
/**
* Get the distance between two 2D vectors.
*
* @param {Vec2} a Reference to a source Vec2 object.
* @param {Vec2} b Reference to a source Vec2 object.
* @return {Number}
*/
static distance(a: Vec2, b: Vec2): number {
return Math.sqrt(Vec2Utils.distanceSq(a, b));
}
/**
* Get the distance squared between two 2D vectors.
*
* @param {Vec2} a Reference to a source Vec2 object.
* @param {Vec2} b Reference to a source Vec2 object.
* @return {Number}
*/
static distanceSq(a: Vec2, b: Vec2): number {
return ((a.x - b.x) * (a.x - b.x)) + ((a.y - b.y) * (a.y - b.y));
}
/**
* Project two 2D vectors onto another vector.
*
* @param {Vec2} a Reference to a source Vec2 object.
* @param {Vec2} b Reference to a source Vec2 object.
* @param {Vec2} out The output Vec2 that is the result of the operation.
* @return {Vec2} A Vec2.
*/
static project(a: Vec2, b: Vec2, out?: Vec2 = new Vec2): Vec2 {
var amt = a.dot(b) / b.lengthSq();
if (amt != 0)
{
out.setTo(amt * b.x, amt * b.y);
}
return out;
}
/**
* Project this vector onto a vector of unit length.
*
* @param {Vec2} a Reference to a source Vec2 object.
* @param {Vec2} b Reference to a source Vec2 object.
* @param {Vec2} out The output Vec2 that is the result of the operation.
* @return {Vec2} A Vec2.
*/
static projectUnit(a: Vec2, b: Vec2, out?: Vec2 = new Vec2): Vec2 {
var amt = a.dot(b);
if (amt != 0)
{
out.setTo(amt * b.x, amt * b.y);
}
return out;
}
/**
* Right-hand normalize (make unit length) a 2D vector.
*
* @param {Vec2} a Reference to a source Vec2 object.
* @param {Vec2} out The output Vec2 that is the result of the operation.
* @return {Vec2} A Vec2.
*/
static normalRightHand(a: Vec2, out?: Vec2 = this): Vec2 {
return out.setTo(a.y * -1, a.x);
}
/**
* Normalize (make unit length) a 2D vector.
*
* @param {Vec2} a Reference to a source Vec2 object.
* @param {Vec2} out The output Vec2 that is the result of the operation.
* @return {Vec2} A Vec2.
*/
static normalize(a: Vec2, out?: Vec2 = new Vec2): Vec2 {
var m = a.length();
if (m != 0)
{
out.setTo(a.x / m, a.y / m);
}
return out;
}
/**
* The dot product of two 2D vectors.
*
* @param {Vec2} a Reference to a source Vec2 object.
* @param {Vec2} b Reference to a source Vec2 object.
* @return {Number}
*/
static dot(a: Vec2, b: Vec2): number {
return ((a.x * b.x) + (a.y * b.y));
}
/**
* The cross product of two 2D vectors.
*
* @param {Vec2} a Reference to a source Vec2 object.
* @param {Vec2} b Reference to a source Vec2 object.
* @return {Number}
*/
static cross(a: Vec2, b: Vec2): number {
return ((a.x * b.y) - (a.y * b.x));
}
/**
* The angle between two 2D vectors.
*
* @param {Vec2} a Reference to a source Vec2 object.
* @param {Vec2} b Reference to a source Vec2 object.
* @return {Number}
*/
static angle(a: Vec2, b: Vec2): number {
return Math.atan2(a.x * b.y - a.y * b.x, a.x * b.x + a.y * b.y);
}
/**
* The angle squared between two 2D vectors.
*
* @param {Vec2} a Reference to a source Vec2 object.
* @param {Vec2} b Reference to a source Vec2 object.
* @return {Number}
*/
static angleSq(a: Vec2, b: Vec2): number {
return a.subtract(b).angle(b.subtract(a));
}
/**
* Rotate a 2D vector around the origin to the given angle (theta).
*
* @param {Vec2} a Reference to a source Vec2 object.
* @param {Vec2} b Reference to a source Vec2 object.
* @param {Number} theta The angle of rotation in radians.
* @param {Vec2} out The output Vec2 that is the result of the operation.
* @return {Vec2} A Vec2.
*/
static rotate(a: Vec2, b: Vec2, theta: number, out?: Vec2 = new Vec2): Vec2 {
var x = a.x - b.x;
var y = a.y - b.y;
return out.setTo(x * Math.cos(theta) - y * Math.sin(theta) + b.x, x * Math.sin(theta) + y * Math.cos(theta) + b.y);
}
/**
* Clone a 2D vector.
*
* @param {Vec2} a Reference to a source Vec2 object.
* @param {Vec2} out The output Vec2 that is the result of the operation.
* @return {Vec2} A Vec2 that is a copy of the source Vec2.
*/
static clone(a: Vec2, out?: Vec2 = new Vec2): Vec2 {
return out.setTo(a.x, a.y);
}
/**
* Reflect this vector on an arbitrary axis.
*
* @param {Vec2} axis The vector representing the axis.
* @return {Vec2} This for chaining.
*/
/*
static reflect(axis): Vec2 {
var x = this.x;
var y = this.y;
this.project(axis).scale(2);
this.x -= x;
this.y -= y;
return this;
}
*/
/**
* Reflect this vector on an arbitrary axis (represented by a unit vector)
*
* @param {Vec2} axis The unit vector representing the axis.
* @return {Vec2} This for chaining.
*/
/*
static reflectN(axis): Vec2 {
var x = this.x;
var y = this.y;
this.projectN(axis).scale(2);
this.x -= x;
this.y -= y;
return this;
}
static getMagnitude(): number {
return Math.sqrt(Math.pow(this.x, 2) + Math.pow(this.y, 2));
}
*/
}
}

View file

@ -1,4 +1,4 @@
/// <reference path="Game.ts" />
/// <reference path="../Game.ts" />
/**
* Phaser - Cache
@ -35,16 +35,19 @@ module Phaser {
* @type {object}
*/
private _canvases;
/**
* Image key-value container.
* @type {object}
*/
private _images;
/**
* Sound key-value container.
* @type {object}
*/
private _sounds;
/**
* Text key-value container.
* @type {object}

View file

@ -1,4 +1,4 @@
/// <reference path="Game.ts" />
/// <reference path="../Game.ts" />
/**
* Phaser - Loader
@ -480,7 +480,7 @@ module Phaser {
if (!xml || !xml.documentElement || xml.getElementsByTagName("parsererror").length)
{
throw new Error("Phaser.Loader. Invalid Texture Atlas XML given");
throw new Error("Phaser.Loader. Invalid XML given");
}
var file = this._fileList[key];

View file

@ -7,9 +7,9 @@
* Contains methods and ideas from verlet-js by Sub Protocol, SAT.js by Jim Riecken and N by Metanet Software. Brandon Jones, Colin MacKenzie IV
*/
module Phaser {
module Phaser.Math {
export class Vector2 {
export class Vec2 {
/**
* Creates a new Vector2 object.

View file

@ -1,19 +1,22 @@
/**
* Phaser
*
* v0.9.6 - May 21st 2013
*
* A small and feature-packed 2D canvas game framework born from the firey pits of Flixel and Kiwi.
*
* Richard Davey (@photonstorm)
*
* Many thanks to Adam Saltsman (@ADAMATOMIC) for releasing Flixel on which Phaser took a lot of inspiration.
*
* "If you want your children to be intelligent, read them fairy tales."
* "If you want them to be more intelligent, read them more fairy tales."
* -- Albert Einstein
*/
var Phaser;
(function (Phaser) {
Phaser.VERSION = 'Phaser version 0.9.6';
Phaser.Point = (typeof Float32Array !== 'undefined') ? Float32Array : Array;
Phaser.Vec2 = (typeof Float32Array !== 'undefined') ? Float32Array : Array;
Phaser.Matrix = (typeof Float32Array !== 'undefined') ? Float32Array : Array;
})(Phaser || (Phaser = {}));
var Phaser;
(function (Phaser) {
(function (Geom2) {
var Point = (function () {
function Point() { }
Point.prototype.add = function (a, b) {
a[0] = b[0];
return a;
};
return Point;
})();
Geom2.Point = Point;
})(Phaser.Geom2 || (Phaser.Geom2 = {}));
var Geom2 = Phaser.Geom2;
})(Phaser || (Phaser = {}));

View file

@ -1,27 +0,0 @@
/// <reference path="../../Game.d.ts" />
module Phaser {
class Animation {
constructor(game: Game, parent: Sprite, frameData: FrameData, name: string, frames, delay: number, looped: bool);
private _game;
private _parent;
private _frames;
private _frameData;
private _frameIndex;
private _timeLastFrame;
private _timeNextFrame;
public name: string;
public currentFrame: Frame;
public isFinished: bool;
public isPlaying: bool;
public looped: bool;
public delay: number;
public frameTotal : number;
public frame : number;
public play(frameRate?: number, loop?: bool): void;
public restart(): void;
public stop(): void;
public update(): bool;
public destroy(): void;
private onComplete();
}
}

View file

@ -1,7 +0,0 @@
/// <reference path="../../Game.d.ts" />
module Phaser {
class AnimationLoader {
static parseSpriteSheet(game: Game, key: string, frameWidth: number, frameHeight: number, frameMax: number): FrameData;
static parseJSONData(game: Game, json): FrameData;
}
}

View file

@ -1,23 +0,0 @@
/// <reference path="../../Game.d.ts" />
module Phaser {
class Frame {
constructor(x: number, y: number, width: number, height: number, name: string);
public x: number;
public y: number;
public width: number;
public height: number;
public index: number;
public name: string;
public rotated: bool;
public rotationDirection: string;
public trimmed: bool;
public sourceSizeW: number;
public sourceSizeH: number;
public spriteSourceSizeX: number;
public spriteSourceSizeY: number;
public spriteSourceSizeW: number;
public spriteSourceSizeH: number;
public setRotation(rotated: bool, rotationDirection: string): void;
public setTrim(trimmed: bool, actualWidth, actualHeight, destX, destY, destWidth, destHeight): void;
}
}

View file

@ -1,18 +0,0 @@
/// <reference path="../../Game.d.ts" />
module Phaser {
class FrameData {
constructor();
private _frames;
private _frameNames;
public total : number;
public addFrame(frame: Frame): Frame;
public getFrame(index: number): Frame;
public getFrameByName(name: string): Frame;
public checkFrameName(name: string): bool;
public getFrameRange(start: number, end: number, output?: Frame[]): Frame[];
public getFrameIndexes(output?: number[]): number[];
public getFrameIndexesByName(input: string[]): number[];
public getAllFrames(): Frame[];
public getFrames(range: number[]): Frame[];
}
}

View file

@ -1,35 +0,0 @@
/// <reference path="../../Game.d.ts" />
module Phaser {
class Finger {
constructor(game: Game);
private _game;
public identifier: number;
public active: bool;
public point: Point;
public circle: Circle;
public withinGame: bool;
public clientX: number;
public clientY: number;
public pageX: number;
public pageY: number;
public screenX: number;
public screenY: number;
public x: number;
public y: number;
public target;
public isDown: bool;
public isUp: bool;
public timeDown: number;
public duration: number;
public timeUp: number;
public justPressedRate: number;
public justReleasedRate: number;
public start(event): void;
public move(event): void;
public leave(event): void;
public stop(event): void;
public justPressed(duration?: number): bool;
public justReleased(duration?: number): bool;
public toString(): string;
}
}

View file

@ -1,24 +0,0 @@
/// <reference path="../../Game.d.ts" />
/// <reference path="../../Signal.d.ts" />
module Phaser {
class Input {
constructor(game: Game);
private _game;
public mouse: Mouse;
public keyboard: Keyboard;
public touch: Touch;
public x: number;
public y: number;
public scaleX: number;
public scaleY: number;
public worldX: number;
public worldY: number;
public onDown: Signal;
public onUp: Signal;
public update(): void;
public reset(): void;
public getWorldX(camera?: Camera): number;
public getWorldY(camera?: Camera): number;
public renderDebugInfo(x: number, y: number, color?: string): void;
}
}

View file

@ -1,117 +0,0 @@
/// <reference path="../../Game.d.ts" />
module Phaser {
class Keyboard {
constructor(game: Game);
private _game;
private _keys;
private _capture;
public start(): void;
public addKeyCapture(keycode): void;
public removeKeyCapture(keycode: number): void;
public clearCaptures(): void;
public onKeyDown(event: KeyboardEvent): void;
public onKeyUp(event: KeyboardEvent): void;
public reset(): void;
public justPressed(keycode: number, duration?: number): bool;
public justReleased(keycode: number, duration?: number): bool;
public isDown(keycode: number): bool;
static A: number;
static B: number;
static C: number;
static D: number;
static E: number;
static F: number;
static G: number;
static H: number;
static I: number;
static J: number;
static K: number;
static L: number;
static M: number;
static N: number;
static O: number;
static P: number;
static Q: number;
static R: number;
static S: number;
static T: number;
static U: number;
static V: number;
static W: number;
static X: number;
static Y: number;
static Z: number;
static ZERO: number;
static ONE: number;
static TWO: number;
static THREE: number;
static FOUR: number;
static FIVE: number;
static SIX: number;
static SEVEN: number;
static EIGHT: number;
static NINE: number;
static NUMPAD_0: number;
static NUMPAD_1: number;
static NUMPAD_2: number;
static NUMPAD_3: number;
static NUMPAD_4: number;
static NUMPAD_5: number;
static NUMPAD_6: number;
static NUMPAD_7: number;
static NUMPAD_8: number;
static NUMPAD_9: number;
static NUMPAD_MULTIPLY: number;
static NUMPAD_ADD: number;
static NUMPAD_ENTER: number;
static NUMPAD_SUBTRACT: number;
static NUMPAD_DECIMAL: number;
static NUMPAD_DIVIDE: number;
static F1: number;
static F2: number;
static F3: number;
static F4: number;
static F5: number;
static F6: number;
static F7: number;
static F8: number;
static F9: number;
static F10: number;
static F11: number;
static F12: number;
static F13: number;
static F14: number;
static F15: number;
static COLON: number;
static EQUALS: number;
static UNDERSCORE: number;
static QUESTION_MARK: number;
static TILDE: number;
static OPEN_BRACKET: number;
static BACKWARD_SLASH: number;
static CLOSED_BRACKET: number;
static QUOTES: number;
static BACKSPACE: number;
static TAB: number;
static CLEAR: number;
static ENTER: number;
static SHIFT: number;
static CONTROL: number;
static ALT: number;
static CAPS_LOCK: number;
static ESC: number;
static SPACEBAR: number;
static PAGE_UP: number;
static PAGE_DOWN: number;
static END: number;
static HOME: number;
static LEFT: number;
static UP: number;
static RIGHT: number;
static DOWN: number;
static INSERT: number;
static DELETE: number;
static HELP: number;
static NUM_LOCK: number;
}
}

View file

@ -1,24 +0,0 @@
/// <reference path="../../Game.d.ts" />
module Phaser {
class Mouse {
constructor(game: Game);
private _game;
private _x;
private _y;
public button: number;
static LEFT_BUTTON: number;
static MIDDLE_BUTTON: number;
static RIGHT_BUTTON: number;
public isDown: bool;
public isUp: bool;
public timeDown: number;
public duration: number;
public timeUp: number;
public start(): void;
public reset(): void;
public onMouseDown(event: MouseEvent): void;
public update(): void;
public onMouseMove(event: MouseEvent): void;
public onMouseUp(event: MouseEvent): void;
}
}

View file

@ -1,40 +0,0 @@
/// <reference path="../../Game.d.ts" />
/// <reference path="Finger.d.ts" />
module Phaser {
class Touch {
constructor(game: Game);
private _game;
public x: number;
public y: number;
private _fingers;
public finger1: Finger;
public finger2: Finger;
public finger3: Finger;
public finger4: Finger;
public finger5: Finger;
public finger6: Finger;
public finger7: Finger;
public finger8: Finger;
public finger9: Finger;
public finger10: Finger;
public latestFinger: Finger;
public isDown: bool;
public isUp: bool;
public touchDown: Signal;
public touchUp: Signal;
public start(): void;
private consumeTouchMove(event);
private onTouchStart(event);
private onTouchCancel(event);
private onTouchEnter(event);
private onTouchLeave(event);
private onTouchMove(event);
private onTouchEnd(event);
public calculateDistance(finger1: Finger, finger2: Finger): void;
public calculateAngle(finger1: Finger, finger2: Finger): void;
public checkOverlap(finger1: Finger, finger2: Finger): void;
public update(): void;
public stop(): void;
public reset(): void;
}
}

84
Plugins/Plugins.csproj Normal file
View file

@ -0,0 +1,84 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<ProjectGuid>{1106D1E1-DCE3-4CAF-8096-6C85F765D519}</ProjectGuid>
<ProjectTypeGuids>{349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}</ProjectTypeGuids>
<OutputType>Library</OutputType>
<OutputPath>bin</OutputPath>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<DebugType>full</DebugType>
<DebugSymbols>true</DebugSymbols>
<UseIISExpress>true</UseIISExpress>
<IISExpressSSLPort />
<IISExpressAnonymousAuthentication />
<IISExpressWindowsAuthentication />
<IISExpressUseClassicPipelineMode />
</PropertyGroup>
<ItemGroup>
<Content Include="verlet\AngleConstraint.js">
<DependentUpon>AngleConstraint.ts</DependentUpon>
</Content>
<Content Include="verlet\Composite.js">
<DependentUpon>Composite.ts</DependentUpon>
</Content>
<Content Include="verlet\DistanceConstraint.js">
<DependentUpon>DistanceConstraint.ts</DependentUpon>
</Content>
<Content Include="verlet\Particle.js">
<DependentUpon>Particle.ts</DependentUpon>
</Content>
<Content Include="verlet\PinConstraint.js">
<DependentUpon>PinConstraint.ts</DependentUpon>
</Content>
<Content Include="verlet\VerletManager.js">
<DependentUpon>VerletManager.ts</DependentUpon>
</Content>
<XamlAppDef Include="verlet\VerletManager.ts" />
<XamlAppDef Include="verlet\PinConstraint.ts" />
<XamlAppDef Include="verlet\Particle.ts" />
<XamlAppDef Include="verlet\DistanceConstraint.ts" />
<XamlAppDef Include="verlet\Composite.ts" />
<XamlAppDef Include="verlet\AngleConstraint.ts" />
</ItemGroup>
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
</PropertyGroup>
<PropertyGroup>
<RootNamespace>Plugins</RootNamespace>
</PropertyGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Import Project="$(VSToolsPath)\WebApplications\Microsoft.WebApplication.targets" Condition="'$(VSToolsPath)' != ''" />
<ProjectExtensions>
<VisualStudio>
<FlavorProperties GUID="{349c5851-65df-11da-9384-00065b846f21}">
<WebProjectProperties>
<UseIIS>True</UseIIS>
<AutoAssignPort>True</AutoAssignPort>
<DevelopmentServerPort>0</DevelopmentServerPort>
<DevelopmentServerVPath>/</DevelopmentServerVPath>
<IISUrl>http://localhost:61985/</IISUrl>
<NTLMAuthentication>False</NTLMAuthentication>
<UseCustomServer>False</UseCustomServer>
<CustomServerUrl>
</CustomServerUrl>
<SaveServerSettingsInUserFile>False</SaveServerSettingsInUserFile>
</WebProjectProperties>
</FlavorProperties>
</VisualStudio>
</ProjectExtensions>
<PropertyGroup Condition="'$(Configuration)' == 'Debug'">
<TypeScriptTarget>ES3</TypeScriptTarget>
<TypeScriptIncludeComments>true</TypeScriptIncludeComments>
<TypeScriptSourceMap>true</TypeScriptSourceMap>
<TypeScriptModuleKind>AMD</TypeScriptModuleKind>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)' == 'Release'">
<TypeScriptTarget>ES3</TypeScriptTarget>
<TypeScriptIncludeComments>false</TypeScriptIncludeComments>
<TypeScriptSourceMap>false</TypeScriptSourceMap>
<TypeScriptModuleKind>AMD</TypeScriptModuleKind>
</PropertyGroup>
<Import Project="$(VSToolsPath)\TypeScript\Microsoft.TypeScript.targets" />
</Project>

1
Plugins/app.js.map Normal file
View file

@ -0,0 +1 @@
{"version":3,"file":"app.js","sources":["app.ts"],"names":["Greeter","Greeter.constructor","Greeter.start","","Greeter.stop","onload"],"mappings":"AAAA;IAKIA,SALEA,OAAOA,CAKGA,OAAoBA;QAC5BC,IAAIA,CAACA,OAAOA,GAAGA,OAAOA;QACtBA,IAAIA,CAACA,OAAOA,CAACA,SAASA,IAAIA,eAAeA;QACzCA,IAAIA,CAACA,IAAIA,GAAGA,QAAQA,CAACA,aAAaA,CAACA,MAAMA,CAACA;QAC1CA,IAAIA,CAACA,OAAOA,CAACA,WAAWA,CAACA,IAAIA,CAACA,IAAIA,CAACA;QACnCA,IAAIA,CAACA,IAAIA,CAACA,SAASA,GAAGA,IAAIA,IAAIA,EAAEA,CAACA,WAAWA,EAAEA;IAClDA,CAACA;IAEDD,0BAAAA;QAAAE,iBAECA;QADGA,IAAIA,CAACA,UAAUA,GAAGA,WAAWA,CAACA;YAAMC,OAAAA,KAAIA,CAACA,IAAIA,CAACA,SAASA,GAAGA,IAAIA,IAAIA,EAAEA,CAACA,WAAWA,EAAEA,CAAAA;QAA9CA,CAA8CA,EAAED,GAAGA,CAACA;IAC5FA,CAACA;IAEDF,yBAAAA;QACII,YAAYA,CAACA,IAAIA,CAACA,UAAUA,CAACA;IACjCA,CAACA;IAELJ;AAACA,CAAAA,IAAA;AAED,MAAM,CAAC,MAAM,GAAG;IACZK,IAAIA,EAAEA,GAAGA,QAAQA,CAACA,cAAcA,CAACA,SAASA,CAACA,CAACA;IAC5CA,IAAIA,OAAOA,GAAGA,IAAIA,OAAOA,CAACA,EAAEA,CAACA,CAACA;IAC9BA,OAAOA,CAACA,KAAKA,EAAEA;AACnBA,CAACA"}

View file

@ -0,0 +1,59 @@
var Phaser;
(function (Phaser) {
/// <reference path="../Game.ts" />
/// <reference path="Particle.ts" />
/// <reference path="../geom/Vector2.ts" />
/**
* Phaser - AngleConstraint
*
* constrains 3 particles to an angle
*/
(function (Verlet) {
var AngleConstraint = (function () {
/**
* Creates a new AngleConstraint object.
* @class AngleConstraint
* @constructor
* @param {Number} x The x coordinate of vector2
* @param {Number} y The y coordinate of vector2
* @return {AngleConstraint} This object
**/
function AngleConstraint(a, b, c, stiffness) {
this.a = a;
this.b = b;
this.c = c;
this.angle = this.b.pos.angle2(this.a.pos, this.c.pos);
this.stiffness = stiffness;
}
AngleConstraint.prototype.relax = function (stepCoef) {
var angle = this.b.pos.angle2(this.a.pos, this.c.pos);
var diff = angle - this.angle;
if(diff <= -Math.PI) {
diff += 2 * Math.PI;
} else if(diff >= Math.PI) {
diff -= 2 * Math.PI;
}
diff *= stepCoef * this.stiffness;
this.a.pos = this.a.pos.rotate(this.b.pos, diff);
this.c.pos = this.c.pos.rotate(this.b.pos, -diff);
this.b.pos = this.b.pos.rotate(this.a.pos, diff);
this.b.pos = this.b.pos.rotate(this.c.pos, -diff);
};
AngleConstraint.prototype.render = function (ctx) {
ctx.beginPath();
ctx.moveTo(this.a.pos.x, this.a.pos.y);
ctx.lineTo(this.b.pos.x, this.b.pos.y);
ctx.lineTo(this.c.pos.x, this.c.pos.y);
var tmp = ctx.lineWidth;
ctx.lineWidth = 5;
ctx.strokeStyle = "rgba(255,255,0,0.2)";
ctx.stroke();
ctx.lineWidth = tmp;
};
return AngleConstraint;
})();
Verlet.AngleConstraint = AngleConstraint;
})(Phaser.Verlet || (Phaser.Verlet = {}));
var Verlet = Phaser.Verlet;
})(Phaser || (Phaser = {}));
//@ sourceMappingURL=AngleConstraint.js.map

View file

@ -0,0 +1 @@
{"version":3,"file":"AngleConstraint.js","sources":["AngleConstraint.ts"],"names":["Phaser","Phaser.Verlet","Phaser.Verlet.AngleConstraint","Phaser.Verlet.AngleConstraint.constructor","Phaser.Verlet.AngleConstraint.relax","Phaser.Verlet.AngleConstraint.render"],"mappings":"AAUA,IAAO,MAAM;AA+DZ,CA/DD,UAAO,MAAM;IAVbA,mCAAmCA;IACnCA,oCAAoCA;IACpCA,2CAA2CA;IAE3CA;;;;MAIEA;KAEKA,UAAOA,MAAMA;QAEhBC;YAUIC;;;;;;;eADGA;YACHA,SAVSA,eAAeA,CAUZA,CAAyBA,EAAEA,CAAyBA,EAAEA,CAAyBA,EAAEA,SAAiBA;gBAE1GC,IAAIA,CAACA,CAACA,GAAGA,CAACA;gBACVA,IAAIA,CAACA,CAACA,GAAGA,CAACA;gBACVA,IAAIA,CAACA,CAACA,GAAGA,CAACA;gBACVA,IAAIA,CAACA,KAAKA,GAAGA,IAAIA,CAACA,CAACA,CAACA,GAAGA,CAACA,MAAMA,CAACA,IAAIA,CAACA,CAACA,CAACA,GAAGA,EAAEA,IAAIA,CAACA,CAACA,CAACA,GAAGA,CAACA;gBACtDA,IAAIA,CAACA,SAASA,GAAGA,SAASA;YAE9BA,CAACA;YAQDD,kCAAAA,UAAaA,QAAgBA;gBAEzBE,IAAIA,KAAKA,GAAGA,IAAIA,CAACA,CAACA,CAACA,GAAGA,CAACA,MAAMA,CAACA,IAAIA,CAACA,CAACA,CAACA,GAAGA,EAAEA,IAAIA,CAACA,CAACA,CAACA,GAAGA,CAACA,CAACA;gBACtDA,IAAIA,IAAIA,GAAGA,KAAKA,GAAGA,IAAIA,CAACA,KAAKA,CAACA;gBAE9BA,GAAIA,IAAIA,IAAIA,CAACA,IAAIA,CAACA,EAAEA,CAACA;oBACjBA,IAAIA,IAAIA,CAACA,GAAGA,IAAIA,CAACA,EAAEA;iBAACA,MACnBA,GAAIA,IAAIA,IAAIA,IAAIA,CAACA,EAAEA,CAACA;oBACrBA,IAAIA,IAAIA,CAACA,GAAGA,IAAIA,CAACA,EAAEA;iBAACA;gBAExBA,IAAIA,IAAIA,QAAQA,GAAGA,IAAIA,CAACA,SAASA;gBAEjCA,IAAIA,CAACA,CAACA,CAACA,GAAGA,GAAGA,IAAIA,CAACA,CAACA,CAACA,GAAGA,CAACA,MAAMA,CAACA,IAAIA,CAACA,CAACA,CAACA,GAAGA,EAAEA,IAAIA,CAACA;gBAChDA,IAAIA,CAACA,CAACA,CAACA,GAAGA,GAAGA,IAAIA,CAACA,CAACA,CAACA,GAAGA,CAACA,MAAMA,CAACA,IAAIA,CAACA,CAACA,CAACA,GAAGA,EAAEA,CAACA,IAAIA,CAACA;gBACjDA,IAAIA,CAACA,CAACA,CAACA,GAAGA,GAAGA,IAAIA,CAACA,CAACA,CAACA,GAAGA,CAACA,MAAMA,CAACA,IAAIA,CAACA,CAACA,CAACA,GAAGA,EAAEA,IAAIA,CAACA;gBAChDA,IAAIA,CAACA,CAACA,CAACA,GAAGA,GAAGA,IAAIA,CAACA,CAACA,CAACA,GAAGA,CAACA,MAAMA,CAACA,IAAIA,CAACA,CAACA,CAACA,GAAGA,EAAEA,CAACA,IAAIA,CAACA;YAErDA,CAACA;YAEDF,mCAAAA,UAAcA,GAAGA;gBAEbG,GAAGA,CAACA,SAASA,EAAEA;gBACfA,GAAGA,CAACA,MAAMA,CAACA,IAAIA,CAACA,CAACA,CAACA,GAAGA,CAACA,CAACA,EAAEA,IAAIA,CAACA,CAACA,CAACA,GAAGA,CAACA,CAACA,CAACA;gBACtCA,GAAGA,CAACA,MAAMA,CAACA,IAAIA,CAACA,CAACA,CAACA,GAAGA,CAACA,CAACA,EAAEA,IAAIA,CAACA,CAACA,CAACA,GAAGA,CAACA,CAACA,CAACA;gBACtCA,GAAGA,CAACA,MAAMA,CAACA,IAAIA,CAACA,CAACA,CAACA,GAAGA,CAACA,CAACA,EAAEA,IAAIA,CAACA,CAACA,CAACA,GAAGA,CAACA,CAACA,CAACA;gBACtCA,IAAIA,GAAGA,GAAGA,GAAGA,CAACA,SAASA,CAACA;gBACxBA,GAAGA,CAACA,SAASA,GAAGA,CAACA;gBACjBA,GAAGA,CAACA,WAAWA,GAAGA,qBAAqBA;gBACvCA,GAAGA,CAACA,MAAMA,EAAEA;gBACZA,GAAGA,CAACA,SAASA,GAAGA,GAAGA;YAEvBA,CAACA;YAELH;AAACA,QAADA,CAACA,IAAAD;QA3DDA,yCA2DCA,QAAAA;IAELA,CAACA,yCAAAD;IA/DMA;AA+DNA,CAAAA,2BAAA"}

144
Plugins/verlet/Composite.js Normal file
View file

@ -0,0 +1,144 @@
var Phaser;
(function (Phaser) {
/// <reference path="../Game.ts" />
/// <reference path="../geom/Vector2.ts" />
/// <reference path="Particle.ts" />
/// <reference path="PinConstraint.ts" />
/**
* Phaser - Verlet - Composite
*
*
*/
(function (Verlet) {
var Composite = (function () {
/**
* Creates a new Composite object.
* @class Composite
* @constructor
* @param {Number} x The x coordinate of vector2
* @param {Number} y The y coordinate of vector2
* @return {Composite} This object
**/
function Composite(game) {
/**
* Texture of the particles to be rendered.
*/
this._texture = null;
// local rendering related temp vars to help avoid gc spikes
this._sx = 0;
this._sy = 0;
this._sw = 0;
this._sh = 0;
this._dx = 0;
this._dy = 0;
this._dw = 0;
this._dh = 0;
this._hw = 0;
this._hh = 0;
this.drawParticles = null;
this.drawConstraints = null;
this.hideConstraints = true;
this.constraintLineColor = 'rgba(200,200,200,1)';
this._game = game;
this.sprites = [];
this.particles = [];
this.constraints = [];
this.frameBounds = new Phaser.Quad();
}
Composite.prototype.createDistanceConstraint = // Create Constraints
function (a, b, stiffness, distance) {
if (typeof distance === "undefined") { distance = null; }
this.constraints.push(new Phaser.Verlet.DistanceConstraint(a, b, stiffness, distance));
return this.constraints[this.constraints.length - 1];
};
Composite.prototype.createAngleConstraint = function (a, b, c, stiffness) {
this.constraints.push(new Phaser.Verlet.AngleConstraint(a, b, c, stiffness));
return this.constraints[this.constraints.length - 1];
};
Composite.prototype.createPinConstraint = function (a, pos) {
this.constraints.push(new Phaser.Verlet.PinConstraint(a, pos));
return this.constraints[this.constraints.length - 1];
};
Composite.prototype.loadGraphic = /**
* Load a graphic for this Composite. The graphic cannot be a SpriteSheet yet.
* @param key {string} Key of the graphic you want to load for this sprite.
* @return {Composite} This object
*/
function (key) {
if(this._game.cache.getImage(key) !== null) {
if(this._game.cache.isSpriteSheet(key) == false) {
this._texture = this._game.cache.getImage(key);
this.frameBounds.width = this._texture.width;
this.frameBounds.height = this._texture.height;
this._hw = Math.floor(this.frameBounds.width / 2);
this._hh = Math.floor(this.frameBounds.width / 2);
this.drawParticles = this.render;
this.drawConstraints = this.renderConstraints;
}
}
return this;
};
Composite.prototype.renderConstraints = function (context) {
if(this.hideConstraints == true || this.constraints.length == 0) {
return;
}
var i;
context.beginPath();
for(i in this.constraints) {
if(this.constraints[i].b) {
context.moveTo(this.constraints[i].a.pos.x, this.constraints[i].a.pos.y);
context.lineTo(this.constraints[i].b.pos.x, this.constraints[i].b.pos.y);
}
}
context.strokeStyle = this.constraintLineColor;
context.stroke();
context.closePath();
};
Composite.prototype.render = function (context) {
this._sx = 0;
this._sy = 0;
this._sw = this.frameBounds.width;
this._sh = this.frameBounds.height;
this._dw = this.frameBounds.width;
this._dh = this.frameBounds.height;
this._sx = Math.round(this._sx);
this._sy = Math.round(this._sy);
this._sw = Math.round(this._sw);
this._sh = Math.round(this._sh);
this._dw = Math.round(this._dw);
this._dh = Math.round(this._dh);
var i;
for(i in this.particles) {
//this._dx = cameraOffsetX + (this.frameBounds.topLeft.x - camera.worldView.x);
//this._dy = cameraOffsetY + (this.frameBounds.topLeft.y - camera.worldView.y);
this._dx = this.particles[i].pos.x - this._hw;
this._dy = this.particles[i].pos.y - this._hh;
this._dx = Math.round(this._dx);
this._dy = Math.round(this._dy);
context.drawImage(this._texture, // Source Image
this._sx, // Source X (location within the source image)
this._sy, // Source Y
this._sw, // Source Width
this._sh, // Source Height
this._dx, // Destination X (where on the canvas it'll be drawn)
this._dy, // Destination Y
this._dw, // Destination Width (always same as Source Width unless scaled)
this._dh);
// Destination Height (always same as Source Height unless scaled)
}
};
Composite.prototype.pin = function (index, pos) {
if (typeof pos === "undefined") { pos = null; }
if(pos == null) {
pos = this.particles[index].pos;
}
var pc = new Phaser.Verlet.PinConstraint(this.particles[index], pos);
this.constraints.push(pc);
return pc;
};
return Composite;
})();
Verlet.Composite = Composite;
})(Phaser.Verlet || (Phaser.Verlet = {}));
var Verlet = Phaser.Verlet;
})(Phaser || (Phaser = {}));

View file

@ -0,0 +1,52 @@
var Phaser;
(function (Phaser) {
/// <reference path="../Game.ts" />
/// <reference path="Particle.ts" />
/// <reference path="../geom/Vector2.ts" />
/**
* Phaser - DistanceConstraint
*
* Constrains to initial distance
*/
(function (Verlet) {
var DistanceConstraint = (function () {
/**
* Creates a new DistanceConstraint object.
* @class DistanceConstraint
* @constructor
* @param {Number} x The x coordinate of vector2
* @param {Number} y The y coordinate of vector2
* @return {DistanceConstraint} This object
**/
function DistanceConstraint(a, b, stiffness, distance) {
if (typeof distance === "undefined") { distance = null; }
this.a = a;
this.b = b;
if(distance === null) {
this.distance = a.pos.sub(b.pos).length();
} else {
this.distance = distance;
}
this.stiffness = stiffness;
}
DistanceConstraint.prototype.relax = function (stepCoef) {
var normal = this.a.pos.sub(this.b.pos);
var m = normal.length2();
normal.mutableScale(((this.distance * this.distance - m) / m) * this.stiffness * stepCoef);
this.a.pos.mutableAdd(normal);
this.b.pos.mutableSub(normal);
};
DistanceConstraint.prototype.render = function (ctx) {
ctx.beginPath();
ctx.moveTo(this.a.pos.x, this.a.pos.y);
ctx.lineTo(this.b.pos.x, this.b.pos.y);
ctx.strokeStyle = "#d8dde2";
ctx.stroke();
ctx.closePath();
};
return DistanceConstraint;
})();
Verlet.DistanceConstraint = DistanceConstraint;
})(Phaser.Verlet || (Phaser.Verlet = {}));
var Verlet = Phaser.Verlet;
})(Phaser || (Phaser = {}));

View file

@ -0,0 +1,36 @@
var Phaser;
(function (Phaser) {
/// <reference path="../Game.ts" />
/// <reference path="../geom/Vector2.ts" />
/**
* Phaser - Verlet - Particle
*
*
*/
(function (Verlet) {
var Particle = (function () {
/**
* Creates a new Particle object.
* @class Particle
* @constructor
* @param {Number} x The x coordinate of vector2
* @param {Number} y The y coordinate of vector2
* @return {Particle} This object
**/
function Particle(pos) {
this.pos = (new Vector2()).mutableSet(pos);
this.lastPos = (new Vector2()).mutableSet(pos);
}
Particle.prototype.render = function (ctx) {
ctx.beginPath();
ctx.arc(this.pos.x, this.pos.y, 2, 0, 2 * Math.PI);
ctx.fillStyle = "#2dad8f";
ctx.fill();
};
return Particle;
})();
Verlet.Particle = Particle;
})(Phaser.Verlet || (Phaser.Verlet = {}));
var Verlet = Phaser.Verlet;
})(Phaser || (Phaser = {}));
//@ sourceMappingURL=Particle.js.map

View file

@ -0,0 +1 @@
{"version":3,"file":"Particle.js","sources":["Particle.ts"],"names":["Phaser","Phaser.Verlet","Phaser.Verlet.Particle","Phaser.Verlet.Particle.constructor","Phaser.Verlet.Particle.render"],"mappings":"AASA,IAAO,MAAM;AAkCZ,CAlCD,UAAO,MAAM;IATbA,mCAAmCA;IACnCA,2CAA2CA;IAE3CA;;;;MAIEA;KAEKA,UAAOA,MAAMA;QAEhBC;YAUIC;;;;;;;eADGA;YACHA,SAVSA,QAAQA,CAULA,GAAYA;gBAEpBC,IAAIA,CAACA,GAAGA,GAAGA,CAACA,IAAIA,OAAOA,EAAEA,EAAEA,UAAUA,CAACA,GAAGA,CAACA;gBAC1CA,IAAIA,CAACA,OAAOA,GAAGA,CAACA,IAAIA,OAAOA,EAAEA,EAAEA,UAAUA,CAACA,GAAGA,CAACA;YAGlDA,CAACA;YAKDD,4BAAAA,UAAcA,GAAGA;gBAEhBE,GAAGA,CAACA,SAASA,EAAEA;gBACfA,GAAGA,CAACA,GAAGA,CAACA,IAAIA,CAACA,GAAGA,CAACA,CAACA,EAAEA,IAAIA,CAACA,GAAGA,CAACA,CAACA,EAAEA,CAACA,EAAEA,CAACA,EAAEA,CAACA,GAACA,IAAIA,CAACA,EAAEA,CAACA;gBAChDA,GAAGA,CAACA,SAASA,GAAGA,SAASA;gBACzBA,GAAGA,CAACA,IAAIA,EAAEA;YAEXA,CAACA;YAELF;AAACA,QAADA,CAACA,IAAAD;QA9BDA,2BA8BCA,QAAAA;IAELA,CAACA,yCAAAD;IAlCMA;AAkCNA,CAAAA,2BAAA"}

View file

@ -0,0 +1,39 @@
var Phaser;
(function (Phaser) {
/// <reference path="../Game.ts" />
/// <reference path="Particle.ts" />
/// <reference path="../geom/Vector2.ts" />
/**
* Phaser - PinConstraint
*
* Constrains to static / fixed point
*/
(function (Verlet) {
var PinConstraint = (function () {
/**
* Creates a new PinConstraint object.
* @class PinConstraint
* @constructor
* @param {Number} x The x coordinate of vector2
* @param {Number} y The y coordinate of vector2
* @return {PinConstraint} This object
**/
function PinConstraint(a, pos) {
this.a = a;
this.pos = (new Phaser.Vector2()).mutableSet(pos);
}
PinConstraint.prototype.relax = function () {
this.a.pos.mutableSet(this.pos);
};
PinConstraint.prototype.render = function (ctx) {
ctx.beginPath();
ctx.arc(this.pos.x, this.pos.y, 6, 0, 2 * Math.PI);
ctx.fillStyle = "rgba(0,153,255,0.1)";
ctx.fill();
};
return PinConstraint;
})();
Verlet.PinConstraint = PinConstraint;
})(Phaser.Verlet || (Phaser.Verlet = {}));
var Verlet = Phaser.Verlet;
})(Phaser || (Phaser = {}));

View file

@ -0,0 +1,271 @@
var Phaser;
(function (Phaser) {
/// <reference path="Game.ts" />
/// <reference path="geom/Vector2.ts" />
/// <reference path="verlet/Composite.ts" />
/// <reference path="verlet/Particle.ts" />
/// <reference path="verlet/DistanceConstraint.ts" />
/// <reference path="verlet/PinConstraint.ts" />
/// <reference path="verlet/AngleConstraint.ts" />
/**
* Phaser - Verlet
*
* Based on verlet-js by Sub Protocol released under MIT
*/
(function (Verlet) {
var VerletManager = (function () {
/**
* Creates a new Vector2 object.
* @class Vector2
* @constructor
* @param {Number} x The x coordinate of vector2
* @param {Number} y The y coordinate of vector2
* @return {Vector2} This object
**/
function VerletManager(game, width, height) {
this._v = new Phaser.Vector2();
this.composites = [];
this.step = 16;
this.selectionRadius = 20;
this.draggedEntity = null;
this.highlightColor = '#4f545c';
this.hideNearestEntityCircle = false;
this._game = game;
this.width = width;
this.height = height;
this.gravity = new Phaser.Vector2(0, 0.2);
this.friction = 0.99;
this.groundFriction = 0.8;
this.canvas = game.stage.canvas;
this.context = game.stage.context;
this._game.input.onDown.add(this.mouseDownHandler, this);
this._game.input.onUp.add(this.mouseUpHandler, this);
}
VerletManager.prototype.intersectionTime = /**
* Computes time of intersection of a particle with a wall
*
* @param {Vec2} line walls root position
* @param {Vec2} p particle position
* @param {Vec2} dir walls direction
* @param {Vec2} v particles velocity
*/
function (wall, p, dir, v) {
if(dir.x != 0) {
var denominator = v.y - dir.y * v.x / dir.x;
if(denominator == 0) {
return undefined;
}// Movement is parallel to wall
var numerator = wall.y + dir.y * (p.x - wall.x) / dir.x - p.y;
return numerator / denominator;
} else {
if(v.x == 0) {
return undefined;
}// parallel again
var denominator = v.x;
var numerator = wall.x - p.x;
return numerator / denominator;
}
};
VerletManager.prototype.intersectionPoint = function (wall, p, dir, v) {
var t = this.intersectionTime(wall, p, dir, v);
return new Phaser.Vector2(p.x + v.x * t, p.y + v.y * t);
};
VerletManager.prototype.bounds = function (particle) {
this._v.mutableSet(particle.pos);
this._v.mutableSub(particle.lastPos);
if(particle.pos.y > this.height - 1) {
particle.pos.mutableSet(this.intersectionPoint(new Phaser.Vector2(0, this.height - 1), particle.lastPos, new Phaser.Vector2(1, 0), this._v));
}
if(particle.pos.x < 0) {
particle.pos.mutableSet(this.intersectionPoint(new Phaser.Vector2(0, 0), particle.pos, new Phaser.Vector2(0, 1), this._v));
}
if(particle.pos.x > this.width - 1) {
particle.pos.mutableSet(this.intersectionPoint(new Phaser.Vector2(this.width - 1, 0), particle.pos, new Phaser.Vector2(0, 1), this._v));
}
};
VerletManager.prototype.createPoint = function (pos) {
var composite = new Phaser.Verlet.Composite(this._game);
composite.particles.push(new Phaser.Verlet.Particle(pos));
this.composites.push(composite);
return composite;
};
VerletManager.prototype.createLineSegments = function (vertices, stiffness) {
var composite = new Phaser.Verlet.Composite(this._game);
var i;
for(i in vertices) {
composite.particles.push(new Phaser.Verlet.Particle(vertices[i]));
if(i > 0) {
composite.constraints.push(new Phaser.Verlet.DistanceConstraint(composite.particles[i], composite.particles[i - 1], stiffness));
}
}
this.composites.push(composite);
return composite;
};
VerletManager.prototype.createCloth = function (origin, width, height, segments, pinMod, stiffness) {
var composite = new Phaser.Verlet.Composite(this._game);
var xStride = width / segments;
var yStride = height / segments;
var x;
var y;
for(y = 0; y < segments; ++y) {
for(x = 0; x < segments; ++x) {
var px = origin.x + x * xStride - width / 2 + xStride / 2;
var py = origin.y + y * yStride - height / 2 + yStride / 2;
composite.particles.push(new Phaser.Verlet.Particle(new Phaser.Vector2(px, py)));
if(x > 0) {
composite.constraints.push(new Phaser.Verlet.DistanceConstraint(composite.particles[y * segments + x], composite.particles[y * segments + x - 1], stiffness));
}
if(y > 0) {
composite.constraints.push(new Phaser.Verlet.DistanceConstraint(composite.particles[y * segments + x], composite.particles[(y - 1) * segments + x], stiffness));
}
}
}
for(x = 0; x < segments; ++x) {
if(x % pinMod == 0) {
composite.pin(x);
}
}
this.composites.push(composite);
return composite;
};
VerletManager.prototype.createTire = function (origin, radius, segments, spokeStiffness, treadStiffness) {
var stride = (2 * Math.PI) / segments;
var i;
var composite = new Phaser.Verlet.Composite(this._game);
// particles
for(i = 0; i < segments; ++i) {
var theta = i * stride;
composite.particles.push(new Verlet.Particle(new Phaser.Vector2(origin.x + Math.cos(theta) * radius, origin.y + Math.sin(theta) * radius)));
}
var center = new Verlet.Particle(origin);
composite.particles.push(center);
// constraints
for(i = 0; i < segments; ++i) {
composite.constraints.push(new Verlet.DistanceConstraint(composite.particles[i], composite.particles[(i + 1) % segments], treadStiffness));
composite.constraints.push(new Verlet.DistanceConstraint(composite.particles[i], center, spokeStiffness));
composite.constraints.push(new Verlet.DistanceConstraint(composite.particles[i], composite.particles[(i + 5) % segments], treadStiffness));
}
this.composites.push(composite);
return composite;
};
VerletManager.prototype.update = function () {
if(this.composites.length == 0) {
return;
}
var i, j, c;
for(c in this.composites) {
for(i in this.composites[c].particles) {
var particles = this.composites[c].particles;
// calculate velocity
var velocity = particles[i].pos.sub(particles[i].lastPos).scale(this.friction);
// ground friction
if(particles[i].pos.y >= this.height - 1 && velocity.length2() > 0.000001) {
var m = velocity.length();
velocity.x /= m;
velocity.y /= m;
velocity.mutableScale(m * this.groundFriction);
}
// save last good state
particles[i].lastPos.mutableSet(particles[i].pos);
// gravity
particles[i].pos.mutableAdd(this.gravity);
// inertia
particles[i].pos.mutableAdd(velocity);
}
}
// handle dragging of entities
if(this.draggedEntity) {
this.draggedEntity.pos.mutableSet(this._game.input.position);
}
// relax
var stepCoef = 1 / this.step;
for(c in this.composites) {
var constraints = this.composites[c].constraints;
for(i = 0; i < this.step; ++i) {
for(j in constraints) {
constraints[j].relax(stepCoef);
}
}
}
// bounds checking
for(c in this.composites) {
var particles = this.composites[c].particles;
for(i in particles) {
this.bounds(particles[i]);
}
}
};
VerletManager.prototype.mouseDownHandler = function () {
var nearest = this.nearestEntity();
if(nearest) {
this.draggedEntity = nearest;
}
};
VerletManager.prototype.mouseUpHandler = function () {
this.draggedEntity = null;
};
VerletManager.prototype.nearestEntity = function () {
var c, i;
var d2Nearest = 0;
var entity = null;
var constraintsNearest = null;
// find nearest point
for(c in this.composites) {
var particles = this.composites[c].particles;
for(i in particles) {
var d2 = particles[i].pos.distance2(this._game.input.position);
if(d2 <= this.selectionRadius * this.selectionRadius && (entity == null || d2 < d2Nearest)) {
entity = particles[i];
constraintsNearest = this.composites[c].constraints;
d2Nearest = d2;
}
}
}
// search for pinned constraints for this entity
for(i in constraintsNearest) {
if(constraintsNearest[i] instanceof Verlet.PinConstraint && constraintsNearest[i].a == entity) {
entity = constraintsNearest[i];
}
}
return entity;
};
VerletManager.prototype.render = function () {
var i, c;
for(c in this.composites) {
// draw constraints
if(this.composites[c].drawConstraints) {
this.composites[c].drawConstraints(this.context, this.composites[c]);
} else {
var constraints = this.composites[c].constraints;
for(i in constraints) {
constraints[i].render(this.context);
}
}
// draw particles
if(this.composites[c].drawParticles) {
this.composites[c].drawParticles(this.context, this.composites[c]);
} else {
var particles = this.composites[c].particles;
for(i in particles) {
particles[i].render(this.context);
}
}
}
// highlight nearest / dragged entity
var nearest = this.draggedEntity || this.nearestEntity();
if(nearest && this.hideNearestEntityCircle == false) {
this.context.beginPath();
this.context.arc(nearest.pos.x, nearest.pos.y, 8, 0, 2 * Math.PI);
this.context.strokeStyle = this.highlightColor;
this.context.stroke();
this.context.closePath();
}
};
return VerletManager;
})();
Verlet.VerletManager = VerletManager;
})(Phaser.Verlet || (Phaser.Verlet = {}));
var Verlet = Phaser.Verlet;
})(Phaser || (Phaser = {}));

2962
build/phaser.d.ts vendored

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

21
jsdoc.conf.json Normal file
View file

@ -0,0 +1,21 @@
{
"tags": {
"allowUnknownTags": true
},
"source": {
"include": [ "Phaser" ],
"exclude": [],
"includePattern": ".+\\.js(doc)?$",
"excludePattern": "(^|\\/|\\\\)_"
},
"plugins": [],
"opts": {
"encoding": "utf8",
"destination": "Docs",
"recurse": true,
"query": "value",
"private": true,
"lenient": true
},
"jsVersion": 180
}