Frame 1
function NotifyBallCrossingMidfield() {
_root._gravityLocked = false;
_root._ballInUpperHalf = !_root._ballInUpperHalf;
}
function FlipGravity(force) {
if (force || (!_root._gravityLocked)) {
_root._gravityForce._y = -_root._gravityForce._y;
_root._gravityLocked = true;
}
}
function GetLocationHash(x, y) {
return((Math.floor(x) * 600) + Math.floor(y));
}
function RegisterPegEmitter(x, y, emitter) {
if (!_root._arrPegEmitters) {
_root._arrPegEmitters = new Array();
}
_root._arrPegEmitters[GetLocationHash(x, y)] = emitter;
}
function DiscardPegEmitter(x, y) {
_root._arrPegEmitters[GetLocationHash(x, y)] = null;
}
function GetPegEmitter(x, y) {
if (!_root._arrPegEmitters) {
return(null);
}
return(_root._arrPegEmitters[GetLocationHash(x, y)]);
}
_root._nextEmissionDepth = 1000;
_root._pegRestitution = 0.7;
_root._wallRestitution = 1;
_root._paddleRestitution = 0.7;
_root._gravityForce = new Object();
_root._gravityForce._x = 0;
_root._gravityForce._y = 0.8;
_root._theGame.theBall._x = _root._theGame.theBall._x + Math.random();
_root._theGame.theBall._velX = Math.random();
_root._theGame.theBall._velY = Math.random();
_root._ballInUpperHalf = true;
_root._lowerPaddle = _root._theGame._paddle0;
_root._upperPaddle = _root._theGame._paddle1;
Symbol 2 MovieClip [CBall] Frame 1
#initclip 7
function CBall() {
this._mass = 1;
this._maxForceDist = 100;
this._magneticForceScalar = 1;
this._drag = 0;
this._restitution = 1;
this._collisionRadius = 6;
this._nSimulationSubsteps = 1;
}
function DotProduct(x1, y1, x2, y2) {
return((x1 * x2) + (y1 * y2));
}
function SafeSqrt(x) {
if (x < 1E-5) {
return(0);
}
return(Math.sqrt(x));
}
CBall.prototype = new MovieClip();
Object.registerClass("CBall", CBall);
CBall.prototype.ApplyGravity = function (pctTime) {
this._velX = this._velX + (_root._gravityForce._x * pctTime);
this._velY = this._velY + (_root._gravityForce._y * pctTime);
};
CBall.prototype.IsColliding = function (obj) {
if (obj._collisionRadius == undefined) {
obj._collisionRadius = Math.max(obj._width, obj._height) * 0.5;
}
var deltaX = (this._x - obj._x);
var deltaY = (this._y - obj._y);
var distSquared = ((deltaX * deltaX) + (deltaY * deltaY));
return(distSquared <= ((this._collisionRadius * this._collisionRadius) + (obj._collisionRadius * obj._collisionRadius)));
};
CBall.prototype.ResolveCollision = function (obj, pctTime) {
var deltaX = (this._x - obj._x);
var deltaY = (this._y - obj._y);
var dist = Math.sqrt((deltaX * deltaX) + (deltaY * deltaY));
var momentumX = ((this._velX + this._forceAccumX) + _root._gravityForce._x);
var momentumY = ((this._velY + this._forceAccumY) + _root._gravityForce._y);
var speed = SafeSqrt((this._velX * this._velX) + (this._velY * this._velY));
var dotIncidence = Math.abs(DotProduct(deltaX / dist, deltaY / dist, this._velX / speed, this._velY / speed));
var invDotIncidence = (1 - dotIncidence);
if (obj._restitution == undefined) {
obj._restitution = 1;
}
this._x = this._prevX;
this._y = this._prevY;
var impactRestitution = (this._restitution * obj._restitution);
this._forceAccumX = this._forceAccumX + ((-this._velX) * dotIncidence);
this._forceAccumY = this._forceAccumY + ((-this._velY) * dotIncidence);
this._velX = this._velX + (((speed * (deltaX / dist)) * impactRestitution) * dotIncidence);
this._velY = this._velY + (((speed * (deltaY / dist)) * impactRestitution) * dotIncidence);
};
CBall.prototype.ConsiderCollisionWithObject = function (obj, pctTime) {
if (!obj._visible) {
return(false);
}
if (this.IsColliding(obj)) {
this.ResolveCollision(obj, pctTime);
return(true);
}
return(false);
};
CBall.prototype.FindAndHandlePegCollisions = function (pctTime) {
var pegSpacing = 44.9;
var pegOffset = 22.45;
var pegSpaceXgrid = Math.round(this._x / pegSpacing);
var pegSpaceYgrid = Math.round(this._y / pegSpacing);
var pegSpaceXws = (pegSpaceXgrid * pegSpacing);
var pegSpaceYws = (pegSpaceYgrid * pegSpacing);
if ((((pegSpaceXgrid <= 2) && (pegSpaceXgrid >= -2)) && (pegSpaceYgrid <= 1)) && (pegSpaceYgrid >= -1)) {
var deltaX = (this._x - pegSpaceXws);
var deltaY = (this._y - pegSpaceYws);
var distSquared = ((deltaX * deltaX) + (deltaY * deltaY));
var minCollisionDist = (this._collisionRadius + 5);
minCollisionDist = minCollisionDist * minCollisionDist;
if (distSquared < minCollisionDist) {
var pegProxyObj = new Object();
pegProxyObj._x = pegSpaceXws;
pegProxyObj._y = pegSpaceYws;
pegProxyObj._restitution = _root._pegRestitution;
this.ResolveCollision(pegProxyObj, pctTime);
var pegEmitterName = ("CPegEmitter" + (++_root._nPegEmitters));
this._parent.attachMovie("CPegEmitter", pegEmitterName, ++this._parent._nextEmitterDepth, {_x:pegSpaceXws, _y:pegSpaceYws, _intensity:SafeSqrt((this._velX * this._velX) + (this._velY * this._velY))});
}
}
pegSpaceXgrid = Math.round((this._x - pegOffset) / pegSpacing);
pegSpaceYgrid = Math.round((this._y - pegOffset) / pegSpacing);
pegSpaceXws = (pegSpaceXgrid * pegSpacing) + pegOffset;
pegSpaceYws = (pegSpaceYgrid * pegSpacing) + pegOffset;
if ((((pegSpaceXgrid <= 2) && (pegSpaceXgrid >= -3)) && (pegSpaceYgrid <= 1)) && (pegSpaceYgrid >= -2)) {
var deltaX = (this._x - pegSpaceXws);
var deltaY = (this._y - pegSpaceYws);
var distSquared = ((deltaX * deltaX) + (deltaY * deltaY));
var minCollisionDist = (this._collisionRadius + 5);
minCollisionDist = minCollisionDist * minCollisionDist;
if (distSquared < minCollisionDist) {
var pegProxyObj = new Object();
pegProxyObj._x = pegSpaceXws;
pegProxyObj._y = pegSpaceYws;
pegProxyObj._restitution = _root._pegRestitution;
this.ResolveCollision(pegProxyObj, pctTime);
var pegEmitterName = ("CPegEmitter" + (++_root._nPegEmitters));
this._parent.attachMovie("CPegEmitter", pegEmitterName, ++this._parent._nextEmitterDepth, {_x:pegSpaceXws, _y:pegSpaceYws, _intensity:SafeSqrt((this._velX * this._velX) + (this._velY * this._velY))});
}
}
};
CBall.prototype.FindAndHandlePaddleCollisions = function () {
var collidingPaddle = null;
var ballResetY;
var yOffset;
if ((this._y >= (185 - this._collisionRadius)) && ((this._y - this._velY) <= (185 - this._collisionRadius))) {
collidingPaddle = _root._lowerPaddle;
ballResetY = 185 - this._collisionRadius;
} else if ((this._y <= (-185 + this._collisionRadius)) && ((this._y - this._velY) >= (-185 + this._collisionRadius))) {
collidingPaddle = _root._upperPaddle;
ballResetY = -185 + this._collisionRadius;
}
if (collidingPaddle) {
if ((this._x >= ((collidingPaddle._x - (collidingPaddle._width * 0.5)) - this._collisionRadius)) && (this._x <= ((collidingPaddle._x + (collidingPaddle._width * 0.5)) + this._collisionRadius))) {
this._x = this._prevX;
this._y = ballResetY;
this._velX = this._velX + ((this._x - collidingPaddle._x) * 0.3);
this._velY = (-this._velY) * _root._paddleRestitution;
_root.FlipGravity();
}
}
};
CBall.prototype.FindAndHandleCollisions = function (pctTime) {
if (this._x < (-112.5 + this._collisionRadius)) {
var wallProxyObj = new Object();
wallProxyObj._x = this._x - 1;
wallProxyObj._y = this._y;
wallProxyObj._restitution = _root._wallRestitution;
this.ResolveCollision(wallProxyObj, pctTime);
} else if (this._x > (112.5 - this._collisionRadius)) {
var wallProxyObj = new Object();
wallProxyObj._x = this._x + 1;
wallProxyObj._y = this._y;
wallProxyObj._restitution = _root._wallRestitution;
this.ResolveCollision(wallProxyObj, pctTime);
}
this.FindAndHandlePegCollisions(pctTime);
this.FindAndHandlePaddleCollisions(pctTime);
};
CBall.prototype.onEnterFrame = function () {
if (_root._isPaused) {
return(undefined);
}
if (Key.isDown(32)) {
this._x = 0;
this._y = -160;
this._velX = Math.random();
this._velY = Math.random();
}
var substepTimeSlice = (1 / this._nSimulationSubsteps);
var i;
i = 0;
while (i < this._nSimulationSubsteps) {
this.Simulate(substepTimeSlice);
i++;
}
if ((_root._ballInUpperHalf && (this._y > 0)) || ((!_root._ballInUpperHalf) && (this._y < 0))) {
_root.NotifyBallCrossingMidfield();
}
if (this._lastTrailDropPosX == undefined) {
this._lastTrailDropPosX = this._prevX;
this._lastTrailDropPosY = this._prevY;
}
var trailDeltaX = (this._x - this._lastTrailDropPosX);
var trailDeltaY = (this._y - this._lastTrailDropPosY);
var distSinceLastTrail = SafeSqrt((trailDeltaX * trailDeltaX) + (trailDeltaY * trailDeltaY));
if (distSinceLastTrail > 10) {
var trailSegmentName = ("CTrailSegment" + (++this._parent._nTrailSegments));
this._parent.attachMovie("CTrailSegment", trailSegmentName, ++this._parent._nextTrailSegmentDepth);
this._parent[trailSegmentName].SetPosition(this._x, this._y, this._lastTrailDropPosX, this._lastTrailDropPosY);
this._lastTrailDropPosX = this._x;
this._lastTrailDropPosY = this._y;
}
this._forceAccumX = 0;
this._forceAccumY = 0;
};
CBall.prototype.Simulate = function (pctTime) {
this._prevX = this._x;
this._prevY = this._y;
if (_root._pctSpeed) {
pctTime = _root._pctSpeed / 100;
}
this.ApplyGravity(pctTime);
this.FindAndHandleCollisions(pctTime);
if ((!this._outOfBounds) && (this._y > (191 + this._collisionRadius))) {
this._parent.ScoreForPlayer(1);
this._outOfBounds = true;
} else if ((!this._outOfBounds) && (this._y < (-191 - this._collisionRadius))) {
this._parent.ScoreForPlayer(0);
this._outOfBounds = true;
}
var accelX = (this._forceAccumX / this._mass);
var accelY = (this._forceAccumY / this._mass);
this._velX = this._velX + (accelX * pctTime);
this._velY = this._velY + (accelY * pctTime);
this._velX = this._velX - (this._velX * this._drag);
this._velY = this._velY - (this._velY * this._drag);
if (Math.abs(this._velX) < 0.1) {
this._velX = ((Math.random() > 0.5) ? 0.1 : -0.1);
}
this._x = this._x + (this._velX * pctTime);
this._y = this._y + (this._velY * pctTime);
};
#endinitclip
this.stop();
Symbol 4 MovieClip [CTrailSegment] Frame 1
#initclip 1
function CTrailSegment() {
}
CTrailSegment.prototype = new MovieClip();
Object.registerClass("CTrailSegment", CTrailSegment);
CTrailSegment.prototype.SetPosition = function (startX, startY, endX, endY) {
this._x = startX;
this._y = startY;
var deltaX = (endX - startX);
var deltaY = (endY - startY);
this._rotation = (Math.atan2(deltaY, deltaX) / (Math.PI*2)) * 360;
var magnitude = SafeSqrt((deltaX * deltaX) + (deltaY * deltaY));
this._xscale = magnitude * 10;
};
#endinitclip
Symbol 4 MovieClip [CTrailSegment] Frame 10
removeMovieClip(this);
Symbol 6 MovieClip [CPaddle] Frame 1
#initclip 2
function CPaddle() {
this._maxSpeed = 4;
this._maxAccel = 0.5;
this._score = 0;
}
CPaddle.prototype = new MovieClip();
Object.registerClass("CPaddle", CPaddle);
CPaddle.prototype.AddScore = function () {
this._score++;
};
CPaddle.prototype.onEnterFrame = function () {
if (!this._isPlayerControlled) {
var desiredX = (_root._theGame.theBall._outOfBounds ? 0 : (_root._theGame.theBall._x + ((_root._theGame.theBall._velX > 4) ? (_root._theGame.theBall._velX / this._maxAccel) : 0)));
var desiredDelta = (desiredX - this._x);
var desiredAccel = (desiredDelta - (this._velX / this._maxAccel));
this._accelX = Math.min(Math.max(desiredAccel, -this._maxAccel), this._maxAccel);
this._velX = this._velX + this._accelX;
this._velX = Math.min(Math.max(this._velX, -this._maxSpeed), this._maxSpeed);
this._x = this._x + this._velX;
this._x = Math.max(Math.min(this._x, 112.5 - (this._width * 0.5)), -112.5 + (this._width * 0.5));
}
};
CPaddle.prototype.onMouseMove = function () {
if (this._isPlayerControlled) {
this._x = Math.max(Math.min(_root._xmouse - 275, 112.5 - (this._width * 0.5)), -112.5 + (this._width * 0.5));
}
};
#endinitclip
this.stop();
Symbol 8 MovieClip [CPegEmission] Frame 20
removeMovieClip(this);
Symbol 9 MovieClip [CPegEmitter] Frame 1
#initclip 3
function CPegEmitter() {
if (_root.GetPegEmitter(this._x, this._y) != null) {
removeMovieClip(this);
return(undefined);
}
_root.RegisterPegEmitter(this._x, this._y, this);
this._nEmissions = Math.min(Math.floor(this._intensity / 5), 3);
if (this._nEmissions < 1) {
this._nEmissions = 1;
}
this._spawnInterval = setInterval(this, "Spawn", 150);
this.Spawn();
}
CPegEmitter.prototype = new MovieClip();
Object.registerClass("CPegEmitter", CPegEmitter);
CPegEmitter.prototype.Spawn = function () {
this._nEmissions--;
if (this._nEmissions < 0) {
clearInterval(this._spawnInterval);
_root.DiscardPegEmitter(this._x, this._y);
removeMovieClip(this);
return(undefined);
}
var emissionName = ("CPegEmission" + (++this._parent._nPegEmissions));
this._parent.attachMovie("CPegEmission", emissionName, ++this._parent._nextEmissionDepth, {_x:this._x, _y:this._y, _alpha:Math.min(this._intensity * 10, 100)});
};
#endinitclip
this.stop();
Symbol 11 MovieClip [CDarkPaddle] Frame 1
#initclip 4
Object.registerClass("CDarkPaddle", CPaddle);
#endinitclip
this.stop();
Symbol 13 MovieClip [CWhitePaddle] Frame 1
#initclip 5
Object.registerClass("CWhitePaddle", CPaddle);
#endinitclip
this.stop();
Symbol 15 MovieClip [CPeg] Frame 1
this.stop();
Symbol 20 MovieClip [CPaChongGame] Frame 1
#initclip 6
function CPaChongGame() {
this._nextTrailSegmentDepth = 1000;
this._nextEmissionDepth = 10000;
this._nextEmitterDepth = 50000;
_root._serveInterval = setInterval(this, "Serve", 1500);
}
CPaChongGame.prototype = new MovieClip();
Object.registerClass("CPaChongGame", CPaChongGame);
CPaChongGame.prototype.ScoreForPlayer = function (ndxPlayer) {
var scoringPlayer = this["_paddle" + ndxPlayer];
scoringPlayer.AddScore();
clearInterval(_root._serveInterval);
_root._serveInterval = setInterval(this, "Serve", 1500);
};
CPaChongGame.prototype.Serve = function () {
clearInterval(_root._serveInterval);
removeMovieClip(this.theBall);
this.attachMovie("CBall", "theBall", 10000, {_x:0, _y:((_root._gravityForce._y > 0) ? -140 : 140), _velX:Math.random() * 5});
};
#endinitclip
Mouse.hide();
this.stop();
Instance of Symbol 13 MovieClip [CWhitePaddle] "_paddle0" in Symbol 20 MovieClip [CPaChongGame] Frame 1
onClipEvent (load) {
this._isPlayerControlled = true;
}