Permalink
Browse files

Bug fixes in box colliders and added visual box colliders for easy de…

…bugging.

* fixed bug which caused box collider to get wrong offset when resizing
* added PxBoxColliderRenderer for easier collision debugging
* remove unused interfaces
  • Loading branch information...
1 parent 416aadb commit 4e31dba53587473ef274f57ab2b96c6b96bc5b5b @johanp committed Mar 31, 2012
View
2 CHANGELOG
@@ -11,6 +11,8 @@ CHANGELOG
* added a grid collider to the collider example
* fixed bug which caused box colliders to collide with other box colliders by default
* fixed bug which caused entity local position to be scaled by entity local scale
+* fixed bug which caused box collider to get wrong offset when resizing
+* added PxBoxColliderRenderer for easier collision debugging
2012-03-11, 0.4.1, "the sneaky update"
View
2 examples/ExampleLauncher.as
@@ -14,7 +14,7 @@ package examples {
var engine : PxEngine = new PxEngine( 320, 240, 2 );
addChild( engine );
- // engine.showPerformance = true;
+ engine.showPerformance = true;
// engine.pauseOnFocusLost = false;
engine.pushScene( new MenuScene() );
View
26 examples/platformer/PlatformerScene.as
@@ -1,9 +1,12 @@
package examples.platformer {
import examples.assets.AssetFactory;
import flash.geom.Point;
+ import pixelizer.components.collision.PxBoxColliderComponent;
+ import pixelizer.components.collision.PxBoxColliderRenderComponent;
import pixelizer.components.render.PxTextFieldComponent;
import pixelizer.Pixelizer;
import pixelizer.prefabs.gui.PxTextFieldEntity;
+ import pixelizer.PxEngine;
import pixelizer.PxEntity;
import pixelizer.PxInput;
import pixelizer.PxScene;
@@ -31,6 +34,7 @@ package examples.platformer {
// player
private var _player : Player;
+
public function PlatformerScene() {
// background color of scene
@@ -97,7 +101,28 @@ package examples.platformer {
if ( PxInput.isPressed( PxInput.KEY_ESC ) ) {
engine.popScene();
}
+
+
+ if ( PxInput.isPressed( PxInput.KEY_D ) ) {
+ forEachEntity( entityRoot, addColliderRenderer );
+ }
+ if ( PxInput.isPressed( PxInput.KEY_E ) ) {
+ forEachEntity( entityRoot, removeColliderRenderer );
+ }
+ }
+
+ private function addColliderRenderer( pEntity : PxEntity ) : void {
+ if ( pEntity.hasComponentByClass( PxBoxColliderComponent ) ) {
+ if ( !pEntity.hasComponentByClass( PxBoxColliderRenderComponent ) ) {
+ pEntity.addComponent( new PxBoxColliderRenderComponent() );
+ }
+ }
+ }
+ private function removeColliderRenderer( pEntity : PxEntity ) : void {
+ if ( pEntity.hasComponentByClass( PxBoxColliderRenderComponent ) ) {
+ pEntity.removeComponent( new PxBoxColliderRenderComponent() );
+ }
}
private function spawnPickup() : void {
@@ -122,6 +147,7 @@ package examples.platformer {
}
}
_actionLayer.addEntity( pickup )
+
}
}
View
1 pixelizer/PxEngine.as
@@ -7,6 +7,7 @@ package pixelizer {
import flash.system.System;
import flash.utils.getTimer;
import flash.utils.Timer;
+ import pixelizer.components.collision.PxColliderComponent;
import pixelizer.components.render.PxBlitRenderComponent;
import pixelizer.components.render.PxTextFieldComponent;
import pixelizer.prefabs.gui.PxTextFieldEntity;
View
8 pixelizer/PxScene.as
@@ -184,6 +184,14 @@ package pixelizer {
public function getEntitiesByHandle( pRootEntity : PxEntity, pHandle : String, pEntityVector : Vector.<PxEntity> ) : void {
return _entityRoot.getEntitiesByHandle( pRootEntity, pHandle, pEntityVector );
}
+
+
+ public function forEachEntity( pEntityRoot : PxEntity, pFunction : Function ) : void {
+ pFunction( pEntityRoot );
+ for each( var e : PxEntity in pEntityRoot.entities ) {
+ forEachEntity( e, pFunction );
+ }
+ }
}
}
View
2 pixelizer/components/collision/PxBoxColliderComponent.as
@@ -41,6 +41,8 @@ package pixelizer.components.collision {
public function setSize( pWidth : Number, pHeight : Number ) : void {
collisionBox.halfWidth = pWidth / 2;
collisionBox.halfHeight = pHeight / 2;
+ collisionBox.offsetX = pWidth / 2;
+ collisionBox.offsetY = pHeight / 2;
}
View
55 pixelizer/components/collision/PxBoxColliderRenderComponent.as
@@ -0,0 +1,55 @@
+package pixelizer.components.collision
+{
+ import flash.display.BitmapData;
+ import pixelizer.components.collision.PxBoxColliderComponent;
+ import pixelizer.components.render.PxBlitRenderComponent;
+
+ /**
+ * ...
+ * @author Johan Peitz
+ */
+ public class PxBoxColliderRenderComponent extends PxBlitRenderComponent
+ {
+ private var _boxColliderComp : PxBoxColliderComponent = null;
+ private var _renderComp : PxBlitRenderComponent = null;
+
+ public function PxBoxColliderRenderComponent() : void {
+ super();
+ alpha = 0.5;
+ }
+
+
+ override public function dispose() : void {
+ _boxColliderComp = null;
+ super.dispose();
+ }
+
+ override public function update( pDT : Number ) : void {
+ // get collider info
+ if ( _boxColliderComp == null ) {
+ _boxColliderComp = entity.getComponentByClass( PxBoxColliderComponent ) as PxBoxColliderComponent;
+ }
+
+ // use collider info
+ if ( _boxColliderComp != null ) {
+ if ( bitmapData != null ) {
+ bitmapData.dispose();
+ }
+ bitmapData = new BitmapData( _boxColliderComp.collisionBox.halfWidth * 2, _boxColliderComp.collisionBox.halfHeight * 2, false, 0xFF00FF );
+
+ /*
+ if ( _renderComp == null ) {
+ _renderComp = entity.getComponentByClass( PxBlitRenderComponent ) as PxBlitRenderComponent;
+ }
+ hotspot.x = _renderComp.hotspot.x;
+ hotspot.y = _renderComp.hotspot.y;
+ */
+ }
+
+ // keep calm and carry on
+ super.update( pDT );
+ }
+
+ }
+
+}
View
17 pixelizer/components/render/IPxRenderableComponent.as
@@ -1,17 +0,0 @@
-package pixelizer.components.render
-{
- import flash.display.BitmapData;
- import flash.geom.Point;
- import flash.geom.Rectangle;
- import pixelizer.utils.PxRenderStats;
-
- /**
- * ...
- * @author Johan Peitz
- */
- public interface IPxRenderableComponent
- {
- function render( pView : Rectangle, pBitmapData : BitmapData, pPosition : Point, pRotation : Number, pScaleX : Number, pScaleY : Number, pRenderStats : PxRenderStats ) : void ;
- }
-
-}
View
7 pixelizer/components/render/PxBlitRenderComponent.as
@@ -13,7 +13,7 @@ package pixelizer.components.render {
* Allows for graphical output using the blit renderer.
* Can be animated using animation components.
*/
- public class PxBlitRenderComponent extends PxComponent implements IPxRenderableComponent {
+ public class PxBlitRenderComponent extends PxComponent {
private var _bufferTopLeft_ : Point;
private var _globalTopLeft_ : Point;
@@ -54,7 +54,7 @@ package pixelizer.components.render {
renderOffset.x = renderOffset.y = 0;
_matrix = Pixelizer.matrixPool.fetch();
- _colorTransform = new ColorTransform( 1, 1, 1, 1, 0, 0, 0, 0 );
+ _colorTransform = new ColorTransform( );
bitmapData = pBitmapData;
@@ -64,7 +64,8 @@ package pixelizer.components.render {
if ( pHotspot != null ) {
hotspot.x = pHotspot.x;
hotspot.y = pHotspot.y;
- } else if ( pBitmapData != null ) {
+ }
+ else if ( pBitmapData != null ) {
hotspot.x = pBitmapData.width / 2;
hotspot.y = pBitmapData.height / 2;
}
View
2 pixelizer/render/IPxRenderer.as
@@ -13,7 +13,7 @@ package pixelizer.render {
* Renders a scene.
* @param pScene Scene to render.
*/
- function render( pScene : PxScene, pComponentToRender : Class = null ) : void;
+ function render( pScene : PxScene ) : void;
/**
* Returns the display object for the renderer. The display object contains the visible result of each render pass.
View
16 pixelizer/render/PxBlitRenderer.as
@@ -8,7 +8,6 @@ package pixelizer.render {
import flash.utils.getTimer;
import pixelizer.components.collision.PxColliderComponent;
import pixelizer.components.PxComponent;
- import pixelizer.components.render.IPxRenderableComponent;
import pixelizer.components.render.PxBlitRenderComponent;
import pixelizer.Pixelizer;
import pixelizer.PxEntity;
@@ -69,7 +68,7 @@ package pixelizer.render {
* any PxBlitRenderComponents found.
* @param pScene Scene to render.
*/
- public function render( pScene : PxScene, pComponentToRender : Class = null ) : void {
+ public function render( pScene : PxScene ) : void {
// clear bitmap data
if ( pScene.background ) {
_surface.bitmapData.fillRect( _surface.bitmapData.rect, pScene.backgroundColor );
@@ -78,15 +77,12 @@ package pixelizer.render {
if ( pScene.camera != null ) {
_view.width = pScene.camera.view.width;
_view.height = pScene.camera.view.height;
- if ( pComponentToRender == null ) {
- pComponentToRender = PxBlitRenderComponent;
- }
- renderComponents( pComponentToRender, pScene.entityRoot, pScene, pScene.entityRoot.transform.position, pScene.entityRoot.transform.rotation, pScene.entityRoot.transform.scaleX, pScene.entityRoot.transform.scaleY );
+ renderComponents( pScene.entityRoot, pScene, pScene.entityRoot.transform.position, pScene.entityRoot.transform.rotation, pScene.entityRoot.transform.scaleX, pScene.entityRoot.transform.scaleY );
}
}
- private function renderComponents( pComponentClass : Class, pEntity : PxEntity, pScene : PxScene, pPosition : Point, pRotation : Number, pScaleX : Number, pScaleY : Number ) : void {
+ private function renderComponents( pEntity : PxEntity, pScene : PxScene, pPosition : Point, pRotation : Number, pScaleX : Number, pScaleY : Number ) : void {
_view.x = pScene.camera.view.x * pEntity.transform.scrollFactorX;
_view.y = pScene.camera.view.y * pEntity.transform.scrollFactorY;
@@ -103,12 +99,12 @@ package pixelizer.render {
pos.y = pPosition.y + e.transform.position.y * pScaleY;
}
- var renderableComponents : Vector.<PxComponent> = e.getComponentsByClass( pComponentClass );
- for each ( var renderableComponent : IPxRenderableComponent in renderableComponents ) {
+ var renderableComponents : Vector.<PxComponent> = e.getComponentsByClass( PxBlitRenderComponent );
+ for each ( var renderableComponent : PxBlitRenderComponent in renderableComponents ) {
renderableComponent.render( _view, _surface.bitmapData, pos, pRotation + e.transform.rotation, pScaleX * e.transform.scaleX, pScaleY * e.transform.scaleY, _renderStats );
}
- renderComponents( pComponentClass, e, pScene, pos, pRotation + e.transform.rotation, pScaleX * e.transform.scaleX, pScaleY * e.transform.scaleY );
+ renderComponents( e, pScene, pos, pRotation + e.transform.rotation, pScaleX * e.transform.scaleX, pScaleY * e.transform.scaleY );
Pixelizer.pointPool.recycle( pos );
}
}

0 comments on commit 4e31dba

Please sign in to comment.