9 敵増量中
player moves to mouse cursor.
click to dash.
// player moves to mouse cursor.
// click to dash.
package {
import flash.display.*;
import flash.geom.*;
import flash.events.*;
import flash.text.*;
// import net.hires.debug.Stats;
[SWF(width = "465", height = "465", frameRate = "30", backgroundColor = "#808080")]
public class Test extends Sprite {
private const VMEM_W:int = 465, VMEM_H:int = 465;
private const BOX_SIZE:int = 10;
private const LINE_DIV:uint = 100;
private var vmem:BitmapData = new BitmapData(VMEM_W, VMEM_H, false, 0x808080);
private var input:Input;
private var player:Player;
private var enemies:Vector.<Enemy> = new Vector.<Enemy>;
private var cam:Vec2 = new Vec2(0, 0);
private function toRect(x:Number, y:Number, size:Number) : Rectangle {
var vw:Number = VMEM_W / 2;
var vh:Number = VMEM_H / 2;
var s:Number = size / 2;
return new Rectangle(
-s + x + vw - cam.x, - s + vh - y + cam.y,
size, size);
}
private function drawLine(x1:Number, y1:Number, x2:Number, y2:Number, color:Number) : void {
var dx:Number = x2 - x1;
var dy:Number = y2 - y1;
for (var i:uint = 0; i <= LINE_DIV; i++) {
drawRect(x1 + dx * i / LINE_DIV, y1 + dy * i / LINE_DIV, 4, color);
}
}
private function drawRect(x:Number, y:Number, size:Number, color:Number) : void {
var r:Rectangle = toRect(x, y, size);
vmem.fillRect(r, color);
}
private function getCursor() : Vec2 {
return new Vec2(-VMEM_W / 2 + stage.mouseX + cam.x, VMEM_H / 2 - stage.mouseY + cam.y);
}
public function update(event:Event) : void {
input.update(getCursor());
player.update();
for each (var enemy:Enemy in enemies) enemy.update();
cam.adds(player.v.sub(cam).mul(0.18));
for each (var enemy:Enemy in enemies) {
if (player.collide(enemy)) {
player.OnCollide(enemy);
enemy.OnCollide();
}
}
vmem.lock();
vmem.fillRect(vmem.rect, 0x808080);
vmem.fillRect(toRect(0, 0, STAGE_LEN), 0x000000);
vmem.fillRect(toRect(0, 0, STAGE_LEN - 10), 0x404040);
vmem.fillRect(toRect(0, 0, STAGE_LEN - 20), 0x808080);
vmem.fillRect(toRect(player.v.x, player.v.y, player.len + 2), 0x000000);
vmem.fillRect(toRect(player.v.x, player.v.y, player.len), player.col);
for each (var enemy in enemies) {
vmem.fillRect(toRect(enemy.v.x, enemy.v.y, enemy.len + 2), 0x000000);
vmem.fillRect(toRect(enemy.v.x, enemy.v.y, enemy.len), enemy.col);
}
vmem.unlock();
}
function newEnemy(pos:Vec2, a:Angle) {
var enemy:Enemy = new Enemy(player);
enemy.v = pos;
enemy.a = a;
enemies.push(enemy);
}
function Test() {
input = new Input();
player = new Player(input);
var text:TextField = new TextField();
text.text = "test1 moving box";
for (var i:uint = 0; i < 21; i++) {
var a:Angle = new Angle(Misc.randRange(0, Math.PI * 2));
var v:Vec2 = a.vec().muls(Misc.randRange(100, 200));
newEnemy(v, a);
}
addChild(text);
addChild(new Bitmap(vmem));
addEventListener(Event.ENTER_FRAME, update);
stage.addEventListener(MouseEvent.MOUSE_DOWN,
function(e:MouseEvent) : void { input.OnMouseDown(); } );
stage.addEventListener(MouseEvent.MOUSE_UP,
function(e:MouseEvent) : void { input.OnMouseUp(); } );
// addChild(new Stats());
}
}
}
const STAGE_LEN:Number = 400;
class Input {
private var mb:Boolean;
private var nmb:Boolean;
private var pmb:Boolean;
private var mv:Vec2;
function Input() {
mb = pmb = nmb = false;
}
function update(v:Vec2) {
pmb = nmb;
nmb = mb;
mv = v;
}
function pos() : Vec2 {
return new Vec2(mv.x, mv.y);
}
function btnDown() : Boolean {
return nmb && (!pmb);
}
function btnHold() : Boolean {
return nmb;
}
function OnMouseDown() {
mb = true;
}
function OnMouseUp() {
mb = false;
}
}
class Box {
public var v:Vec2
public var col:uint;
public var len:Number;
function Box(av:Vec2, c:uint, l:Number) {
v = av; col = c; len = l;
}
function collide(b:Box) : Boolean {
var dv:Vec2 = v.sub(b.v);
var l:Number = (len + b.len) / 2;
return (Math.abs(dv.x) < l) && (Math.abs(dv.y) < l);
}
}
class Player extends Box {
private var i:Input;
public var stat:uint;
private var dangle:Angle;
private var f:uint;
private static const DASH_F:uint = 15;
private static const DASH_V:Number = 8;
private static const FUTTOBI_F:uint = 25;
private static const FUTTOBI_V:Number = 4;
private static const LEN:Number = 20
private static const vr:Number = 0.2;
private static const vl:Number = 2;
private static const COL_NORMAL:uint = 0xffffff;
private static const COL_DASH:uint = 0xff0000;
private static const COL_FUTTOBI:uint = 0xa0a0a0;
public static const ST_P_NORMAL:uint = 0;
public static const ST_P_DASH:uint = 1;
public static const ST_P_FUTTOBI:uint = 2;
function Player(input:Input) {
super(new Vec2(0, 0), COL_NORMAL, LEN);
i = input;
stat = ST_P_NORMAL;
}
public function update() {
var dv:Vec2 = i.pos().sub(v);
f++;
switch(stat) {
case ST_P_NORMAL:
dv.muls(vr);
dv.divs(Math.max(dv.len(), vl)); dv.muls(vl)
v.adds(dv);
if (i.btnDown()) {
stat = ST_P_DASH;
dangle = i.pos().sub(v).angle();
col = COL_DASH;
f = 0;
}
break;
case ST_P_DASH:
v.adds(dangle.vec().mul(DASH_V));
if (f > DASH_F) {
stat = ST_P_NORMAL;
col = COL_NORMAL;
}
break;
case ST_P_FUTTOBI:
v.adds(dangle.vec().mul(FUTTOBI_V));
if (f > FUTTOBI_F) {
stat = ST_P_NORMAL;
col = COL_NORMAL;
}
break;
}
v.x = Misc.clamp(v.x, (len - STAGE_LEN) / 2, (STAGE_LEN - len) / 2);
v.y = Misc.clamp(v.y, (len - STAGE_LEN) / 2, (STAGE_LEN - len) / 2);
}
public function OnCollide(b:Enemy) {
switch (stat) {
case ST_P_NORMAL:
if (b.stat == Enemy.ST_E_NORMAL) {
dangle = v.sub(b.v).angle();
stat = ST_P_FUTTOBI;
col = COL_FUTTOBI;
f = 0;
}
break;
case ST_P_DASH:
break;
case ST_P_FUTTOBI:
break;
}
}
}
class Enemy extends Box {
private var p:Player;
public var a:Angle = new Angle(0);
public var stat:uint;
private var f:uint;
private static const NORMAL_V:Number = 1.5;
private static const FUTTOBI_V:Number = 4.0;
private static const FUTTOBI_F:uint = 20;
private static const COL_ENEMY:uint = 0xa030a0;
private static const COL_ENEMY_F:uint = 0x802080;
private static const LEN:Number = 20;
public static const ST_E_NORMAL:uint = 0;
public static const ST_E_FUTTOBI:uint = 1;
function Enemy(ap:Player) {
super(new Vec2(100, 0), COL_ENEMY, LEN);
p = ap;
stat = ST_E_NORMAL;
f = 0;
}
public function update() {
switch (stat) {
case ST_E_NORMAL:
var d:Vec2 = p.v.sub(v);
var da:Angle = d.angle().sub(a);
da.muls(0.04);
a.adds(da);
v.adds(a.vec().mul(NORMAL_V));
break;
case ST_E_FUTTOBI:
v.adds(a.vec().mul(FUTTOBI_V));
if (f > FUTTOBI_F) {
stat = ST_E_NORMAL;
col = COL_ENEMY;
}
break;
}
f++;
}
public function OnCollide() {
if (p.stat == Player.ST_P_DASH) {
stat = ST_E_FUTTOBI;
a = v.sub(p.v).angle();
col = COL_ENEMY_F;
f = 0;
}
}
}
class Angle {
public var a:Number = 0;
function Angle(n:Number) {
a = n;
round();
}
function add(n:Angle) : Angle {
return new Angle(a + n.a);
}
function adds(n:Angle) : Angle{
a += n.a;
round();
return this;
}
function sub(n:Angle) : Angle {
return new Angle(a - n.a);
}
function subs(n:Angle) : Angle {
a - n.a;
round();
return this;
}
function mul(n:Number) : Angle {
return new Angle(a * n);
}
function muls(n:Number) : Angle{
a *= n;
round();
return this;
}
function div(n:Number) : Angle {
return new Angle(a / n);
}
function divs(n:Number) : Angle{
a /= n;
round();
return this;
}
function round() {
while (a >= Math.PI) a -= Math.PI * 2
while (a < -Math.PI) a += Math.PI * 2
}
function cos() : Number {
return Math.cos(a);
}
function sin() : Number {
return Math.sin(a);
}
function vec() : Vec2 {
return new Vec2(cos(), sin());
}
function reverse() : Angle {
return new Angle(a + Math.PI);
}
}
class Vec2 {
public var x:Number = 0, y:Number = 0;
function Vec2(ax:Number = 0, ay:Number = 0) {
x = ax; y = ay;
}
function set(ax:Number = 0, ay:Number = 0) {
x = ax; y = ay;
}
function add(v:Vec2) : Vec2 {
return new Vec2(x + v.x, y + v.y);
}
function adds(v:Vec2) : Vec2 {
x += v.x; y += v.y;
return this;
}
function sub(v:Vec2) : Vec2 {
return new Vec2(x - v.x, y - v.y);
}
function subs(v:Vec2) : Vec2 {
x -= v.x; y -= v.y;
return this;
}
function mul(n:Number) : Vec2 {
return new Vec2(x * n, y * n);
}
function muls(n:Number) : Vec2 {
x *= n; y *= n;
return this;
}
function div(n:Number) : Vec2 {
return new Vec2(x / n, y / n);
}
function divs(n:Number) : Vec2 {
x /= n; y /= n;
return this;
}
function len() : Number {
return Math.sqrt(x * x + y * y);
}
function angle() : Angle {
return new Angle(Math.atan2(y, x));
}
}
class Misc {
static function clamp(x:Number, min:Number, max:Number) : Number {
return Math.min(Math.max(x, min), max);
}
static function randRange(min:Number, max:Number) : Number {
return min + (max - min) * Math.random();
}
}