(function () {
	function z() {
		n = +new Date;
		for (var a = 0; a < d.length; a++)
			d[a].think()
	}
	function A() {
		n = +new Date;
		b.clearRect(0, 0, f.width, f.height);
		var a = null != c ? c.x - 100 : 0;
		h = 0 == h ? a : Math.round((h + a) / 2);
		for (a =  - (Math.floor(h / 2) % 288); a < f.width; )
			b.drawImage(m, 0, 0, 288, 512, a, 0, 288, 512), a += 288;
		b.save();
		b.translate(-h, 0);
		for (a = 0; a < p.length; a++)
			p[a].draw();
		b.restore();
		for (a =  - (Math.floor(h) % 336); a < f.width; )
			b.drawImage(m, 584, 0, 336, 111, a, 401, 336, 111), a += 336;
		b.save();
		b.translate(-h, 0);
		for (a = 0; a < d.length; a++)
			d[a] != c && d[a].draw();
		null != c && c.draw();
		b.restore();
		b.fillText("Score: " + K() + " | Players: " + B + " | Distance: " + (null == c ? 0 : Math.floor(c.x / 200)) + " | Server: " + t.slice(5), 20, 20);
		v.clearRect(0, 0, f.width, f.height);
		v.drawImage(r, 0, 0, f.width, f.height);
		window.requestAnimationFrame(A)
	}
	function K() {
		if (null == c)
			return 0;
		for (var a = 0, e = 0; e < p.length; e++)
			p[e].x < w || c.x > p[e].x + 30 && ++a;
		return a
	}
	function C(a, e, b, c) {
		this.id = a;
		this.x = e;
		this.y = b;
		this.vx = D;
		this.vy = 0;
		this.seed = 9999 * Math.random();
		this.sx = e;
		this.sy = b;
		this.fx = e;
		this.fy = b;
		this.nick =
			c
	}
	function E(a, e) {
		this.x = a;
		this.y = e
	}
	function L() {
		if (null != c && (~~c.x != F || ~~c.y != G)) {
			var a = new ArrayBuffer(9),
			e = new DataView(a);
			e.setUint8(0, 1);
			e.setInt32(1, ~~c.x, !0);
			e.setInt32(5, ~~c.y, !0);
			k.send(a);
			F = c.x;
			G = c.y
		}
	}
	function u() {
		var a = q.value,
		e = new ArrayBuffer(32),
		b = new DataView(e);
		b.setUint8(0, 5);
		for (var c = 0; c < a.length && 16 > c; c++) {
			var d = a.charCodeAt(c);
			128 <= d || b.setUint8(c + 1, d)
		}
		k.send(e)
	}
	function H(a, b) {
		return a.x + a.r < b.x || a.y + a.r < b.y || a.x - a.r > b.x + b.width || a.y - a.r > b.y + b.height ? !1 : !0
	}
	window.onload = function () {
		q =
			document.getElementById("nickname");
		q.onchange = u;
		q.onkeydown = u;
		q.onkeyup = u;
		q.onkeypress = u;
		f = document.getElementById("canvas");
		v = f.getContext("2d");
		r = document.createElement("canvas");
		r.width = f.width;
		r.height = f.height;
		b = r.getContext("2d");
		t = "ws://localhost:2828";
		t = I[~~(I.length * Math.random())];
		k = new WebSocket(t);
		k.binaryType = "arraybuffer";
		k.onopen = function (a) {
			console.log("Socket connected")
		};
		k.onmessage = function (a) {
			a = a.data;
			switch ((new DataView(a)).getUint8(0)) {
			case 0:
				J = (new DataView(a)).getInt32(1,
					!0);
				break;
			case 1:
				a = new DataView(a);
				for (var b = 0; b < d.length; b++)
					d[b].wasSeen = !1;
				B = a.getInt32(1, !0);
				for (var f = a.getInt32(5, !0), g = 9, b = 0; b < f; b++) {
					for (var h = a.getInt32(g + 0, !0), m = a.getInt32(g + 4, !0), n = a.getInt32(g + 8, !0), g = g + 12, k = ""; ; ) {
						var l = a.getUint8(g++);
						if (0 == l)
							break;
						k += String.fromCharCode(l)
					}
					l = x[h];
					null == l ? (l = new C(h, m, n, k), d.push(l), x[h] = l, l.id == J && null == c && (c = l)) : (l.wasSeen = !0, l.update(m, n, k))
				}
				for (b = 0; b < d.length; b++)
					null == d[b].id || d[b].wasSeen || (delete x[d[b].id], d[b].destroy(), d.splice(b, 1), --b);
				f =
					a.getInt32(g, !0);
				g += 4;
				for (b = 0; b < f; b++)
					p.push(new E(a.getInt32(g + 0, !0), a.getInt32(g + 4, !0))), g += 8
			}
		};
		z();
		A();
		setInterval(z, 1E3 / 60);
		setInterval(L, 50)
	};
	var f,
	r,
	b,
	v,
	m = new Image;
	m.src = "atlas.png";
	var h = 0,
	d = [],
	x = {},
	p = [],
	n = 0,
	c = null,
	y = !1,
	J = null,
	s = !1,
	B = "?",
	w = 100,
	q,
	D = 2,
	k,
	I = "ws://flapmmo.com:2828 ws://flapmmo.com:2829 ws://flapmmo.com:2830 ws://96.126.121.68:2828 ws://96.126.121.68:2829 ws://96.126.121.68:2830 ws://198.58.104.108:2828 ws://198.58.104.108:2829 ws://198.58.104.108:2830 ws://198.58.122.72:2828 ws://198.58.122.72:2829 ws://198.58.122.72:2830".split(" "),
	t = null;
	C.prototype = {
		id : 0,
		x : 0,
		y : 0,
		vx : 0,
		vy : 0,
		seed : 0,
		wasSeen : !0,
		nick : null,
		rotation : 0,
		targetRotation : 0,
		sx : 0,
		sy : 0,
		fx : 0,
		fy : 0,
		start : 0,
		end : 0,
		draw : function () {
			b.save();
			b.translate(2 * Math.floor(this.x / 2), 2 * Math.floor(this.y / 2));
			var a = Math.floor((n + this.seed) / 200) % 2;
			b.rotate(this.rotation);
			this == c ? (b.beginPath(), b.fillStyle = "rgba(255, 255, 255, 0.5)", b.arc(0, 0, 30, 0, 2 * Math.PI, !1), b.fill(), b.drawImage(m, 230, 762 + 52 * a, 34, 24, -17, -12, 34, 24)) : (b.globalAlpha *= 0.5, b.drawImage(m, 6 + 56 * a, 982, 34, 24, -17, -12, 34, 24), b.globalAlpha /=
				0.5);
			b.rotate(-this.rotation);
			if (a = this.nick)
				b.textAlign = "center", b.fillStyle = "#000000", b.fillText(a, 1, -20), b.fillText(a, -1, -20), b.fillText(a, 0, -21), b.fillText(a, 0, -19), b.fillStyle = "#FFFFFF", b.fillText(a, 0, -20);
			b.restore()
		},
		think : function () {
			if (this == c) {
				for (var a = 0; a < p.length; a++)
					if (p[a].collidesWith(this)) {
						s = !0;
						break
					}
				this.vy += 0.4;
				389 <= this.y && (this.vy = this.vx = 0, s = !0);
				s ? (this.vx = 0, 389 < this.y && (this.y = 389)) : y && (y = !1, this.vy = -8);
				this.x += this.vx;
				this.y += this.vy;
				this.targetRotation = Math.atan2(this.vy,
						this.vx)
			} else
				a = (n - this.start) / (this.end - this.start), 1 < a && (a = 1), this.x = this.sx + (this.fx - this.sx) * a, this.y = this.sy + (this.fy - this.sy) * a, this.targetRotation = Math.atan2(this.fy - this.sy, this.fx - this.sx);
			for (; 180 < this.targetRotation; )
				this.targetRotation -= 360;
			for (; -180 > this.targetRotation; )
				this.targetRotation += 360;
			this.rotation = (this.rotation + this.targetRotation) / 2
		},
		update : function (a, b, d) {
			this != c && (this.sx = this.x, this.sy = this.y, this.fx = a, this.fy = b, this.start = n, this.end = n + 200);
			d && (this.nick = d)
		},
		destroy : function () {}

	};
	E.prototype = {
		x : 0,
		y : 0,
		getHeight : function () {
			return 124
		},
		draw : function () {
			this.isValid() || (b.globalAlpha *= 0.5);
			b.drawImage(m, 112, 646, 52, 320, this.x, this.y - 320, 52, 320);
			b.drawImage(m, 168, 646, 52, 320, this.x, this.y + this.getHeight(), 52, 320);
			this.isValid() || (b.globalAlpha /= 0.5)
		},
		collidesWith : function (a) {
			return this.isValid() ? H({
				x : a.x,
				y : a.y,
				r : 12
			}, {
				x : this.x,
				y : this.y - 320,
				width : 52,
				height : 320
			}) || H({
				x : a.x,
				y : a.y,
				r : 12
			}, {
				x : this.x,
				y : this.y + this.getHeight(),
				width : 52,
				height : 320
			}) : !1
		},
		isValid : function () {
			return this.x > w + 200
		}
	};
	var F,
	G;
	document.body.onkeydown = function (a) {
		s && (null == c || 389 <= c.y) ? null != c && (c.vx = D, c.vy = 0, c.x = 100, c.y = 50, w = c.x, s = !1) : null != c && 0 < c.y && (y = !0)
	};
	window.requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || function (a) {
		setTimeout(a, 1E3 / 60)
	}
})();