Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package client.ui.scene;
- import haxe.Timer;
- import openfl.display.BitmapData;
- import openfl.display.Graphics;
- import openfl.display.Sprite;
- import openfl.display.Stage;
- import openfl.display.Tilesheet;
- import openfl.geom.Matrix;
- import openfl.geom.Rectangle;
- import openfl.Lib;
- #if neko
- import neko.vm.Thread;
- import neko.vm.Mutex;
- #end
- #if cpp
- import cpp.vm.Thread;
- import cpp.vm.Mutex;
- #end
- class SharedData<T> {
- private var mutex:Mutex;
- private var _value:T;
- public var value(get, set):T;
- public function new( value:T = null ) {
- mutex = new Mutex();
- this.value = value;
- }
- private function get_value():T {
- var ret:T;
- mutex.acquire();
- ret = _value;
- mutex.release();
- return ret;
- }
- private function set_value(v:T):T {
- mutex.acquire();
- _value = v;
- mutex.release();
- return v;
- }
- }
- class Cacher {
- private var _graphics:Graphics;
- private var _thread:Thread;
- private var _backAnimationTilesheet:SharedData<Tilesheet>;
- public var backAnimationTilesheet( get, null ):Tilesheet;
- private function get_backAnimationTilesheet():Tilesheet {
- return _backAnimationTilesheet.value;
- }
- private var _timer:Timer;
- public function new( graphics:Graphics, stamp:Sprite, width:Int, height:Int, radius:Int ):Void {
- _graphics = graphics;
- _thread = Thread.create( build );
- _thread.sendMessage( Thread.current() );
- _thread.sendMessage( stamp );
- _thread.sendMessage( width );
- _thread.sendMessage( height );
- _thread.sendMessage( radius );
- _backAnimationTilesheet = new SharedData<Tilesheet>();
- _thread.sendMessage( _backAnimationTilesheet );
- _timer = new Timer( 1000 );
- _timer.run = checkOver;
- }
- private function checkOver():Void {
- var over:Thread;
- over = Thread.readMessage( false );
- if ( over == null )
- return;
- _timer.stop();
- _timer.run = null;
- _timer = null;
- backAnimationTilesheet.drawTiles( _graphics, [ 0, 0, 0 ] );
- trace( "over" );
- }
- static private function build():Void {
- var mainThread:Thread;
- var stamp:Sprite;
- var width:Int;
- var height:Int;
- var radius:Int;
- var sahredBackAnimationTilesheet:SharedData<Tilesheet>;
- var bitmapData:BitmapData;
- var tilesheet:Tilesheet;
- var matrix:Matrix;
- var offX:Float;
- var offY:Float;
- mainThread = Thread.readMessage( true );
- stamp = Thread.readMessage( true );
- width = Thread.readMessage( true );
- height = Thread.readMessage( true );
- radius = Thread.readMessage( true );
- sahredBackAnimationTilesheet = Thread.readMessage( true );
- bitmapData = new BitmapData( width, height, false, 0xFF0000 );
- tilesheet = new Tilesheet( bitmapData );
- stamp.graphics.beginFill( 0x00FF00, 1 );
- stamp.graphics.drawCircle( 0, 0, 10 );
- stamp.graphics.endFill();
- Sys.sleep( 2 );
- for ( i in 0...10 ) {
- offX = Math.random() * width;
- offY = Math.random() * height;
- if ( true ) {
- matrix = new Matrix();
- matrix.translate( offX, offY );
- bitmapData.draw( stamp, matrix, null, null, new Rectangle( offX - 10, offY - 10, 20, 20 ) );
- }
- else {
- bitmapData.fillRect( new Rectangle( offX - 10, offY - 10, 20, 20 ), 0x00FF00 );
- }
- tilesheet.addTileRect( new Rectangle( 0, 0, width, height ) );
- }
- sahredBackAnimationTilesheet.value = tilesheet;
- mainThread.sendMessage( Thread.current );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement