-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
git-svn-id: http://svn.draknek.org/misc/trong@17 b6a31afa-5007-4aed-addc-1e5ce73e4e8a
- Loading branch information
alan
committed
Mar 19, 2010
0 parents
commit ddfd60c
Showing
29 changed files
with
1,359 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package | ||
{ | ||
public class AIController extends Controller | ||
{ | ||
private var paddle: Paddle; | ||
private var game: Game; | ||
|
||
private var targetY: Number = 240; | ||
|
||
public function AIController (_paddle: Paddle, _game: Game) | ||
{ | ||
paddle = _paddle; | ||
game = _game; | ||
} | ||
|
||
private function findTarget (): void | ||
{ | ||
var t: Number = (game.ball.x - paddle.x) / -game.ball.vx; | ||
|
||
if (t > 65 || t < 0) { return; } | ||
|
||
targetY += (game.ball.y + game.ball.vy * t - targetY) * (1 - t * 0.005) * 0.5; | ||
} | ||
|
||
public override function getDirection (): int | ||
{ | ||
findTarget(); | ||
|
||
var diff: Number = targetY - paddle.y; | ||
|
||
if (diff > paddle.size / 2) | ||
{ | ||
return 1; | ||
} | ||
else if (diff < -paddle.size / 2) | ||
{ | ||
return -1; | ||
} | ||
|
||
return 0; | ||
} | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package | ||
{ | ||
import flash.display.*; | ||
import flash.text.*; | ||
import flash.events.MouseEvent; | ||
|
||
public class AdjustableNumberTextField extends Sprite | ||
{ | ||
public var numberTextField: NumberTextField; | ||
|
||
public var min: int; | ||
public var max: int; | ||
|
||
public var callback: Function; | ||
|
||
private var up: Sprite; | ||
private var down: Sprite; | ||
|
||
public function AdjustableNumberTextField (_x: Number, _y: Number, _prefix: String, _autoSize: String = TextFieldAutoSize.CENTER, textSize: Number = 16, _callback: Function = null, _min: int = 0, _max: int = 99) | ||
{ | ||
x = _x; | ||
y = _y; | ||
|
||
min = _min; | ||
max = _max; | ||
|
||
callback = _callback; | ||
|
||
numberTextField = new NumberTextField(0, 0, _prefix, "left", textSize); | ||
|
||
addChild(numberTextField); | ||
|
||
up = new Sprite(); | ||
down = new Sprite(); | ||
|
||
buttonify(up); | ||
buttonify(down); | ||
|
||
alignify(); | ||
} | ||
|
||
private function buttonify (button: Sprite): void | ||
{ | ||
button.buttonMode = true; | ||
button.mouseChildren = false; | ||
|
||
button.addEventListener(MouseEvent.ROLL_OVER, function (param: * = 0) : void {numberTextField.textColor = 0x00FF00}); | ||
button.addEventListener(MouseEvent.ROLL_OUT, function (param: * = 0) : void {numberTextField.textColor = 0xFFFFFF}); | ||
|
||
var valChange: int = (button == up) ? 1 : -1; | ||
|
||
button.addEventListener(MouseEvent.CLICK, function (param:*=null):void {value += valChange;}); | ||
|
||
button.y = numberTextField.height * 0.5; | ||
|
||
button.graphics.beginFill(0x000000); | ||
button.graphics.drawRect(0, -0.5, 1, 1); | ||
button.graphics.endFill(); | ||
|
||
button.graphics.lineStyle(0, 0xFFFFFF); | ||
button.graphics.moveTo(0.1, valChange*0.3); | ||
button.graphics.lineTo(0.5, -valChange*0.3); | ||
button.graphics.lineTo(0.9, valChange*0.3); | ||
|
||
button.scaleX = numberTextField.height * 0.5; | ||
button.scaleY = button.scaleX; | ||
|
||
addChild(button); | ||
} | ||
|
||
private function alignify (): void | ||
{ | ||
var space: Number = 2; | ||
|
||
var totalWidth: Number = down.width + numberTextField.width + up.width + 2 * space; | ||
|
||
if (numberTextField.autoSize == "left") | ||
{ | ||
down.x = 0; | ||
} | ||
else if (numberTextField.autoSize == "center") | ||
{ | ||
down.x = -totalWidth * 0.5; | ||
} | ||
else if (numberTextField.autoSize == "right") | ||
{ | ||
down.x = -totalWidth; | ||
} | ||
|
||
numberTextField.x = down.x + down.width + space; | ||
up.x = numberTextField.x + numberTextField.width + space; | ||
} | ||
|
||
public function set value(_value: int): void | ||
{ | ||
numberTextField.value = _value; | ||
|
||
if (numberTextField.value < min) | ||
{ | ||
numberTextField.value = min; | ||
} | ||
else if (numberTextField.value > max) | ||
{ | ||
numberTextField.value = max; | ||
} | ||
|
||
alignify(); | ||
|
||
if (callback != null) | ||
{ | ||
callback(numberTextField.value); | ||
} | ||
} | ||
|
||
public function get value(): int | ||
{ | ||
return numberTextField.value; | ||
} | ||
|
||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
package | ||
{ | ||
import flash.display.*; | ||
import flash.events.*; | ||
import flash.ui.*; | ||
|
||
public class Ball extends Shape | ||
{ | ||
public var vx: Number; | ||
public var vy: Number; | ||
|
||
private var x1: Number; | ||
private var y1: Number; | ||
private var x2: Number; | ||
private var y2: Number; | ||
|
||
private var game: Game; | ||
|
||
private var contact: LineIntersection; | ||
|
||
private var respawn: Boolean = false; | ||
|
||
public function Ball (_game: Game) | ||
{ | ||
game = _game; | ||
|
||
graphics.beginFill(0x00FF00); | ||
graphics.drawCircle(0, 0, 4); | ||
graphics.endFill(); | ||
|
||
spawn(); | ||
} | ||
|
||
private function spawn (): void | ||
{ | ||
respawn = false; | ||
|
||
x = 320; | ||
y = 240; | ||
|
||
vx = Math.random() * 2 + 3; | ||
vx *= (Math.random() < 0.5) ? -1 : 1; | ||
|
||
vy = Math.random() * 6 - 3; | ||
} | ||
|
||
private function test (ax: Number, ay: Number, bx: Number, by: Number, r: Number = 6): Boolean | ||
{ | ||
contact = new LineIntersection(); | ||
|
||
var movement: Line = new Line(x1, y1, x2, y2); | ||
var barrier: Line = new Line(ax, ay, bx, by); | ||
|
||
var hit: Boolean = false; | ||
|
||
if (r > 0) | ||
{ | ||
hit = Line.intersectsR(movement, barrier, r, contact); | ||
} | ||
else | ||
{ | ||
hit = Line.intersects(movement, barrier, contact); | ||
} | ||
|
||
if (hit) | ||
{ | ||
x2 = contact.x; | ||
y2 = contact.y; | ||
|
||
var nx: Number = by - ay; | ||
var ny: Number = ax - bx; | ||
|
||
var nz: Number = Math.sqrt(nx*nx + ny*ny); | ||
|
||
nx /= nz; | ||
ny /= nz; | ||
|
||
var speed: Number = vx*nx + vy*ny; | ||
|
||
//trace("Old speed: " + vx + ", " + vy); | ||
|
||
vx -= 2 * nx * speed; | ||
vy -= 2 * ny * speed; | ||
|
||
//trace("New speed: " + vx + ", " + vy); | ||
|
||
x2 += (1 - contact.t) * vx; | ||
y2 += (1 - contact.t) * vy; | ||
|
||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public function update (): void | ||
{ | ||
if (respawn) | ||
{ | ||
spawn(); | ||
} | ||
|
||
x1 = x; | ||
y1 = y; | ||
|
||
x2 = x + vx; | ||
y2 = y + vy; | ||
|
||
var p1: Paddle = game.player1; | ||
var p2: Paddle = game.player2; | ||
|
||
if (test(p1.x, p1.top, p1.x, p1.bottom, 6)) | ||
{ | ||
var diff: Number = contact.y - p1.y; | ||
|
||
vy += diff * 0.1; | ||
vy -= p1.vy * 0.1; | ||
|
||
vx += 0.25; | ||
} | ||
|
||
if(test(p2.x, p2.top, p2.x, p2.bottom, 6)) | ||
{ | ||
diff = contact.y - p2.y; | ||
|
||
vy += diff * 0.1; | ||
vy -= p2.vy * 0.1; | ||
|
||
vx -= 0.25; | ||
} | ||
|
||
if (test(0, 0, 0, 480, 4)) // left | ||
{ | ||
game.score2.value += 1; | ||
|
||
if (vx > 3) | ||
{ | ||
vx -= 0.25; | ||
} | ||
|
||
respawn = true; | ||
} | ||
|
||
if(test(640, 480, 640, 0, 4)) // right | ||
{ | ||
game.score1.value += 1; | ||
|
||
if (vx < -3) | ||
{ | ||
vx += 0.25; | ||
} | ||
|
||
respawn = true; | ||
} | ||
|
||
test(0, 480, 640, 480, 4); // bottom | ||
test(640, 0, 0, 0, 4); // top | ||
|
||
var xArr: Array = game.lineBall.xArray; | ||
var yArr: Array = game.lineBall.yArray; | ||
|
||
for (var i: uint = 0; i < xArr.length - 1; i++) | ||
{ | ||
test(xArr[i], yArr[i], xArr[i+1], yArr[i+1]); | ||
} | ||
|
||
x = x2;// - 0.01 * vx; | ||
y = y2;// - 0.01 * vy; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package | ||
{ | ||
import flash.display.*; | ||
import flash.text.*; | ||
import flash.events.MouseEvent; | ||
|
||
public class Button extends Sprite | ||
{ | ||
public var textField: MyTextField; | ||
|
||
public function Button (_text: String, _textSize: Number = 16, _width: Number = 0, _colour: int = 0x000000) | ||
{ | ||
textField = new MyTextField(10, 5, _text, "left", _textSize); | ||
|
||
var _height: Number = textField.height + 10; | ||
|
||
_width = Math.max(_width, textField.width + 20); | ||
|
||
textField.x = _width / 2 - textField.width / 2; | ||
|
||
addChild(textField); | ||
|
||
/*graphics.beginFill(_colour); | ||
graphics.drawRect(0, 0, _width, _height); | ||
graphics.endFill(); | ||
graphics.lineStyle(4, 0x000000); | ||
graphics.drawRect(0, 0, _width, _height);*/ | ||
|
||
buttonMode = true; | ||
mouseChildren = false; | ||
|
||
addEventListener(MouseEvent.ROLL_OVER, function (param: * = 0) : void {textField.textColor = 0x00FF00}); | ||
addEventListener(MouseEvent.ROLL_OUT, function (param: * = 0) : void {textField.textColor = 0xFFFFFF}); | ||
} | ||
|
||
} | ||
} | ||
|
Oops, something went wrong.