1+ /// <reference path="../Game.ts" />
2+ /// <reference path="../core/Rectangle.ts" />
3+ /// <reference path="../math/Vec2Utils.ts" />
4+ /// <reference path="PhysicsManager.ts" />
5+ /// <reference path="IPhysicsShape.ts" />
6+
7+ /**
8+ * Phaser - Physics - Circle
9+ */
10+
11+ module Phaser . Physics {
12+
13+ export class Circle implements IPhysicsShape {
14+
15+ constructor ( game : Game , sprite : Sprite , x : number , y : number , diameter : number ) {
16+
17+ this . game = game ;
18+ this . world = game . world . physics ;
19+
20+ if ( sprite !== null )
21+ {
22+ this . sprite = sprite ;
23+ this . scale = Vec2Utils . clone ( this . sprite . scale ) ;
24+ }
25+ else
26+ {
27+ this . sprite = null ;
28+ this . physics = null ;
29+ this . scale = new Vec2 ( 1 , 1 ) ;
30+ }
31+
32+ this . radius = diameter / 2 ;
33+ this . bounds = new Rectangle ( x + Math . round ( diameter / 2 ) , y + Math . round ( diameter / 2 ) , diameter , diameter ) ;
34+ this . position = new Vec2 ( x + this . bounds . halfWidth , y + this . bounds . halfHeight ) ;
35+ this . oldPosition = new Vec2 ( x + this . bounds . halfWidth , y + this . bounds . halfHeight ) ;
36+ this . offset = new Vec2 ( 0 , 0 ) ;
37+
38+ }
39+
40+ public game : Game ;
41+ public world : PhysicsManager ;
42+ public sprite : Sprite ;
43+ public physics : Phaser . Components . Physics ;
44+
45+ public position : Vec2 ;
46+ public oldPosition : Vec2 ;
47+ public offset : Vec2 ;
48+ public scale : Vec2 ;
49+ public bounds : Rectangle ;
50+
51+ public radius : number ;
52+ public oH : number ;
53+ public oV : number ;
54+
55+ public preUpdate ( ) {
56+
57+ this . oldPosition . copyFrom ( this . position ) ;
58+
59+ if ( this . sprite )
60+ {
61+ this . position . setTo ( ( this . sprite . x + this . bounds . halfWidth ) + this . offset . x , ( this . sprite . y + this . bounds . halfHeight ) + this . offset . y ) ;
62+
63+ // Update scale / dimensions
64+ if ( Vec2Utils . equals ( this . scale , this . sprite . scale ) == false )
65+ {
66+ this . scale . copyFrom ( this . sprite . scale ) ;
67+ // needs to be radius based (+ square)
68+ //this.bounds.width = this.sprite.width;
69+ //this.bounds.height = this.sprite.height;
70+ }
71+ }
72+
73+ }
74+
75+ public update ( ) {
76+
77+ this . bounds . x = this . position . x ;
78+ this . bounds . y = this . position . y ;
79+
80+ }
81+
82+ public setSize ( width : number , height : number ) {
83+
84+ this . bounds . width = width ;
85+ this . bounds . height = height ;
86+
87+ }
88+
89+ public render ( context :CanvasRenderingContext2D ) {
90+
91+ context . beginPath ( ) ;
92+ context . strokeStyle = 'rgb(0,255,0)' ;
93+ context . arc ( this . position . x , this . position . y , this . radius , 0 , Math . PI * 2 ) ;
94+ context . stroke ( ) ;
95+ context . closePath ( ) ;
96+
97+ // center point
98+ context . fillStyle = 'rgb(0,255,0)' ;
99+ context . fillRect ( this . position . x , this . position . y , 2 , 2 ) ;
100+
101+ if ( this . oH == 1 )
102+ {
103+ context . beginPath ( ) ;
104+ context . strokeStyle = 'rgb(255,0,0)' ;
105+ context . moveTo ( this . position . x - this . bounds . halfWidth , this . position . y - this . bounds . halfHeight ) ;
106+ context . lineTo ( this . position . x - this . bounds . halfWidth , this . position . y + this . bounds . halfHeight ) ;
107+ context . stroke ( ) ;
108+ context . closePath ( ) ;
109+ }
110+ else if ( this . oH == - 1 )
111+ {
112+ context . beginPath ( ) ;
113+ context . strokeStyle = 'rgb(255,0,0)' ;
114+ context . moveTo ( this . position . x + this . bounds . halfWidth , this . position . y - this . bounds . halfHeight ) ;
115+ context . lineTo ( this . position . x + this . bounds . halfWidth , this . position . y + this . bounds . halfHeight ) ;
116+ context . stroke ( ) ;
117+ context . closePath ( ) ;
118+ }
119+
120+ if ( this . oV == 1 )
121+ {
122+ context . beginPath ( ) ;
123+ context . strokeStyle = 'rgb(255,0,0)' ;
124+ context . moveTo ( this . position . x - this . bounds . halfWidth , this . position . y - this . bounds . halfHeight ) ;
125+ context . lineTo ( this . position . x + this . bounds . halfWidth , this . position . y - this . bounds . halfHeight ) ;
126+ context . stroke ( ) ;
127+ context . closePath ( ) ;
128+ }
129+ else if ( this . oV == - 1 )
130+ {
131+ context . beginPath ( ) ;
132+ context . strokeStyle = 'rgb(255,0,0)' ;
133+ context . moveTo ( this . position . x - this . bounds . halfWidth , this . position . y + this . bounds . halfHeight ) ;
134+ context . lineTo ( this . position . x + this . bounds . halfWidth , this . position . y + this . bounds . halfHeight ) ;
135+ context . stroke ( ) ;
136+ context . closePath ( ) ;
137+ }
138+
139+ }
140+
141+ }
142+
143+ }
0 commit comments