IsoWorld
盤上をクリックするとブロックが置かれるよ。
/**
* Copyright fakestar0826 ( http://wonderfl.net/user/fakestar0826 )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/nlOu
*/
// forked from fakestar0826's IsoMetrics
package {
import flash.events.MouseEvent;
import flash.geom.Point;
import flash.display.Sprite;
public class FlashTest extends Sprite {
private var world:IsoWorld;
public function FlashTest() {
// write as3 code here..
stage.align = "TL";
stage.scaleMode = "noScale";
world = new IsoWorld();
world.x = stage.stageWidth * 0.5;
world.y = 100;
addChild(world);
for(var i:int = 0;i < 20;i++)
{
for(var j:int = 0;j < 20;j++)
{
var tile:DrawnIsoTile = new DrawnIsoTile(20, 0xCCCCCC);
tile.position = new Point3D(i * 20, 0, j * 20);
world.addChildToFloor(tile);
}
}
stage.addEventListener(MouseEvent.CLICK, onClick);
}
private function onClick(e:MouseEvent):void
{
var box:DrawnIsoBox = new DrawnIsoBox(20, Math.random() * 0xFFFFFF, 20);
var pos:Point3D = IsoUtils.screenToIso(new Point(world.mouseX, world.mouseY));
pos.x = Math.round(pos.x / 20) * 20;
pos.y = Math.round(pos.y / 20) * 20;
pos.z = Math.round(pos.z / 20) * 20;
box.position = pos;
world.addChildToWorld(box);
}
}
}
import flash.geom.Rectangle;
import flash.geom.Point;
class Point3D
{
public var x:Number;
public var y:Number;
public var z:Number;
public function Point3D(x:Number = 0, y:Number = 0, z:Number = 0):void
{
this.x = x;
this.y = y;
this.z = z;
}
}
class IsoUtils
{
public static const Y_CORRECT:Number = Math.cos(-Math.PI / 6) * Math.SQRT2;//1.2247.....
public static function isoToScreen(pos:Point3D):Point
{
var screenX:Number = pos.x - pos.z;
var screenY:Number = pos.y * Y_CORRECT + (pos.x + pos.z) * 0.5;
return new Point(screenX, screenY);
}
public static function screenToIso(point:Point):Point3D
{
var xpos:Number = point.y + point.x * 0.5;
var ypos:Number = 0;
var zpos:Number = point.y - point.x * 0.5;
return new Point3D(xpos, ypos, zpos);
}
}
import flash.display.Sprite;
class IsoObject extends Sprite
{
protected var _position:Point3D;
protected var _size:Number;
protected var _walkable:Boolean;
protected var _vx:Number;
protected var _vy:Number;
protected var _vz:Number;
public static const Y_CORRECT:Number = Math.cos(-Math.PI / 6) * Math.SQRT2;
public function IsoObject(size:Number)
{
_size = size;
_position = new Point3D();
updateScreenPosition();
}
protected function updateScreenPosition():void
{
var screenPos:Point = IsoUtils.isoToScreen(_position);
super.x = screenPos.x;
super.y = screenPos.y;
}
override public function toString():String
{
return "[IsoObject (x:" + _position.x + ", y:" + _position.y + ", z:" + _position.z + ")]";
}
override public function set x(value:Number):void
{
_position.x = value;
updateScreenPosition();
}
override public function get x():Number
{
return _position.x;
}
override public function set y(value:Number):void
{
_position.y = value;
updateScreenPosition();
}
override public function get y():Number
{
return _position.y;
}
override public function set z(value:Number):void
{
_position.z = value;
updateScreenPosition();
}
override public function get z():Number
{
return _position.z;
}
public function set position(value:Point3D):void
{
_position = value;
updateScreenPosition();
}
public function get position():Point3D
{
return _position;
}
public function get depth():Number
{
return (_position.x + _position.z) * 0.866 - _position.y * 0.707;
}
public function set walkable(value:Boolean):void
{
_walkable = value;
}
public function get walkable():Boolean
{
return _walkable;
}
public function get size():Number
{
return _size;
}
public function get rect():Rectangle
{
return new Rectangle(x - size / 2, z - size / 2, size, size);
}
public function set vx(value:Number):void
{
_vx = value;
}
public function get vx():Number
{
return _vx;
}
public function set vy(value:Number):void
{
_vy = value;
}
public function get vy():Number
{
return _vy;
}
public function set vz(value:Number):void
{
_vz = value;
}
public function get vz():Number
{
return _vz;
}
}
class DrawnIsoTile extends IsoObject
{
protected var _height:Number;
protected var _color:uint;
public function DrawnIsoTile(size :Number, color:uint, height:Number = 0)
{
super(size);
_color = color;
_height = height;
draw();
}
protected function draw():void
{
graphics.clear();
graphics.beginFill(_color);
graphics.lineStyle(0, 0, 5);
graphics.moveTo(-_size, 0);
graphics.lineTo(0, -_size * 0.5);
graphics.lineTo(_size, 0);
graphics.lineTo(0, _size * 0.5);
graphics.lineTo(-_size, 0);
graphics.endFill();
}
override public function set height(value:Number):void
{
_height = value;
draw();
}
override public function get height():Number
{
return _height;
}
public function set color(value:uint):void
{
_color = value;
draw();
}
public function get color():uint
{
return _color;
}
}
class DrawnIsoBox extends DrawnIsoTile
{
public function DrawnIsoBox(size :Number, color:uint, height:Number = 0)
{
super(size, color, height);
}
override protected function draw():void
{
graphics.clear();
var red:int = _color >> 16;
var green:int = _color >> 8 & 0xFF;
var blue:int = _color & 0xFF;
var leftShadow:uint = (red * 0.5) << 16 | (green * 0.5) << 8 | (blue * 0.5);
var rightShadow:uint = (red * 0.75) << 16 | (green * 0.75) << 8 | (blue * 0.75);
var h:Number = _height * Y_CORRECT;
graphics.beginFill(_color);
graphics.lineStyle(0, 0, 5);
graphics.moveTo(-_size, -h);
graphics.lineTo(0, -_size * 0.5 - h);
graphics.lineTo(_size, -h);
graphics.lineTo(0, _size * 0.5 - h);
graphics.lineTo(-_size, -h);
graphics.endFill();
graphics.beginFill(leftShadow);
graphics.lineStyle(0, 0, 5);
graphics.moveTo(-_size, -h);
graphics.lineTo(0, _size * 0.5 - h);
graphics.lineTo(0, _size * 0.5);
graphics.lineTo(-_size, 0);
graphics.lineTo(-_size, -h);
graphics.endFill();
graphics.beginFill(rightShadow);
graphics.lineStyle(0, 0, 5);
graphics.moveTo(_size, -h);
graphics.lineTo(0, _size * 0.5 - h);
graphics.lineTo(0, _size * 0.5);
graphics.lineTo(_size, 0);
graphics.lineTo(_size, -h);
graphics.endFill();
}
}
class IsoWorld extends Sprite
{
private var _floor:Sprite;
private var _world:Sprite;
private var _objects:Array;
public function IsoWorld()
{
_floor = new Sprite();
addChild(_floor);
_world = new Sprite();
addChild(_world);
_objects = new Array();
}
public function addChildToWorld(child:IsoObject):void
{
_world.addChild(child);
_objects.push(child);
sort();
}
public function addChildToFloor(child:IsoObject):void
{
_floor.addChild(child);
}
public function sort():void
{
_objects.sortOn("depth", Array.NUMERIC);
var length:int = _objects.length;
for(var i:int = 0;i < length; i++)
{
_world.setChildIndex(_objects[i], i);
}
}
}