なんとかディウスっぽい蟹さん forked from: なんとかディウスっぽい背景
左右キーとスペースで操作できます
// forked from gyuque's なんとかディウスっぽい背景
// 左右キーとスペースで操作できます
package
{
import flash.display.*;
import flash.geom.*;
import flash.events.*;
import flash.ui.*;
[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 mBGen:MountainGen = new MountainGen(80);
private var mTGen:MountainGen = new MountainGen(80, 1);
private var mScreenBmp:Bitmap;
private var mScreen:BitmapData = new BitmapData(W, H, false, 0);
private var mBGScreen:BitmapData = new BitmapData(W, H, true, 0);
private var mStarbg:StarBG = new StarBG(W, H, 50);
private var mKani:Kani;
private var legs:Array = new Array();
private var count:int = 0;
function Nemesis()
{
mScreenBmp = new Bitmap(mScreen);
addChild(mScreenBmp);
mBGen.step();
mTGen.step();
for(var i:int = 0; i < W; i++)
{
mBGen.step();
mTGen.step();
mBGen.draw(mBGScreen, i, 160);
mTGen.draw(mBGScreen, i, 0, false);
}
legs.push(new Leg(-80, 1));
legs.push(new Leg( 0, 1));
legs.push(new Leg( 80, 1));
legs.push(new Leg(-80, -1));
legs.push(new Leg( 0, -1));
legs.push(new Leg( 80, -1));
mKani = new Kani(legs);
addEventListener(Event.ENTER_FRAME, tick);
stage.addEventListener(KeyboardEvent.KEY_DOWN, Key.onKeyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, Key.onKeyUpHandler);
}
private function tick(e:Event):void
{
var leg:Leg;
mStarbg.main();
mKani.main();
if(count % 2 == 0) {scroll(1);}
count++;
//描画
mScreen.fillRect(mScreen.rect, 0);
mStarbg.draw(mScreen);
var matrix:Matrix = new Matrix();
for each(leg in legs)
{
matrix.translate(leg.x, leg.y);
mScreen.draw(leg.sprite, matrix);
matrix.identity();
}
matrix.translate(mKani.x, mKani.y);
mScreen.draw(mKani, matrix);
mScreen.copyPixels(mBGScreen, mScreen.rect, new Point(0, 0));
//当たり判定
for each(leg in legs) {
leg.hitflag = mBGScreen.getPixel32(leg.x, leg.y) != 0x00000000;
}
}
public function scroll(vx:int):void
{
mBGScreen.scroll(-vx, 0);
mBGScreen.fillRect(new Rectangle(W-vx, 0, vx, H), 0);
var i:int;
mBGen.step();
mTGen.step();
for (i = 0; i < vx; i++)
{
mBGen.step();
mTGen.step();
mBGen.draw(mBGScreen, W-vx+i, 160);
mTGen.draw(mBGScreen, W-vx+i, 0, false);
}
mKani.x -= vx;
for each(var leg:Leg in legs) {leg.x -= vx;}
}
}
}
import flash.display.*;
import flash.events.*;
import flash.geom.*;
import flash.ui.*;
class Kani extends Sprite
{
public var body:Body = new Body();
public var legs:Array = new Array();
public var auto:Boolean;
public var dir:String = "none";
public var timer:int;
public var dance:int;
function Kani(legs:Array)
{
x = 200;
y = 120;
auto = false;
timer = 100;
this.legs = legs;
for each(var leg:Leg in legs)
{
leg.parent = this;
leg.x = x+leg.offset - 20+Math.random()*40;
leg.y = 120 + 100*leg.invert;
}
addChild(body);
}
public function main():void
{
var leg:Leg;
if(auto == false)
{
if(Key.isLeft == true) {dir = "left"; timer = 90;}
else if(Key.isRight == true) {dir = "right"; timer = 90;}
else {dir = "none";}
if(Key.isSpace == true && dance == 0) {dance = 20; timer = 90;}
timer--;
if(timer == 0) {auto = true;}
}
if(auto == true)
{
if(timer == 0)
{
if(dir == "none")
{
if(x < 160) {dir = "right";}
else {dir = "left";}
}
else {
if(x < 50 && dir != "right") {
timer = 60;
}
if(x > 300 && dir != "left") {
timer = 60;
}
}
}
else {
dir = "none";
if(timer == 40) {
dance = 20;
}
timer--;
}
if(Key.isLeft == true || Key.isRight == true)
{
auto = false;
timer = 90;
}
}
if(dir == "left") {x -= 2;}
if(dir == "right") {x += 2;}
if(dir != "none") {
for each(leg in legs) {
leg.move(dir);
}
}
if(dance > 0)
{
if(dance > 15) {body.x = 2;}
else if(dance > 10) {body.x = 0;}
else if(dance > 5) {body.x = -2;}
else {body.x = 0;}
dance--;
}
graphics.clear();
for each(leg in legs)
{
graphics.lineStyle(6, 0xFF80FF);
graphics.moveTo(leg.offset/8, 10*leg.invert);
graphics.lineTo((leg.x-x), (leg.y-y)-70*leg.invert);
graphics.endFill();
}
}
}
class Body extends Sprite
{
function Body()
{
graphics.beginFill(0xC060C0);
graphics.lineStyle(2, 0xFF80FF);
graphics.drawRect(-15, -15, 30, 30);
graphics.endFill();
}
}
class Leg
{
public var parent:Kani;
public var sprite:Sprite;
public var x:Number;
public var y:Number;
public var offset:Number;
public var invert:int;
public var hitflag:Boolean;
public var moveflag:Boolean;
public var gear:int;
function Leg(offset:Number, invert:int)
{
this.offset = offset;
this.invert = invert;
sprite = new Sprite();
sprite.graphics.beginFill(0xC060C0);
sprite.graphics.lineStyle(0, 0xFF80FF);
sprite.graphics.lineTo(-5, -40*invert);
sprite.graphics.lineTo(-5, -60*invert);
sprite.graphics.lineTo(-2, -70*invert);
sprite.graphics.lineTo(2, -70*invert);
sprite.graphics.lineTo(5, -60*invert);
sprite.graphics.lineTo(5, -40*invert);
sprite.graphics.lineTo(0, 0);
sprite.graphics.endFill();
}
public function move(dir:String):void
{
var threshold_l:Number = parent.x+offset + 30;
var threshold_r:Number = parent.x+offset - 30;
if(moveflag == false) {
if(threshold_l < x || threshold_r > x)
{
moveflag = true;
gear = -12;
}
if(threshold_r > x)
{
moveflag = true;
gear = 12;
}
}
if(moveflag == true)
{
if(dir == "left" && threshold_l-50 < x)
{
x -= 4;
if(gear < 0) {
y -= 4 * invert;
}
else {
y += 4 * invert;
if(hitflag == true) {
moveflag = false;
}
if(y < 0 || y > 240) {
gear = -12;
}
}
gear++;
}
else if(dir == "right" && threshold_r+50 > x)
{
x += 4;
if(gear > 0) {
y -= 4 * invert;
}
else {
y += 4 * invert;
if(hitflag == true) {
moveflag = false;
}
if(y < 0 || y > 240) {
gear = 12;
}
}
gear--;
}
}
}
}
class StarBG
{
private var mWidth:int;
private var mHeight:int;
private var mStars:Array;
private var mN:int;
private var mCount:int = 0;
function StarBG(w:int, h:int, n:int)
{
mWidth = w;
mHeight = h;
mN = n;
mStars = new Array(n);
for (var i:int = 0;i < n;i++) {
mStars[i] = {
x : Math.random()*w, y : Math.random()*h, speed : -Math.random()*1.5-0.5,
color : ((Math.random()*0xC0+0x40)<<8)+((Math.random()*0xC0+0x40)<<16)+(Math.random()*0xC0+0x40),
blink : Math.floor(Math.random())*40+20
};
}
}
public function main():void
{
var n:int = mN;
for each(var star:Object in mStars)
{
star.x += star.speed;
if(star.x < 0)
{
star.x += mWidth;
star.y = Math.random()*mHeight;
star.speed = -Math.random()*1.5-0.5;
}
}
mCount++;
}
public function draw(b:BitmapData):void
{
for each(var star:Object in mStars)
{
if((mCount % star.blink) == 1) {b.setPixel(star.x, star.y, 0xFFFFFF);}
else {b.setPixel(star.x, star.y, star.color);}
}
}
}
class Key
{
public static var isLeft:Boolean = false;
public static var isRight:Boolean = false;
public static var isSpace:Boolean = false;
public static function onKeyDownHandler(e:KeyboardEvent):void
{
switch(e.keyCode) {
case Keyboard.LEFT: Key.isLeft = true; break;
case Keyboard.RIGHT: Key.isRight = true; break;
case Keyboard.SPACE: Key.isSpace = true; break;
}
}
public static function onKeyUpHandler(e:KeyboardEvent):void
{
switch(e.keyCode) {
case Keyboard.LEFT: Key.isLeft = false; break;
case Keyboard.RIGHT: Key.isRight = false; break;
case Keyboard.SPACE: Key.isSpace = false; break;
}
}
}
class MountainGen
{
private var mPrevBuffer:Array;
private var mHeight:int;
private var mCount:int = 100;
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;
}
}