package
{
import flash.display.*;
import flash.events.*;
import flash.geom.*;
/*
The base class for all objects in the game.
*/
public class GameObject
{
// object position
public var position:Point = new Point(0, 0);
// higher zOrder objects are rendered on top of lower ones
public var zOrder:int = 0;
// the bitmap data to display
public var graphics:GraphicsResource = null;
// true if the object is active in the game
public var inuse:Boolean = false;
public var collisionArea:Rectangle;
public var collisionName:String = CollisionIdentifiers.NONE;
public function get CollisionArea():Rectangle
{
return new Rectangle(position.x, position.y, collisionArea.width, collisionArea.height);
}
public function GameObject()
{
}
public function startupGameObject(graphics:GraphicsResource, position:Point, z:int = 0):void
{
if (!inuse)
{
this.graphics = graphics;
this.zOrder = z;
this.position = position.clone();
this.inuse = true;
GameObjectManager.Instance.addGameObject(this);
setupCollision();
}
}
public function shutdown():void
{
if (inuse)
{
graphics = null;
inuse = false;
GameObjectManager.Instance.removeGameObject(this);
}
}
public function copyToBackBuffer(db:BitmapData):void
{
db.copyPixels(graphics.bitmap, graphics.bitmap.rect, position, graphics.bitmapAlpha, new Point(0, 0), true);
}
public function enterFrame(dt:Number):void
{
}
public function click(event:MouseEvent):void
{
}
public function mouseDown(event:MouseEvent):void
{
}
public function mouseUp(event:MouseEvent):void
{
}
public function mouseMove(event:MouseEvent):void
{
}
protected function setupCollision():void
{
collisionArea = graphics.bitmap.rect;
}
public function collision(other:GameObject):void
{
}
}
}
We have added two new properties: collisionArea, and collisionName. The collisionArea property represents a rectangle that defines the collision area described above. The collisionName property is a name assigned to a GameObject that defines what type of object it is, at least as far as the collision system will be concerned. For example a weapon fired by the player will have a collisionName of “PlayerWeapon”, while an enemy might have a collision name of “Enemy”. By default we set it to "None" through the CollisionIdentifiers.NONE property.
In addition we also have three new functions: collision, CollisionArea and setupCollision. The collision function is another empty function that is expected to be overridden by an extending class. It will be called by GameObjectManager when a collision has been detected. The setupCollision function is used to save the size of the graphic for use with the collision detection system. CollisionArea returns the collisionArea rectangle at the GameObjects current position on the screen.
You may be wondering why we bother having the collisionArea property at all considering it is exactly the same as graphics.bitmap.rect. This is because later on (in part 7) we will be adding animation to the game. The animation class will override the setupCollision function with its own specialised logic. For now though the collision area will be the same as the graphic rectangle.
package
{
public class CollisionIdentifiers
{
public static const NONE:String = "None";
public static const PLAYER:String = "Player";
public static const PLAYERWEAPON:String = "PlayerWeapon";
public static const ENEMYWEAPON:String = "EnemyWeapon";
public static const ENEMY:String = "Enemy";
public static const POWERUP:String = "Powerup";
}
}
Like the ZOrders class, the CollisionIdentifiers class is used to hold a number of predefined static properties; in this case collision names. Again the purpose of this is to facilitate self documentation. CollisionIdentifiers.PLAYER is self explainatory, whereas the string "Player" doesn't have the same inherent meaning.
Lets now look at the changes made to the GameObjectManager class.