[Alternativa3D v5] Improved Maze 3D
/**
* Copyright amyneon ( http://wonderfl.net/user/amyneon )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/40V1
*/
// forked from paq's forked from: [Alternativa3D] Maze(酔うので改良しました)
// forked from clockmaker's flash on 2009-3-19
package
{
import alternativ5.engine3d.controllers.*;
import alternativ5.engine3d.core.*;
import alternativ5.engine3d.display.*;
import alternativ5.engine3d.materials.*;
import alternativ5.engine3d.primitives.*;
import alternativ5.types.*;
import alternativ5.utils.*;
import flash.net.*;
import flash.display.*;
import flash.events.*;
import flash.geom.*;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
[SWF(frameRate="60")]
public class Main extends Sprite
{
private const WIDTH_NUM:int = 10;
private const HEIGHT_NUM:int = 10;
private const LINE_SIZE:int = 500;
private const MAZE_ARR:Array =
[
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 1, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 1, 1, 0, 1, 0, 1, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 1, 0, 0],
[0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 1, 1, 1, 1, 0, 0],
[0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
]
private const MAZE2_ARR:Array =
[
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 1, 0, 0, 1, 1, 1, 0],
[0, 0, 0, 1, 0, 0, 1, 0, 0, 0],
[0, 0, 1, 1, 0, 0, 0, 0, 1, 0],
[0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
[0, 0, 0, 1, 1, 0, 1, 1, 1, 0],
[0, 1, 0, 0, 0, 0, 1, 1, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
]
private const WALL_ARR:Array =
[
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
]
private var scene:Scene3D;
private var view:View;
private var camera:Camera3D;
// The camera controller
private var controller:WalkController;
private var jump_camera:Boolean;
//private var controller_tween:Boolean = false;
public function Main()
{
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
// Creating scene
scene = new Scene3D();
scene.root = new Object3D();
// create box
for (var i:int = 0; i < MAZE_ARR.length; i++ )
{
for (var j:int = 0; j < MAZE_ARR[i].length; j++ )
{
if (MAZE_ARR[i][j] == 0) continue;
var box:Box = Box(scene.root.addChild(new Box(LINE_SIZE, LINE_SIZE, LINE_SIZE)));
box.cloneMaterialToAllSurfaces(new DevMaterial(0,0x800000));
box.x = LINE_SIZE * (i - WIDTH_NUM / 2);
box.y = LINE_SIZE * (j - HEIGHT_NUM / 2);
}
}
for (i = 0; i < MAZE2_ARR.length; i++ )
{
for (j = 0; j < MAZE2_ARR[i].length; j++ )
{
if (MAZE2_ARR[i][j] == 0) continue;
box = Box(scene.root.addChild(new Box(LINE_SIZE, LINE_SIZE, LINE_SIZE)));
box.cloneMaterialToAllSurfaces(new DevMaterial(0,0xA0522D));
box.x = LINE_SIZE * (i - WIDTH_NUM / 2);
box.y = LINE_SIZE * (j - HEIGHT_NUM / 2);
box.z = LINE_SIZE;
}
}
// create exterior wall
for (i = 0; i < WALL_ARR.length; i++ )
{
for (j = 0; j < WALL_ARR[i].length; j++ )
{
if (WALL_ARR[i][j] == 0) continue;
var box2:Box = Box(scene.root.addChild(new Box(LINE_SIZE, LINE_SIZE, LINE_SIZE*20)));
box2.cloneMaterialToAllSurfaces(new DevMaterial(0,0x0000FF));
box2.x = LINE_SIZE * (i - WIDTH_NUM / 2);
box2.y = LINE_SIZE * (j - HEIGHT_NUM / 2);
}
}
// floor
var plane:Plane = Plane(scene.root.addChild(new Plane(LINE_SIZE * MAZE_ARR.length, LINE_SIZE * MAZE_ARR.length)));
plane.cloneMaterialToAllSurfaces(new FillMaterial(0x006400));
plane.z = -LINE_SIZE / 2;
plane.rotationX = 0 * Math.PI / 180;
// Adding camera and view
camera = new Camera3D();
camera.rotationX = MathUtils.toRadian(-90)
camera.rotationZ = MathUtils.toRadian(120)
scene.root.addChild(camera);
view = new View();
addChild(view);
view.camera = camera;
// 徒歩のコントローラーを作成
controller = new WalkController(stage);
// キーボード操作を可能にする機能
controller.setDefaultBindings();
// キーボードショートカットの整理
controller.bindKey(KeyboardUtils.UP, ObjectController.ACTION_FORWARD);
controller.bindKey(KeyboardUtils.DOWN, ObjectController.ACTION_BACK);
// WalkControllerの対象を設定
controller.object = camera;
// 障害物の衝突設定
controller.checkCollisions = true;
// 徒歩の速度
controller.speed = 700;
// ジャンプ(スペースでジャンプできます)の速度
controller.jumpSpeed = 1250;
// 重力の速度
controller.gravity = 600;
// 視点(制御点)の高さ
controller.objectZPosition = 1;
//controller.flyMode = true;
// FPS display launch
//FPS.init(stage);
stage.addEventListener(Event.RESIZE, onResize);
stage.addEventListener(Event.ENTER_FRAME, onEnterFrame);
onResize(null);
var myTextField:TextField = new TextField();
myTextField.autoSize = TextFieldAutoSize.LEFT;
myTextField.text = 'Click to play. W,S,A,D to move. Space to jump';
addChild(myTextField);
var myFormat:TextFormat = new TextFormat();
myFormat.color = 0xFFFFFF;
myTextField.setTextFormat(myFormat);
}
private function onEnterFrame(e:Event):void
{
if (!controller.onGround && camera.rotationX >= -2.75 && !jump_camera) {
camera.rotationX -= 0.02;
if (camera.rotationX < -2.75) {
camera.rotationX = -2.75;
jump_camera = true;
}
}else {
camera.rotationX += 0.005;
if (camera.rotationX >= MathUtils.toRadian( -90)) {
camera.rotationX = MathUtils.toRadian(-90)
}
}
if (controller.onGround && camera.rotationX <= MathUtils.toRadian( -90)) {
jump_camera = false;
camera.rotationX += 0.05;
}
// interface
controller.processInput();
// Scene calculating
scene.calculate();
}
// Resize Handler
private function onResize(e:Event):void
{
view.width = stage.stageWidth;
view.height = stage.stageHeight;
// Top
var bgMatrix:Matrix = new Matrix();
bgMatrix.rotate(90 * Math.PI / 180);
graphics.clear()
graphics.beginFill(0xFFFFFF); // Top Color
graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
}
}
}