この地形を見てるとSFCaveとかの類を思い出す
キーを押すと上昇、離すと下降します
// forked from ton's ゲームっぽくしてみた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(160);
private var mTGen:MountainGen = new MountainGen(160, 1);
private var mScreenBmp:Bitmap;
private var mScreen:BitmapData = new BitmapData(W, H, false, 0);
private var mStarbg:StarBG = new StarBG(W, H, 50);
private var mBGScreen:BitmapData = new BitmapData(W, H, true, 0);
private var player:Player = new Player();
private var isDown:Boolean = false;
private var action:String;
private var count:int;
function Nemesis()
{
mScreenBmp = new Bitmap(mScreen);
addChild(mScreenBmp);
player.bmp = new BitmapData(W, H, true, 0);
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, onEnterFrame);
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUpHandler);
changeAction("play");
}
private function onKeyDownHandler(e:KeyboardEvent):void {isDown = true;}
private function onKeyUpHandler(e:KeyboardEvent):void {isDown = false;}
private function changeAction(str:String):void
{
action = str;
count = -1;
}
private function onEnterFrame(e:Event):void
{
switch(action)
{
case "play":
play();
draw();
hitcheck();
break;
case "miss":
miss();
draw();
break;
}
count++;
}
private function play():void
{
if(count == 0)
{
player.x = 70;
player.y = H / 2;
player.vx = 3;
player.vy = -2;
player.visible = true;
}
mStarbg.tick();
if(isDown == true) {
player.vy -= 0.25;
}
else {
player.vy += 0.25;
}
player.y += player.vy;
scroll(player.vx);
}
private var particles:Array;
private function miss():void
{
if(count == 0)
{
player.visible = false;
particles = new Array();
for (var i:int = 0; i < 20; i++)
{
particles[i] = {
x : player.x-16 + Math.random()*32,
y : player.y-16 + Math.random()*32,
vx : -3 + Math.random()*6,
vy : -3 + Math.random()*6,
size : 30
};
}
}
mStarbg.tick();
for each(var obj:Object in particles)
{
obj.x += obj.vx;
obj.y += obj.vy;
obj.vx /= 1.1;
obj.vy /= 1.1;
obj.size /= 1.2;
}
if(count >= 30)
{
particles = new Array();
changeAction("play");
}
}
private function scroll(vx:Number):void
{
player.bmp.scroll(-vx, 0);
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, 80);
mTGen.draw(mBGScreen, W-vx+i, 0, false);
}
}
private function draw():void
{
mScreen.fillRect(mScreen.rect, 0);
mStarbg.draw(mScreen);
player.draw();
mScreen.copyPixels(player.bmp, player.bmp.rect, new Point(0, 0));
mScreen.copyPixels(mBGScreen, mScreen.rect, new Point(0, 0));
for each(var obj:Object in particles) {
mScreen.fillRect(new Rectangle(obj.x-obj.size/2, obj.y-obj.size/2, obj.size, obj.size), 0xFFFFFF);
}
}
private function hitcheck():void
{
if(mScreen.getPixel(player.x, player.y) != 0xFFFFFF)
{
changeAction("miss");
}
}
}
}
import flash.display.*;
import flash.events.*;
import flash.geom.*;
import flash.filters.*;
class MountainGen
{
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
{
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()*3-2,
color : ((Math.random()*0xC0+0x40)<<8)+((Math.random()*0xC0+0x40)<<16)+(Math.random()*0xC0+0x40),
blink : Math.floor(Math.random())*40+15
};
}
}
public function tick():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()*3-2;
}
}
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 Player
{
public var x:Number;
public var y:Number;
public var vx:Number;
public var vy:Number;
public var bmp:BitmapData;
public var visible:Boolean;
public var state:String = "normal";
public function draw():void
{
bmp.colorTransform(bmp.rect, new ColorTransform(0.7, 0.9, 0.98, 1, 0, 0, 0, 0));
bmp.applyFilter(bmp, bmp.rect, new Point(0, 0), new BlurFilter(0, 2, 1));
if(visible == true) {
bmp.fillRect(new Rectangle(x-1, y-Math.abs(vy)/2-2, vx+1, Math.abs(vy)+4), 0xFFFFFFFF);
}
}
}