Skip to content

Instantly share code, notes, and snippets.

@larsiusprime larsiusprime/AABB.hx
Last active May 24, 2018

Embed
What would you like to do?
Defender's Quest Performance Profiling References
package com.leveluplabs.tdrpg.qtree;
import com.leveluplabs.tdrpg.Popup_OverworldMenu;
import flixel.util.FlxDestroyUtil.IFlxDestroyable;
import flixel.util.FlxPool;
/**
* ...
* @author larsiusprime
*/
class AABB implements IFlxDestroyable
{
public var center_x:Float;
public var center_y:Float;
public var halfSize_x:Float;
public var halfSize_y:Float;
private var destroyed:Bool = false;
private static var pool:FlxPool<AABB> = new FlxPool<AABB>(AABB);
public function destroy()
{
destroyed = true;
center_x = 0;
center_y = 0;
halfSize_x = 0;
halfSize_y = 0;
}
public function new(){}
public static function put(aabb:AABB)
{
if (aabb == null) return;
if (@:privateAccess aabb.destroyed) return;
pool.putUnsafe(aabb);
}
public static function get(Center_x:Float, Center_y:Float, HalfSize_x:Float, HalfSize_y:Float):AABB
{
var aabb = pool.get();
@:privateAccess aabb.destroyed = false;
aabb.center_x = Center_x;
aabb.center_y = Center_y;
aabb.halfSize_x = HalfSize_x;
aabb.halfSize_y = HalfSize_y;
return aabb;
}
public static function create(Center_x:Float,Center_y:Float,HalfSize_x:Float,HalfSize_y:Float):AABB
{
var aabb = new AABB();
aabb.center_x = Center_x;
aabb.center_y = Center_y;
aabb.halfSize_x = HalfSize_x;
aabb.halfSize_y = HalfSize_y;
return aabb;
}
public inline function set(centerX:Float, centerY:Float, halfX:Float, halfY:Float):Void
{
center_x = centerX;
center_y = centerY;
halfSize_x = halfX;
halfSize_y = halfY;
}
public inline function containsPoint(X:Float,Y:Float):Bool
{
var value = false;
if (X >= center_x - halfSize_x && X <= center_x + halfSize_x)
{
if (Y >= center_y - halfSize_y && Y <= center_y + halfSize_y)
{
value = true;
}
}
return value;
}
public inline function intersectAABB(other:AABB):Bool
{
var x1 = (center_x - other.center_x);
if (x1 < 0) x1 *= -1;
var y1 = (center_y - other.center_y);
if (y1 < 0) y1 *= -1;
if (x1 > (halfSize_x + other.halfSize_x)) return false;
if (y1 > (halfSize_y + other.halfSize_y)) return false;
return true;
}
}
package com.leveluplabs.tdrpg;
import haxe.io.BytesData;
import lime.utils.Bytes;
/**
* ...
* @author
*/
class ByteMap
{
private var bytes:Bytes;
public var width:Int;
public var height:Int;
private static inline var MAX:Int = 255;
public function new(w:Int,h:Int)
{
var mapSize = Math.ceil(w * h);
width = w;
height = h;
bytes = Bytes.alloc(mapSize);
}
public function print():String
{
var str = "";
for(i in 0...height)
{
str = "";
for(j in 0...width)
{
var value = bytes.get(i*width+j);
str += value + " ";
}
trace(str);
}
return str;
}
public function peek(x:Int, y:Int):Int
{
if (false == (x >= 0 && y >= 0 && x < width && y < height))
{
return 0;
}
var i = y * width + x;
return bytes.get(i);
}
public function poke(x:Int, y:Int, value:Int)
{
if (false == (x >= 0 && y >= 0 && x < width && y < height))
{
return;
}
var i = y * width + x;
return bytes.set(i,value);
}
public function clear()
{
bytes.fill(0, bytes.length, 0);
}
public function blit(x:Int, y:Int, w:Int, h:Int, value:Int)
{
if (x < 0) x = 0;
if (y < 0) y = 0;
if (x + w >= width) w = (width-x);
if (y + h >= height) h = (height-y);
for (i in 0...h)
{
var row = y + i;
var pos = row * width + x;
bytes.fill(pos, w, value);
}
}
public function blitEmptyTest(x:Int, y:Int, w:Int, h:Int, value:Int):Float
{
if (x < 0) x = 0;
if (y < 0) y = 0;
if (x + w >= width) w = (width-x);
if (y + h >= height) h = (height-y);
var area = w * h;
var cells = 0;
var emptyCells = 0;
for (i in 0...h)
{
var row = y + i;
var pos = row * width + x;
for (j in pos...pos + w)
{
var v = bytes.get(j);
cells += v;
v += value;
if (v == 0)
{
emptyCells++;
}
if (v < MAX)
{
bytes.set(j, v);
}
}
}
if (emptyCells > 0) return 0;
return cells / area;
}
}
package com.leveluplabs.tdrpg.qtree;
import flixel.util.FlxDestroyUtil.IFlxDestroyable;
import flixel.util.FlxPool;
import haxe.ds.Vector;
/**
* ...
* @author
*/
class XYVector implements IFlxDestroyable
{
public var size(default, null):Int;
private var xy:Vector<Float>;
private var int_id:Vector<Int>;
private var destroyed:Bool = false;
public static inline var CAPACITY:Int = 64;
private static var pool:FlxPool<XYVector> = new FlxPool<XYVector>(XYVector);
public function new()
{
size = 0;
xy = new Vector<Float>(CAPACITY*2);
int_id = new Vector<Int>(CAPACITY*2);
}
public static function put(xy:XYVector)
{
if (xy == null) return;
if (@:privateAccess xy.destroyed) return;
pool.putUnsafe(xy);
}
public static function get():XYVector
{
var xy = pool.get();
@:privateAccess xy.destroyed = false;
return xy;
}
public function destroy()
{
destroyed = true;
clear();
}
public inline function clear()
{
for (i in 0...size)
{
var index = i * 2;
xy[index] = 0.0;
xy[index + 1] = 0.0;
int_id[index] = 0;
int_id[index + 1] = 0;
}
size = 0;
}
public inline function set(i:Int, X:Float, Y:Float, int:Int, id:Int)
{
var index = i * 2;
xy[index] = X;
xy[index + 1] = Y;
int_id[index] = int;
int_id[index + 1] = id;
}
public inline function getX(i:Int):Float{ return xy[i * 2]; }
public inline function getY(i:Int):Float{ return xy[(i * 2) + 1]; }
public inline function getInt(i:Int):Int { return int_id[i * 2]; }
public inline function getId(i:Int):Int { return int_id[(i * 2) + 1]; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.