Skip to content

Instantly share code, notes, and snippets.

@zaynyatyi
Last active June 2, 2016 07:51
Show Gist options
  • Select an option

  • Save zaynyatyi/afe98aa1225060d49fcb8a6d626358c5 to your computer and use it in GitHub Desktop.

Select an option

Save zaynyatyi/afe98aa1225060d49fcb8a6d626358c5 to your computer and use it in GitHub Desktop.
Just pools demonstration
package;
import openfl.Assets;
import openfl.display.Bitmap;
import openfl.display.Graphics;
import openfl.display.Sprite;
import openfl.events.TimerEvent;
import openfl.utils.Timer;
class Entity extends Sprite
{
static inline var RADIUS_DELTA:Int = 50;
static inline var MIN_RADIUS:Int = 20;
static inline var TIME_DELTA:Int = 500;
static inline var MIN_TIME:Int = 100;
public var isInUse:Bool = false;
public var callback:Entity->Void;
var bitmap:Bitmap;
var timer:Timer;
public function new()
{
super();
}
public function init():Void
{
if (isInUse) {
handleTimerComplete(null);
trace("we have to reinit already assigned entity");
}
isInUse = true;
var graphics:Graphics = this.graphics;
graphics.clear();
graphics.beginFill(Math.floor(Math.random() * 0xffffff));
graphics.drawCircle(0, 0, MIN_RADIUS + Math.floor(Math.random() * RADIUS_DELTA));
graphics.endFill();
if (timer == null) timer = new Timer(0, 1);
timer.delay = MIN_TIME + Math.floor(Math.random() * TIME_DELTA);
timer.addEventListener(TimerEvent.TIMER_COMPLETE, handleTimerComplete);
timer.start();
}
function handleTimerComplete(event:TimerEvent):Void
{
isInUse = false;
if (timer != null) {
timer.removeEventListener(TimerEvent.TIMER_COMPLETE, handleTimerComplete);
timer.reset();
}
if (callback != null) callback(this);
}
}
package;
import openfl.events.Event;
import openfl.display.Sprite;
import openfl.Lib;
import Entity;
class Main extends Sprite
{
static inline var ADDED_AMOUNT:Int = 1;
static inline var CANVAS_WIDTH:Int = 1024;
static inline var CANVAS_HEIGHT:Int = 768;
var staticPool:PoolStatic;
var dynamicPool:PoolDynamic;
public function new()
{
super();
addEventListener(Event.ADDED_TO_STAGE, handleAddedToStage);
}
function handleAddedToStage(event:Event):Void
{
#if staticpool
staticPool = new PoolStatic(10);
#elseif dynamicpool
dynamicPool = new PoolDynamic(10);
#end
removeEventListener(Event.ADDED_TO_STAGE, handleAddedToStage);
addEventListener(Event.ENTER_FRAME, handleEnterFrame);
}
function handleEnterFrame(event:Event):Void
{
for (index in 0...ADDED_AMOUNT) {
#if staticpool
getFromStatic();
#elseif dynamicpool
getFromDynamic();
#else
create();
#end
}
}
function create():Void
{
var entity:Entity = new Entity();
entity.init();
entity.callback = destroyEntity;
positionAndAdd(entity);
}
function getFromStatic():Void
{
var entity:Entity = staticPool.get();
entity.init();
entity.callback = releaseEntity;
positionAndAdd(entity);
}
function getFromDynamic():Void
{
var entity:Entity = dynamicPool.get();
entity.init();
entity.callback = releaseEntity;
positionAndAdd(entity);
}
function destroyEntity(entity:Entity):Void
{
entity.callback = null;
removeChild(entity);
entity = null;
}
function releaseEntity(entity:Entity):Void
{
entity.callback = null;
removeChild(entity);
}
function positionAndAdd(entity:Entity):Void
{
entity.x = Math.floor(Math.random() * CANVAS_WIDTH);
entity.y = Math.floor(Math.random() * CANVAS_HEIGHT);
addChild(entity);
}
}
package;
class PoolDynamic
{
var list:Array<Entity>;
public function new(initialCapacity:Int)
{
list = [];
for (index in 0...initialCapacity) {
list.push(new Entity());
}
}
public function get():Entity
{
var object:Entity = null;
var isFreeExists:Bool = false;
var index = 0;
while (index < list.length) {
if (!list[index].isInUse) {
object = list[index];
isFreeExists = true;
break;
}
index++;
}
if (!isFreeExists) {
trace("we have to extend pool");
object = new Entity();
list.push(object);
}
return object;
}
}
package;
class PoolStatic
{
var list:Array<Entity>;
var currentIndex:Int;
public function new(initialCapacity:Int)
{
list = [];
currentIndex = 0;
for (index in 0...initialCapacity) {
list.push(new Entity());
}
}
public function get():Entity
{
var object:Entity = list[currentIndex];
currentIndex++;
if (currentIndex >= list.length) currentIndex = 0;
return object;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment