なんとかディウスっぽい背景
package
{
import flash.display.*;
import flash.geom.*;
import flash.events.*;
[SWF(width="320", height="240", backgroundColor="0x000000", frameRate="30")]
public class Nemesis extends Sprite
{
public static const W:int = 320;
public static const H:int = 240;
private var mScroll:int = 0;
private var mMyPos:Point;
private var mBGen:MountainGen = new MountainGen(160);
private var mTGen:MountainGen = new MountainGen(160, 1);
private var mBGScreen:BitmapData = new BitmapData(W, H, true, 0);
private var mBGScreenBmp:Bitmap;
private var mBGScreenBmp2:Bitmap;
private var mStarbg:StarBG = new StarBG(W, H, 30);
function Nemesis()
{
addChild(mStarbg);
mBGScreenBmp = new Bitmap(mBGScreen);
addChild(mBGScreenBmp);
mBGScreenBmp2 = new Bitmap(mBGScreen);
addChild(mBGScreenBmp2);
var i:int;
mBGen.step();
mTGen.step();
for (i = 0;i < W;i++) {
mBGen.step();
mTGen.step();
mBGen.draw(mBGScreen, i, 80);
mTGen.draw(mBGScreen, i, 0, false);
}
addEventListener(Event.ENTER_FRAME, tick);
}
private var _clrrc:Rectangle = new Rectangle(0, 0, 1, H);
private function tick(e:Event):void
{
var ox:int = mScroll % W;
_clrrc.x = ox;
mBGScreen.fillRect(_clrrc, 0);
mStarbg.tick();
mBGen.step();
mTGen.step();
mStarbg.draw();
mBGen.draw(mBGScreen, ox, 80);
mTGen.draw(mBGScreen, ox, 0, false);
mBGScreenBmp.x = -ox;
mBGScreenBmp2.x = W-ox;
mScroll++;
}
}
}
class MountainGen
{
import flash.display.*;
private var mPrevBuffer:Array;
private var mHeight:int;
private var mCount:int = 0;
private var tmpBuffer:Array;
private var mGenFunc:Function;
function MountainGen(h:int, generator:int = 0)
{
mHeight = h;
mGenFunc = generator ? genWav2 : genWav;
mPrevBuffer = new Array(h);
tmpBuffer = new Array(h);
}
public function draw(b:BitmapData, x:int, y:int, rev:Boolean = true):void
{
var i:int;
for (i = 0;i < mHeight;i++) {
if (tmpBuffer[i]) {
var c:int = tmpBuffer[i];
b.setPixel32(x, rev ? (y+mHeight-i) : (y+i), 0xff000000 | (c/3+11) | ((c/5 + 170)<<16) | ((c/2+60) << 8));
}
}
}
public function step():void
{
var t:Number = Number(mCount) * 0.02;
var h:int = mGenFunc(t) * mHeight;
var i:int, k:int, m:int;
for (i = 0;i < mHeight;i++) {
mPrevBuffer[i] = tmpBuffer[i];
}
for (i = 0;i < mHeight;i++) {
tmpBuffer[i] = (i < h) ? (Math.random()*80 + 80) : 0;
if (tmpBuffer[i]) {
if (mPrevBuffer[i])
tmpBuffer[i] = (tmpBuffer[i] + mPrevBuffer[i]*7)/8;
if (mPrevBuffer[i] == 0 || i == (h-1)) {
m = 50;
for (k = i;k >= 0 && m > 0;k--, m-=4) {
if (m > 33) m--;
tmpBuffer[k] += m;
m += Math.random()*7;
}
}
if (mPrevBuffer[i+1] && i == (h-1)) {
m = -48;
for (k = i;k >= 0 && m < 0;k--, m++) {
tmpBuffer[k] += m;
if (tmpBuffer[k]<1) tmpBuffer[k] = 1;
}
}
}
}
for (i = 0;i < mHeight;i++)
tmpBuffer[i] = (tmpBuffer[i] < 0) ? 0 : (tmpBuffer[i] > 255) ? 255 : tmpBuffer[i];
mCount++;
}
private static function genWav(t:Number, nest:int = 0):Number
{
var v:Number = Math.sin(t);
v += Math.cos(t*3) * 0.1;
v += Math.cos(0.1 + t*10) * 0.02;
v *= Math.cos(t*0.1);
if (nest < 5)
v += genWav(t+1, ++nest);
v = v*0.2 + 0.3;
return (v<0) ? 0 : (v>1) ? 1 : v;
}
private static function genWav2(t:Number, nest:int = 0):Number
{
var v:Number = Math.sin(t);
v += Math.cos(t*3) * 0.1;
v += Math.cos(0.1 + t*9) * 0.02;
v *= Math.cos(0.2 + t*0.15);
if (nest < 5)
v += genWav2(t+1, ++nest);
v = v*0.2 + 0.2;
return (v<0) ? 0 : (v>1) ? 1 : v;
}
}
class StarBG extends flash.display.Sprite
{
import flash.display.*;
import flash.geom.Point;
private var mWidth:int;
private var mHeight:int;
private var mStars:Array;
private var mStarVs:Array;
private var mN:int;
function StarBG(w:int, h:int, n:int)
{
mWidth = w;
mHeight = h;
mN = n;
mStars = new Array(n);
mStarVs = new Array(n);
for (var i:int = 0;i < n;i++) {
mStars[i] = new Point(int( Math.random()*w ), int( Math.random()*h ));
mStarVs[i] = Math.random() + 0.2;
}
}
public function tick():void
{
var n:int = mN;
for (var i:int = 0;i < n;i++) {
mStars[i].x -= Number(mStarVs[i]);
if (mStars[i].x < 0) {
mStars[i].x += mWidth;
mStars[i].y = int( Math.random()*mHeight );
}
}
}
public function draw():void
{
var g:Graphics = graphics;
var n:int = mN;
g.clear();
for (var i:int = 0;i < n;i++) {
g.beginFill(0xffffff);
g.drawCircle( mStars[i].x, mStars[i].y , 0.4);
}
}
}