76 lines
2.5 KiB
C#
76 lines
2.5 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class Laser : MonoBehaviour
|
|
{
|
|
public bool flipDirection;
|
|
public int power = 1;
|
|
|
|
float maxDistance = 1000.0f;
|
|
bool isHorizontal;
|
|
LineRenderer laserLine;
|
|
GameObject lastChargePoint;
|
|
|
|
void Start()
|
|
{
|
|
laserLine = GetComponent<LineRenderer>();
|
|
isHorizontal = GetComponent<Turret>().isHorizontal;
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
laserLine.SetPosition(0, transform.position);
|
|
RaycastHit2D hit;
|
|
|
|
if (isHorizontal)
|
|
{
|
|
float endpointY = flipDirection ? transform.position.y - maxDistance : transform.position.y + maxDistance;
|
|
hit = Physics2D.Raycast(transform.position, flipDirection ? Vector2.down : Vector2.up);
|
|
|
|
if (hit.collider) laserLine.SetPosition(1, hit.point);
|
|
else laserLine.SetPosition(1, new Vector3(transform.position.x, endpointY, 0));
|
|
} else
|
|
{
|
|
float endpointX = flipDirection ? transform.position.x - maxDistance : transform.position.x + maxDistance;
|
|
hit = Physics2D.Raycast(transform.position, flipDirection ? Vector2.left : Vector2.right);
|
|
|
|
if (hit.collider) laserLine.SetPosition(1, hit.point);
|
|
else laserLine.SetPosition(1, new Vector3(endpointX, transform.position.y, 0));
|
|
}
|
|
|
|
if (hit.collider)
|
|
{
|
|
if (hit.collider.tag == "Mirror")
|
|
{
|
|
Debug.Log("Hit Mirror");
|
|
if (isHorizontal)
|
|
{
|
|
// TODO
|
|
} else
|
|
{
|
|
// TODO
|
|
}
|
|
}
|
|
if (hit.collider.tag == "Charge Point")
|
|
{
|
|
if (hit.collider.gameObject != lastChargePoint) hit.collider.GetComponent<ChargePoint>().adjustCharges(power);
|
|
lastChargePoint = hit.collider.gameObject;
|
|
}
|
|
} else
|
|
{
|
|
if (lastChargePoint) lastChargePoint.GetComponent<ChargePoint>().adjustCharges(power * -1);
|
|
lastChargePoint = null;
|
|
}
|
|
}
|
|
|
|
void OnDrawGizmosSelected()
|
|
{
|
|
float distance = 1.0f;
|
|
isHorizontal = GetComponent<Turret>().isHorizontal;
|
|
Vector3 offset = isHorizontal ? new Vector3(0, distance, 0) : new Vector3(distance, 0, 0);
|
|
|
|
Gizmos.color = Color.blue;
|
|
Gizmos.DrawLine(transform.position, flipDirection ? transform.position - offset : transform.position + offset);
|
|
}
|
|
}
|