forked from: Saqoosha challenge for amateurs
forked from Saqoosha challenge for amateurs (diff: 479)
UnionFrocessing マリオの音 http://wonderfl.net/code/82e45099bf2f4d24451e00eb58e67835d927d405 @author naoto koshikawa
ActionScript3 source code
/**
* Copyright naoto5959 ( http://wonderfl.net/user/naoto5959 )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/l1B6
*/
// forked from checkmate's Saqoosha challenge for amateurs
package
{
import com.flashdynamix.utils.SWFProfiler;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageQuality;
import flash.display.StageScaleMode;
import org.libspark.thread.EnterFrameThreadExecutor;
import org.libspark.thread.Thread;
[SWF(width="465",height="465",backgroundColor=0x000000)]
/**
* UnionFrocessing
* マリオの音
* http://wonderfl.net/code/82e45099bf2f4d24451e00eb58e67835d927d405
*
* @author naoto koshikawa
*/
public class UnionFrocessing extends Sprite
{
//----------------------------------------------------------------------
// static properties
//----------------------------------------------------------------------
//------------------------------
// public static properties
//------------------------------
/**
* Union host
*/
public static const HOST:String = "tryunion.com";
/**
* Union ports
*/
public static const PORTS:Array = [9100, 9101];
/**
* Room ID
*/
public static const ROOM_ID:String = "net.wonderfl.naoto5959.frocessing.rainbow";
//----------------------------------------------------------------------
// methods
//----------------------------------------------------------------------
//------------------------------
// public methods
//------------------------------
/**
* constructor
*/
public function UnionFrocessing()
{
Wonderfl.capture_delay( 10 );
SWFProfiler.init(this);
stage.scaleMode = StageScaleMode.NO_SCALE;
//stage.align = StageAlign.TOP_LEFT;
stage.quality = StageQuality.HIGH;
Thread.initialize(new EnterFrameThreadExecutor());
new MainThread(this).start();
}
}
}
import flash.display.Stage;
import flash.events.MouseEvent;
import flash.filters.BlurFilter;
import flash.geom.Point;
import flash.system.Capabilities;
import frocessing.display.F5MovieClip2D;
import frocessing.display.F5MovieClip2DBmp;
import net.user1.reactor.IClient;
import net.user1.reactor.Reactor;
import net.user1.reactor.ReactorEvent;
import net.user1.reactor.Room;
import net.user1.reactor.RoomEvent;
import net.user1.reactor.RoomManager;
import net.user1.reactor.RoomManagerEvent;
import org.libspark.betweenas3.BetweenAS3;
import org.libspark.betweenas3.easing.*;
import org.libspark.thread.Thread;
import org.si.sion.*;
import frocessing.color.ColorLerp;
import org.libspark.betweenas3.core.easing.IEasing;
class Gradation {
private var _colors:Array;
private var _easing:IEasing;
public function Gradation(...args) {
_colors = args.concat();
_easing = Linear.linear;
}
public function setEasing(easing:IEasing):void {
_easing = easing;
}
public function getColor(position:Number):uint {
position = (position < 0 ? 0 : position > 1 ? 1 : position) * (_colors.length - 1);
var idx:int = position;
var alpha:Number = _easing.calculate(position - idx, 0, 1, 1);
if (alpha == 0) {
return _colors[idx];
} else {
return ColorLerp.lerp(_colors[idx], _colors[idx + 1], alpha);
}
}
}
/**
* メインスレッド
*/
class MainThread extends Thread
{
private var _main:UnionFrocessing;
private var _loadingThread:LoadingThread;
private var _uiThread:UIThread;
private var _unionThread:UnionThread;
private var _listenerThread:ListenerThread;
/**
*
* @param main mainドキュメントクラス
*/
public function MainThread(main:UnionFrocessing):void
{
_main = main;
}
override protected function run():void
{
_loadingThread = new LoadingThread(_main);
_loadingThread.start();
next(connect);
}
private function connect():void
{
_unionThread = new UnionThread();
_unionThread.start();
_unionThread.join();
next(draw);
}
private function draw():void
{
_uiThread = new UIThread(_main);
_uiThread.start();
_uiThread.join();
next(listen);
}
private function listen():void
{
_listenerThread = new ListenerThread(_unionThread.reactor, _uiThread);
_listenerThread.start();
_listenerThread.join();
next(done);
}
private function done():void
{
_loadingThread.notify();
}
}
/**
* Loading中に回すアレを制御するスレッド
*/
class LoadingThread extends Thread
{
private var _main:UnionFrocessing;
private var _loadingCircle:LoadingCircle;
public function LoadingThread(main:UnionFrocessing):void
{
_main = main;
}
override protected function run():void
{
_loadingCircle = new LoadingCircle();
_main.addChild(_loadingCircle);
// notify()されるまで待つ
wait();
next(stop);
}
private function stop():void
{
_main.removeChild(_loadingCircle);
_loadingCircle.noLoop();
_loadingCircle = null;
}
}
/**
* Loading中にくるくる回すアレ
*/
class LoadingCircle extends F5MovieClip2D
{
private static const COLOR_NUM:Number = 24;
private static const RADIUS:Number = 16;
private static const EACH_WIDTH:Number = 6;
private static const EACH_HEIGHT:Number = 2;
private var _count:Number = 0;
public function setup():void
{
size(465, 465);
colorMode(HSV, COLOR_NUM, 1.0, 1.0, 1.0);
noStroke();
background(0);
}
public function draw():void
{
translate(465 / 2, 465 / 2);
rotate(Math.PI * 2 * (_count / COLOR_NUM));
for (var i:int = 0; i < COLOR_NUM; i++)
{
fill(i, 1.0, 1.0, 1.0);
rect(RADIUS, 0, EACH_WIDTH, EACH_HEIGHT, 2, 2);
rotate(-Math.PI * 2 / COLOR_NUM);
}
if (COLOR_NUM < ++_count) _count = 0;
}
}
/**
* Unionに接続するスレッド
*/
class UnionThread extends Thread
{
private var _reactor:Reactor;
public function get reactor():Reactor
{
return _reactor;
}
public function UnionThread():void
{
}
override protected function run():void
{
_reactor = new Reactor(null, Capabilities.playerType == "StandAlone");
event(_reactor, ReactorEvent.READY, _reactor_readyHandler);
_reactor.addEventListener(ReactorEvent.CLOSE, _reactor_closeHandler);
_reactor.connect.apply(null,
[UnionFrocessing.HOST].concat(UnionFrocessing.PORTS));
}
private function _reactor_readyHandler(event:ReactorEvent):void
{
}
private function _reactor_closeHandler(event:ReactorEvent):void
{
throw(new Error("connection closed"));
}
}
/**
* User Interface スレッド
*/
class UIThread extends Thread
{
private var _main:UnionFrocessing;
public function get main():UnionFrocessing
{
return _main;
}
private var _sounds:Sounds;
public function get sounds():Sounds
{
return _sounds;
}
private var _rainbow:Rainbow;
public function get ranbow():Rainbow
{
return _rainbow;
}
public function UIThread(main:UnionFrocessing):void
{
_main = main;
_sounds = new Sounds();
}
override protected function run():void
{
_rainbow = new Rainbow(this);
_main.addChild(_rainbow);
_rainbow.x = 0;
_rainbow.y = 0;
}
}
class Rainbow extends F5MovieClip2DBmp
{
private static const COLORS:int = 32;
private var _ui:UIThread;
private var _node:Number = 36;
private var _width:Number = 465;
private var _height:Number = 465
private var _centerX:Number = 0;
private var _centerY:Number = 0;
private var _count:Number = 1;
private var _gradation:Gradation;
public function Rainbow(ui:UIThread):void
{
_ui = ui;
}
public function setup():void
{
size(_width, _height);
background(0);
_centerX = _width / 2;
_centerY = _height / 2;
_gradation = new Gradation(
0xff0000, 0xff3000, 0xff6000, 0xff8f00, 0xffbf00, 0xffef00, 0xdfff00, 0xafff00,
0x80ff00, 0x50ff00, 0x20ff00, 0x00ff10, 0x00ff40, 0x00ff70, 0x00ff9f, 0x00ffcf,
0x00ffff, 0x00cfff, 0x009fff, 0x0070ff, 0x0040ff, 0x0010ff, 0x2000ff, 0x5000ff,
0x8000ff, 0xaf00ff, 0xdf00ff, 0xff00ef, 0xff00bf, 0xff008f, 0xff0060, 0xff0030
);
_gradation.setEasing(Expo.easeOut);
}
public function draw():void
{
var radius:Number = random(0, random(_count, (1 - _width / _count)));
noFill();
translate(_centerX, _centerY);
beginShape();
rotate( -Math.PI * 2/ _node);
stroke(_gradation.getColor(radius / _width));
for (var i:int = 0; i < _node + 3; i++)
{
curveVertex( - radius, 0);
rotate(Math.PI *2 / _node);
}
endShape();
if (isMousePressed)
{
_ui.sounds.play("fumi");
_centerX = mouseX;
_centerY = mouseY;
bitmapData.applyFilter(bitmapData, bitmapData.rect, new Point(), new BlurFilter(16, 16, 2));
}
_count ++;
}
public function blur(clickX:Number, clickY:Number):void
{
_centerX = clickX;
_centerY = clickY;
bitmapData.applyFilter(bitmapData, bitmapData.rect, new Point(), new BlurFilter(16, 16, 2));
}
}
/**
* Sounds
*/
class Sounds
{
private var _driver:SiONDriver;
private var _driver2:SiONDriver;
private var _data:Object;
public function Sounds():void
{
_driver2 = new SiONDriver();
_driver = SiONDriver.mutex;
var header:String = "t200;#EFFECT0{speaker20};";
// The following 4 lines are avoiding bugs in curren version.
// In this version, the sequenceOn() function cannot refer tables in the SiONData,
// so we have to define all tables on main SiONData played by SiONDriver.play().
// In new version(0.57), the sequenceOn() function can refer the tables in SiONData and following mmls are not requeired.
header += "#TABLE0{(0,128)8};#TABLE1{(0,384)8};#TABLE2{(128,0)12,0};#TABLE3{(90,934)12};";
header += "#TABLE4{(0,-1280)4};#TABLE5{(0,-1280)6};#TABLE6{(0,-2560)6};#TABLE7{(0,36)36};";
header += "#TABLE8{14,0,13,0,13,0,12,0,12,,11,0,9,0,9,0,8,0,8,0,7,0,6,0,6,0,5,0,5}*8+31;";
header += "#TABLE9{12,6,13,8,13,3,9,10,15,12,6,11,13,14};";
var nosound:SiONData = _driver.compile(header);
_data = { };
_data["kick"] = _driver.compile("t200;%1@4s63l128o5b-<cr16f64");
_data["fumi"] = _driver.compile("t200;#TABLE0{(0,128)8};#TABLE1{(0,384)8};%1@4s63q8l10na0np1o5a<gnanp");
_data["join"] = _driver.compile("t200;%1@4s63q8l64o7[e>ad<e>ad>gc>f>a+r16]3");
_data["leave"] = _driver.compile("t200;#TABLE7{(0,36)36};%1@4s63q8o4nt7,2b-1");
_driver2.bpm = 200;
_driver2.play(nosound);
}
public function play(key:String):void
{
if (_data[key] is SiONData)
{
_driver2.sequenceOn(_data[key], null, 0, 0, 0);
}
}
}
/**
* Listener スレッド
*/
class ListenerThread extends Thread
{
private var _reactor:Reactor;
private var _roomManager:RoomManager;
private var _room:Room;
private var _ui:UIThread;
private var _stage:Stage;
public function ListenerThread(reactor:Reactor, ui:UIThread):void
{
_reactor = reactor;
_roomManager = _reactor.getRoomManager();
_ui = ui;
_stage = _ui.main.stage;
}
override protected function run():void
{
_room = _roomManager.createRoom(UnionFrocessing.ROOM_ID);
event(_roomManager, RoomManagerEvent.CREATE_ROOM_RESULTS,
_roomManager_createRoomResultsHandler);
}
private function _roomManager_createRoomResultsHandler(
e:RoomManagerEvent):void
{
event(_room, RoomEvent.JOIN, _room_joinHandler);
_room.addMessageListener("click", clickMessage);
_room.addEventListener(RoomEvent.ADD_CLIENT, _room_addClientHandler);
_room.addEventListener(RoomEvent.REMOVE_CLIENT, _room_removeClientHandler);
_room.join();
}
private function _room_joinHandler(e:RoomEvent):void
{
activate();
}
private function _room_addClientHandler(e:RoomEvent):void
{
_ui.sounds.play("join");
}
private function _room_removeClientHandler(e:RoomEvent):void
{
_ui.sounds.play("leave");
}
private function activate():void
{
_stage.addEventListener(MouseEvent.CLICK, _stage_clickHandler);
}
private function _stage_clickHandler(event:MouseEvent):void
{
_room.sendMessage("click", false, null, event.stageX, event.stageY);
}
private function clickMessage(
fromClient:IClient, x:Number, y:Number):void
{
_ui.sounds.play("kick");
_ui.ranbow.blur(x, y);
}
}