MCMC用固定弾幕回避学習結果再生機
/**
* Copyright uwi ( http://wonderfl.net/user/uwi )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/supo
*/
// forked from uwi's 固定弾幕回避学習結果再生機
// forked from uwi's GAで固定弾幕回避学習
package {
import flash.text.TextField;
import flash.display.*;
import flash.filters.*;
import flash.geom.*;
import flash.events.*;
import flash.ui.*;
import com.bit101.components.*;
[SWF(backgroundColor="#000000", frameRate="30")]
public class MCMCReplayer extends Sprite {
private var _bullets : Array;
private var _ct : int;
private var _myx : Point;
private var _nhit : int;
private const R_ME : Number = 5.0;
private var _tf : TextField;
private var _tfinput : TextField;
private var _tfptn : TextField;
private var _submit : PushButton;
private var _stop : PushButton;
private var _space : BitmapData;
private var _shapeself : Shape;
private var _bmdbullets : Array;
private const W : Number = 400;
private const H : Number = 465;
private var _shotPattern : Array;
private var _avptn : String = "000000000000011111111111100000007772222222222222222222222288777777777777777555777775555500000000077776655500077766666666666666666666600000000000000000000777774444444444444440000000000000000000000000000066666666666666444333333333333336647777777777772222555555555555555333333333333333377777777777111118888888888888222225555555555555777777711111111111110002222222222222222266666666333777777777722222222228852244888880022222222222222222222222573333332211111111111111144444444488888888888866666666666666666666666644444444444444446600000000055555555555555555555555555555555555000000000000000000000000000000";
private const P0 : Point = new Point(0, 0);
private const WEAKBLUR : BitmapFilter = new BlurFilter(2.0, 2.0);
public function MCMCReplayer() {
Wonderfl.capture_delay(5);
// 宇宙
_space = new BitmapData(W, H, false, 0x000000);
var bmpspace : Bitmap = new Bitmap(_space);
addChild(bmpspace);
_shapeself = new Shape();
// 弾描画
var bevel : BitmapFilter = new GradientBevelFilter(4.0, 45, [0xffffff, 0x0000ff], [1.0, 1.0], [70, 255], 4.0, 4.0, 1, 2, "inner");
_bmdbullets = [null];
for(var r : int = 1; r <= 50;r++){
var sh : Shape = new Shape();
var g : Graphics = sh.graphics;
g.lineStyle(1.0, 0x999999);
g.beginFill(0xeeeeee);
g.drawCircle(r, r, r);
g.endFill();
var bmdbullet : BitmapData = new BitmapData(r * 2, r * 2, true, 0x00000000);
bmdbullet.draw(sh);
bmdbullet.applyFilter(bmdbullet, bmdbullet.rect, P0, bevel);
bmdbullet.applyFilter(bmdbullet, bmdbullet.rect, P0, WEAKBLUR);
_bmdbullets.push(bmdbullet);
}
// デバッグ用
_tf = new TextField();
setParams(_tf, {
autoSize : "left",
textColor : 0xffffff,
borderColor : 0xffffff,
border : true
});
addChild(_tf);
var tfinputhead : TextField = new TextField();
setParams(tfinputhead, {
text : "x y r v interval",
textColor : 0xffffff,
borderColor : 0xffffff,
border : true,
x : 370,
y : 0,
width : 90,
height : 20
});
addChild(tfinputhead);
_tfinput = new TextField();
setParams(_tfinput, {
text : "0 0 20 10 6\n150 0 20 10 6\n400 0 20 10 6",
textColor : 0xffffff,
borderColor : 0xffffff,
border : true,
x : 370,
y : 20,
width : 90,
height : 150
});
addChild(_tfinput);
_tfptn = new TextField();
setParams(_tfptn, {
type : "input",
text : "",
textColor : 0xffffff,
borderColor : 0xffffff,
border : true,
x : 370,
y : 200,
width : 90,
height : 200,
wordWrap : true
});
addChild(_tfptn);
_submit = new PushButton(this, 370, 410, "Replay", onSubmit);
_submit.width = 90;
_stop = new PushButton(this, 370, 430, "Stop/Resume", onStop);
_stop.width = 90;
_state = 0;
}
private static function setParams(t : Object, v : Object) : Object
{
for(var k : String in v){
t[k] = v[k];
}
return t;
}
private var _state : int;
private function onSubmit(e : MouseEvent) : void
{
init();
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
private function onStop(e : MouseEvent) : void
{
if(_state == 0)return;
if(hasEventListener(Event.ENTER_FRAME)){
removeEventListener(Event.ENTER_FRAME, onEnterFrame);
}else{
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
}
private function init() : void
{
_state = 1;
_myx = new Point(W / 2, H / 2);
_bullets = [];
_ct = 0;
_nhit = 0;
if(_tfptn.text.length > 1){
_avptn = _tfptn.text.replace(/[\r\n ]/g, "");
}
_shotPattern = [];
for each(var line : String in _tfinput.text.split(/[\r\n]/)){
var seg : Array = line.split(' ');
if(seg.length == 5){
if(Number(seg[2]) > 0 && Number(seg[3]) > 0 && int(seg[4]) > 0){
_shotPattern.push({
x : Number(seg[0]),
y : Number(seg[1]),
r : Number(seg[2]),
v : Number(seg[3]),
interval : int(seg[4])
});
}
}
}
}
private function onEnterFrame(e : Event) : void
{
draw();
judge();
for each(var ptn : Object in _shotPattern){
if(_ct % ptn.interval == 0){
addBullet(ptn.x, ptn.y, ptn.r, ptn.v);
}
}
moveMe();
moveBullets();
_ct++;
_tf.text =
"time : " + _ct +
"\nhit : " + _nhit +
"\nbullets : " + _bullets.length;
}
private const ST : Array = [
[0, 0],
[6, 0], [4, -4], [0, -6], [-4, -4],
[-6, 0], [-4, 4], [0, 6], [4, 4]
];
private function moveMe() : void
{
if(_ct >= _avptn.length){
_state = 1;
removeEventListener(Event.ENTER_FRAME, onEnterFrame);
return;
}
_myx.x += ST[int(_avptn.charAt(_ct))][0];
_myx.y += ST[int(_avptn.charAt(_ct))][1];
}
private function moveBullets() : void
{
// 弾
for each(var b : Bullet in _bullets){
b.xx += b.vx;
b.xy += b.vy;
}
}
// 弾削除
private function removeBullet(i : int) : void
{
if(i < _bullets.length - 1){
_bullets[i] = _bullets.pop();
}else{
_bullets.pop();
}
}
// 当たり判定
private function judge() : void
{
for(var i : int = _bullets.length - 1;i >= 0;i--){
var b : Bullet = _bullets[i];
if(
(b.xx - _myx.x) * (b.xx - _myx.x) +
(b.xy - _myx.y) * (b.xy - _myx.y)
< (b.r + R_ME) * (b.r + R_ME)){
_nhit++;
removeBullet(i);
continue;
}
if(b.xx < 0 || b.xx > W || b.xy < 0 || b.xy > H){
removeBullet(i);
}
}
if(_myx.x < 0 || _myx.x > W || _myx.y < 0 || _myx.y > H){
_nhit+=10;
_myx.x = W / 2;
_myx.y = H / 2;
}
}
// 描画
private function draw() : void
{
_space.lock();
_space.fillRect(_space.rect, 0x000000);
var g : Graphics = _shapeself.graphics;
g.clear();
// self
g.lineStyle(1.0, 0x333399);
g.beginFill(0x3333ff);
g.drawCircle(_myx.x, _myx.y, 5);
g.endFill();
_space.draw(_shapeself, null, null, BlendMode.ADD);
// bullets
for each(var b : Bullet in _bullets){
_space.copyPixels(_bmdbullets[int(b.r)], _bmdbullets[int(b.r)].rect,
new Point(b.xx - b.r, b.xy - b.r));
}
_space.unlock();
}
// 弾追加
private function addBullet(x : Number, y : Number, r : Number, v : Number) : void
{
var vr : Number = Math.sqrt((_myx.x - x) * (_myx.x - x) + (_myx.y - y) * (_myx.y - y));
var b : Bullet = new Bullet();
b.xx = x;
b.xy = y;
b.vx = v * (_myx.x - x) / vr;
b.vy = v * (_myx.y - y) / vr;
b.r = r;
_bullets.push(b);
}
}
}
class Bullet
{
public var xx : Number;
public var xy : Number;
public var vx : Number;
public var vy : Number;
public var r : Number;
}