package com.ezardlabs.lostsector.objects.projectiles;

import com.ezardlabs.dethsquare.Collider;
import com.ezardlabs.dethsquare.GameObject;
import com.ezardlabs.dethsquare.RectF;
import com.ezardlabs.dethsquare.Script;
import com.ezardlabs.dethsquare.multiplayer.Network;
import com.ezardlabs.lostsector.Game.DamageType;
import com.ezardlabs.lostsector.objects.enemies.Enemy;

public class LankaBeam extends Script {
	private long chargeTime;
	private int direction;
	private long startTime;
	private int width;

	public LankaBeam(long chargeTime) {
		this.chargeTime = chargeTime;
	}

	@Override
	public void start() {
		startTime = System.currentTimeMillis();
		float closestX = direction == 1 ? Float.MAX_VALUE : Float.MIN_VALUE;
		for (Collider c : Collider.staticColliders) {
			if (c.gameObject.name.equals("Door") && c.gameObject.getTag() == null) continue;
			if (!c.gameObject.name.equals("Locker") && c.bounds.top < transform.position.y && c.bounds.bottom > transform.position.y) {
				if (direction == 1 && c.bounds.left > transform.position.x && c.bounds.left < closestX) {
					closestX = c.bounds.left;
				} else if (direction == -1 && c.bounds.right < transform.position.x && c.bounds.right > closestX) {
					closestX = c.bounds.right;
				}
			}
		}
		width = (int) Math.abs(transform.position.x - closestX);
		if (direction == -1) transform.position.x -= width;
		gameObject.renderer.setSize(width, 0);
		gameObject.renderer.setzIndex(3);
	}

	@Override
	public void update() {
		float halfHeight = ((System.currentTimeMillis() - startTime) / (float) chargeTime) * 9f;
		if (System.currentTimeMillis() - startTime < chargeTime) {
			gameObject.renderer.setSize(width, (int) (halfHeight * 2));
			gameObject.renderer.setOffsets(0, (int) -halfHeight);
		} else {
			gameObject.renderer.setImage("images/white.png", width, halfHeight * 2);
			Network.destroy(gameObject, 100);
			gameObject.removeComponent(LankaBeam.class);
			RectF beam = new RectF(transform.position.x, transform.position.y - halfHeight, transform.position.x + width, transform.position.y + halfHeight);
			Collider c;
			for (GameObject go : GameObject.findAllWithTag("enemy")) {
				if ((c = go.getComponent(Collider.class)) != null && c.bounds.intersect(beam)) {
					//noinspection ConstantConditions
					go.getComponentOfType(Enemy.class).applyDamage(10, DamageType.SLASH, direction == 1 ? transform.position : transform.position.offset(width, 0));
				}
			}
		}
	}

	public void setDirection(int direction) {
		this.direction = direction;
	}
}

@@ -134,16 +134,109 @@
 Object;%0A
+import com.ezardlabs.dethsquare.Physics;%0Aimport com.ezardlabs.dethsquare.Physics.RaycastHit;%0A
 import c
@@ -306,16 +306,57 @@
 Script;%0A
+import com.ezardlabs.dethsquare.Vector2;%0A
 import c
@@ -796,611 +796,97 @@
 ;%0A%09%09
-float closestX = direction == 1 ? Float.MAX_VALUE : Float.MIN_VALUE;%0A%09%09for (Collider c : Collider.staticColliders) %7B%0A%09%09%09if (c.gameObject.name.equals(%22Door%22) && c.gameObject.getTag() == null) continue;%0A%09%09%09if (!c.gameObject.name.equals(%22Locker%22) && c.bounds.top %3C transform.position.y && c.bounds.bottom %3E transform.position.y) %7B%0A%09%09%09%09if (direction == 1 && c.bounds.left %3E transform.position.x && c.bounds.left %3C closestX) %7B%0A%09%09%09%09%09closestX = c.bounds.left;%0A%09%09%09%09%7D else if (direction == -1 && c.bounds.right %3C transform.position.x && c.bounds.right %3E closestX) %7B%0A%09%09%09%09%09closestX = c.bounds.right;%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%7D
+RaycastHit hit = Physics.raycast(transform.position, new Vector2(1, 0), Float.MAX_VALUE);
 %0A%09%09w
@@ -934,16 +934,19 @@
 x - 
-closestX
+hit.point.x
 );%0A%09

Change hit detection to use raycasts instead of custom code