flash on 2013-7-19
package
{
import flash.display.Stage;
import flash.display.Sprite;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.events.Event;
[SWF(backgroundColor='#000000', width='465', height='465', frameRate='60')]
public class PixelPlaneGame extends Sprite
{
public var displayContainer:Bitmap;
public var display:BitmapData;
public var clearBuffer:BitmapData;
public var clearPoint:Point = new Point();
public var gameScale:int = 1;
public var things:Vector.<Player> = new Vector.<Player>();
public function PixelPlaneGame()
{
display = new BitmapData(stage.stageWidth / gameScale, stage.stageHeight / gameScale, true, 0xFF000000);
clearBuffer = display.clone();
displayContainer = new Bitmap(display);
displayContainer.scaleX = displayContainer.scaleY = gameScale;
addChild(displayContainer);
Assets.prepareGraphics();
display.draw(Assets.sprites[0].images);
stage.addEventListener(Event.ENTER_FRAME, tick);
const NUM_THINGS:int = 50;
for(var i:int = 0; i < NUM_THINGS; i++)
{
var player:Player = new Player();
player.position = new Point(200*Math.random(),200*Math.random());
player.velocity1D = 4.0;// * (1.0 - (0.4 * ((NUM_THINGS - i)/NUM_THINGS)));
player.rotation = -Math.PI + (Math.PI * 2 * Math.random());
things.push(player);
}
}
public function tick(e:Event):void
{
// Clear view
display.copyPixels(clearBuffer, clearBuffer.rect, clearPoint);
for(var i:int = 0; i < things.length; i++)
{
var fp:Point = i > 0 ? things[i-1].position : new Point(mouseX / gameScale, mouseY / gameScale);
var player:Player = things[i];
player.rotation = MathUtils.rtusm(player.rotation, Math.atan2(fp.y - player.position.y, fp.x - player.position.x), 5.0);
player.move();
player.render(display);
}
}
}
}
import flash.display.BitmapData;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.geom.Matrix;
class Assets
{
static public var rotations:int = 16;
static public var _:int = 0;
static public var designs:Vector.<Vector.<uint>> = new <Vector.<uint>>[
new <uint>[
1, 1, 1, _, _, _, _, 1, 1, 1,
1, 1, 1, _, _, _, _, 1, 1, 1,
1, 1, 1, _, _, _, _, 1, 1, 1,
1, 1, 1, _, _, _, _, 1, 1, 1,
1, 1, 1, _, _, _, _, 1, 1, 1,
1, 1, 1, _, _, _, _, 1, 1, 1,
1, 1, 1, _, _, _, _, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1
/*_, _, _, 1, 1, _, _, _,
_, _, 1, 1, 1, 1, _, _,
_, _, 1, 1, 1, 1, _, _,
_, _, 1, 1, 1, 1, _, _,
_, _, 1, 1, 1, 1, _, _,
_, 1, 1, 1, 1, 1, 1, _,
1, 1, 1, _, _, 1, 1, 1,
1, 1, _, _, _, _, 1, 1*/
]
];
static public const ID_PLAYER:int = 0;
// storage
static public var sprites:Vector.<AssetData>;
static public function prepareGraphics():void
{
Assets.sprites = new Vector.<AssetData>(Assets.designs.length)
for(var i:int = 0; i < Assets.designs.length; i++)
{
var size:int = Math.sqrt(Assets.designs[i].length),
base:BitmapData = new BitmapData(size, size, true, 0x00000000);
for(var p:int = 0; p < size * size; p++)
base.setPixel32(p % size, int(p / size), Assets.designs[i][p] > 0 ? 0xFFFFFFFF : 0x00000000);
var assetData:AssetData = new AssetData(new BitmapData(size * Assets.rotations * 2, size * 2, true, 0x00000000), size),
padding:int = size / 2,
mat:Matrix = new Matrix();
for(p = 0; p < Assets.rotations; p++)
{
var angle:Number = Math.PI * 2 / rotations * p;
mat.identity();
mat.translate(-size / 2, -size / 2);
mat.rotate(angle);
mat.translate(size + (p * size * 2), size);
assetData.images.draw(base, mat);
}
cleanImage(assetData.images);
Assets.sprites[i] = assetData;
}
}
static public function cleanImage(image:BitmapData, tolerance:Number = 0.5):void
{
for(var i:int = 0; i < image.height; i++)
{
for(var j:int = 0; j < image.width; j++)
{
var col:uint;
if((col = (image.getPixel32(j, i) >>> 24)) / 0xFF < tolerance)
image.setPixel32(j, i, 0x00000000);
else if(col < 0xFF)
image.setPixel32(j, i, 0xFFFFFFFF);
}
}
}
}
class AssetData
{
public var images:BitmapData;
public var size:int;
public function AssetData(image:BitmapData, _size:int)
{
images = image;
size = _size;
}
}
class GameObject
{
// render shortcuts
static public var pt:Point = new Point();
static public var ap:Point = new Point();
static public var rt:Rectangle = new Rectangle();
static public const TYPE_PARTICLE:int = 0;
static public const TYPE_SHIP:int = 1;
public var spriteID:int;
public var type:int;
public var position:Point;
public var velocity2D:Point;
public var velocity1D:Number;
public var rotation:Number;
public function GameObject()
{
velocity1D = 0.0;
velocity2D = new Point();
rotation = 0.0;
}
public function move():void
{
switch(type)
{
case TYPE_PARTICLE:
{
position.x += velocity2D.x;
position.y += velocity2D.y;
break;
}
case TYPE_SHIP:
{
position.x += velocity1D * Math.cos(rotation);
position.y += velocity1D * Math.sin(rotation);
break;
}
}
if(rotation < -Math.PI) rotation += Math.PI * 2;
if(rotation > Math.PI) rotation -= Math.PI * 2;
}
public function render(view:BitmapData):void
{
var imageSize:int = Assets.sprites[spriteID].size,
image:BitmapData = Assets.sprites[spriteID].images,
rot:int = Assets.rotations * ((rotation + Math.PI) / (Math.PI * 2));
rot = (rot < 0 ? 0 : (rot >= Assets.rotations ? Assets.rotations - 1 : rot));
rot = (rot + (Assets.rotations * 0.75)) % Assets.rotations;
pt.x = int(position.x) - imageSize;
pt.y = int(position.y) - imageSize;
rt.x = rot * imageSize * 2;
rt.y = 0;
rt.width = rt.height = imageSize * 2;
ap.x = rt.x;
ap.y = rt.y;
view.copyPixels(image, rt, pt, image, ap, true);
}
}
/*
Actual actors
*/
class Player extends GameObject
{
public function Player()
{
spriteID = Assets.ID_PLAYER;
type = TYPE_SHIP;
super();
}
}
class MathUtils
{
static public function rtusm(from:Number, to:Number, smoothness:Number):Number
{
var a:Number = (from > to ? from : to) - (from < to ? from : to);
var b:Number = ((-Math.PI + (from < to ? from : to)) + (Math.PI - (from > to ? from : to))) + (Math.PI * 2);
if(a < b)
from -= (from - to) / smoothness;
else
if(from > to)
from += b / smoothness;
else
from -= b / smoothness;
from = from < -Math.PI ? from + (Math.PI * 2) : from;
from = from > Math.PI ? from - (Math.PI * 2) : from;
return from;
}
}