Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tile Map #210

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 67 additions & 20 deletions src/flambe/Entity.hx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ using Lambda;

/** This entity's first component. */
public var firstComponent (default, null) :Component = null;

//Use setZOrder() to set this value instead of zOrder = x
public var zOrder : Int = 0;
public var orderOfArrival : Int = 1;

public static var globalOrderOfArrival : Int = 1;

public function new ()
{
Expand Down Expand Up @@ -165,39 +171,80 @@ using Lambda;
return untyped _compMap[name];
}

/**
/**
* Adds a child to this entity.
* @param append Whether to add the entity to the end or beginning of the child list.
* @returns This instance, for chaining.
*/
public function addChild (entity :Entity, append :Bool=true)
public function addChild (entity :Entity, append :Bool = true, ?zOrder : Int)
{
if (entity.parent != null) {
entity.parent.removeChild(entity);
}
entity.parent = this;

if (append) {
// Append it to the child list
var tail = null, p = firstChild;
while (p != null) {
tail = p;
p = p.next;
}
if (tail != null) {
tail.next = entity;
} else {
firstChild = entity;
}

} else {
// Prepend it to the child list
entity.next = firstChild;
firstChild = entity;
}
if (append) {
var tail = null, p = firstChild;

while (p != null) {
tail = p;
p = p.next;
}
if (tail != null) {
if (zOrder == null) {
zOrder = tail.zOrder;
}
if (tail.zOrder <= zOrder) {
tail.next = entity;
} else {
var p = firstChild;
var pre : Entity = null;
while (p != null) {
if (p.zOrder > zOrder) {
if (pre != null) {
pre.next = entity;
entity.next = p;
} else {
entity.next = firstChild;
firstChild = entity;
}
break;
} else {
pre = p;
p = p.next;
}
}
}
} else {
firstChild = entity;
if (zOrder == null) {
zOrder = 0;
}
}
} else {
if (firstChild == null) {
zOrder = 0;
} else {
zOrder = firstChild.zOrder - 1;
}
entity.next = firstChild;
firstChild = entity;
}

entity.zOrder = zOrder;

return this;
}

public function setZOrder(z : Int) {
if (this.zOrder == z) {
return;
} else {
this.zOrder = z;
this.parent.addChild(this, true, this.zOrder);
}

}

public function removeChild (entity :Entity)
{
Expand Down
31 changes: 31 additions & 0 deletions src/flambe/math/Rectangle.hx
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,37 @@ class Rectangle
{
return x == other.x && y == other.y && width == other.width && height == other.height;
}

public function intersect(rect : Rectangle) : Bool {
return !(rectGetMaxX() < rect.rectGetMinX() ||
rect.rectGetMaxX() < rectGetMinX() ||
rectGetMaxY() < rect.rectGetMinY() ||
rect.rectGetMaxY() < rectGetMinY());
}

public function rectGetMaxX() : Float{
return (x + width);
}

public function rectGetMidX() : Float {
return ((x + width) / 2);
}

public function rectGetMinX() : Float {
return x;
}

public function rectGetMaxY() : Float{
return (y + height);
}

public function rectGetMidY() : Float {
return ((y + height) / 2);
}

public function rectGetMinY() : Float {
return this.y;
}

#if debug @:keep #end public function toString () :String
{
Expand Down
35 changes: 35 additions & 0 deletions src/flambe/math/Size.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package flambe.math;

/**
* ...
* @author Ang Li(李昂)
*/
class Size
{

public var width : Float;
public var height : Float;
public function new(?width : Float = 0, ?height : Float = 0)
{
this.width = width;
this.height = height;
}

public function setSize(width : Float , height : Float) {
this.width = width;
this.height = height;
}

public function equals(size : Size) : Bool {
if (this.width == size.width && this.height == size.height) {
return true;
} else {
return false;
}
}

public function toString() : String {
return '$width x $height';
}

}
180 changes: 180 additions & 0 deletions src/flambe/tilemap/TMXBase64.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
package flambe.tilemap;

import haxe.io.Bytes;
import haxe.io.BytesInput;
import haxe.zip.InflateImpl;
#if flash
import flash.utils.ByteArray;
import flash.utils.Endian;
#end

class TMXBase64
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you considered using haxe.crypto.BaseCode instead of maintaining your own base64 implementation?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I tried BaseCode.decode(), but it does not work.
My base64 string is "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=".
but the BaseCode requires that base length must be a power of two. Then I tried to removed the "=", but the result of decode is empty.

{

public function new()
{

}
private static inline var BASE64_CHARS:String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

public static function decode(input : String ) : String {
input = StringTools.ltrim(input);
input = StringTools.rtrim(input);
var output : Array<String> = [];
var enc1 : Int;
var enc2 : Int;
var enc3 : Int;
var enc4 : Int;
var i : Int = 0;
var chr1 : Int;
var chr2 : Int;
var chr3 : Int;

while (i < input.length) {
enc1 = BASE64_CHARS.indexOf(input.charAt(i++));
enc2 = BASE64_CHARS.indexOf(input.charAt(i++));
enc3 = BASE64_CHARS.indexOf(input.charAt(i++));
enc4 = BASE64_CHARS.indexOf(input.charAt(i++));

chr1 = (enc1 << 2) | (enc2 >> 4);
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
chr3 = ((enc3 & 3) << 6) | enc4;

output.push(String.fromCharCode(chr1));
if (enc3 != 64) {
output.push(String.fromCharCode(chr2));
}
if (enc4 != 64) {
output.push(String.fromCharCode(chr3));
}
}

var o : String = output.join('');
var count : Int = 0;
for (i in output) {
var x = i.charCodeAt(0);
count++;
}
return o;
}

public static function decodeAsArray(input : String , lineWidth : Int , ?bytes : Int = 4) : Array<Int> {
var dec : String = decode(input);
var ar : Array<Int> = [];
var len : Int = Std.int(dec.length / bytes);

for (i in 0...len) {
ar[i] = 0;
var j = bytes - 1;
while (j >= 0) {
var t = dec.charCodeAt((i * bytes) + j) << (j * 8);
ar[i] += t;
j--;
}
}
return ar;
}

private static function decodeAsOneArray(input : String , lineWidth : Int , ?bytes : Int = 4) : Array<Int> {
var dec : String = decode(input);
var ar : Array<Int> = [];
var len : Int = Std.int(dec.length / bytes);

for (i in 0...len) {
ar[i] = 0;
var j = bytes - 1;
while (j >= 0) {
var t = dec.charCodeAt((i * bytes) + j) << (j * 8);
ar[i] += t;
j--;
}
}
return ar;
}

private static function decodeAsArrayBytes(byte : Bytes , lineWidth : Int , ?bytes : Int = 4) : Array<Int> {
var ar : Array<Int> = [];
var len : Int = Std.int(byte.length / bytes);

for (i in 0...len) {
ar[i] = 0;
var j = bytes - 1;
while (j >= 0) {
var t = byte.get((i * bytes) + j) << (j * 8);
ar[i] += t;
j--;
}
}

return ar;
}

#if js
public static function unzip(input : String, lineWidth : Int ) : Array<Int> {
var tempString1 : String = decode(input);
var arr : Array<Int> = TMXBase64.decodeAsOneArray(input, 0, 1);

var b = Bytes.ofData(cast arr);
var bytes : Bytes = InflateImpl.run(new BytesInput(b));
var ret : Array<Int> = TMXBase64.decodeAsArrayBytes(bytes, lineWidth);

return ret;
}
#end

#if flash
public static function unzip(chunk : String, lineWidth : Int) : Array<Int> {
var result:Array<Int> = new Array<Int>();
var data:ByteArray = base64ToByteArray(chunk);
data.uncompress();
data.endian = Endian.LITTLE_ENDIAN;
while(data.position < data.length)
{

result.push(data.readInt());
}
return result;
}

private static function base64ToByteArray(data:String):ByteArray
{
var output:ByteArray = new ByteArray();

var lookup:Array<Int> = new Array<Int>();
var c:Int;
for (c in 0...BASE64_CHARS.length)
{
lookup[BASE64_CHARS.charCodeAt(c)] = c;
}

var i:Int = 0;
while (i < data.length - 3)
{
if (data.charAt(i) == " " || data.charAt(i) == "\n")
{
i++; continue;
}

var a0:Int = lookup[data.charCodeAt(i)];
var a1:Int = lookup[data.charCodeAt(i + 1)];
var a2:Int = lookup[data.charCodeAt(i + 2)];
var a3:Int = lookup[data.charCodeAt(i + 3)];

//convert to and write 3 bytes
if(a1 < 64)
output.writeByte((a0 << 2) + ((a1 & 0x30) >> 4));
if(a2 < 64)
output.writeByte(((a1 & 0x0f) << 4) + ((a2 & 0x3c) >> 2));
if(a3 < 64)
output.writeByte(((a2 & 0x03) << 6) + a3);

i += 4;
}

output.position = 0;
return output;
}
#end


}
Loading