シールドビット・テンカイ
機動戦士ガンダム00 2nd に登場するケルディムガンダムのシールドビットっぽい動きをさせてみました。
/**
* Copyright axcel_work ( http://wonderfl.net/user/axcel_work )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/uoo2
*/
package {
import org.papervision3d.cameras.CameraType;
import org.papervision3d.view.*;
import flash.events.DataEvent;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.utils.Timer;
[SWF(width="465", height="465", backgroundColor="0xFFFFFF", frameRate="60")]
/**
* @author axcelwork
*/
public class Index extends BasicView {
//
private var _status:Status;
private var _bit:ShieldBit;
private var _factroy:EnemyFactory;
private var _timer:Timer;
private var _enemyArray:Vector.<Enemy>;
private var _bitArray:Vector.<Bit>;
/**
*
*/
public function Index() {
super(this.stage.stageWidth, this.stage.stageHeight, false, true, CameraType.FREE);
//
this._camera.x = 0;
this._camera.z = 0;
//
//this._camera.x = -400;
//this._camera.y = 400;
//this._camera.z = 400;
//this._camera.rotationX = 90;
//this._camera.rotationY = 40;
this.stage.addEventListener(MouseEvent.MOUSE_MOVE, moveHandler);
this.startRendering();
Config.stage = this.stage;
this._enemyArray = new Vector.<Enemy>();
this._bitArray = new Vector.<Bit>();
// ステータス表示
this._status = new Status();
// シールドビット作成
this._bit = new ShieldBit(this.scene, this._bitArray, this._enemyArray);
this._bit.addEventListener(DataEvent.DATA, enemyHit);
this._timer = new Timer(Config.ENEMY_TIME);
this._timer.addEventListener(TimerEvent.TIMER, createEnemy);
this._timer.start();
this._factroy = new EnemyFactory(this.scene, this._enemyArray);
this._factroy.addEventListener(Event.INIT, enemyAlert);
this._factroy.addEventListener(Event.CHANGE, bodyDamage);
this.stage.addEventListener(Event.ENTER_FRAME, loop);
//this.addChild(new StatsView(this.renderer));
}
// マウスが動いたとき
private function moveHandler(e:MouseEvent):void {
}
private function bodyDamage(e:Event):void {
this._status.update();
}
private function enemyAlert(e:Event):void {
this._bit.alert();
}
private function enemyHit(e:DataEvent):void {
}
private function createEnemy(e:TimerEvent):void {
this._factroy.create();
}
private function loop(e:Event):void {
this._bit.step();
this._factroy.step();
for (var i:int = 0;i < this._enemyArray.length;i++) {
for (var j:int = 0;j < this._bitArray.length;j++) {
if(this._bitArray[j].hitTestObject(this._enemyArray[i])) {
this._bitArray[j].hit();
}
}
}
}
}
}
import fl.motion.easing.Quadratic;
import com.bit101.components.Label;
import com.flashdynamix.motion.Tweensy;
import org.papervision3d.materials.WireframeMaterial;
import org.papervision3d.materials.utils.MaterialsList;
import org.papervision3d.objects.primitives.Cone;
import org.papervision3d.objects.primitives.Cylinder;
import org.papervision3d.scenes.Scene3D;
import flash.display.Sprite;
import flash.display.Stage;
import flash.events.DataEvent;
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.TimerEvent;
import flash.utils.Timer;
class ShieldBit extends EventDispatcher {
private var _scene:Scene3D;
private var _core:Bit;
private var NUMBER:int = Config.BIT_NUM;
private var _bits:Vector.<Bit>;
private var _enemys:Vector.<Enemy>;
/**
*
*/
public function ShieldBit(scene:Scene3D, bits:Vector.<Bit>, enemys:Vector.<Enemy>):void {
this._scene = scene;
this._bits = bits;
this._enemys = enemys;
for (var i:int = 0;i < this.NUMBER;i++) {
this._core = new Bit(i);
this._core.name = "Bit" + i;
this._core.z = Config.BIT_POS_Z + (Math.random() * 400);
this._bits.push(this._core);
this._scene.addChild(this._core);
this._core.addEventListener(DataEvent.DATA, hit);
}
}
private function hit(e:DataEvent):void {
dispatchEvent(new DataEvent(DataEvent.DATA, false, false, e.data));
}
public function alert():void {
for (var i:int = 0;i < this.NUMBER;i++) {
if(!this._bits[i]._isActive) {
this._bits[i].active(this._enemys);
return;
}
}
}
public function step():void {
for (var i:int = 0;i < this._bits.length;i++) {
if(this._bits[i]._isDestroy) {
this._scene.removeChild(this._bits[i]);
var num:int = this._bits.indexOf(this._bits[i], 0);
this._bits.splice(num, 1);
this.NUMBER -= 1;
}
}
}
}
class Bit extends Cylinder {
private var _console:Label;
private var _material:WireframeMaterial = new WireframeMaterial(0xCCCCCC);
private var _materialList:MaterialsList = new MaterialsList();
// 耐久値
public var _endurance:int = Config.HITPOINT;
private const WIDTH:int = 7;
private const DEPTH:int = 2;
private var _timer:Timer;
private var _targetEnmy:Enemy;
public var _isActive:Boolean = false;
public var _isHit:Boolean = false;
public var _isDestroy:Boolean = false;
/**
*
*/
public function Bit(num:int):void {
this._materialList.addMaterial(this._material, "all");
this._timer = new Timer(7500, 1);
this._timer.addEventListener(TimerEvent.TIMER_COMPLETE, release);
super(this._material, this.WIDTH, this.DEPTH, 6, 1);
this.scaleZ = 3;
this.rotationX = -90;
this._console = new Label(Config.stage, 10, 20 * num + 30, "Bit" + num + " : " + this._endurance.toString());
}
public function active(enemys:Vector.<Enemy>):void {
this._isActive = true;
this._timer.start();
for (var i:int = 0;i < enemys.length;i++) {
if(!enemys[i]._isTarget) {
enemys[i]._isTarget = true;
this._targetEnmy = enemys[i];
Tweensy.to(this, {x: enemys[i].x, y: enemys[i].y}, Config.BIT_SPEED, Quadratic.easeInOut);
}
}
}
private function release(e:TimerEvent):void {
this._isActive = false;
this._timer.reset();
}
public function hit():void {
dispatchEvent(new DataEvent(DataEvent.DATA, false, false, this.name));
this._isActive = false;
this._timer.reset();
this._targetEnmy._isHit = true;
this._endurance -= Config.DAMAGE;
this._console.text = this.name + " : " + this._endurance;
if(this._endurance <= 0) {
this._isDestroy = true;
}
else if(this._endurance <= Config.HITPOINT / 10) {
this.material = new WireframeMaterial(0xFF0000);
}
else if(this._endurance <= Config.HITPOINT / 2) {
this.material = new WireframeMaterial(0xFFFF00);
}
}
}
class EnemyFactory extends EventDispatcher {
private var _scene:Scene3D;
private var _array:Vector.<Enemy>;
private var _core:Enemy;
private const RECTANGLE:int = Config.RECTANGLE;
public function EnemyFactory(scene:Scene3D, array:Vector.<Enemy>):void {
this._scene = scene;
this._array = array;
}
public function create():void {
this._core = new Enemy();
this._core.x = Math.random() * this.RECTANGLE - (this.RECTANGLE / 2);
this._core.y = Math.random() * this.RECTANGLE - (this.RECTANGLE / 2);
this._core.z = Config.ENEMY_RANGE;
this._scene.addChild(this._core);
this._array.push(this._core);
dispatchEvent(new Event(Event.INIT));
}
public function step():void {
if(this._array) {
for (var i:int = 0;i < this._array.length;i++) {
this._array[i].z -= Config.ENEMY_SPEED;
if(this._array[i]._isHit) {
this._scene.removeChild(this._array[i]);
var num1:int = this._array.indexOf(this._array[i], 0);
this._array.splice(num1, 1);
Status.scoreAdd();
}
else if(this._array[i].z <= -500) {
this._scene.removeChild(this._array[i]);
var num2:int = this._array.indexOf(this._array[i], 0);
this._array.splice(num2, 1);
dispatchEvent(new Event(Event.CHANGE));
}
}
}
}
}
class Enemy extends Cone {
private var _material:WireframeMaterial = new WireframeMaterial(0x9abdf1);
private var _materialList:MaterialsList = new MaterialsList();
private const WIDTH:int = 5;
private const HEIGHT:int = 20;
public var _isTarget:Boolean = false;
public var _isHit:Boolean = false;
public var _isBorder:Boolean = false;
/**
*
*/
public function Enemy():void {
this._materialList.addMaterial(this._material, "all");
super(this._material, this.WIDTH, this.HEIGHT, 5, 1);
this.rotationX = -90;
}
public function lookOn():void {
this._isTarget = true;
}
}
class Status {
private var _hpBar:Sprite;
private var _bgBar:Sprite;
private var _hpNum:Number;
private static var _scorelabel:Label;
private static var _score:int = 0;
public function Status() {
this._hpBar = new Sprite();
this._hpBar.graphics.beginFill(0x6ab732);
this._hpBar.graphics.drawRect(0, 0, Config.stage.stageWidth - 23, 6);
this._hpBar.graphics.endFill();
this._hpBar.x = 12;
this._hpBar.y = 12;
this._bgBar = new Sprite();
this._bgBar.graphics.beginFill(0xffffff);
this._bgBar.graphics.lineStyle(1, 0xcccccc);
this._bgBar.graphics.drawRect(0, 0, Config.stage.stageWidth - 20, 9);
this._bgBar.graphics.endFill();
this._bgBar.x = 10;
this._bgBar.y = 10;
Config.stage.addChild(this._bgBar);
Config.stage.addChild(this._hpBar);
this._hpNum = Config.DAMAGE;
_scorelabel = new Label(Config.stage, 10, 440, "Score : 0");
}
public function update():void {
this._hpBar.width -= (Config.DAMAGE / 10);
}
public static function scoreAdd():void {
_score += 10;
_scorelabel.text = "Score : " + _score;
}
}
class Config {
// ビットの枚数
public static var BIT_NUM:int = 14;
// ビットの初期位置
public static var BIT_POS_Z:int = 200;
// ビットの移動スピード
public static var BIT_SPEED:Number = 0.3;
// 装甲値
public static var HITPOINT:int = 600;
// ダメージ
public static var DAMAGE:int = 10;
// 敵の生成スピード
public static var ENEMY_TIME:int = 100;
// 敵の出現位置
public static var ENEMY_RANGE:int = 3000;
// 出現範囲
public static var RECTANGLE:int = 400;
// 敵の進軍スピード
public static var ENEMY_SPEED:int = 30;
private static var _stage:Stage;
public static function set stage(value:Stage):void {
_stage = value;
}
public static function get stage():Stage {
return _stage;
}
}